优化Framework.ResMgr抽象代理类使逻辑更整洁
This commit is contained in:
@@ -1,39 +1,41 @@
|
||||
import { Asset, AssetManager, assetManager, resources } from 'cc';
|
||||
import { BundleProxy } from './BundleProxy';
|
||||
import { ResProxy } from './ResProxy';
|
||||
|
||||
/**
|
||||
* 资源管理器
|
||||
* 职责:
|
||||
* - 统一管理资源加载
|
||||
* - 提供从bundle中按路径加载资源的接口
|
||||
* - 支持资源预加载
|
||||
* - 统一管理Bundle和资源
|
||||
* - 提供BundleProxy和ResProxy的创建和管理
|
||||
* - 提供便捷的资源加载接口
|
||||
* - 管理资源缓存和释放
|
||||
*/
|
||||
export class ResMgr {
|
||||
private static _instance: ResMgr | null = null;
|
||||
|
||||
/**
|
||||
* 资源缓存
|
||||
* key: bundleName:path
|
||||
* value: Asset
|
||||
* BundleProxy缓存
|
||||
* key: bundleName
|
||||
* value: BundleProxy
|
||||
*/
|
||||
private _cache: Map<string, Asset>;
|
||||
private _bundleProxies: Map<string, BundleProxy>;
|
||||
|
||||
/**
|
||||
* Bundle缓存
|
||||
* key: bundleName
|
||||
* value: AssetManager.Bundle
|
||||
* ResProxy缓存
|
||||
* key: bundleName:path
|
||||
* value: ResProxy
|
||||
*/
|
||||
private _bundles: Map<string, AssetManager.Bundle>;
|
||||
private _resProxies: Map<string, ResProxy<any>>;
|
||||
|
||||
/**
|
||||
* 构造函数(私有)
|
||||
*/
|
||||
private constructor() {
|
||||
this._cache = new Map<string, Asset>();
|
||||
this._bundles = new Map<string, AssetManager.Bundle>();
|
||||
this._bundleProxies = new Map<string, BundleProxy>();
|
||||
this._resProxies = new Map<string, ResProxy<any>>();
|
||||
|
||||
// 默认添加resources bundle
|
||||
this._bundles.set('resources', resources);
|
||||
this._bundleProxies.set('resources', new BundleProxy('resources', resources));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,27 +48,6 @@ export class ResMgr {
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Bundle
|
||||
* @param bundleName bundle名称
|
||||
*/
|
||||
private getBundle(bundleName: string): AssetManager.Bundle | null {
|
||||
// 从缓存中获取
|
||||
if (this._bundles.has(bundleName)) {
|
||||
return this._bundles.get(bundleName)!;
|
||||
}
|
||||
|
||||
// 使用 assetManager.getBundle() 方法获取已加载的 Bundle
|
||||
const bundle = assetManager.getBundle(bundleName);
|
||||
if (bundle) {
|
||||
this._bundles.set(bundleName, bundle);
|
||||
return bundle;
|
||||
}
|
||||
|
||||
console.error(`[ResMgr] Bundle "${bundleName}" 不存在`);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缓存key
|
||||
*/
|
||||
@@ -74,6 +55,55 @@ export class ResMgr {
|
||||
return `${bundleName}:${path}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建BundleProxy
|
||||
* @param bundleName bundle名称
|
||||
* @returns BundleProxy
|
||||
*/
|
||||
getBundleProxy(bundleName: string): BundleProxy {
|
||||
if (!this._bundleProxies.has(bundleName)) {
|
||||
// 尝试从assetManager获取已加载的bundle
|
||||
const bundle = assetManager.getBundle(bundleName);
|
||||
const bundleProxy = new BundleProxy(bundleName, bundle || undefined);
|
||||
this._bundleProxies.set(bundleName, bundleProxy);
|
||||
}
|
||||
return this._bundleProxies.get(bundleName)!;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载Bundle
|
||||
* @param bundleName bundle名称
|
||||
* @returns Promise<BundleProxy>
|
||||
*/
|
||||
async loadBundle(bundleName: string): Promise<BundleProxy> {
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
await bundleProxy.loadBundle();
|
||||
return bundleProxy;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建ResProxy
|
||||
* @param bundleName bundle名称
|
||||
* @param path 资源路径
|
||||
* @param type 资源类型
|
||||
* @returns ResProxy
|
||||
*/
|
||||
getResProxy<T extends Asset>(
|
||||
bundleName: string,
|
||||
path: string,
|
||||
type: new (...args: any[]) => T
|
||||
): ResProxy<T> {
|
||||
const cacheKey = this.getCacheKey(bundleName, path);
|
||||
|
||||
if (!this._resProxies.has(cacheKey)) {
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
const resProxy = bundleProxy.createResProxy(path, type);
|
||||
this._resProxies.set(cacheKey, resProxy);
|
||||
}
|
||||
|
||||
return this._resProxies.get(cacheKey)! as ResProxy<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载单个资源
|
||||
* @param bundleName bundle名称
|
||||
@@ -81,44 +111,13 @@ export class ResMgr {
|
||||
* @param type 资源类型
|
||||
* @returns Promise<资源>
|
||||
*/
|
||||
load<T extends Asset>(
|
||||
async load<T extends Asset>(
|
||||
bundleName: string,
|
||||
path: string,
|
||||
type: new (...args: any[]) => T
|
||||
): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cacheKey = this.getCacheKey(bundleName, path);
|
||||
|
||||
// 检查缓存
|
||||
if (this._cache.has(cacheKey)) {
|
||||
const cached = this._cache.get(cacheKey) as T;
|
||||
console.log(`[ResMgr] 从缓存加载资源: ${path}`);
|
||||
resolve(cached);
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取bundle
|
||||
const bundle = this.getBundle(bundleName);
|
||||
if (!bundle) {
|
||||
reject(new Error(`Bundle "${bundleName}" 不存在`));
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载资源
|
||||
console.log(`[ResMgr] 开始加载资源: ${bundleName}/${path}`);
|
||||
bundle.load(path, type, (err, asset) => {
|
||||
if (err) {
|
||||
console.error(`[ResMgr] 加载资源失败: ${bundleName}/${path}`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// 缓存资源
|
||||
this._cache.set(cacheKey, asset as Asset);
|
||||
console.log(`[ResMgr] 资源加载成功: ${bundleName}/${path}`);
|
||||
resolve(asset as T);
|
||||
});
|
||||
});
|
||||
const resProxy = this.getResProxy(bundleName, path, type);
|
||||
return resProxy.load();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,43 +128,14 @@ export class ResMgr {
|
||||
* @param onProgress 进度回调
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
preload<T extends Asset>(
|
||||
async preload<T extends Asset>(
|
||||
bundleName: string,
|
||||
path: string,
|
||||
type: new (...args: any[]) => T,
|
||||
onProgress?: (finished: number, total: number) => void
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 获取bundle
|
||||
const bundle = this.getBundle(bundleName);
|
||||
if (!bundle) {
|
||||
reject(new Error(`Bundle "${bundleName}" 不存在`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[ResMgr] 开始预加载资源: ${bundleName}/${path}`);
|
||||
|
||||
// 预加载资源
|
||||
bundle.preload(
|
||||
path,
|
||||
type,
|
||||
(finished, total) => {
|
||||
if (onProgress) {
|
||||
onProgress(finished, total);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.error(`[ResMgr] 预加载资源失败: ${bundleName}/${path}`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[ResMgr] 资源预加载成功: ${bundleName}/${path}`);
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
const resProxy = this.getResProxy(bundleName, path, type);
|
||||
return resProxy.preload(onProgress);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,42 +143,35 @@ export class ResMgr {
|
||||
* @param bundleName bundle名称
|
||||
* @param dir 目录路径
|
||||
* @param type 资源类型
|
||||
* @param onProgress 进度回调
|
||||
* @returns Promise<资源数组>
|
||||
*/
|
||||
loadDir<T extends Asset>(
|
||||
async loadDir<T extends Asset>(
|
||||
bundleName: string,
|
||||
dir: string,
|
||||
type: new (...args: any[]) => T
|
||||
type: new (...args: any[]) => T,
|
||||
onProgress?: (finished: number, total: number) => void
|
||||
): Promise<T[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 获取bundle
|
||||
const bundle = this.getBundle(bundleName);
|
||||
if (!bundle) {
|
||||
reject(new Error(`Bundle "${bundleName}" 不存在`));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[ResMgr] 开始加载目录: ${bundleName}/${dir}`);
|
||||
|
||||
// 加载目录
|
||||
bundle.loadDir(dir, type, (err, assets) => {
|
||||
if (err) {
|
||||
console.error(`[ResMgr] 加载目录失败: ${bundleName}/${dir}`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// 缓存所有资源
|
||||
assets.forEach((asset) => {
|
||||
const path = `${dir}/${asset.name}`;
|
||||
const cacheKey = this.getCacheKey(bundleName, path);
|
||||
this._cache.set(cacheKey, asset);
|
||||
});
|
||||
|
||||
console.log(`[ResMgr] 目录加载成功: ${bundleName}/${dir}, 共 ${assets.length} 个资源`);
|
||||
resolve(assets as T[]);
|
||||
});
|
||||
});
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
return bundleProxy.loadDir(dir, type, onProgress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 预加载目录
|
||||
* @param bundleName bundle名称
|
||||
* @param dir 目录路径
|
||||
* @param type 资源类型
|
||||
* @param onProgress 进度回调
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
async preloadDir<T extends Asset>(
|
||||
bundleName: string,
|
||||
dir: string,
|
||||
type: new (...args: any[]) => T,
|
||||
onProgress?: (finished: number, total: number) => void
|
||||
): Promise<void> {
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
return bundleProxy.preloadDir(dir, type, onProgress);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -219,17 +182,16 @@ export class ResMgr {
|
||||
release(bundleName: string, path: string): void {
|
||||
const cacheKey = this.getCacheKey(bundleName, path);
|
||||
|
||||
// 从缓存中移除
|
||||
if (this._cache.has(cacheKey)) {
|
||||
const asset = this._cache.get(cacheKey)!;
|
||||
this._cache.delete(cacheKey);
|
||||
|
||||
// 释放资源
|
||||
const bundle = this.getBundle(bundleName);
|
||||
if (bundle) {
|
||||
bundle.release(path);
|
||||
console.log(`[ResMgr] 释放资源: ${bundleName}/${path}`);
|
||||
}
|
||||
// 从ResProxy缓存中获取并释放
|
||||
if (this._resProxies.has(cacheKey)) {
|
||||
const resProxy = this._resProxies.get(cacheKey)!;
|
||||
resProxy.release();
|
||||
// 可选:从缓存中移除ResProxy
|
||||
// this._resProxies.delete(cacheKey);
|
||||
} else {
|
||||
// 如果没有ResProxy,直接从BundleProxy释放
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
bundleProxy.release(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,55 +201,136 @@ export class ResMgr {
|
||||
* @param dir 目录路径
|
||||
*/
|
||||
releaseDir(bundleName: string, dir: string): void {
|
||||
// 释放所有以dir开头的缓存资源
|
||||
// 释放所有以dir开头的ResProxy
|
||||
const prefix = `${bundleName}:${dir}`;
|
||||
const keysToDelete: string[] = [];
|
||||
|
||||
this._cache.forEach((_, key) => {
|
||||
this._resProxies.forEach((resProxy, key) => {
|
||||
if (key.startsWith(prefix)) {
|
||||
resProxy.release();
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
// 可选:从缓存中移除ResProxy
|
||||
keysToDelete.forEach((key) => {
|
||||
this._cache.delete(key);
|
||||
// this._resProxies.delete(key);
|
||||
});
|
||||
|
||||
// 释放目录
|
||||
const bundle = this.getBundle(bundleName);
|
||||
if (bundle) {
|
||||
bundle.releaseAll();
|
||||
console.log(`[ResMgr] 释放目录资源: ${bundleName}/${dir}, 共 ${keysToDelete.length} 个资源`);
|
||||
}
|
||||
// 从BundleProxy释放目录
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
bundleProxy.releaseDir(dir);
|
||||
|
||||
console.log(`[ResMgr] 释放目录资源: ${bundleName}/${dir}, 共 ${keysToDelete.length} 个资源`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放Bundle的所有资源
|
||||
* @param bundleName bundle名称
|
||||
*/
|
||||
releaseBundle(bundleName: string): void {
|
||||
// 释放该Bundle下所有的ResProxy
|
||||
const prefix = `${bundleName}:`;
|
||||
const keysToDelete: string[] = [];
|
||||
|
||||
this._resProxies.forEach((resProxy, key) => {
|
||||
if (key.startsWith(prefix)) {
|
||||
resProxy.release();
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
// 从缓存中移除ResProxy
|
||||
keysToDelete.forEach((key) => {
|
||||
this._resProxies.delete(key);
|
||||
});
|
||||
|
||||
// 释放BundleProxy的所有资源
|
||||
const bundleProxy = this.getBundleProxy(bundleName);
|
||||
bundleProxy.releaseAll();
|
||||
|
||||
console.log(`[ResMgr] 释放Bundle所有资源: ${bundleName}, 共 ${keysToDelete.length} 个`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放所有资源
|
||||
*/
|
||||
releaseAll(): void {
|
||||
console.log(`[ResMgr] 释放所有资源, 共 ${this._cache.size} 个`);
|
||||
this._cache.clear();
|
||||
console.log(`[ResMgr] 释放所有资源, 共 ${this._resProxies.size} 个ResProxy`);
|
||||
|
||||
// 释放所有bundle的资源
|
||||
this._bundles.forEach((bundle, name) => {
|
||||
// 释放所有ResProxy
|
||||
this._resProxies.forEach((resProxy) => {
|
||||
resProxy.release();
|
||||
});
|
||||
this._resProxies.clear();
|
||||
|
||||
// 释放所有BundleProxy的资源(除了resources)
|
||||
this._bundleProxies.forEach((bundleProxy, name) => {
|
||||
if (name !== 'resources') {
|
||||
bundle.releaseAll();
|
||||
bundleProxy.releaseAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存大小
|
||||
* 获取缓存的ResProxy数量
|
||||
*/
|
||||
getCacheSize(): number {
|
||||
return this._cache.size;
|
||||
return this._resProxies.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的BundleProxy数量
|
||||
*/
|
||||
getBundleCount(): number {
|
||||
return this._bundleProxies.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存(不释放资源)
|
||||
*/
|
||||
clearCache(): void {
|
||||
console.log(`[ResMgr] 清空缓存, 共 ${this._cache.size} 个`);
|
||||
this._cache.clear();
|
||||
console.log(`[ResMgr] 清空缓存, 共 ${this._resProxies.size} 个ResProxy`);
|
||||
|
||||
// 清空所有ResProxy的缓存
|
||||
this._resProxies.forEach((resProxy) => {
|
||||
resProxy.clearCache();
|
||||
});
|
||||
this._resProxies.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁Bundle
|
||||
* @param bundleName bundle名称
|
||||
*/
|
||||
destroyBundle(bundleName: string): void {
|
||||
if (bundleName === 'resources') {
|
||||
console.warn('[ResMgr] 不能销毁resources bundle');
|
||||
return;
|
||||
}
|
||||
|
||||
// 释放并移除该Bundle的所有ResProxy
|
||||
const prefix = `${bundleName}:`;
|
||||
const keysToDelete: string[] = [];
|
||||
|
||||
this._resProxies.forEach((resProxy, key) => {
|
||||
if (key.startsWith(prefix)) {
|
||||
resProxy.release();
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
keysToDelete.forEach((key) => {
|
||||
this._resProxies.delete(key);
|
||||
});
|
||||
|
||||
// 销毁BundleProxy
|
||||
if (this._bundleProxies.has(bundleName)) {
|
||||
const bundleProxy = this._bundleProxies.get(bundleName)!;
|
||||
bundleProxy.destroy();
|
||||
this._bundleProxies.delete(bundleName);
|
||||
}
|
||||
|
||||
console.log(`[ResMgr] 销毁Bundle: ${bundleName}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user