@xiacg/exia-assets 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ import { Asset, AssetManager } from 'cc';
2
+
3
+ interface IAssetConfig {
4
+ /** bundle名下的资源路径 必填 */
5
+ path: string;
6
+ /** 资源类型 默认 Asset */
7
+ type?: typeof Asset;
8
+ /** 是否是单个文件 默认是文件夹 */
9
+ isFile?: boolean;
10
+ /** bundle名 默认 resources */
11
+ bundle?: string;
12
+ }
13
+ /** 资源加载的状态类型 */
14
+ declare enum StateType {
15
+ Error = 0,
16
+ Wait = 1,
17
+ Loading = 2,
18
+ Finish = 3
19
+ }
20
+ declare enum ErrorCode {
21
+ /** 文件加载失败 */
22
+ FileLoadFailed = 1,
23
+ /** 资源包加载失败 */
24
+ BundleLoadFailed = 2
25
+ }
26
+
27
+ declare class AssetInfo implements IAssetConfig {
28
+ /** 固定的属性 */
29
+ get type(): typeof Asset;
30
+ get path(): string;
31
+ get isFile(): boolean;
32
+ get bundle(): string;
33
+ get assetBundle(): AssetManager.Bundle;
34
+ /** 可变的属性 */
35
+ get status(): StateType;
36
+ set status(status: StateType);
37
+ }
38
+
39
+ declare class AssetLoaderAgent {
40
+ /**
41
+ * 设置最大并行数量
42
+ * @param {number} parallel 最大并行数量
43
+ */
44
+ set parallel(parallel: number);
45
+ /**
46
+ * 设置失败重试次数
47
+ * @param {number} retry 失败重试次数 默认: 0
48
+ */
49
+ set retry(retry: number);
50
+ /** 通过索引获取 */
51
+ protected getAssetInfo(index: number): AssetInfo;
52
+ protected downloadFaildAnalysis(): void;
53
+ /**
54
+ * @param res 设置回调函数
55
+ * @param {Function} res.complete 加载完成回调
56
+ * @param {Function} res.fail 加载失败回调
57
+ * @param {Function} res.progress 加载进度回调
58
+ */
59
+ setCallbacks(res: {
60
+ complete: () => void;
61
+ fail?: (code: number, msg: string) => void;
62
+ progress?: (percent: number) => void;
63
+ }): void;
64
+ }
65
+
66
+ declare class AssetLoader extends AssetLoaderAgent {
67
+ constructor(batchName?: string);
68
+ /**
69
+ * 开始加载资源
70
+ * @param {IAssetConfig[]} res.configs 资源配置
71
+ */
72
+ start(assetConfigs: IAssetConfig[]): void;
73
+ private onStart;
74
+ /** 重新加载失败的资源 */
75
+ retryDownLoadFailedAssets(): void;
76
+ }
77
+
78
+ declare class AssetPool {
79
+ /** 添加资源 */
80
+ static add(asset: Asset[] | Asset, bundle?: AssetManager.Bundle, batchName?: string): void;
81
+ private static addToBatch;
82
+ static getAllAssetPaths(): string[];
83
+ /**
84
+ * 检查资源是否存在
85
+ * @param path 资源在bundle下的路径
86
+ * @param bundlename 资源bundle名 默认 resources
87
+ */
88
+ static has(path: string, bundlename?: string): boolean;
89
+ /**
90
+ * 获取资源
91
+ * @param path 资源在bundle下的路径
92
+ * @param bundlename 资源bundle名 默认 resources
93
+ */
94
+ static get<T extends Asset>(path: string, bundlename?: string): T;
95
+ /**
96
+ * 按 uuid 判断资源是否存在
97
+ */
98
+ static hasUUID(uuid: string): boolean;
99
+ /**
100
+ * 按 uuid 获取资源
101
+ */
102
+ static getByUUID<T extends Asset>(uuid: string): T;
103
+ /**
104
+ * 按资源加载批次释放资源
105
+ * @param batchName 资源加载批次名 对应 AssetLoader 实例化时传入的 name
106
+ */
107
+ static releaseBatchAssets(batchName: string): void;
108
+ /**
109
+ * 按资源路径释放资源
110
+ * @param path 资源在bundle下的路径
111
+ * @param bundlename 资源bundle名 默认 resources
112
+ */
113
+ static releasePath(path: string, bundlename?: string): void;
114
+ /**
115
+ * 按 bundle、文件夹和资源类型释放资源
116
+ * @param dir 资源在bundle下的路径
117
+ * @param bundlename 资源bundle名 默认 resources
118
+ * @param asset 资源类型 不传表示所有类型的资源
119
+ */
120
+ static releaseDir(dir: string, bundlename?: string, asset?: typeof Asset): Promise<boolean>;
121
+ /**
122
+ * 按 uuid 释放资源
123
+ */
124
+ static releaseUUID(uuid: string): void;
125
+ /**
126
+ * 释放所有加载的资源
127
+ */
128
+ static releaseAll(): void;
129
+ }
130
+
131
+ declare class AssetUtils {
132
+ /** 获取资源数量 */
133
+ static getResourceCount(dir: string, type: typeof Asset, bundle?: AssetManager.Bundle): number;
134
+ /** 获取资源名称 */
135
+ static getUUIDs(dir: string, type: typeof Asset, bundle?: AssetManager.Bundle): string[];
136
+ /** 加载 bundle */
137
+ static loadBundle(bundlename: string): Promise<AssetManager.Bundle>;
138
+ /**
139
+ * 加载单个资源
140
+ * @param bundle 资源包名或资源包
141
+ * @param path 资源路径
142
+ * @param type 资源类型
143
+ * @param callbacks 回调函数
144
+ */
145
+ static loadFile<T extends typeof Asset>(bundle: string | AssetManager.Bundle, path: string, type: T, callbacks?: {
146
+ complete?: (asset: Asset) => void;
147
+ fail?: (code: ErrorCode, msg: string) => void;
148
+ }): void;
149
+ /**
150
+ * 加载文件夹下的资源
151
+ * @param bundle 资源包名或资源包
152
+ * @param path 资源路径
153
+ * @param type 资源类型
154
+ * @param callbacks 回调函数
155
+ * @param callbacks.progress 进度回调 value: 已完成数量 total: 总数量
156
+ */
157
+ static loadDir<T extends typeof Asset>(bundle: string | AssetManager.Bundle, path: string, type: T, callbacks?: {
158
+ complete?: (assets: Asset[]) => void;
159
+ fail?: (code: ErrorCode, msg: string) => void;
160
+ progress?: (value: number, total: number) => void;
161
+ }): void;
162
+ }
163
+
164
+ export { AssetLoader, AssetPool, AssetUtils, ErrorCode };
165
+ export type { IAssetConfig };
@@ -0,0 +1 @@
1
+ "use strict";var t,e,s=require("cc");!function(t){t[t.Error=0]="Error",t[t.Wait=1]="Wait",t[t.Loading=2]="Loading",t[t.Finish=3]="Finish"}(t||(t={})),exports.ErrorCode=void 0,(e=exports.ErrorCode||(exports.ErrorCode={}))[e.FileLoadFailed=1]="FileLoadFailed",e[e.BundleLoadFailed=2]="BundleLoadFailed";class a{constructor(e,a,i=t.Wait){this._type=s.Asset,this._path="",this._isFile=!1,this._bundle="resources",this._assetBundle=s.resources,this._status=t.Wait,this._type=e.type||s.Asset,this._path=e.path,this._isFile=e.isFile||!1,this._bundle=e.bundle||"resources",this._assetBundle=a,this._status=i}get type(){return this._type}get path(){return this._path}get isFile(){return this._isFile}get bundle(){return this._bundle}get assetBundle(){return this._assetBundle}get status(){return this._status}set status(t){this._status=t}}class i{constructor(){this._maxParallel=10,this._maxRetry=0,this._assetInfos=[],this._completeCounts=new Map,this._isComplete=!1}set parallel(t){this._maxParallel=Math.max(1,t)}set retry(t){this._maxRetry=t}initialize(){this._isComplete=!1,this._assetInfos.length=0,this._completeCounts.clear()}setTotalCount(t,e,s){let a=`${t}:${e}`;this._completeCounts.set(a,{value:0,total:s})}updateCompleteCount(t,e,s,a){let i=`${t}:${e}`;if(this._completeCounts.has(i)){let t=this._completeCounts.get(i);t.value=Math.min(s,a),t.total=a}else this._completeCounts.set(i,{value:Math.min(s,a),total:a});this.progressUpdate()}addAssetInfo(t){this._assetInfos.push(t)}getAssetInfo(t){return this._assetInfos[t]}getAssetsCount(){return this._assetInfos.length}getFirstWaitIndex(){return this._assetInfos.findIndex(e=>e.status==t.Wait)}isAllFinished(){return this._assetInfos.every(e=>e.status==t.Finish)}resetErrorAssets(){let e=0;for(const s of this._assetInfos)s.status==t.Error&&(s.status=t.Wait,e++);return e}downloadFaildAnalysis(){let e=[];for(const s of this._assetInfos)s.status==t.Error&&e.push(`bundle:${s.bundle} path:${s.path}`);let s=`加载失败资源:\n${e.join("\n")}`;this.failCallback(exports.ErrorCode.FileLoadFailed,s)}setCallbacks(t){this._complete=t.complete||(()=>{}),this._fail=t.fail||(()=>{}),this._progress=t.progress||(()=>{})}progressUpdate(){if(!this._progress)return;let t=0,e=0;for(const{value:s,total:a}of this._completeCounts.values())t+=s,e+=a;this._progress(Math.max(Math.min(t/e,1),0))}completeAll(){var t;this._isComplete||(this._isComplete=!0,null===(t=this._complete)||void 0===t||t.call(this))}failCallback(t,e){var s;null===(s=this._fail)||void 0===s||s.call(this,t,e)}}class l{static getResourceCount(t,e,a=s.resources){return"/"===(t=s.assetManager.utils.normalize(t))[t.length-1]&&(t=t.slice(0,-1)),a.getDirWithPath(t,e).length}static getUUIDs(t,e,a=s.resources){let i=[],l=s.assetManager.utils.normalize(t);"/"===l[l.length-1]&&(l=l.slice(0,-1));let r=a.getDirWithPath(l,e);for(const t of r)i.push(t.uuid);return i}static loadBundle(t){return new Promise((e,a)=>{let i=s.assetManager.getBundle(t);i?e(i):s.assetManager.loadBundle(t,(t,s)=>{t?a(t):e(s)})})}static loadFile(t,e,s,a){"string"==typeof t?l.loadBundle(t).then(t=>{t.load(e,s,(s,i)=>{var l,r;s?null===(l=null==a?void 0:a.fail)||void 0===l||l.call(a,exports.ErrorCode.FileLoadFailed,`加载资源失败【bundle:${t} path:${e}】`):null===(r=null==a?void 0:a.complete)||void 0===r||r.call(a,i)})}).catch(e=>{var s;null===(s=null==a?void 0:a.fail)||void 0===s||s.call(a,exports.ErrorCode.BundleLoadFailed,`加载bundle[${t}]失败`)}):t.load(e,s,(s,i)=>{var l,r;s?null===(l=null==a?void 0:a.fail)||void 0===l||l.call(a,exports.ErrorCode.FileLoadFailed,`加载资源失败【bundle:${t.name} path:${e}】`):null===(r=null==a?void 0:a.complete)||void 0===r||r.call(a,i)})}static loadDir(t,e,s,a){"string"==typeof t?l.loadBundle(t).then(t=>{t.loadDir(e,s,(t,e)=>{var s;null===(s=null==a?void 0:a.progress)||void 0===s||s.call(a,t,e)},(s,i)=>{var l,r;s?null===(l=null==a?void 0:a.fail)||void 0===l||l.call(a,exports.ErrorCode.FileLoadFailed,`加载文件夹失败【bundle:${t} path:${e}】`):null===(r=null==a?void 0:a.complete)||void 0===r||r.call(a,i)})}).catch(e=>{var s;null===(s=null==a?void 0:a.fail)||void 0===s||s.call(a,exports.ErrorCode.BundleLoadFailed,`加载bundle[${t}]失败`)}):t.loadDir(e,s,(t,e)=>{var s;null===(s=null==a?void 0:a.progress)||void 0===s||s.call(a,t,e)},(s,i)=>{var l,r;s?null===(l=null==a?void 0:a.fail)||void 0===l||l.call(a,exports.ErrorCode.FileLoadFailed,`加载文件夹失败【bundle:${t} path:${e}】`):null===(r=null==a?void 0:a.complete)||void 0===r||r.call(a,i)})}}class r{static add(t,e=s.resources,a=""){if(Array.isArray(t))for(const s of t)this.add(s,e,a);else{let s=t.uuid||t._uuid;if(this._uuidToName.has(s))return;t.addRef();let i=e.getAssetInfo(s),l=this.getKey(i.path,e.name);this._uuidToName.set(s,l),this._assets.set(l,t),this.addToBatch(a,l,t)}}static addToBatch(t,e,s){t&&(this._batchAssetNames.has(t)||this._batchAssetNames.set(t,new Set),this._batchAssetNames.get(t).add(e),this._assetToBatchName.set(s,t))}static getAllAssetPaths(){return Array.from(this._assets.keys())}static has(t,e="resources"){let s=this.getKey(t,e);return this._assets.has(s)}static get(t,e="resources"){let s=this.getKey(t,e);return this._assets.has(s)||console.warn(`获取资源失败: 资源 bundle:${e}, path:${t} 未加载`),this._assets.get(s)}static hasUUID(t){return!!this._uuidToName.has(t)}static getByUUID(t){this._uuidToName.has(t)||console.warn(`获取资源失败: 资源 uuid:${t} 未加载`);let e=this._uuidToName.get(t);return this._assets.get(e)}static releaseBatchAssets(t){if(!this._batchAssetNames.has(t))return;let e=this._batchAssetNames.get(t);for(const t of e)this.release(t);this._batchAssetNames.delete(t)}static releasePath(t,e="resources"){let s=this.getKey(t,e);this.release(s)}static releaseDir(t,e="resources",a){return new Promise((i,r)=>{if("resources"==e){let e=l.getUUIDs(t,a,s.resources);for(const t of e)this.releaseUUID(t);i(!0)}else l.loadBundle(e).then(e=>{let s=l.getUUIDs(t,a,e);for(const t of s)this.releaseUUID(t);i(!0)}).catch(t=>{r(!1)})})}static releaseUUID(t){if(this._uuidToName.has(t)){let e=this._uuidToName.get(t);this.release(e)}}static releaseAll(){this._assets.forEach((t,e)=>{t.decRef()}),this._assets.clear(),this._uuidToName.clear(),this._batchAssetNames.clear()}static release(t){if(this._assets.has(t)){let e=this._assets.get(t);if(this._assetToBatchName.has(e)){let s=this._assetToBatchName.get(e);this._batchAssetNames.get(s).delete(t),this._assetToBatchName.delete(e)}this._uuidToName.delete(e.uuid),e.decRef(),this._assets.delete(t)}else console.warn(`释放资源失败: 资源【${t}】未加载`)}static getKey(t,e="resources"){return`${e}:${t}`}}r._assets=new Map,r._uuidToName=new Map,r._batchAssetNames=new Map,r._assetToBatchName=new WeakMap;exports.AssetLoader=class extends i{constructor(t){super(),this._name="",this._parallel=0,this._retry=0,this._initSuccess=!1,this._configs=[],this._name=t||""}start(t){this._configs=t,this.onStart()}onStart(){this.initialize(),this._initSuccess=!1;let t=this._configs.length;for(const e of this._configs){let i=e.bundle||"resources";if("resources"==i){let r=l.getResourceCount(e.path,e.type,s.resources);this.setTotalCount(i,e.path,r),this.addAssetInfo(new a(e,s.resources)),t--,t<=0&&this.initSuccess()}else l.loadBundle(i).then(s=>{let r=l.getResourceCount(e.path,e.type,s);this.setTotalCount(i,e.path,r),this.addAssetInfo(new a(e,s)),t--,t<=0&&this.initSuccess()}).catch(t=>{this._retry<this._maxRetry?(this._retry++,this.onStart()):this.failCallback(exports.ErrorCode.BundleLoadFailed,`加载bundle【${i}】失败`)})}}initSuccess(){this._initSuccess=!0,this._parallel=0;let t=Math.min(this.getAssetsCount(),this._maxParallel);for(let e=0;e<t;e++)this.loadNext()}loadNext(){let t=this.getFirstWaitIndex();t>-1?this.loadItem(t):this.isAllFinished()?this.completeAll():this._parallel>0||(this._retry<this._maxRetry?this.retryLoad():this.downloadFaildAnalysis())}loadItem(e){let s=this.getAssetInfo(e);s.status=t.Loading,this._parallel++,s.isFile?l.loadFile(s.assetBundle,s.path,s.type,{complete:e=>{this._parallel--,s.status=t.Finish,r.add(e,s.assetBundle,this._name),this.updateCompleteCount(s.bundle,s.path,1,1),this.loadNext()},fail:()=>{this._parallel--,s.status=t.Error,this.loadNext()}}):l.loadDir(s.assetBundle,s.path,s.type,{complete:e=>{this._parallel--,s.status=t.Finish,r.add(e,s.assetBundle,this._name),this.loadNext()},fail:()=>{this._parallel--,s.status=t.Error,this.loadNext()},progress:(t,e)=>{t>0&&e>0&&this.updateCompleteCount(s.bundle,s.path,t,e)}})}retryDownLoadFailedAssets(){this._parallel=0,this._retry=0,this._initSuccess?this.retryLoad():(this._retry++,this.onStart())}retryLoad(){this._retry++;let t=this.resetErrorAssets(),e=Math.min(t,this._maxParallel);for(let t=0;t<e;t++)this.loadNext()}},exports.AssetPool=r,exports.AssetUtils=l;
@@ -0,0 +1 @@
1
+ import{Asset as t,resources as s,assetManager as e}from"cc";var a,i;!function(t){t[t.Error=0]="Error",t[t.Wait=1]="Wait",t[t.Loading=2]="Loading",t[t.Finish=3]="Finish"}(a||(a={})),function(t){t[t.FileLoadFailed=1]="FileLoadFailed",t[t.BundleLoadFailed=2]="BundleLoadFailed"}(i||(i={}));class l{constructor(e,i,l=a.Wait){this._type=t,this._path="",this._isFile=!1,this._bundle="resources",this._assetBundle=s,this._status=a.Wait,this._type=e.type||t,this._path=e.path,this._isFile=e.isFile||!1,this._bundle=e.bundle||"resources",this._assetBundle=i,this._status=l}get type(){return this._type}get path(){return this._path}get isFile(){return this._isFile}get bundle(){return this._bundle}get assetBundle(){return this._assetBundle}get status(){return this._status}set status(t){this._status=t}}class o{constructor(){this._maxParallel=10,this._maxRetry=0,this._assetInfos=[],this._completeCounts=new Map,this._isComplete=!1}set parallel(t){this._maxParallel=Math.max(1,t)}set retry(t){this._maxRetry=t}initialize(){this._isComplete=!1,this._assetInfos.length=0,this._completeCounts.clear()}setTotalCount(t,s,e){let a=`${t}:${s}`;this._completeCounts.set(a,{value:0,total:e})}updateCompleteCount(t,s,e,a){let i=`${t}:${s}`;if(this._completeCounts.has(i)){let t=this._completeCounts.get(i);t.value=Math.min(e,a),t.total=a}else this._completeCounts.set(i,{value:Math.min(e,a),total:a});this.progressUpdate()}addAssetInfo(t){this._assetInfos.push(t)}getAssetInfo(t){return this._assetInfos[t]}getAssetsCount(){return this._assetInfos.length}getFirstWaitIndex(){return this._assetInfos.findIndex(t=>t.status==a.Wait)}isAllFinished(){return this._assetInfos.every(t=>t.status==a.Finish)}resetErrorAssets(){let t=0;for(const s of this._assetInfos)s.status==a.Error&&(s.status=a.Wait,t++);return t}downloadFaildAnalysis(){let t=[];for(const s of this._assetInfos)s.status==a.Error&&t.push(`bundle:${s.bundle} path:${s.path}`);let s=`加载失败资源:\n${t.join("\n")}`;this.failCallback(i.FileLoadFailed,s)}setCallbacks(t){this._complete=t.complete||(()=>{}),this._fail=t.fail||(()=>{}),this._progress=t.progress||(()=>{})}progressUpdate(){if(!this._progress)return;let t=0,s=0;for(const{value:e,total:a}of this._completeCounts.values())t+=e,s+=a;this._progress(Math.max(Math.min(t/s,1),0))}completeAll(){var t;this._isComplete||(this._isComplete=!0,null===(t=this._complete)||void 0===t||t.call(this))}failCallback(t,s){var e;null===(e=this._fail)||void 0===e||e.call(this,t,s)}}class h{static getResourceCount(t,a,i=s){return"/"===(t=e.utils.normalize(t))[t.length-1]&&(t=t.slice(0,-1)),i.getDirWithPath(t,a).length}static getUUIDs(t,a,i=s){let l=[],o=e.utils.normalize(t);"/"===o[o.length-1]&&(o=o.slice(0,-1));let h=i.getDirWithPath(o,a);for(const t of h)l.push(t.uuid);return l}static loadBundle(t){return new Promise((s,a)=>{let i=e.getBundle(t);i?s(i):e.loadBundle(t,(t,e)=>{t?a(t):s(e)})})}static loadFile(t,s,e,a){"string"==typeof t?h.loadBundle(t).then(t=>{t.load(s,e,(e,l)=>{var o,h;e?null===(o=null==a?void 0:a.fail)||void 0===o||o.call(a,i.FileLoadFailed,`加载资源失败【bundle:${t} path:${s}】`):null===(h=null==a?void 0:a.complete)||void 0===h||h.call(a,l)})}).catch(s=>{var e;null===(e=null==a?void 0:a.fail)||void 0===e||e.call(a,i.BundleLoadFailed,`加载bundle[${t}]失败`)}):t.load(s,e,(e,l)=>{var o,h;e?null===(o=null==a?void 0:a.fail)||void 0===o||o.call(a,i.FileLoadFailed,`加载资源失败【bundle:${t.name} path:${s}】`):null===(h=null==a?void 0:a.complete)||void 0===h||h.call(a,l)})}static loadDir(t,s,e,a){"string"==typeof t?h.loadBundle(t).then(t=>{t.loadDir(s,e,(t,s)=>{var e;null===(e=null==a?void 0:a.progress)||void 0===e||e.call(a,t,s)},(e,l)=>{var o,h;e?null===(o=null==a?void 0:a.fail)||void 0===o||o.call(a,i.FileLoadFailed,`加载文件夹失败【bundle:${t} path:${s}】`):null===(h=null==a?void 0:a.complete)||void 0===h||h.call(a,l)})}).catch(s=>{var e;null===(e=null==a?void 0:a.fail)||void 0===e||e.call(a,i.BundleLoadFailed,`加载bundle[${t}]失败`)}):t.loadDir(s,e,(t,s)=>{var e;null===(e=null==a?void 0:a.progress)||void 0===e||e.call(a,t,s)},(e,l)=>{var o,h;e?null===(o=null==a?void 0:a.fail)||void 0===o||o.call(a,i.FileLoadFailed,`加载文件夹失败【bundle:${t} path:${s}】`):null===(h=null==a?void 0:a.complete)||void 0===h||h.call(a,l)})}}class r{static add(t,e=s,a=""){if(Array.isArray(t))for(const s of t)this.add(s,e,a);else{let s=t.uuid||t._uuid;if(this._uuidToName.has(s))return;t.addRef();let i=e.getAssetInfo(s),l=this.getKey(i.path,e.name);this._uuidToName.set(s,l),this._assets.set(l,t),this.addToBatch(a,l,t)}}static addToBatch(t,s,e){t&&(this._batchAssetNames.has(t)||this._batchAssetNames.set(t,new Set),this._batchAssetNames.get(t).add(s),this._assetToBatchName.set(e,t))}static getAllAssetPaths(){return Array.from(this._assets.keys())}static has(t,s="resources"){let e=this.getKey(t,s);return this._assets.has(e)}static get(t,s="resources"){let e=this.getKey(t,s);return this._assets.has(e)||console.warn(`获取资源失败: 资源 bundle:${s}, path:${t} 未加载`),this._assets.get(e)}static hasUUID(t){return!!this._uuidToName.has(t)}static getByUUID(t){this._uuidToName.has(t)||console.warn(`获取资源失败: 资源 uuid:${t} 未加载`);let s=this._uuidToName.get(t);return this._assets.get(s)}static releaseBatchAssets(t){if(!this._batchAssetNames.has(t))return;let s=this._batchAssetNames.get(t);for(const t of s)this.release(t);this._batchAssetNames.delete(t)}static releasePath(t,s="resources"){let e=this.getKey(t,s);this.release(e)}static releaseDir(t,e="resources",a){return new Promise((i,l)=>{if("resources"==e){let e=h.getUUIDs(t,a,s);for(const t of e)this.releaseUUID(t);i(!0)}else h.loadBundle(e).then(s=>{let e=h.getUUIDs(t,a,s);for(const t of e)this.releaseUUID(t);i(!0)}).catch(t=>{l(!1)})})}static releaseUUID(t){if(this._uuidToName.has(t)){let s=this._uuidToName.get(t);this.release(s)}}static releaseAll(){this._assets.forEach((t,s)=>{t.decRef()}),this._assets.clear(),this._uuidToName.clear(),this._batchAssetNames.clear()}static release(t){if(this._assets.has(t)){let s=this._assets.get(t);if(this._assetToBatchName.has(s)){let e=this._assetToBatchName.get(s);this._batchAssetNames.get(e).delete(t),this._assetToBatchName.delete(s)}this._uuidToName.delete(s.uuid),s.decRef(),this._assets.delete(t)}else console.warn(`释放资源失败: 资源【${t}】未加载`)}static getKey(t,s="resources"){return`${s}:${t}`}}r._assets=new Map,r._uuidToName=new Map,r._batchAssetNames=new Map,r._assetToBatchName=new WeakMap;class n extends o{constructor(t){super(),this._name="",this._parallel=0,this._retry=0,this._initSuccess=!1,this._configs=[],this._name=t||""}start(t){this._configs=t,this.onStart()}onStart(){this.initialize(),this._initSuccess=!1;let t=this._configs.length;for(const e of this._configs){let a=e.bundle||"resources";if("resources"==a){let i=h.getResourceCount(e.path,e.type,s);this.setTotalCount(a,e.path,i),this.addAssetInfo(new l(e,s)),t--,t<=0&&this.initSuccess()}else h.loadBundle(a).then(s=>{let i=h.getResourceCount(e.path,e.type,s);this.setTotalCount(a,e.path,i),this.addAssetInfo(new l(e,s)),t--,t<=0&&this.initSuccess()}).catch(t=>{this._retry<this._maxRetry?(this._retry++,this.onStart()):this.failCallback(i.BundleLoadFailed,`加载bundle【${a}】失败`)})}}initSuccess(){this._initSuccess=!0,this._parallel=0;let t=Math.min(this.getAssetsCount(),this._maxParallel);for(let s=0;s<t;s++)this.loadNext()}loadNext(){let t=this.getFirstWaitIndex();t>-1?this.loadItem(t):this.isAllFinished()?this.completeAll():this._parallel>0||(this._retry<this._maxRetry?this.retryLoad():this.downloadFaildAnalysis())}loadItem(t){let s=this.getAssetInfo(t);s.status=a.Loading,this._parallel++,s.isFile?h.loadFile(s.assetBundle,s.path,s.type,{complete:t=>{this._parallel--,s.status=a.Finish,r.add(t,s.assetBundle,this._name),this.updateCompleteCount(s.bundle,s.path,1,1),this.loadNext()},fail:()=>{this._parallel--,s.status=a.Error,this.loadNext()}}):h.loadDir(s.assetBundle,s.path,s.type,{complete:t=>{this._parallel--,s.status=a.Finish,r.add(t,s.assetBundle,this._name),this.loadNext()},fail:()=>{this._parallel--,s.status=a.Error,this.loadNext()},progress:(t,e)=>{t>0&&e>0&&this.updateCompleteCount(s.bundle,s.path,t,e)}})}retryDownLoadFailedAssets(){this._parallel=0,this._retry=0,this._initSuccess?this.retryLoad():(this._retry++,this.onStart())}retryLoad(){this._retry++;let t=this.resetErrorAssets(),s=Math.min(t,this._maxParallel);for(let t=0;t<s;t++)this.loadNext()}}export{n as AssetLoader,r as AssetPool,h as AssetUtils,i as ErrorCode};