代码如下,大神们帮我看看是哪个环节初夏你问题?
“use strict”;
cc.log(‘UpdateScene.js’);
if (!pu.UpdateScene) {
const ClsName = ‘UpdateScene’;
const LogTag = ‘UpdateScene’;
const UpdateScene = cc.Class({
extends: cc.Component,
ctor(){
let that = this;
that._assetMgr = null; //更新下载器
},
_checkCallbacks(pDestroy) {
pDestroy = pDestroy || false;
let that = this;
if (pDestroy) {
} else {
}
},
onDestroy() {
let that = this;
that._checkCallbacks(true);
if(that._updateListener){
that._assetMgr.setEventCallback(null);
that._updateListener = null;
}
},
_initNodeFields(){
let that = this;
pu.LayerUtils.injectSceneNodes(that);
that._updateNode = that._root.getChildByName('Update');
that._prgNode = that._root.getChildByName('Prg');
Log.i(LogTag, 'prgNode: {0}', that._prgNode.active);
},
onLoad(){
let that = this;
// Hot update is only available in Native build
if(!cc.sys.isNative){
return;
}
that._checkResUpdate();
GameBridge.closeSplash();
that._initNodeFields();
that._prgNode.active = true;
that._prgNode.getChildByName('Txt').getComponent(cc.Label).string = '正在更新,请稍后......';
that._updateNode.active = true;
Log.d(LogTag,'onLoad');
},
//==========
setPrg(pPrg) {
let that = this;
let prgBar = that._prgNode.getComponent(cc.ProgressBar);
prgBar.progress = pPrg;
},
//========== begin: 热更新;
_tryInitAssetMgr() {
let that = this;
if (that._assetMgr) {
Log.i(LogTag, '_tryInitAssetMgr: _assetMgr has init!');
return;
}
let remoteAssetsDirPath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + pu.Config.hotUpdateDirName);
//在模拟器上: /Applications/CocosCreator.app/Contents/Resources/cocos2d-x/simulator/mac/Simulator.app/Contents/Resources/cowboy-remote-asset
Log.i(LogTag, '_checkResUpdate: remoteAssetsDirPath: {0}', remoteAssetsDirPath);
let versionCompareHandle = function (pLocalVersion, pServerVersion) {
Log.d(LogTag, 'JS Custom Version Compare: version A is {0}, version B is {1}', pLocalVersion, pServerVersion);
pu.Config.localResVer = pLocalVersion;
pu.LocalStorageExt.setItem(pu.Config.AppId, 'localResVer', pLocalVersion);
that._serverResVer = pServerVersion;
let lDiff = parseInt(pLocalVersion) - parseInt(pServerVersion);
return lDiff;
};
// Init with empty manifest url for testing custom manifest
that._assetMgr = new jsb.AssetsManager('', remoteAssetsDirPath, versionCompareHandle);
that._assetMgr.setPacketUrl('http://58.211.107.66:9010/update_Rotate/');
// if (!cc.sys.ENABLE_GC_FOR_NATIVE_OBJECTS) { //native对象未启用gc?
// that._assetMgr.retain();
// }
//验证下载资源是否正确的;
that._assetMgr.setVerifyCallback(function (path, asset) { //1个asset条目下载完会调用一下来验证;
// When asset is compressed, we don't need to check its md5, because zip file have been deleted.
let compressed = asset.compressed;
// Retrieve the correct md5 value.
let expectedMD5 = asset.md5;
// asset.path is relative path and path is absolute.
let relativePath = asset.path;
// The size of asset file, but this value could be absent.
let bytes = asset.size;
// let fileBytes = jsb.fileUtils.getFileSize(path);
// let diffBytes = bytes - fileBytes;
// if (diffBytes >= 0.1 || diffBytes <= -0.1) {
// Log.e(LogTag, 'diffBytes: {0}', diffBytes);
// return false;
// }
if (compressed) {
//panel.info.string = "Verification passed : " + relativePath;
Log.d(LogTag, 'Verification passed : {0}', relativePath);
return true;
} else {
//panel.info.string = "Verification passed : " + relativePath + ' (' + expectedMD5 + ')';
Log.d(LogTag, 'Verification passed : {0}, {1} ({2})', path, relativePath, expectedMD5);
return true;
}
});
if (cc.sys.os === cc.sys.OS_ANDROID) {
// Some Android device may slow down the download process when concurrent tasks is too much.
// The value may not be accurate, please do more test and find what's most suitable for your game.
that._assetMgr.setMaxConcurrentTask(2);
//this.panel.info.string = "Max concurrent tasks count have been limited to 2";
}
//# /Users/mac/Documents/_phoneu/PU_SLOTS/assets/resources/Manifest/local/project.manifest
//读取原始Mainfest
let manifestStr = jsb.fileUtils.getStringFromFile(cc.url.raw('resources/Manifest/project.manifest'));
try {
let manifestJObj = JSON.parse(manifestStr);
if (pu.Consts.AppType.Ios == pu.Config.appType || pu.Consts.AppType.Android == pu.Config.appType) {
that._assetMgr.loadLocalManifest(cc.url.raw('resources/Manifest/project.manifest')); //加载项目的project.manifest;
} else {
manifestJObj.packageUrl = pu.Config.packetUrl;
manifestJObj.remoteManifestUrl = cc.path.join(manifestJObj.packageUrl, 'project.manifest');
manifestJObj.remoteVersionUrl = cc.path.join(manifestJObj.packageUrl, 'version.manifest');
Log.i(LogTag, 'apk manifest: ver url: {0}, ver: {1}', manifestJObj.remoteVersionUrl, manifestJObj.version);
let customManifestStr = JSON.stringify(manifestJObj);
let manifest = new jsb.Manifest(customManifestStr, remoteAssetsDirPath);
that._assetMgr.loadLocalManifest(manifest, remoteAssetsDirPath);
}
} catch (ex) {
Log.e(LogTag, 'manifest parse fail: {0}', ex.toString());
}
if (!that._assetMgr.getLocalManifest() || !that._assetMgr.getLocalManifest().isLoaded()) {
Log.e(LogTag, 'Failed to load local hotupdate manifest');
//todo: 代码中删除manifest?
return;
}
Log.i(LogTag, 'tryInit');
},
_checkResUpdate() {
let that = this;
if (pu.Config.noResUpdate) { //忽略热更;
that._showLogin();
return;
}
Log.i(LogTag, '_checkResUpdate');
//that._prgNode.active = true;
//that._prgNode.getChildByName('Txt').getComponent(cc.Label).string = '正在检查资源更新';
that._tryInitAssetMgr();
that._assetMgr.setEventCallback(that.onAssetMgr_VerCheckResult.bind(that));
//cc.eventManager.addListener(that._checkListener, 1);
// that._checkListenerAdd = true;
that._assetMgr.checkUpdate(); //检查更新;
},
onAssetMgr_VerCheckResult(pEvt) {
let that = this;
Log.i(LogTag, 'Code: {0}',pEvt.getEventCode());
switch (pEvt.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: //No local manifest file found, hot update skipped
Log.e(LogTag, 'onAssetMgr_VerCheckResult: ERROR_NO_LOCAL_MANIFEST');
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST: //Fail to download manifest file, hot update skipped
//#manifest中的remote文件不存在时, 会出现这边的错误; 比如将服务器的删除;
Log.e(LogTag, 'onAssetMgr_VerCheckResult: ERROR_DOWNLOAD_MANIFEST');
pu.CommonTipLayer.showLayerUpdateResFail();
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND: //发现新版本;
that._assetMgr.prepareUpdate();
let totalBytes = pEvt.getTotalBytes();
let remindBytes = 1024 * 1024;
let totalM = pu.MathExt.fixedNum(totalBytes/remindBytes, 1);
pu.CommonTipLayer.showLayerResUpdateConfirm(totalM, function(pType) {
if (pu.ILayer.Dlg_Btn.Btn_Yes == pType) {
that.startUpdate();
} else {
GameBridge.exit();
}
});
that.startUpdate();
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE: //已经是最新版本;
Log.i(LogTag, 'onAssetMgr_VerCheckResult: ALREADY_UP_TO_DATE');
that._showLogin();
break;
default:
// verCheckEnd = false;
return;
}
that._assetMgr.setEventCallback(null);
that._checkListener = null;
},
_showLogin(){
let that = this;
Log.i(LogTag, ' showLogin');
that.setPrg(1);
that.scheduleOnce(function (pDt) {
that._prgNode.active = false;
that._updateNode.active = false;
cc.director.loadScene('LobbyScene');
}, 0.1);
},
/**
* #开始热更;
*/
startUpdate() {
let that = this;
that._assetMgr.setEventCallback(that.onAssetMgr_UpdateRes.bind(that));
// cc.eventManager.addListener(that._updateListener, 1);
// that._updateListenerAdd = true;
that._retryNum = 3;
that._assetMgr.update();
},
onAssetMgr_UpdateRes(pEvt) {
let that = this;
switch (pEvt.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: //No local manifest file found, hot update skipped
Log.e(LogTag, 'onAssetMgr_UpdateRes: ERROR_NO_LOCAL_MANIFEST');
that._updateFail();
break;
case jsb.EventAssetsManager.UPDATE_PROGRESSION: //流程中;
let totalBytes = pEvt.getTotalBytes();
if (totalBytes > 0) {
let percent = pEvt.getPercent();
if (percent >= 1) {
that.setPrg(0.99); //留1%给解压缩;
} else {
that.setPrg(percent);
}
}
if (CC_DEBUG) {
Log.d(LogTag, 'prg: {0}, perByFile: {1}, totalFiles: {2}, totalBytes: {3}',
pEvt.getPercent(), pEvt.getPercentByFile(), pEvt.getTotalFiles(), pEvt.getTotalBytes());
}
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST: //Fail to download manifest file, hot update skipped
Log.e(LogTag, 'onAssetMgr_UpdateRes: ERROR_DOWNLOAD_MANIFEST');
that._updateFail();
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE: //Already up to date with the latest remote version
that._showLogin();
break;
case jsb.EventAssetsManager.UPDATE_FINISHED: //热更完毕;
that.setPrg(1);
if (that._serverResVer) {
pu.Config.localResVer = that._serverResVer;
pu.LocalStorageExt.setItem(pu.Config.AppId, 'localResVer', that._serverResVer);
} else {
Log.i(LogTag, 'UPDATE_FINISHED: serverResVer: {0}', that._serverResVer);
}
that._updateOk();
break;
case jsb.EventAssetsManager.UPDATE_FAILED: //更新结束, 但整个流程中存在更新失败;
if (that._retryNum > 0) {
that._retryNum--;
Log.e(LogTag, 'UPDATE_FAILED: retry: {0}', that._retryNum);
that._assetMgr.downloadFailedAssets();
} else { //重试次数用完;
that._updateFail();
}
break;
case jsb.EventAssetsManager.ERROR_UPDATING: //流程中: manifest中的单个文件更新失败时(比如url访问不了), 会收到这个;
Log.e(LogTag, 'onAssetMgr_UpdateRes: ERROR_UPDATING: assetId: {0}, msg: {1}', pEvt.getAssetId(), pEvt.getMessage());
break;
case jsb.EventAssetsManager.ERROR_DECOMPRESS: //流程中:
Log.e(LogTag, 'onAssetMgr_UpdateRes: ERROR_DECOMPRESS: {0}', pEvt.getMessage());
break;
default:
break;
}
},
/**
* #热更过程中某个流程出错了;
* @private
*/
_updateFail() {
let that = this;
that._assetMgr.setEventCallback(null);
that._updateListener = null;
pu.CommonTipLayer.showLayerUpdateResFail();
},
/**
* #整个热更过程没有任何错误发生;
*/
_updateOk() {
let that = this;
that._assetMgr.setEventCallback(null);
that._updateListener = null;
// cc.eventManager.removeListener(that._updateListener);
// that._updateListenerAdd = false;
cc.audioEngine.stopAll();
cc.game.restart();
that.scheduleOnce(function (pDt) {
//pu.CommonTipLayer.showLayerUpdateResFinish(false);
}, 0.05);
},
//========== end: 热更新;
});
module.exports = UpdateScene;
pu.UpdateScene = UpdateScene;
UpdateScene.getRemoteAssetsDirPath = function () {
let remoteAssetsDirPath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + pu.Config.hotUpdateDirName);
return remoteAssetsDirPath;
};
UpdateScene.getRemoteAssetsDirTempPath = function () {
let remoteAssetsDirPath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + pu.Config.hotUpdateDirName + '_temp');
return remoteAssetsDirPath;
};
} else {
cc.log(‘UpdateScene.js has load!’);
}