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 group bundle releases what it took, not what the manifest says now

esenginec774d7954633
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.ts
index 92b03bb9a..514e0d121 100644
--- a/sdk/src/asset/Assets.ts
+++ b/sdk/src/asset/Assets.ts
@@ -157,10 +157,22 @@ export interface AssetBundle {
materials: Map<string, MaterialResult>;
spine: Map<string, SpineResult>;
fonts: Map<string, FontResult>;
+ /**
+ * What this load actually acquired. Release through it rather than by group
+ * name: a manifest can be updated between the two, and then the group's
+ * membership is no longer the set this bundle took out.
+ */
+ scope: AssetScope;
+ /** Give back everything this bundle acquired. Idempotent. */
+ release(): void;
}
function emptyBundle(): AssetBundle {
- return { textures: new Map(), materials: new Map(), spine: new Map(), fonts: new Map() };
+ const scope = new AssetScope();
+ return {
+ textures: new Map(), materials: new Map(), spine: new Map(), fonts: new Map(),
+ scope, release: () => scope.releaseAll(),
+ };
}
@@ -181,22 +193,26 @@ interface GroupAssetChannel {
const GROUP_ASSET_CHANNELS: Readonly<Record<string, GroupAssetChannel>> = {
texture: {
- load: (a, path, b) => a.loadTexture(path).then((r) => { b.textures.set(path, r); }),
+ load: (a, path, b) => a.acquireTexture(path)
+ .then((l) => { b.scope.add(l); b.textures.set(path, l.value); }),
release: (a, path) => a.releaseTexture(path),
displayable: true,
},
material: {
- load: (a, path, b) => a.loadMaterial(path).then((r) => { b.materials.set(path, r); }),
+ load: (a, path, b) => a.acquireTyped<MaterialResult>('material', path)
+ .then((l) => { b.scope.add(l); b.materials.set(path, l.value); }),
release: (a, path) => a.releaseTyped('material', path),
displayable: true,
},
font: {
- load: (a, path, b) => a.loadFont(path).then((r) => { b.fonts.set(path, r); }),
+ load: (a, path, b) => a.acquireTyped<FontResult>('font', path)
+ .then((l) => { b.scope.add(l); b.fonts.set(path, l.value); }),
release: (a, path) => a.releaseTyped('font', path),
displayable: true,
},
'bitmap-font': {
- load: (a, path, b) => a.loadFont(path).then((r) => { b.fonts.set(path, r); }),
+ load: (a, path, b) => a.acquireTyped<FontResult>('font', path)
+ .then((l) => { b.scope.add(l); b.fonts.set(path, l.value); }),
release: (a, path) => a.releaseTyped('font', path),
displayable: true,
},
@@ -209,12 +225,12 @@ const GROUP_ASSET_CHANNELS: Readonly<Record<string, GroupAssetChannel>> = {
displayable: true,
},
prefab: {
- load: (a, path) => a.loadPrefab(path).then(() => {}),
+ load: (a, path, b) => a.acquireTyped('prefab', path).then((l) => { b.scope.add(l); }),
release: (a, path) => a.releaseTyped('prefab', path),
displayable: false,
},
audio: {
- load: (a, path) => a.loadAudio(path).then(() => {}),
+ load: (a, path, b) => a.acquireTyped('audio', path).then((l) => { b.scope.add(l); }),
release: (a, path) => a.releaseTyped('audio', path),
displayable: false,
},
@@ -784,14 +800,11 @@ export class Assets {
}
/**
- * Release every asset {@link loadGroup} acquired for a group — the
- * symmetric other half of on-demand delivery. Each asset goes through its
- * type's canonical release channel, so reference counting decides what
- * actually happens: an asset another scene or group still holds survives;
- * one nobody holds drops to the evictable warm cache (textures, audio)
- * or unloads. Call it when the player leaves the area the group backs;
- * bouncing back is then absorbed by the warm caches instead of the
- * network. Requires a manifest, like loadGroup; no-op without one.
+ * Release every asset the group's CURRENT membership names.
+ *
+ * @details Prefer `bundle.release()` on what {@link loadGroup} returned:
+ * this door re-reads the manifest, so an update between the two
+ * releases a set the load never took. No-op without a manifest.
*/
releaseGroup(groupName: string): void {
const model = this.manifestModel_;
diff --git a/sdk/tests/asset-generation-ownership.test.ts b/sdk/tests/asset-generation-ownership.test.ts
index ec0de9013..658adba90 100644
--- a/sdk/tests/asset-generation-ownership.test.ts
+++ b/sdk/tests/asset-generation-ownership.test.ts
@@ -16,6 +16,7 @@ import { Assets } from '../src/asset/Assets';
import { AssetRefLedger } from '../src/asset/AssetRefLedger';
import { AssetScope } from '../src/asset/AssetLease';
import type { Backend } from '../src/asset/Backend';
+import type { AddressableManifest } from '../src/asset/AddressableManifest';
vi.mock('../src/wasm/resourceManager', () => ({
requireResourceManager: () => ({ releaseTexture: vi.fn(), invalidateTexturePath: vi.fn(() => false) }),
@@ -185,6 +186,35 @@ describe('asset ownership is generation-exact', () => {
expect(assets.sizes().refRows).toBe(base); // and none stranded
});
+ it('a group bundle releases what it took, not what the manifest says today', async () => {
+ // Releasing by group NAME re-reads the manifest to decide what to give
+ // back. With atomic manifest updates that set is no longer the one this
+ // load took out, so the bundle carries its own receipts instead.
+ const unloaded: string[] = [];
+ const assets = makeAssets(unloaded);
+ const base = assets.sizes().refRows;
+ const manifestWith = (path: string): AddressableManifest => ({
+ version: '2.0',
+ groups: {
+ area: {
+ bundleMode: 'local', labels: [],
+ assets: { [path]: { path, type: 'font', size: 8, labels: [] } },
+ },
+ },
+ });
+
+ assets.setManifest(manifestWith('old.ttf'));
+ const bundle = await assets.loadGroup('area');
+ expect(bundle.scope.size).toBe(1);
+
+ // The world moves on: a hot update rewrites what "area" contains.
+ assets.setManifest(manifestWith('new.ttf'));
+
+ bundle.release();
+ expect(unloaded).toEqual(['gen1']); // what it actually took
+ expect(assets.sizes().refRows).toBe(base); // and nothing stranded
+ });
+
it('a scope releases what it actually acquired', async () => {
const unloaded: string[] = [];
const assets = makeAssets(unloaded);
diff --git a/sdk/tests/asset-group-channel-parity.test.ts b/sdk/tests/asset-group-channel-parity.test.ts
index 85e8b0e7d..0da06f774 100644
--- a/sdk/tests/asset-group-channel-parity.test.ts
+++ b/sdk/tests/asset-group-channel-parity.test.ts
@@ -46,14 +46,19 @@ const backend = {
const TYPES: Array<{
type: AddressableAssetType;
loadMethod: keyof Assets;
+ /** The typed door's first argument; absent when the method takes the path first. */
+ loadArg?: string;
releasesVia: string | null;
}> = [
- { type: 'texture', loadMethod: 'loadTexture', releasesVia: 'texture' },
- { type: 'material', loadMethod: 'loadMaterial', releasesVia: 'material' },
- { type: 'font', loadMethod: 'loadFont', releasesVia: 'font' },
- { type: 'bitmap-font', loadMethod: 'loadFont', releasesVia: 'font' },
- { type: 'prefab', loadMethod: 'loadPrefab', releasesVia: 'prefab' },
- { type: 'audio', loadMethod: 'loadAudio', releasesVia: 'audio' },
+ // The group loads through the ACQUIRE doors, so what it takes out is
+ // recorded as a receipt on the bundle rather than left to be guessed from
+ // the manifest at release time.
+ { type: 'texture', loadMethod: 'acquireTexture', releasesVia: 'texture' },
+ { type: 'material', loadMethod: 'acquireTyped', loadArg: 'material', releasesVia: 'material' },
+ { type: 'font', loadMethod: 'acquireTyped', loadArg: 'font', releasesVia: 'font' },
+ { type: 'bitmap-font', loadMethod: 'acquireTyped', loadArg: 'font', releasesVia: 'font' },
+ { type: 'prefab', loadMethod: 'acquireTyped', loadArg: 'prefab', releasesVia: 'prefab' },
+ { type: 'audio', loadMethod: 'acquireTyped', loadArg: 'audio', releasesVia: 'audio' },
// Skeletons bind to spawned entities and belong to the SpineManager
// lifecycle; releasing one here could yank it from under a live entity.
{ type: 'spine', loadMethod: 'loadSpine', releasesVia: null },
@@ -77,15 +82,22 @@ describe.each(TYPES)('group channel: $type', (row) => {
const assets = Assets.create({ backend, module: mockModule });
assets.setManifest(manifestWith(row.type));
- const load = vi.spyOn(assets, row.loadMethod as 'loadTexture')
- .mockResolvedValue({ handle: 1, width: 1, height: 1 } as never);
+ const load = vi.spyOn(assets, row.loadMethod as 'acquireTexture')
+ .mockResolvedValue({
+ key: `${row.type}:a.bin`, generation: 1,
+ value: { handle: 1, width: 1, height: 1 }, release: () => {},
+ } as never);
// releaseTexture and releaseTyped are the only two release doors; both
// are watched so a row cannot pass by going through neither.
const releaseTexture = vi.spyOn(assets, 'releaseTexture').mockImplementation(() => {});
const releaseTyped = vi.spyOn(assets, 'releaseTyped').mockImplementation(() => {});
await assets.loadGroup('dlc');
- expect(load, `loadGroup never loaded a ${row.type}`).toHaveBeenCalledWith('a.bin');
+ if (row.loadArg) {
+ expect(load, `loadGroup never loaded a ${row.type}`).toHaveBeenCalledWith(row.loadArg, 'a.bin');
+ } else {
+ expect(load, `loadGroup never loaded a ${row.type}`).toHaveBeenCalledWith('a.bin');
+ }
assets.releaseGroup('dlc');
@@ -111,8 +123,10 @@ describe('group release addressing', () => {
// a leak that looks exactly like a working release.
const assets = Assets.create({ backend, module: mockModule });
assets.setManifest(manifestWith('texture'));
- const load = vi.spyOn(assets, 'loadTexture')
- .mockResolvedValue({ handle: 1, width: 1, height: 1 } as never);
+ const load = vi.spyOn(assets, 'acquireTexture').mockResolvedValue({
+ key: 'texture:a.bin', generation: 1,
+ value: { handle: 1, width: 1, height: 1 }, release: () => {},
+ } as never);
const release = vi.spyOn(assets, 'releaseTexture').mockImplementation(() => {});
await assets.loadGroup('dlc');
diff --git a/sdk/tests/asset-group-loading.test.ts b/sdk/tests/asset-group-loading.test.ts
index ffb80a2a1..0c3a0433b 100644
--- a/sdk/tests/asset-group-loading.test.ts
+++ b/sdk/tests/asset-group-loading.test.ts
@@ -139,10 +139,12 @@ describe('Assets.loadGroup', () => {
it('dispatches every asset in a group through the typed loader, into the bundle', async () => {
const assets = createAssets();
assets.setManifest(createManifest());
- // Spy the single texture channel so the assertion is about loadGroup's
- // dispatch, not the mock's image-decode pipeline.
- const spy = vi.spyOn(assets, 'loadTexture')
- .mockResolvedValue({ handle: 42, width: 64, height: 64 } as any);
+ // Spy the texture channel's acquire door so the assertion is about
+ // loadGroup's dispatch, not the mock's image-decode pipeline.
+ const spy = vi.spyOn(assets, 'acquireTexture').mockResolvedValue({
+ key: 'texture:x', generation: 1,
+ value: { handle: 42, width: 64, height: 64 }, release: () => {},
+ } as any);
const progressCalls: [number, number][] = [];
const bundle = await assets.loadGroup('main', (loaded, total) => {
@@ -159,8 +161,10 @@ describe('Assets.loadGroup', () => {
it('loads a different group independently', async () => {
const assets = createAssets();
assets.setManifest(createManifest());
- const spy = vi.spyOn(assets, 'loadTexture')
- .mockResolvedValue({ handle: 7, width: 1, height: 1 } as any);
+ const spy = vi.spyOn(assets, 'acquireTexture').mockResolvedValue({
+ key: 'texture:x', generation: 1,
+ value: { handle: 7, width: 1, height: 1 }, release: () => {},
+ } as any);
const bundle = await assets.loadGroup('extra');
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('tex3.png');
@@ -193,7 +197,7 @@ describe('Assets.loadGroup', () => {
sub.mockClear();
const assets = createAssets();
assets.setManifest(createManifest());
- vi.spyOn(assets, 'loadTexture').mockResolvedValue({ handle: 1, width: 1, height: 1 } as any);
+ vi.spyOn(assets, 'acquireTexture').mockResolvedValue({ key: 'texture:x', generation: 1, value: { handle: 1, width: 1, height: 1 }, release: () => {} } as any);
await assets.loadGroup('extra'); // createManifest: 'extra' is bundleMode 'lazy'
expect(sub).toHaveBeenCalledWith('extra');
});
@@ -203,7 +207,7 @@ describe('Assets.loadGroup', () => {
sub.mockClear();
const assets = createAssets();
assets.setManifest(createManifest());
- vi.spyOn(assets, 'loadTexture').mockResolvedValue({ handle: 1, width: 1, height: 1 } as any);
+ vi.spyOn(assets, 'acquireTexture').mockResolvedValue({ key: 'texture:x', generation: 1, value: { handle: 1, width: 1, height: 1 }, release: () => {} } as any);
await assets.loadGroup('main'); // bundleMode 'local'
expect(sub).not.toHaveBeenCalled();
});
diff --git a/sdk/tests/hot-update-runtime.test.ts b/sdk/tests/hot-update-runtime.test.ts
index cb3fd54bd..abc6f592b 100644
--- a/sdk/tests/hot-update-runtime.test.ts
+++ b/sdk/tests/hot-update-runtime.test.ts
@@ -82,8 +82,10 @@ describe('Assets.loadGroup — remote group', () => {
const assets = createAssets(backendServing());
assets.setManifest(cdnManifest('aaaa', 'rev-1'));
assets.setRemoteRoot('https://cdn.example.com/build/');
- const spy = vi.spyOn(assets, 'loadTexture')
- .mockResolvedValue({ handle: 1, width: 1, height: 1 } as any);
+ const spy = vi.spyOn(assets, 'acquireTexture').mockResolvedValue({
+ key: 'texture:x', generation: 1,
+ value: { handle: 1, width: 1, height: 1 }, release: () => {},
+ } as any);
await assets.loadGroup('cdn');
@@ -94,8 +96,10 @@ describe('Assets.loadGroup — remote group', () => {
const assets = createAssets(backendServing());
assets.setManifest(cdnManifest('bbbb', 'rev-1'));
assets.setRemoteRoot('https://cdn.example.com');
- const spy = vi.spyOn(assets, 'loadTexture')
- .mockResolvedValue({ handle: 1, width: 1, height: 1 } as any);
+ const spy = vi.spyOn(assets, 'acquireTexture').mockResolvedValue({
+ key: 'texture:x', generation: 1,
+ value: { handle: 1, width: 1, height: 1 }, release: () => {},
+ } as any);
await assets.loadGroup('cdn');