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
fix(assets): an empty asset path is no asset, and is asked for by nobody - an empty string in a handle field went to the resolver, came back still a string, was written back as one and asked for the same load again without end; a field of plants whose material had been cleared put it on every cell it grew and the packaged game stopped answering; a path nothing resolves keeps its placeholder
yhhbc2d346dcc0f
diff --git a/sdk/src/asset/assetReferenceTracking.ts b/sdk/src/asset/assetReferenceTracking.tsindex a99d34eb0..505149b68 100644--- a/sdk/src/asset/assetReferenceTracking.ts+++ b/sdk/src/asset/assetReferenceTracking.ts@@ -69,6 +69,9 @@ async function resolveAssetFields(for (const [key, value] of Object.entries(data)) {const wasPath = typeof authored[key] === 'string';if (wasPath) {+ // A path nothing resolved is still a string here, and a string written into a handle field is a path to+ // load: writing it back asked for this same load again, without end. It keeps the placeholder.+ if (typeof value === 'string') continue;if (now[key] === (defaults[key] ?? 0)) patch[key] = value;} else if (value !== authored[key]) {patch[key] = value;diff --git a/sdk/src/ecs/world.ts b/sdk/src/ecs/world.tsindex 638274646..74aae6231 100644--- a/sdk/src/ecs/world.ts+++ b/sdk/src/ecs/world.ts@@ -834,7 +834,7 @@ export class World {if (isBuiltinComponent(component)) {const refs = this.takeAssetRefs_(component, data as Record<string, unknown> | undefined);const merged = this.insertBuiltin_(entity, component, (refs?.clean ?? data) as Partial<ComponentData<C>>) as ComponentData<C>;- if (refs) this.resolveAssetRefs_(entity, component, refs.authored);+ if (refs?.authored) this.resolveAssetRefs_(entity, component, refs.authored);return merged;}return this.insertScript_(entity, component as ComponentDef<any>, data) as ComponentData<C>;@@ -854,7 +854,7 @@ export class World {const refs = this.takeAssetRefs_(component, data as Record<string, unknown>);if (refs) {this.set(entity, component, refs.clean as ComponentData<C>);- this.resolveAssetRefs_(entity, component, refs.authored);+ if (refs.authored) this.resolveAssetRefs_(entity, component, refs.authored);return;}// `set` is insert-or-replace: adding a builtin the entity LACKS must run@@ -928,21 +928,31 @@ export class World {* back as the path while the engine held 0, and nothing drew. A string in* one is a path to load — the same ref a scene file carries — so it leaves* the data here (the field takes its default until the load lands) and goes- * to the resolver.+ * to the resolver. An empty string is no path: it names nothing, so it is the+ * default and nobody is asked to load it (`authored` is null when that is all+ * there was). Sent to the resolver it came back unresolved, was written again+ * as a string, and asked for itself for good.*/private takeAssetRefs_(component: AnyComponentDef, data: Record<string, unknown> | undefined,- ): { clean: Record<string, unknown>; authored: Record<string, unknown> } | null {+ ): { clean: Record<string, unknown>; authored: Record<string, unknown> | null } | null {const fields = (component as { assetFields?: readonly { field: string }[] }).assetFields;if (!data || !fields || fields.length === 0) return null;let clean: Record<string, unknown> | null = null;+ let paths = false;for (const { field } of fields) {const value = data[field];if (typeof value !== 'string') continue;clean ??= { ...data };clean[field] = (component._default as Record<string, unknown>)[field] ?? 0;+ if (value !== '') paths = true;}- return clean ? { clean, authored: data } : null;+ if (!clean) return null;+ if (!paths) return { clean, authored: null };+ // The resolver is handed the paths only: an empty one beside a real one is already the default above.+ const authored: Record<string, unknown> = { ...data };+ for (const { field } of fields) if (authored[field] === '') authored[field] = clean[field];+ return { clean, authored };}private resolveAssetRefs_(entity: Entity, component: AnyComponentDef, authored: Record<string, unknown>): void {diff --git a/sdk/tests/asset-field-paths.test.ts b/sdk/tests/asset-field-paths.test.tsindex 01db3aa1f..b94cebebb 100644--- a/sdk/tests/asset-field-paths.test.ts+++ b/sdk/tests/asset-field-paths.test.ts@@ -106,6 +106,27 @@ describe('asset paths in engine-backed components', () => {expect(world.get(e, Sprite).texture).toBe(7);});+ // An empty string names no asset, and nothing resolves it: it came back from the resolve still a string, was written back+ // as one, and a string in a handle field is a path to load, so the write asked for the same load again, for good. A field+ // of plants whose material had been cleared to '' put it on every cell it grew, and the packaged game stopped answering.+ it('an empty path is no asset, and is asked for by nobody', async () => {+ const mod = createMockModule();+ const world = new World();+ world.connectCpp(mod.getRegistry(), mod);+ const backend = { fetchBinary: vi.fn(async () => new ArrayBuffer(8)), fetchText: vi.fn(async () => ''),+ resolveUrl: (path: string) => `http://test/${path}` } as unknown as Backend;+ const assets = Assets.create({ backend, catalog: Catalog.fromJson(catalogData), module: mockModule });+ const preload = vi.spyOn(assets, 'preloadSceneAssets');+ bindAssetReferenceTracking(world, assets, () => assets);+ const e = world.spawn();+ world.insert(e, Sprite, { texture: '' as never, size: { x: 3, y: 4 } });+ world.set(e, Sprite, { ...world.get(e, Sprite), texture: '' as never });+ await new Promise((r) => setTimeout(r, 120));+ expect(preload).toHaveBeenCalledTimes(0);+ expect(world.get(e, Sprite).texture).toBe(0);+ expect(world.get(e, Sprite).size).toEqual({ x: 3, y: 4 });+ });+// A headless core (a server, the scale suite's world) has no resource manager// and loads nothing. Asking it to failed every path with an Error behind it:// 6000 a scene in the scale suite, which then ran its worker out of memory.