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(asset): fold WeChat runtime manifest resolver into ManifestModel (T1)
yhhb53c1f2a9644
diff --git a/sdk/src/asset/AddressableManifest.ts b/sdk/src/asset/AddressableManifest.tsindex b6926dd36..e04902d82 100644--- a/sdk/src/asset/AddressableManifest.ts+++ b/sdk/src/asset/AddressableManifest.ts@@ -138,4 +138,54 @@ export class ManifestModel {}return null;}++ // -------------------------------------------------------------------------+ // Resolution (uuid/address key → asset → build path)+ // -------------------------------------------------------------------------++ private keyIndex_: Map<string, AddressableManifestAsset> | null = null;+ private pathIndex_: Map<string, AddressableManifestAsset> | null = null;++ private indexes(): { byKey: Map<string, AddressableManifestAsset>; byPath: Map<string, AddressableManifestAsset> } {+ if (!this.keyIndex_ || !this.pathIndex_) {+ const byKey = new Map<string, AddressableManifestAsset>();+ const byPath = new Map<string, AddressableManifestAsset>();+ for (const g of Object.values(this.manifest.groups)) {+ for (const [key, asset] of Object.entries(g.assets)) {+ // First write wins, mirroring the historical hand-built index+ // (a uuid/path appearing in two groups keeps its first entry).+ if (!byKey.has(key)) byKey.set(key, asset);+ if (!byPath.has(asset.path)) byPath.set(asset.path, asset);+ }+ }+ this.keyIndex_ = byKey;+ this.pathIndex_ = byPath;+ }+ return { byKey: this.keyIndex_, byPath: this.pathIndex_ };+ }++ /** Asset by its group-record key (the uuid in editor/WeChat manifests). */+ assetByKey(key: string): AddressableManifestAsset | null {+ return this.indexes().byKey.get(key) ?? null;+ }++ /** Asset by its build path. */+ assetByPath(path: string): AddressableManifestAsset | null {+ return this.indexes().byPath.get(path) ?? null;+ }++ /**+ * Resolve a serialized asset ref (a uuid, an address, or a path) to its+ * build path. `normalize` maps a ref to its expected build path (e.g.+ * `toBuildPath`); it is applied before the lookups and is the fallback when+ * nothing matches. This is the single manifest→path resolution used by the+ * shipped runtimes — callers never re-walk `groups` to build their own.+ */+ resolvePath(ref: string, normalize: (ref: string) => string = (s) => s): string {+ const { byKey, byPath } = this.indexes();+ const resolved = normalize(ref);+ const entry =+ byKey.get(ref) ?? byKey.get(resolved) ?? byPath.get(resolved) ?? byPath.get(ref);+ return entry ? entry.path : resolved;+ }}diff --git a/sdk/src/wechatRuntime.ts b/sdk/src/wechatRuntime.tsindex 6e6d26f15..ceab5f876 100644--- a/sdk/src/wechatRuntime.ts+++ b/sdk/src/wechatRuntime.ts@@ -14,7 +14,7 @@ import { initRuntime } from './runtimeLoader';import { applyBuildRuntimeConfig, type RuntimeBuildConfig } from './defaults';import { platformReadTextFile, platformReadFile, platformInstantiateWasm, platformLoadImagePixels } from './platform';import { toBuildPath } from './assetTypes';-import type { AddressableManifest } from './asset/AddressableManifest';+import { ManifestModel, type AddressableManifest } from './asset/AddressableManifest';import { createWeChatSideModuleHost, type WeChatSideModuleFactories } from './sideModules';import type { Vec2 } from './types';import type { SceneData } from './scene';@@ -49,39 +49,6 @@ export class WeChatAssetProvider implements RuntimeAssetProvider {}}-// =============================================================================-// Manifest Utilities-// =============================================================================--interface ManifestIndex {- assetIndex: Record<string, { path: string }>;- pathIndex: Record<string, { path: string }>;-}--function buildManifestIndex(manifest: AddressableManifest): ManifestIndex {- const assetIndex: Record<string, { path: string }> = {};- const pathIndex: Record<string, { path: string }> = {};- for (const groupName in manifest.groups) {- const group = manifest.groups[groupName];- for (const uuid in group.assets) {- const entry = group.assets[uuid];- assetIndex[uuid] = entry;- pathIndex[entry.path] = entry;- }- }- return { assetIndex, pathIndex };-}--function createPathResolver(index: ManifestIndex): (ref: string) => string {- const { assetIndex, pathIndex } = index;- return (ref: string): string => {- const resolved = toBuildPath(ref);- const entry = assetIndex[ref] || assetIndex[resolved]- || pathIndex[resolved] || pathIndex[ref];- return entry ? entry.path : resolved;- };-}-// =============================================================================// Emscripten WASM Instantiation// =============================================================================@@ -136,8 +103,10 @@ export interface WeChatRuntimeConfig {export async function initWeChatRuntime(config: WeChatRuntimeConfig): Promise<void> {const manifestText = await platformReadTextFile('asset-manifest.json');const manifest: AddressableManifest = JSON.parse(manifestText);- const manifestIndex = buildManifestIndex(manifest);- const resolvePath = createPathResolver(manifestIndex);+ // Single manifest model — same query/resolution core the SDK Assets channel+ // uses, instead of a hand-rolled index walk duplicated here.+ const manifestModel = ManifestModel.fromJson(manifest);+ const resolvePath = (ref: string): string => manifestModel.resolvePath(ref, toBuildPath);const canvas = wx.createCanvas();const info = wx.getSystemInfoSync();diff --git a/sdk/tests/manifest-model.test.ts b/sdk/tests/manifest-model.test.tsindex 0485e4b4f..8e2cf446e 100644--- a/sdk/tests/manifest-model.test.ts+++ b/sdk/tests/manifest-model.test.ts@@ -99,3 +99,56 @@ describe('ManifestModel', () => {expect(ManifestModel.empty().allAssets()).toEqual([]);});});++// A uuid-keyed manifest, as the WeChat export emits (group asset record keyed by+// lowercased uuid). Exercises the resolution path the shipped runtime relies on.+function uuidManifest(): AddressableManifest {+ return {+ version: '2.0',+ groups: {+ main: {+ bundleMode: 'local',+ labels: [],+ assets: {+ 'uuid-1': { path: 'images/a.abc123.png', type: 'texture', size: 1, labels: [] },+ 'uuid-2': { path: 'b.png', address: '@b', type: 'texture', size: 2, labels: [] },+ },+ },+ },+ };+}++describe('ManifestModel resolution', () => {+ const model = () => ManifestModel.fromJson(uuidManifest());++ it('looks up an asset by its record key (uuid) and by path', () => {+ expect(model().assetByKey('uuid-1')?.path).toBe('images/a.abc123.png');+ expect(model().assetByKey('missing')).toBeNull();+ expect(model().assetByPath('b.png')?.path).toBe('b.png');+ expect(model().assetByPath('nope')).toBeNull();+ });++ it('resolvePath: key hit on the raw ref', () => {+ expect(model().resolvePath('uuid-1')).toBe('images/a.abc123.png');+ });++ it('resolvePath: key hit after normalize', () => {+ // ref isn't a key, but normalize maps it onto one.+ const r = model().resolvePath('raw', (s) => (s === 'raw' ? 'uuid-1' : s));+ expect(r).toBe('images/a.abc123.png');+ });++ it('resolvePath: path hit (identity normalize)', () => {+ expect(model().resolvePath('b.png')).toBe('b.png');+ });++ it('resolvePath: path hit after normalize (toBuildPath-style)', () => {+ const r = model().resolvePath('b.src', (s) => (s === 'b.src' ? 'b.png' : s));+ expect(r).toBe('b.png');+ });++ it('resolvePath: miss returns the normalized ref', () => {+ expect(model().resolvePath('nope', (s) => `${s}.x`)).toBe('nope.x');+ expect(model().resolvePath('nope')).toBe('nope');+ });+});