import { Dictionary } from "../utils/Dictionary"; import { IBenzAsset } from "./IBenzAsset"; import { Log, LOG_TAG } from "../../framework/log/Log"; export class BenzAssetManager { private static readonly MAX_RETRY_COUNT: number = 3; private static _instance: BenzAssetManager = new BenzAssetManager(); private _cacheAssetDic = new Dictionary(); private _currentIndex: number = 0; private _lastIndex: number = 0; private _currentAssets: IBenzAsset[]; private _retryCount: number = 0; public static getInstance(): BenzAssetManager { return this._instance; } public loadResource(asset: IBenzAsset, completeCallback: (resource: any) => void, errorCallback: (error: Error) => void = null): void { if(this.hasAsset(asset.fileName)){ Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Load Hit Cache: {0}"+ asset.fileName); completeCallback(this.getAsset(asset.fileName)); return; } cc.loader.loadRes(asset.fileName, asset.type, function (error:Error, resource: any) { try { if(error){ Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Load Error: {0}"+ asset.fileName); if (errorCallback){ errorCallback(error); } }else{ completeCallback(resource); cc.loader.setAutoReleaseRecursively(resource, asset.isAutoRelease); } } catch (e) { Log.error(e); if (errorCallback){ errorCallback(e); } } }); } public loadSceneResource(assetArray: IBenzAsset[], onProgress?: (completedCount: number, totalCount: number, item: any) => void, onLoaded?: (error: Error) => void): void { if(!assetArray || assetArray.length <= 0){ if(onLoaded){ onLoaded(null); } return; } let self = this; self._currentIndex = 0; self._lastIndex = assetArray.length - 1; self._currentAssets = assetArray; self.loadSceneResourceHandle(assetArray[self._currentIndex], onProgress, onLoaded); } protected loadSceneResourceHandle(asset: IBenzAsset, onProgress?: (completedCount: number, totalCount: number, item: any) => void, onLoaded?: (error: Error) => void): void { Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Load {0}"+ asset.fileName); let self = this; this.loadResource(asset, function(resource: any){ if(!asset.isAutoRelease){ Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Add {0}"+ asset.fileName); self.addAsset(asset.fileName, resource); } if(self._currentIndex >= self._lastIndex){ if(onLoaded){ onLoaded(null); } return; } if(onProgress){ onProgress(self._currentIndex + 1, self._lastIndex + 1, resource); } self._retryCount = 0; self._currentIndex++; self.loadSceneResourceHandle(self._currentAssets[self._currentIndex], onProgress, onLoaded); }, function(error: Error){ if(self._retryCount >= BenzAssetManager.MAX_RETRY_COUNT){ if(onLoaded){ onLoaded(error); } return; } self._retryCount++; self.loadSceneResourceHandle(self._currentAssets[self._currentIndex], onProgress, onLoaded); }); } public getAsset(fileName: string): cc.Asset { return this._cacheAssetDic.get(fileName); } public hasAsset(fileName: string): boolean { return this._cacheAssetDic.containsKey(fileName); } public addAsset(fileName: string, asset: cc.Asset): void { if (!asset || !asset.isValid){ Log.log(LOG_TAG.DEBUG,"[BenzAssetManager] Asset {0} is null"+ fileName); return; } cc.loader.setAutoReleaseRecursively(asset, false); this._cacheAssetDic.set(fileName, asset); } public removeAsset(fileName: string): void { if (!this._cacheAssetDic.containsKey(fileName)){ return; } cc.loader.setAutoReleaseRecursively(this._cacheAssetDic.get(fileName), true); this._cacheAssetDic.remove(fileName); } public clearCache(): void { // this._cacheAssetDic.forEach(function(k:string, v:cc.SpriteAtlas) { // if(v){ // var deps = cc.loader.getDependsRecursively(v); // cc.loader.release(deps); // v.destroy(); // } // }); // this._cacheAssetDic.clear(); } }