From 4ef480e6d6202a14bd15bbdaa25e004ac31674cb Mon Sep 17 00:00:00 2001 From: SwayYan Date: Fri, 15 Nov 2024 11:45:15 +0800 Subject: [PATCH] Fix reource load issue in local downloaded npm package (#2436) * fix(shader-lab): resource load issue in local downloaded NPM package --- packages/core/src/asset/ResourceManager.ts | 8 +++----- tests/src/core/resource/ResourceManager.test.ts | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index b76de6f79b..06824dcc7b 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -514,7 +514,7 @@ export class ResourceManager { let assetBaseURL = baseUrl; if (searchStr) { const params = searchStr.split("&"); - for (let i = 0; i < params.length; i++) { + for (let i = params.length - 1; i >= 0; i--) { const param = params[i]; if (param.startsWith(`q=`)) { queryPath = decodeURIComponent(param.split("=")[1]); @@ -573,11 +573,9 @@ export class ResourceManager { Logger.warn(`refId:${refId} is not find in this._editorResourceConfig.`); return Promise.resolve(null); } - const remoteUrl = resourceConfig.path; - const queryPath = new URL(remoteUrl).search; - let url = resourceConfig.virtualPath + queryPath; + let url = resourceConfig.virtualPath; if (key) { - url += (url.indexOf("?") > -1 ? "&" : "?") + "q=" + key; + url += "?q=" + key; } promise = this.load({ diff --git a/tests/src/core/resource/ResourceManager.test.ts b/tests/src/core/resource/ResourceManager.test.ts index a23c188c4e..ee03e106c7 100644 --- a/tests/src/core/resource/ResourceManager.test.ts +++ b/tests/src/core/resource/ResourceManager.test.ts @@ -1,4 +1,4 @@ -import { AssetPromise, ResourceManager, Texture2D } from "@galacean/engine-core"; +import { AssetPromise, AssetType, ResourceManager, Texture2D } from "@galacean/engine-core"; import { WebGLEngine } from "@galacean/engine-rhi-webgl"; import chai, { expect } from "chai"; import spies from "chai-spies"; @@ -81,4 +81,17 @@ describe("ResourceManager", () => { chai.spy.restore(glTFLoader, "load"); }); }); + + describe("gltf subAsset load", () => { + it("invalid q case", async () => { + const loadRes = await engine.resourceManager.load({ + // contains invalid q value cdn url. + url: "https://mdn.alipayobjects.com/huamei_aftkdx/afts/file/A*_Ao1QZtL9fMAAAAAAAAAAAAADteEAQ/mock-project.json", + type: AssetType.Project + }); + expect(loadRes).to.equal(undefined); + }); + + // TODO: case for gltf loader load invalid q url, expect to throw + }); });