错误日志如下:
2018-06-20T10:48:15.511Z - normal: Simulator: D/jswrapper (125): JS: ----热更新- 检查版本—
2018-06-20T10:48:15.511Z - normal: Simulator: D/jswrapper (125): JS: Storage path for remote asset : D:\nhygame\cocoscreator\CocosCreator\resources\cocos2d-x\simulator\win32\newAppUpdate
2018-06-20T10:48:15.511Z - normal: Simulator: D/jswrapper (125): JS: Local manifest URL : D:/nhygame/project/redpacketfairyland/NewProject/assets/resources/varsion/project.manifest
2018-06-20T10:48:15.537Z - normal: Simulator: Fail to retrieve local file content: D:\nhygame\cocoscreator\CocosCreator\resources\cocos2d-x\simulator\win32\newAppUpdate_temp/project.manifest.temp
2018-06-20T10:48:15.537Z - normal: Simulator:
2018-06-20T10:48:15.537Z - normal: Simulator: D/jswrapper (125): JS: 开始更新了
2018-06-20T10:48:15.842Z - normal: Simulator: AssetsManagerEx : Fail to download version file, step skipped
2018-06-20T10:48:15.842Z - normal: Simulator:
2018-06-20T10:48:16.022Z - normal: Simulator: D/jswrapper (125): JS: checkCb–Code: 1
2018-06-20T10:48:16.022Z - normal: Simulator: D/jswrapper (125): JS: Fail to download manifest file or parse Fail, hot update skipped.
2018-06-20T10:48:16.022Z - normal: Simulator: D/jswrapper (125): JS: 热更新失败了
2018-06-20T10:48:16.051Z - normal: Simulator: D/jswrapper (447): ScriptEngine::cleanup begin …
热更新代码如下
checkCb: function (event) {
cc.log('checkCb–Code: ’ + event.getEventCode());
var needSkip=false;
switch (event.getEventCode())
{
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
cc.log(“No local manifest file found, hot update skipped.”);
needSkip=true;
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
cc.log(“Fail to download manifest file or parse Fail, hot update skipped.”);
this.hotUpdataFail();
this._needUpdate=true;
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
cc.log(“Already up to date with the latest remote version.”);
//this._updating = false;
needSkip=true;
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND://3
this._needUpdate = true;
this.hotUpdate();//直接更新
break;
default:
cc.log('其他情况直接返回 checkCb--Code: ' + event.getEventCode());
return;
}
cc.eventManager.removeListener(this._checkListener);
this._checkListener=null;
if(needSkip){
this.callUpdate();
}
},
updateCb: function (event) {
cc.log("-----updateCb----",event.getEventCode(),event)
var needRestart = false;
var failed = false;
var needSkipped=false;
switch (event.getEventCode())
{
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
cc.log('No local manifest file found, hot update skipped.');
needSkipped = true;
break;
case jsb.EventAssetsManager.UPDATE_PROGRESSION:
var percent = event.getPercent();
var percentByFile = event.getPercentByFile();
//var totalFile=event.getTotalFiles();
var fileNum= event.getDownloadedFiles() /event.getTotalFiles();
if(event.getTotalFiles()==0){
fileNum=0;
}
//var beytNum= event.getDownloadedBytes() / event.getTotalBytes();
var msg = event.getMessage();
if (msg) {
cc.log('==msg====',msg);
}
cc.log("--进度信息-下载的大小-"+ event.getDownloadedBytes());
cc.log("文件数百分比进度---",percentByFile);
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
cc.log('Fail to download manifest file, hot update skipped.');
failed = true;
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
cc.log('Already up to date with the latest remote version.');
needSkipped= true;
break;
case jsb.EventAssetsManager.UPDATE_FINISHED:
cc.log('Update finished. ' + event.getMessage());
needRestart = true;
break;
case jsb.EventAssetsManager.UPDATE_FAILED:
cc.log('Update failed. ' + event.getMessage());
this._failCount ++;
if (this._failCount < 5)
{
this._am.downloadFailedAssets();
}
else
{
cc.log('Reach maximum fail count, exit update process');
this._failCount = 0;
failed = true;
}
break;
case jsb.EventAssetsManager.ERROR_UPDATING:
cc.log('Asset update error: ' + event.getAssetId() + ', ' + event.getMessage());
failed=true;
break;
case jsb.EventAssetsManager.ERROR_DECOMPRESS:
cc.log(event.getMessage());
failed=true;
break;
default:
cc.log("其他消息--",event.getEventCode());
break;
}
if(needSkipped){
cc.eventManager.removeListener(this._updateListener);
this.this._updateListener=null;
this.callUpdate();
}
if (failed) {
cc.eventManager.removeListener(this._updateListener);
this._updateListener=null;
this.hotUpdataFail();
}
if (needRestart) {
cc.eventManager.removeListener(this._updateListener);
this._updateListener=null;
// Prepend the manifest's search path
var searchPaths = jsb.fileUtils.getSearchPaths();
var newPaths = this._am.getLocalManifest().getSearchPaths();
Array.prototype.unshift(searchPaths, newPaths);
// This value will be retrieved and appended to the default search path during game startup,
// please refer to samples/js-tests/main.js for detailed usage.
// !!! Re-add the search paths in main.js is very important, otherwise, new scripts won't take effect.
cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
jsb.fileUtils.setSearchPaths(searchPaths);
cc.log("------重新加载游戏----")
cc.game.restart();
}
},
//点击更新按钮更新
hotUpdate: function () {
this.updatePanel.active = true;
cc.log("---开始更新",this._am,this._needUpdate);
if (this._am && this._needUpdate) {
this._updateListener = new jsb.EventListenerAssetsManager(this._am, this.updateCb.bind(this));
cc.eventManager.addListener(this._updateListener, 1);
this._failCount = 0;
this._am.update();
}
},
//点击取消按钮 取消更新
callUpdate:function () {
// cc.director.loadScene(this._mainScene);
},
//更新失败弹出提示
hotUpdataFail:function () {
var storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/')+ 'newAppUpdate');
jsb.fileUtils.removeDirectory(storagePath);
console.log("热更新失败了");
cc.game.restart();
},
//检测热更新版本
checkUpdateVersion:function () {
cc.log("----热更新- 检查版本---");
if (!cc.sys.isNative) {
cc.log("----不是原生环境不需要更新----");
return;
}
var storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/')+ 'newAppUpdate');
cc.log('Storage path for remote asset : ' + storagePath);
cc.log('Local manifest URL : ' + this.manifestUrl);
//版本对比规则
// Setup your own version compare handler, versionA and B is versions in string
// if the return value greater than 0, versionA is greater than B,
// if the return value equals 0, versionA equals to B,
// if the return value smaller than 0, versionA is smaller than B.
var versionCompareHandle = function (versionA, versionB) {
env.version=versionB;
cc.log(" 检查版本: version A is " + versionA + ', version B is ' + versionB);
var vA = versionA.split('.');
var vB = versionB.split('.');
for (var i = 0; i < vA.length; ++i) {
var a = parseInt(vA[i]);
var b = parseInt(vB[i] || 0);
if (a === b) {
continue;
}
else {
if(a>b){
env.version=versionA;
}
return a - b;
}
}
if (vB.length > vA.length) {
return -1;
}
else {
return 0;
}
}.bind(this);
this._am = new jsb.AssetsManager(this.manifestUrl, storagePath,versionCompareHandle);
// this._am = new jsb.AssetsManager("", storagePath,versionCompareHandle);
this._am.retain();
this._am.setMaxConcurrentTask(2);//设置最大线程数 测试阶段慢点走
//读取本地Manifesturl
//this._am.loadLocalManifest(this.manifestUrl);
this._needUpdate = false;
if (this._am.getLocalManifest().isLoaded())
{
this._checkListener = new jsb.EventListenerAssetsManager(this._am, this.checkCb.bind(this));
cc.eventManager.addListener(this._checkListener, 1);
this._am.checkUpdate();
cc.log("开始更新了");
}else {
//this.callUpdate();
cc.log("更新失败了");
this.hotUpdataFail();
}
},
//用一个按钮来触发检查更新
checkUpdate: function () {
if (this._updating) {
//this.panel.info.string = 'Checking or updating ...';
return;
}
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
this._am.loadLocalManifest(this.manifestUrl);
}
if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
//this.panel.info.string = 'Failed to load local manifest ...';
return;
}
// //读取本地Manifesturl
// this._am.loadLocalManifest(this.manifestUrl);
// this._needUpdate = false;
// if (this._am.getLocalManifest().isLoaded())
// {
// this._checkListener = new jsb.EventListenerAssetsManager(this._am, this.checkCb.bind(this));
// cc.eventManager.addListener(this._checkListener, 1);
// this._am.checkUpdate();
// }
this._needUpdate = false;
this._checkListener = new jsb.EventListenerAssetsManager(this._am, this.checkCb.bind(this));
cc.eventManager.addListener(this._checkListener, 1);
this._am.checkUpdate();
// this._updating = true;
},
onDestroy: function () {
if (this._updateListener) {
cc.eventManager.removeListener(this._updateListener);
this._updateListener = null;
}
if (this._am) {
this._am.release();
}
},
//访问外网服务器地址是:https://warp-tech.cn/remote-assets
https://warp-tech.cn/remote-assets/project.manifest 网页访问可下载
之前这个代码还是能跑的,真是感觉见鬼了
热更新入口是:checkUpdateVersion 这个函数