mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-06-04 00:19:33 +08:00
优化流程,添加覆盖安装时,当前高版本时,会把低版本的热更新文件清除
This commit is contained in:
@@ -5,12 +5,20 @@
|
||||
const ErrCode = cc.Enum({
|
||||
laodManifestFailed: 0, //下载manifest失败
|
||||
updateFailed: 1, //更新失败,
|
||||
downloadPackage: -10, //下载新包
|
||||
});
|
||||
|
||||
const MD5 = require("Uint8ArrayMD5");
|
||||
|
||||
cc.Class({
|
||||
extends: cc.Component,
|
||||
|
||||
properties: {
|
||||
// 2.x 修改成cc.asset
|
||||
// manifestUrl: {
|
||||
// type: cc.Asset,
|
||||
// default: null
|
||||
// },
|
||||
manifestUrl: cc.RawAsset,
|
||||
_hotUpdateName: 'game-remote-asset',
|
||||
_nowVersion: ''
|
||||
@@ -24,24 +32,26 @@ cc.Class({
|
||||
this.nextFn = nextFn;
|
||||
this.progressFn = progressFn;
|
||||
this.failedFn = failedFn;
|
||||
// this.loadManifest();
|
||||
// console.log('os=', cc.sys.os);
|
||||
if (cc.sys.isBrowser) {
|
||||
console.log('is not OS_ANDROID or OS_IOS');
|
||||
this.nextFn && nextFn(this._nowVersion);
|
||||
return;
|
||||
}
|
||||
let self = this;
|
||||
this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + this._hotUpdateName);
|
||||
cc.log('Storage path for remote asset : ' + this._storagePath);
|
||||
this.versionCompareHandle = function (versionA, versionB) {
|
||||
console.log('versionLocal=' + versionA + ' versionRemote=' + versionB);
|
||||
self._nowVersion = versionA;
|
||||
var vA = versionA.split('.');
|
||||
var vB = versionB.split('.');
|
||||
//第一位不同时,为整包更新
|
||||
this.isDownloadPackage = false;
|
||||
for (var i = 0; i < vA.length; ++i) {
|
||||
var a = parseInt(vA[i]);
|
||||
var b = parseInt(vB[i] || 0);
|
||||
if (i == 0) {
|
||||
this.isDownloadPackage = (a - b) < 0 ? true : false;
|
||||
// console.log('isDownloadPackage=', this.isDownloadPackage);
|
||||
}
|
||||
if (a === b) {
|
||||
continue;
|
||||
} else {
|
||||
@@ -55,13 +65,13 @@ cc.Class({
|
||||
}
|
||||
};
|
||||
|
||||
this._am = new jsb.AssetsManager('', this._storagePath, this.versionCompareHandle);
|
||||
this._am = new jsb.AssetsManager('', this._storagePath, this.versionCompareHandle.bind(this));
|
||||
if (!cc.sys.ENABLE_GC_FOR_NATIVE_OBJECTS) {
|
||||
//2.x要去this._am.retain();
|
||||
this._am.retain();
|
||||
}
|
||||
|
||||
this._am.setVerifyCallback(function (path, asset) {
|
||||
console.log('path', path, 'setVerifyCallback' + JSON.stringify(asset));
|
||||
var compressed = asset.compressed;
|
||||
/**
|
||||
* 计算md5
|
||||
@@ -75,7 +85,6 @@ cc.Class({
|
||||
return true;
|
||||
} else {
|
||||
var resMD5 = calMD5OfFile(path);
|
||||
console.log('resMD%=', resMD5, 'asset md5=', asset.md5);
|
||||
if (asset.md5 == resMD5) {
|
||||
return true;
|
||||
}
|
||||
@@ -112,7 +121,6 @@ cc.Class({
|
||||
},
|
||||
|
||||
loadManifest() {
|
||||
// console.log('this.manifestUrl=', this.manifestUrl);
|
||||
// 手动设置路径
|
||||
this.manifestUrl = 'res/raw-assets/project.manifest';
|
||||
},
|
||||
@@ -120,8 +128,8 @@ cc.Class({
|
||||
//热更新完成 or 不需要热更新 进入游戏
|
||||
onEnterGame: function () {
|
||||
console.log('热更新完成,', this._nowVersion);
|
||||
cc.sys.localStorage.setItem('gameVersion', '' + this._nowVersion);
|
||||
this.nextFn && this.nextFn(this._nowVersion);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -139,7 +147,7 @@ cc.Class({
|
||||
* @param {*} err
|
||||
*/
|
||||
onFailure(type) {
|
||||
console.log('type=', type);
|
||||
// console.log('type=', type);
|
||||
if (this.failedFn) this.failedFn(type);
|
||||
},
|
||||
|
||||
@@ -147,8 +155,13 @@ cc.Class({
|
||||
* 检查更新
|
||||
*/
|
||||
checkUpdate: function () {
|
||||
console.log('start checkUpdate');
|
||||
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
|
||||
// 2.x修改成以下代码
|
||||
// var url = this.manifestUrl.nativeUrl;
|
||||
// if (cc.loader.md5Pipe) {
|
||||
// url = cc.loader.md5Pipe.transformURL(url);
|
||||
// }
|
||||
// this._am.loadLocalManifest(url);
|
||||
this._am.loadLocalManifest(this.manifestUrl);
|
||||
}
|
||||
if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
|
||||
@@ -156,9 +169,11 @@ cc.Class({
|
||||
this.onFailure(ErrCode.laodManifestFailed);
|
||||
return;
|
||||
}
|
||||
//2.0修改成以下代码
|
||||
// this._am.setEventCallback(this.checkCb.bind(this));
|
||||
this._checkListener = new jsb.EventListenerAssetsManager(this._am, this.checkCb.bind(this));
|
||||
cc.eventManager.addListener(this._checkListener, 1);
|
||||
|
||||
/**------------------------------------------------------- */
|
||||
this._am.checkUpdate();
|
||||
},
|
||||
|
||||
@@ -166,12 +181,19 @@ cc.Class({
|
||||
* 执行热更新逻辑
|
||||
*/
|
||||
hotUpdate: function () {
|
||||
console.log('start hotUpdate');
|
||||
if (this._am && !this._updating) {
|
||||
//2.x修改成以下代码
|
||||
// this._am.setEventCallback(this.updateCb.bind(this));
|
||||
this._updateListener = new jsb.EventListenerAssetsManager(this._am, this.updateCb.bind(this));
|
||||
cc.eventManager.addListener(this._updateListener, 1);
|
||||
|
||||
/**----------------------------------------------------- */
|
||||
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
|
||||
//2.x修改成以下代码
|
||||
// var url = this.manifestUrl.nativeUrl;
|
||||
// if (cc.loader.md5Pipe) {
|
||||
// url = cc.loader.md5Pipe.transformURL(url);
|
||||
// }
|
||||
// this._am.loadLocalManifest(url);
|
||||
this._am.loadLocalManifest(this.manifestUrl);
|
||||
}
|
||||
|
||||
@@ -202,10 +224,17 @@ cc.Class({
|
||||
console.log('ALREADY_UP_TO_DATE');
|
||||
break;
|
||||
case jsb.EventAssetsManager.NEW_VERSION_FOUND:
|
||||
console.log('NEW_VERSION_FOUND', this.isDownloadPackage);
|
||||
/*4发现新的更新*/
|
||||
isNewVersion = true;
|
||||
cc.eventManager.removeListener(this._checkListener);
|
||||
this._checkListener = null;
|
||||
if (this.isDownloadPackage) {
|
||||
console.log('load new package');
|
||||
this.clearHotupdateCache();
|
||||
this.onFailure(ErrCode.downloadPackage);
|
||||
return;
|
||||
}
|
||||
isNewVersion = true;
|
||||
console.log('have new version');
|
||||
//开始更新版本
|
||||
this.hotUpdate();
|
||||
@@ -246,7 +275,7 @@ cc.Class({
|
||||
failed = true;
|
||||
break;
|
||||
case jsb.EventAssetsManager.UPDATE_FINISHED:
|
||||
console.log('UPDATE_FINISHED');
|
||||
// console.log('UPDATE_FINISHED');
|
||||
// this.panel.info.string = 'Update finished. ' + event.getMessage();
|
||||
needRestart = true;
|
||||
break;
|
||||
@@ -267,7 +296,7 @@ cc.Class({
|
||||
|
||||
//下载失败
|
||||
if (failed) {
|
||||
console.log('Hotupdate failure');
|
||||
// console.log('Hotupdate failure');
|
||||
this.onFailure(ErrCode.updateFailed);
|
||||
cc.eventManager.removeListener(this._updateListener);
|
||||
this._updateListener = null;
|
||||
@@ -275,7 +304,7 @@ cc.Class({
|
||||
}
|
||||
|
||||
if (needRestart) {
|
||||
console.log('Hotupdate success');
|
||||
// console.log('Hotupdate success');
|
||||
cc.eventManager.removeListener(this._updateListener);
|
||||
this._updateListener = null;
|
||||
var searchPaths = jsb.fileUtils.getSearchPaths();
|
||||
@@ -288,6 +317,12 @@ cc.Class({
|
||||
}
|
||||
},
|
||||
|
||||
//
|
||||
clearHotupdateCache() {
|
||||
let storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + this._hotUpdateName);
|
||||
jsb.fileUtils.removeDirectory(storagePath);
|
||||
},
|
||||
|
||||
// update (dt) {},
|
||||
|
||||
onDestroy() {
|
||||
|
||||
9
HotUpdateDemo/assets/Script/Uint8ArrayMD5.js.meta
Normal file
9
HotUpdateDemo/assets/Script/Uint8ArrayMD5.js.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.5",
|
||||
"uuid": "b723dc8e-07ac-4a35-b0d9-663f49b82e42",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
||||
Reference in New Issue
Block a user