代码拉取完成,页面将自动刷新
const {ccclass, property} = cc._decorator;
import MD5 = require('./MD5');
@ccclass
export default class HotUpdate extends cc.Component {
@property(cc.Asset)
manifestUrl:cc.Asset = null;
private _updating:boolean = false;
private _canRetry:boolean = false;
private _storagePath:string = '';
private _am:any = null;
private _failCount:number = 0;
onLoad () {
if (!cc.sys.isNative) {
return;
}
this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'qygame-remote-asset');
let versionCompareHandle = function (versionA, 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 {
return a - b;
}
}
if (vB.length > vA.length) {
return -1;
}
else {
return 0;
}
};
this._am = new jsb.AssetsManager(this.manifestUrl.nativeUrl, this._storagePath, versionCompareHandle);
this._am.setVerifyCallback(function (filePath, asset) {
//增加了md5校验,如果不需要可以直接return true
let data = jsb.fileUtils.getDataFromFile(filePath);
let fileMd5 = MD5(data);
let ret = fileMd5 == asset.md5;
if (!ret) {
cc.log('md5 is wrong, file:' + filePath);
}
return ret;
});
//初始化脚本版本信息
if (cc.sys.os === cc.sys.OS_ANDROID) {
//一些安卓设备不支持同时下载文件过多
this._am.setMaxConcurrentTask(2);
} else {
this._am.setMaxConcurrentTask(2);
}
}
onDestroy() {
if(cc.sys.isNative){
this._am.setEventCallback(null);
this._am = null;
}
}
showLog(msg:string) {
cc.log('[HotUpdateModule][showLog]----' + msg);
}
retry() {
if (!this._updating && this._canRetry) {
this._canRetry = false;
this._am.downloadFailedAssets();
}
}
updateCallback(event) {
var needRestart = false;
var failed = false;
switch (event.getEventCode())
{
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.showLog("没有发现本地manifest文件,跳过了热更新.");
failed = true;
break;
//更新进度
case jsb.EventAssetsManager.UPDATE_PROGRESSION:
let percent = event.getPercent();
if (isNaN(percent)) return;
var msg = event.getMessage();
this.disPatchRateEvent(percent);
this.showLog("updateCallback更新进度:" + percent + ', msg: ' + msg);
break;
//下载manifest文件失败,跳过热更新
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.showLog("下载manifest文件失败,跳过热更新.");
failed = true;
break;
//已是最新版本
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.showLog("已是最新版本.");
failed = true;
break;
//更新结束
case jsb.EventAssetsManager.UPDATE_FINISHED:
this.showLog("更新结束."+ event.getMessage());
this.disPatchRateEvent(1);
needRestart = true;
break;
//更新错误
case jsb.EventAssetsManager.UPDATE_FAILED:
this.showLog("更新错误."+ event.getMessage());
this._updating = false;
this._canRetry = true;
this._failCount++;
this.retry();
break;
//更新过程中错误
case jsb.EventAssetsManager.ERROR_UPDATING:
this.showLog('更新过程中错误: ' + event.getAssetId() + ', ' + event.getMessage());
break;
//解压错误
case jsb.EventAssetsManager.ERROR_DECOMPRESS:
this.showLog('解压错误');
break;
default:
break;
}
if (failed) {
this._am.setEventCallback(null);
this._updating = false;
}
if (needRestart) {
this._am.setEventCallback(null);
var searchPaths = jsb.fileUtils.getSearchPaths();
var newPaths = this._am.getLocalManifest().getSearchPaths();
Array.prototype.unshift.apply(searchPaths, newPaths);
cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
jsb.fileUtils.setSearchPaths(searchPaths);
cc.audioEngine.stopAll();
setTimeout(() => {
cc.game.restart();
}, 100);
}
}
hotUpdate() {
if (this._am && !this._updating) {
this._am.setEventCallback(this.updateCallback.bind(this));
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
var url = this.manifestUrl.nativeUrl;
this._am.loadLocalManifest(url);
}
this._failCount = 0;
this._am.update();
this._updating = true;
}
}
checkCallback(event) {
switch (event.getEventCode())
{
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.showLog("没有发现本地manifest文件,跳过了热更新.");
this.hotUpdateFinish(true);
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.showLog("下载manifest文件失败,跳过热更新.");
this.hotUpdateFinish(false);
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.showLog("已更新.");
this.hotUpdateFinish(true);
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND: {
//有新版本
this.showLog("有新版本,需要更新");
this._updating = false;
this.hotUpdate();
return;
}
case jsb.EventAssetsManager.UPDATE_PROGRESSION: {
//有新版本
let percent = event.getPercent();
if (isNaN(percent)) return;
var msg = event.getMessage();
this.showLog("checkCallback更新进度:" + percent + ', msg: ' + msg);
return;
}
default:
console.log('event.getEventCode():' + event.getEventCode());
return;
}
this._am.setEventCallback(null);
this._updating = false;
}
checkUpdate() {
if (this._updating) {
cc.log("检测更新中...");
return;
}
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
var url = this.manifestUrl.nativeUrl;
//@ts-ignore
if (cc.assetManager.md5Pipe) {
//@ts-ignore
url = cc.assetManager.md5Pipe.transformURL(url);
}
this._am.loadLocalManifest(url);
}
if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
this.showLog('加载manifest文件失败');
return;
}
this._am.setEventCallback(this.checkCallback.bind(this));
this._am.checkUpdate();
this._updating = true;
this.disPatchRateEvent(0.01);
}
hotUpdateFinish(result) {
cc.director.emit('HotUpdateFinish',result);
}
disPatchRateEvent(percent) {
if (percent > 1) {
percent = 1;
}
cc.director.emit('HotUpdateRate',percent);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。