123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- const JSB_MD5 = require("jsb_md5");
- // var BundleLoaderManager = require("BundleLoaderManager");
- cc.Class({
- extends: cc.Component,
- properties: {
- _updating: false,
- _canRetry: false,
- },
- goPlanA(Asset, name, refreshnNode) {
- this.bundleName = name;
- this.manifestUrl = Asset;
- this.refreshnNode = refreshnNode;
- this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset/assets/' + this.bundleName);
- this.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, this.versionCompareHandle);
- console.log(this._am._tempVersionPath);
- this._am.setVerifyCallback(function (path, asset) {
- var compressed = asset.compressed;
- var expectedMD5 = asset.md5;
- var relativePath = asset.path;
- var size = asset.size;
- if (compressed) {
- return true;
- }
- else {
- var resMD5 = JSB_MD5(jsb.fileUtils.getDataFromFile(path));
- return asset.md5 == resMD5;
- }
- });
- if (this.versionLabel) {
- this.versionLabel.string = `src:${this._am.getLocalManifest().getVersion()}`;
- }
- //初始化脚本版本信息
- if (cc.sys.os === cc.sys.OS_ANDROID) {
- //一些安卓设备不支持同时下载文件过多
- this._am.setMaxConcurrentTask(2);
- } else {
- this._am.setMaxConcurrentTask(2);
- }
- },
- onDestroy() {
- if (this._am) {
- this._am.setEventCallback(null);
- this._am = null;
- }
- },
- showLog(msg) {
- cc.log('[HotUpdateModule][showLog]----' + msg);
- },
- retry() {
- if (!this._updating && this._canRetry) {
- this._canRetry = false;
- this._am.downloadFailedAssets();
- }
- },
- updateCallback(event) {
- var updateOver = 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, msg);
- 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("已是最新版本.");
- updateOver = true;
- break;
- //更新结束
- case jsb.EventAssetsManager.UPDATE_FINISHED:
- this.showLog("更新结束." + event.getMessage());
- this.disPatchRateEvent(1);
- updateOver = 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;
- this.hotUpdateFinish(false);
- }
- if (updateOver) {
- this.hotUpdateFinish(true);
- }
- },
- 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);
- this.disPatchRateEvent(1);
- 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;
- }
- case jsb.EventAssetsManager.UPDATE_FINISHED:
- this.showLog("Manifest已更新.");
- this.hotUpdateFinish(true);
- this.disPatchRateEvent(1);
- break;
- default:
- console.log('event.getEventCode():' + event.getEventCode());
- return;
- }
- this._am.setEventCallback(null);
- this._updating = false;
- },
- checkUpdate() {
- if (!cc.sys.isNative) {
- return;
- }
- if (this._updating) {
- cc.log("检测更新中...");
- return;
- }
- if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
- var url = this.manifestUrl.nativeUrl;
- 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.log("热更完成");
- },
- disPatchRateEvent(percent) {
- if (percent > 1) {
- percent = 1;
- }
- BundleLoaderManager.updateRefreshnComp(percent, this.refreshnNode);
- },
- });
|