123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import DownloadMgr from "./DownloadMgr";
- class LoadMgr{
- constructor() {
- console.log('LoadMgr初始化');
- this.bNeedRestart = true;
- this.downloadProgress = 0;
- this.manifestUrl = null;
- }
- setManifest(manifest){
- this.manifestUrl = manifest;
- }
- startLoading(){
- if(!cc.sys.isNative){ //非app模式
- this.bNeedRestart = false;
- }else{
- //开始检测热更新
- let savePath = "storagePath_0";
- DownloadMgr.createDownLoad(this.manifestUrl, savePath, this.updateCb.bind(this));
- }
- }
- updateCb(event){
- let needRestart = false;
- let failed = false;
- switch (event.getEventCode()) {
- case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
- //this.panel.info.string = 'No local manifest file found, hot update skipped.';
- failed = true;
- break;
- case jsb.EventAssetsManager.UPDATE_PROGRESSION:
- //this.panel.byteProgress.progress = event.getPercent();
- //this.panel.fileProgress.progress = event.getPercentByFile();
- //this.panel.fileLabel.string = event.getDownloadedFiles() + ' / ' + event.getTotalFiles();
- //this.panel.byteLabel.string = event.getDownloadedBytes() + ' / ' + event.getTotalBytes();
- if (isNaN(event.getPercentByFile())) {
- this.downloadProgress = 0;
- } else {
- this.downloadProgress = event.getPercentByFile();
- }
- var msg = event.getMessage();
- if (msg) {
- //this.panel.info.string = 'Updated file: ' + msg;
- // cc.log(event.getPercent()/100 + '% : ' + msg);
- }
- break;
- case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
- case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
- //this.panel.info.string = 'Fail to download manifest file, hot update skipped.';
- failed = true;
- break;
- case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
- //this.panel.info.string = 'Already up to date with the latest remote version.';
- failed = true;
- break;
- case jsb.EventAssetsManager.UPDATE_FINISHED:
- //this.panel.info.string = 'Update finished. ' + event.getMessage();
- needRestart = true;
- break;
- case jsb.EventAssetsManager.UPDATE_FAILED:
- //this.panel.info.string = 'Update failed. ' + event.getMessage();
- //this.panel.retryBtn.active = true;
- //this._updating = false;
- //this._canRetry = true;
- if (DownloadMgr.iFailDownCount >= 10) {
- failed = true;
- }
- break;
- case jsb.EventAssetsManager.ERROR_UPDATING:
- //this.panel.info.string = 'Asset update error: ' + event.getAssetId() + ', ' + event.getMessage();
- break;
- case jsb.EventAssetsManager.ERROR_DECOMPRESS:
- //this.panel.info.string = event.getMessage();
- break;
- default:
- break;
- }
- if (failed) {
- this.bNeedRestart = false;
- }
- if (needRestart) {
- //this._am.setEventCallback(null);
- //this._updateListener = null;
- // Prepend the manifest's search path
- var searchPaths = jsb.fileUtils.getSearchPaths();
- //var newPaths = this._am.getLocalManifest().getSearchPaths();
- var newPaths = DownloadMgr.getSearchPaths();
- console.log(JSON.stringify(newPaths));
- Array.prototype.unshift.apply(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.audioEngine.stopAll();
- setTimeout(() => {
- console.log("更新完成,重启游戏");
- cc.game.restart();
- }, 100);
- }
- }
- }
- let instance = null;
- LoadMgr.getInstance = function(){
- if(!instance){
- instance = new LoadMgr();
- }
- return instance;
- }
- export default LoadMgr.getInstance();
|