class DownloadMgr{ constructor() { console.log('DownloadMgr初始化'); this.fUpdateCb = null; this.manifestUrl = null; this._storagePath = null; //存储路径 } createDownLoad(manifestUrl, path, cb){ if(!cc.sys.isNative) return; this.manifestUrl = manifestUrl; this.fUpdateCb = cb; this.savePath = path || "storagePath_0"; this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + this.savePath); this.versionCompareHandle = function(versionA, versionB){ let vA = versionA.split('.'); let vB = versionB.split('.'); for(let i=0; i < vA.length; ++i){ let a = parseInt(vA[i]); let 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._storagePath, this.versionCompareHandle); 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 { return true; } }); if (cc.sys.os === cc.sys.OS_ANDROID) { this._am.setMaxConcurrentTask(2); } this._am.setEventCallback(this.updateCb.bind(this)); if (this._am.getState() === jsb.AssetsManager.State.UNINITED) { // Resolve md5 url let url = this.manifestUrl.nativeUrl; if (cc.loader.md5Pipe) { url = cc.loader.md5Pipe.transformURL(url); } this._am.loadLocalManifest(url); } this._am.update(); this.iFailDownCount = 0; } updateCb(event) { switch (event.getEventCode()) { case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: break; case jsb.EventAssetsManager.UPDATE_PROGRESSION: break; case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST: case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST: break; case jsb.EventAssetsManager.ALREADY_UP_TO_DATE: break; case jsb.EventAssetsManager.UPDATE_FINISHED: break; case jsb.EventAssetsManager.UPDATE_FAILED: if (this.iFailDownCount < 10) { this._am.downloadFailedAssets(); this.iFailDownCount++; } else { this.removeTemStorage(); } break; case jsb.EventAssetsManager.ERROR_UPDATING: break; case jsb.EventAssetsManager.ERROR_DECOMPRESS: break; default: break; } if (this.fUpdateCb) { this.fUpdateCb(event); } } onDestroy() { this._am.setEventCallback(null); } removeTemStorage() { console.log("DownloadMgr removeTemStorage........"); } getSearchPaths() { let newPaths = this._am.getLocalManifest().getSearchPaths(); return newPaths; } } let instance = null; DownloadMgr.getInstance = function(){ if(!instance){ instance = new DownloadMgr(); } return instance; } export default DownloadMgr.getInstance();