cocos热更动态版本地址研究
cocos creator 2.x 版本中,关于热更的远程地址,版本等信息来源于工程中写入的project.manifest ,没有提供外部修改接口。如果想更自由的管理版本、热更地址配置,势必要修改源码。但是修改源码对于cocos 引擎版本升级不够友好。经过研究jsb 相关内容,这边提供一个在不修改源码的情况下达到自由控制版本信息的方法。
- 获取本地热更文件project.manifest 获得内容data 并解析
let asset = JSON.parse(data._nativeAsset);
- 修改asset 内容
asset.packageUrl = this.serverUrl + HOT_PACKAGE + version;
asset.remoteManifestUrl = this.serverUrl + HOT_PACKAGE + version + manifest + "/project.manifest";
asset.remoteVersionUrl = this.serverUrl + HOT_PACKAGE + version + manifest + "/version.manifest";
- 判定是否存在存储的热更文件 jsb.fileUtils.isFileExist(this.storagePath + “/project.manifest”),如果存在更新该文件
let str = jsb.fileUtils.getStringFromFile(this.storagePath + "/project.manifest");
let json = JSON.parse(str);
json.packageUrl = asset.packageUrl;
json.remoteManifestUrl = asset.remoteManifestUrl;
json.remoteVersionUrl = asset.remoteVersionUrl;
jsb.fileUtils.writeStringToFile(JSON.stringify(json), this.storagePath + "/project.manifest");
}
这一步的目的是因为源码中,会优先使用存贮的热更文件
// Compare with cached manifest to determine which one to use
if (cachedManifest)
{
bool localNewer = _localManifest->versionGreater(cachedManifest, _versionCompareHandle);
if (localNewer)
{
// Recreate storage, to empty the content
_fileUtils->removeDirectory(_storagePath);
_fileUtils->createDirectory(_storagePath);
CC_SAFE_RELEASE(cachedManifest);
}
else
{
CC_SAFE_RELEASE(_localManifest);
_localManifest = cachedManifest;
}
}
- 构造新的manifest 对象
let mani = new jsb.Manifest(JSON.stringify(asset), this.storagePath);
5.热更管理器加载该配置对象即可。
this._assetManager.loadLocalManifest(mani, this.storagePath);
注意:在构造热更管理器时不要传入原始的manifest 文件,否则loadLocalManifest方法将失效
this._assetManager = new jsb.AssetsManager("", this.storagePath, this.versionCompareHandle);