Skip to content

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.

fix(assets): a core with no resource manager does not load authored asset paths

yhhfe278010dbdb
diff --git a/sdk/src/asset/assetReferenceTracking.ts b/sdk/src/asset/assetReferenceTracking.ts
index 6aa379559..a99d34eb0 100644
--- a/sdk/src/asset/assetReferenceTracking.ts
+++ b/sdk/src/asset/assetReferenceTracking.ts
@@ -8,6 +8,7 @@ import { AssetRefCounter } from './AssetRefCounter';
import { SCENE_FORMAT_VERSION, type SceneData } from '../scene/scene';
import { prepareSpawnOwnership } from '../prefab/preparedAssetOwnership';
import { log } from '../util/logger';
+import { getResourceManager } from '../wasm/resourceManager';
const subscribed = new WeakSet<World>();
@@ -30,7 +31,11 @@ export function bindAssetReferenceTracking(
world.onDespawn(entity => current()?.getRefCounter()?.removeAllRefsForEntity(entity));
world.setAssetFieldResolver((entity, component, authored) => {
const live = current();
- if (live) void resolveAssetFields(world, live, entity, component, authored);
+ // A core with no resource manager (a headless server, a test world) loads
+ // nothing: asking it to turned every authored path into a failed load with
+ // an Error behind it — 6000 a scene in the scale suite, where the JS heap
+ // budget then failed and a worker ran out of memory.
+ if (live && getResourceManager()) void resolveAssetFields(world, live, entity, component, authored);
});
}
diff --git a/sdk/tests/asset-field-paths.test.ts b/sdk/tests/asset-field-paths.test.ts
index f123afa35..01db3aa1f 100644
--- a/sdk/tests/asset-field-paths.test.ts
+++ b/sdk/tests/asset-field-paths.test.ts
@@ -14,8 +14,12 @@ import { World } from '../src/ecs/world';
import { Sprite } from '../src/ecs/component';
import { createMockModule } from './mocks/wasm';
-vi.mock('../src/wasm/resourceManager', () => ({
- requireResourceManager: () => ({
+// One resource manager, answered by both doors as a real core answers them: the
+// path resolver loads only on a core that has one (see assetReferenceTracking).
+// `core.hasResourceManager` false plays a headless core, which has none.
+const core = vi.hoisted(() => ({ hasResourceManager: true }));
+vi.mock('../src/wasm/resourceManager', () => {
+ const rm = {
createTexture: vi.fn(() => 42),
registerExternalTexture: vi.fn(() => 42),
releaseTexture: vi.fn(),
@@ -24,10 +28,9 @@ vi.mock('../src/wasm/resourceManager', () => ({
acquireTextureByPath: vi.fn(() => 0),
invalidateTexturePath: vi.fn(() => false),
getTextureDimensions: vi.fn(() => ({ width: 64, height: 64 })),
- }),
- getResourceManager: () => null,
- evictTextureDimensions: vi.fn(),
-}));
+ };
+ return { requireResourceManager: () => rm, getResourceManager: () => (core.hasResourceManager ? rm : null), evictTextureDimensions: vi.fn() };
+});
// TextureLoader/imageDecode import the concrete base module (cycle-free); the
// rest go through the barrel — mock both ids with one factory.
@@ -102,4 +105,28 @@ describe('asset paths in engine-backed components', () => {
await new Promise((r) => setTimeout(r, 30));
expect(world.get(e, Sprite).texture).toBe(7);
});
+
+ // 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.
+ it('leaves a path alone on a core with no resource manager', async () => {
+ core.hasResourceManager = false;
+ try {
+ 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: 'sprites/hero.png' as never });
+ await new Promise((r) => setTimeout(r, 30));
+ expect(preload).not.toHaveBeenCalled();
+ expect(backend.fetchBinary).not.toHaveBeenCalled();
+ } finally {
+ core.hasResourceManager = true;
+ }
+ });
});