A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(assets): what a restaged .bin really is, answered in one place
esengine2262896c1cd5
diff --git a/sdk/src/asset/compressed.ts b/sdk/src/asset/compressed.tsindex 7fab4067e..5a8543ca0 100644--- a/sdk/src/asset/compressed.ts+++ b/sdk/src/asset/compressed.ts@@ -15,6 +15,7 @@* entry backs the non-WebGL2 fallback path instead.*/import { TextureContent, type ESEngineModule } from '../wasm';+import { unwrapRestagedPath } from '../assetTypes';import { applyBoundTextureSampling, handOverNewTexture } from './glTextureUpload';// =============================================================================@@ -43,13 +44,11 @@ export function isKtx2(bytes: Uint8Array): boolean {return true;}-/** True if `path` names a KTX2 container: its own extension, or the- * `.ktx2.bin` spelling the WeChat export stages (WeChat's code-package- * suffix whitelist has no `ktx2`; `bin` is whitelisted, and the compound- * suffix keeps the container's identity in the name). */+/** True if `path` names a KTX2 container, including the `.ktx2.bin` a packer+ * that refuses the real suffix is given (what restaging looks like is+ * `unwrapRestagedPath`'s to know, not every reader's). */export function isKtx2Path(path: string): boolean {- const p = path.toLowerCase();- return p.endsWith('.ktx2') || p.endsWith('.ktx2.bin');+ return unwrapRestagedPath(path).endsWith('.ktx2');}// =============================================================================diff --git a/sdk/src/assetTypes.ts b/sdk/src/assetTypes.tsindex 103a5e0d0..e7f7da40b 100644--- a/sdk/src/assetTypes.ts+++ b/sdk/src/assetTypes.ts@@ -148,9 +148,38 @@ for (const entry of ASSET_TYPE_REGISTRY) {}}+/**+ * A host whose packer refuses a custom suffix takes the file under `.bin`+ * (exportMiniGame's restage). What it IS is still whatever the real suffix says,+ * so read through that wrapper — but only when a known extension is under it, or+ * a file genuinely named `foo.bin` would read as one with no extension at all.+ */function extractExtension(extensionOrPath: string): string {- const dotIndex = extensionOrPath.lastIndexOf('.');- return dotIndex >= 0 ? extensionOrPath.substring(dotIndex + 1).toLowerCase() : extensionOrPath.toLowerCase();+ const lower = extensionOrPath.toLowerCase();+ const last = (v: string): string => {+ const dot = v.lastIndexOf('.');+ return dot >= 0 ? v.substring(dot + 1) : v;+ };++ if (lower.endsWith('.bin')) {+ const under = last(lower.slice(0, -'.bin'.length));+ if (allExtensions.has(under)) return under;+ }+ return last(lower);+}++/**+ * A path with the restage `.bin` wrapper removed, when it wraps an extension this+ * registry knows. Readers that care about a specific format ask this rather than+ * spelling both paths — one place learns what a restaged file looks like.+ */+export function unwrapRestagedPath(path: string): string {+ const lower = path.toLowerCase();+ if (!lower.endsWith('.bin')) return lower;+ const under = lower.slice(0, -'.bin'.length);+ const dot = under.lastIndexOf('.');+ const ext = dot >= 0 ? under.substring(dot + 1) : under;+ return allExtensions.has(ext) ? under : lower;}export function getAssetTypeEntry(extensionOrPath: string): AssetTypeEntry | undefined {diff --git a/sdk/tests/assetTypes.test.ts b/sdk/tests/assetTypes.test.tsindex 3c622f5a5..8885f2248 100644--- a/sdk/tests/assetTypes.test.ts+++ b/sdk/tests/assetTypes.test.ts@@ -194,3 +194,24 @@ describe('isCustomExtension', () => {expect(isCustomExtension('file.xyz')).toBe(false);});});+++describe('a file restaged under .bin is still what its real suffix says', () => {+ // A packer that refuses custom suffixes takes the file as `<name>.<real>.bin`,+ // and what it IS is still what the real suffix says.+ it('reads through the .bin wrapper for any known extension', () => {+ expect(getAssetTypeEntry('assets/t.ktx2.bin')?.editorType)+ .toBe(getAssetTypeEntry('assets/t.ktx2')?.editorType);+ expect(getAssetTypeEntry('assets/clip.esanim.bin')?.editorType)+ .toBe(getAssetTypeEntry('assets/clip.esanim')?.editorType);+ expect(getAssetTypeEntry('assets/m.esmaterial.bin')?.editorType)+ .toBe(getAssetTypeEntry('assets/m.esmaterial')?.editorType);+ });++ // …but a file that really is named `.bin` keeps reading as one: stripping+ // unconditionally would leave `foo.bin` looking like a file with no extension.+ it('leaves a real .bin alone', () => {+ expect(getAssetTypeEntry('assets/blob.bin')).toBe(getAssetTypeEntry('assets/other.bin'));+ expect(getAssetTypeEntry('assets/unknown.xyz.bin')).toBeUndefined();+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 903a05c31..8c8e0cecd 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,9 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'refactor(assets): what a restaged .bin really is, answered in one place':+ { internal: 'One reader for a packaging spelling; what resolves is unchanged.' },+"fix(export): the shared mini-game entry names no vendor's API":{ internal: 'Fixes an unreleased commit from this same cycle; the shipped behaviour never had it.' },