From 2f56b164029ec7e6b7013b29185c71e38a75a17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 20 Sep 2023 20:01:50 -0700 Subject: [PATCH 1/4] Update ResourceManager.ts --- packages/core/src/asset/ResourceManager.ts | 77 ++++++++++++++-------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index 594ebec844..c443887b8e 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -41,6 +41,8 @@ export class ResourceManager { private _assetPool: Record = Object.create(null); /** Asset url pool, key is the asset path and the value is the asset. */ private _assetUrlPool: Record = Object.create(null); + /** Asset alias pool, key is the asset name and the value is the asset. */ + private _assetAliasPool: Record = Object.create(null); /** Referable resource pool, key is the `instanceID` of resource. */ private _referResourcePool: Record = Object.create(null); @@ -54,7 +56,7 @@ export class ResourceManager { * Create a ResourceManager. * @param engine - Engine to which the current ResourceManager belongs */ - constructor(public readonly engine: Engine) {} + constructor(public readonly engine: Engine) { } /** * Load asset asynchronously through the path. @@ -68,7 +70,7 @@ export class ResourceManager { * @param paths - Path collections * @returns Asset Promise */ - load(paths: string[]): AssetPromise; + load(paths: string[]): AssetPromise; /** * Load the asset asynchronously by asset item information. @@ -82,9 +84,9 @@ export class ResourceManager { * @param assetItems - Asset collection * @returns AssetPromise */ - load(assetItems: LoadItem[]): AssetPromise; + load(assetItems: LoadItem[]): AssetPromise; - load(assetInfo: string | LoadItem | (LoadItem | string)[]): AssetPromise { + load(assetInfo: string | LoadItem | (LoadItem | string)[]): AssetPromise { // single item if (!Array.isArray(assetInfo)) { return this._loadSingleItem(assetInfo); @@ -95,12 +97,12 @@ export class ResourceManager { } /** - * Get the resource from cache by asset url, return the resource object if it loaded, otherwise return null. - * @param url - Resource url + * Get the resource from cache by asset name or url, return the resource object if it loaded, otherwise return null. + * @param aliasOrUrl - Resource name or url * @returns Resource object */ - getFromCache(url: string): T { - return (this._assetUrlPool[url] as T) ?? null; + getFromCache(aliasOrUrl: string): T { + return (this._assetAliasPool[aliasOrUrl] as T) ?? (this._assetUrlPool[aliasOrUrl] as T) ?? null; } /** @@ -196,9 +198,12 @@ export class ResourceManager { /** * @internal */ - _addAsset(path: string, asset: EngineObject): void { - this._assetPool[asset.instanceId] = path; - this._assetUrlPool[path] = asset; + _addAsset(item: string, asset: EngineObject, name?: string): void { + this._assetPool[asset.instanceId] = item; + this._assetUrlPool[item] = asset; + if (name) { + this._setAssetAlias(asset, name); + } } /** @@ -308,7 +313,22 @@ export class ResourceManager { return assetInfo; } - private _loadSingleItem(itemOrURL: LoadItem | string): AssetPromise { + private _setAssetAlias(obj: Object, name?: string) { + if (!name) { + return; + } + const cache = this._assetAliasPool[name]; + if (cache) { + if (obj !== cache) { + console.warn(`Cache already has an asset named "${name}, the newest asset will replace the old one if they have same name."`, obj, "will replace", cache); + } else { + return; + } + } + this._assetAliasPool[name] = obj; + } + + private _loadSingleItem(itemOrURL: LoadItem | string): AssetPromise { const item = this._assignDefaultOptions(typeof itemOrURL === "string" ? { url: itemOrURL } : itemOrURL); // Check url mapping @@ -322,6 +342,7 @@ export class ResourceManager { // Check cache const cacheObject = this._assetUrlPool[assetBaseURL]; if (cacheObject) { + this._setAssetAlias(cacheObject, item.name); // sometimes user could give one resource different names if used in different scenes return new AssetPromise((resolve) => { resolve(this._getResolveResource(cacheObject, paths) as T); }); @@ -344,8 +365,8 @@ export class ResourceManager { if (loadingPromise) { return new AssetPromise((resolve, reject) => { loadingPromise - .then((resource: EngineObject) => { - resolve(resource as T); + .then((resource: T) => { + resolve(resource); }) .catch((error: Error) => { reject(error); @@ -367,7 +388,7 @@ export class ResourceManager { promise.then( (resource: T) => { if (loader.useCache) { - this._addAsset(assetBaseURL, resource as EngineObject); + this._addAsset(assetBaseURL, resource, item.name); } delete loadingPromises[assetBaseURL]; }, @@ -512,7 +533,7 @@ export class ResourceManager { * @param extNames - Name of file extension */ export function resourceLoader(assetType: string, extNames: string[], useCache: boolean = true) { - return >(Target: { new (useCache: boolean): T }) => { + return >(Target: { new(useCache: boolean): T }) => { const loader = new Target(useCache); ResourceManager._addLoader(assetType, loader, extNames); }; @@ -523,18 +544,18 @@ const reEscapeChar = /\\(\\)?/g; const rePropName = RegExp( // Match anything that isn't a dot or bracket. "[^.[\\]]+" + - "|" + - // Or match property names within brackets. - "\\[(?:" + - // Match a non-string expression. - "([^\"'][^[]*)" + - "|" + - // Or match strings (supports escaping characters). - "([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" + - ")\\]" + - "|" + - // Or match "" as the space between consecutive dots or empty brackets. - "(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))", + "|" + + // Or match property names within brackets. + "\\[(?:" + + // Match a non-string expression. + "([^\"'][^[]*)" + + "|" + + // Or match strings (supports escaping characters). + "([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" + + ")\\]" + + "|" + + // Or match "" as the space between consecutive dots or empty brackets. + "(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))", "g" ); From 7925112148b571d77f01243379f5c0d5ba74202d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 20 Sep 2023 20:02:18 -0700 Subject: [PATCH 2/4] Update LoadItem.ts --- packages/core/src/asset/LoadItem.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/core/src/asset/LoadItem.ts b/packages/core/src/asset/LoadItem.ts index 7897f2bd61..dc9d5219cb 100644 --- a/packages/core/src/asset/LoadItem.ts +++ b/packages/core/src/asset/LoadItem.ts @@ -6,6 +6,10 @@ type PickOnlyOne = Keys extends un * Used to describe loading asset. */ export type LoadItem = { + /** + * Resource's alias. + */ + name?: string; /** * Asset Type. */ From e8501bea24e54e390e58bfdfa50f7ef76b7810cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 20 Sep 2023 20:20:21 -0700 Subject: [PATCH 3/4] Update ResourceManager.ts --- packages/core/src/asset/ResourceManager.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index c443887b8e..9838a5812b 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -56,7 +56,7 @@ export class ResourceManager { * Create a ResourceManager. * @param engine - Engine to which the current ResourceManager belongs */ - constructor(public readonly engine: Engine) { } + constructor(public readonly engine: Engine) {} /** * Load asset asynchronously through the path. @@ -86,7 +86,9 @@ export class ResourceManager { */ load(assetItems: LoadItem[]): AssetPromise; - load(assetInfo: string | LoadItem | (LoadItem | string)[]): AssetPromise { + load( + assetInfo: string | LoadItem | (LoadItem | string)[] + ): AssetPromise { // single item if (!Array.isArray(assetInfo)) { return this._loadSingleItem(assetInfo); @@ -320,7 +322,12 @@ export class ResourceManager { const cache = this._assetAliasPool[name]; if (cache) { if (obj !== cache) { - console.warn(`Cache already has an asset named "${name}, the newest asset will replace the old one if they have same name."`, obj, "will replace", cache); + console.warn( + `Cache already has an asset named "${name}", the newest asset will replace the old one if they have same name."`, + obj, + "will replace", + cache + ); } else { return; } From f4fd9858562f0134dde6f2301d2c97377ca65d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E8=81=AA=E6=98=8E?= <601670314@qq.com> Date: Wed, 20 Sep 2023 20:28:01 -0700 Subject: [PATCH 4/4] fix code style --- packages/core/src/asset/ResourceManager.ts | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index 9838a5812b..a8572ceb90 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -540,7 +540,7 @@ export class ResourceManager { * @param extNames - Name of file extension */ export function resourceLoader(assetType: string, extNames: string[], useCache: boolean = true) { - return >(Target: { new(useCache: boolean): T }) => { + return >(Target: { new (useCache: boolean): T }) => { const loader = new Target(useCache); ResourceManager._addLoader(assetType, loader, extNames); }; @@ -551,18 +551,18 @@ const reEscapeChar = /\\(\\)?/g; const rePropName = RegExp( // Match anything that isn't a dot or bracket. "[^.[\\]]+" + - "|" + - // Or match property names within brackets. - "\\[(?:" + - // Match a non-string expression. - "([^\"'][^[]*)" + - "|" + - // Or match strings (supports escaping characters). - "([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" + - ")\\]" + - "|" + - // Or match "" as the space between consecutive dots or empty brackets. - "(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))", + "|" + + // Or match property names within brackets. + "\\[(?:" + + // Match a non-string expression. + "([^\"'][^[]*)" + + "|" + + // Or match strings (supports escaping characters). + "([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" + + ")\\]" + + "|" + + // Or match "" as the space between consecutive dots or empty brackets. + "(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))", "g" );