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
feat(assets): a receipt can be split, joining the era it names
esengine0a2f2c498a6b
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex b67e81d60..ba0fba7bb 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -687,6 +687,7 @@ version: stringgeneration: numberkey: stringrelease: () => void+retain: () => AssetLease<T> | nullvalue: T```diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 7d8b13b1a..a55aef8fe 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -687,6 +687,7 @@ version: stringgeneration: numberkey: stringrelease: () => void+retain: () => AssetLease<T> | nullvalue: T```diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex b551a522e..f5e8e9e28 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -682,6 +682,7 @@ version: stringgeneration: numberkey: stringrelease: () => void+retain: () => AssetLease<T> | nullvalue: T```diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 66c08f352..3a2d1e27e 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -682,6 +682,7 @@ version: stringgeneration: numberkey: stringrelease: () => void+retain: () => AssetLease<T> | nullvalue: T```diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex 88d0b3887..3355ecd3b 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -682,6 +682,7 @@ version: stringgeneration: numberkey: stringrelease: () => void+retain: () => AssetLease<T> | nullvalue: T```diff --git a/sdk/src/asset/AssetLease.ts b/sdk/src/asset/AssetLease.tsindex 138a2f2b7..bf000dbae 100644--- a/sdk/src/asset/AssetLease.ts+++ b/sdk/src/asset/AssetLease.ts@@ -18,6 +18,14 @@ export interface AssetLease<T = unknown> {readonly value: T;/** Give this one acquisition back. Calling twice is a no-op, not a double free. */release(): void;+ /**+ * A second receipt for the SAME generation — an owner splitting what it holds.+ *+ * Not a re-acquire: this era may already be superseded, which is exactly when+ * loading the path again hands back a different instance than the one the+ * splitting owner is bound to. Null when the ledger has forgotten it.+ */+ retain(): AssetLease<T> | null;}/**diff --git a/sdk/src/asset/AssetRefLedger.ts b/sdk/src/asset/AssetRefLedger.tsindex eb30d7f4b..771814cfd 100644--- a/sdk/src/asset/AssetRefLedger.ts+++ b/sdk/src/asset/AssetRefLedger.ts@@ -74,6 +74,24 @@ export class AssetRefLedger<T> {return lease;}+ /**+ * A second holder of the generation `lease` names — a superseded one included.+ *+ * The difference from {@link acquire} is which era the caller joins: acquire+ * asks what a NEW holder should load, which after an invalidate is a new era.+ * An owner SPLITTING what it holds is a second owner of the one already bound.+ */+ retain(lease: AssetRefLease<T>): AssetRefLease<T> | undefined {+ const generations = this.byKey_.get(lease.key);+ const generation = generations?.find((g) => g.id === lease.generation);+ if (!generation) return undefined;+ generation.count++;+ const child: LeaseImpl<T> = {+ key: lease.key, generation: generation.id, value: generation.value, spent: false,+ };+ return child;+ }+/*** The key's current generation is over: its holders keep what they have and* still owe a release, but the next acquire starts a new era rather thandiff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.tsindex 63f727856..90303e957 100644--- a/sdk/src/asset/Assets.ts+++ b/sdk/src/asset/Assets.ts@@ -638,10 +638,7 @@ export class Assets {const clip = anim.getClip(result.clipId);if (clip && !anim.getClip(ref)) anim.aliasClip(ref, clip);}- return {- key: lease.key, generation: lease.generation, value: result,- release: () => this.releaseTypedLease('anim-clip', lease),- };+ return this.typedLease_('anim-clip', lease, result);}async loadTilemap(ref: string): Promise<TilemapResult> {@@ -1395,18 +1392,46 @@ export class Assets {*/async acquireTexture(ref: string): Promise<AssetLease<TextureResult>> {const { result, lease } = await this.acquireTextureVariant_(ref, true);- return {- key: lease.key, generation: lease.generation, value: result,- release: () => this.releaseTextureLease(lease),- };+ return this.textureLease_(lease, result);}/** {@link acquireTexture} for everything that goes through a typed loader. */async acquireTyped<T>(type: string, ref: string): Promise<AssetLease<T>> {const { value, lease } = await this.acquireTyped_<T>(type, ref);+ return this.typedLease_(type, lease, value);+ }++ /**+ * The owner-facing receipt for one ledger acquisition.+ *+ * `retain` goes through the ledger rather than through this class's load+ * path: an owner splitting what it holds joins the era it already has, which+ * after an invalidate is not the era a path resolves to.+ */+ private textureLease_(+ lease: AssetRefLease<number>, value: TextureResult,+ ): AssetLease<TextureResult> {+ return {+ key: lease.key, generation: lease.generation, value,+ release: () => this.releaseTextureLease(lease),+ retain: () => {+ const child = this.textureRefs_.retain(lease);+ return child ? this.textureLease_(child, value) : null;+ },+ };+ }++ /** {@link textureLease_} for everything that goes through a typed loader. */+ private typedLease_<T>(+ type: string, lease: AssetRefLease<unknown>, value: T,+ ): AssetLease<T> {return {key: lease.key, generation: lease.generation, value,release: () => this.releaseTypedLease(type, lease),+ retain: () => {+ const child = this.genericRefs_.retain(lease);+ return child ? this.typedLease_(type, child, value) : null;+ },};}@@ -1929,10 +1954,7 @@ export class Assets {},async acquireTexture(path: string, flipY?: boolean): Promise<AssetLease<TextureResult>> {const { result, lease } = await self.acquireTextureVariant_(path, flipY !== false);- return {- key: lease.key, generation: lease.generation, value: result,- release: () => self.releaseTextureLease(lease),- };+ return self.textureLease_(lease, result);},releaseTexture(path: string): void {self.releaseTexture(path);diff --git a/sdk/tests/asset-generation-ownership.test.ts b/sdk/tests/asset-generation-ownership.test.tsindex c6351f911..15e0163fd 100644--- a/sdk/tests/asset-generation-ownership.test.ts+++ b/sdk/tests/asset-generation-ownership.test.ts@@ -165,6 +165,43 @@ describe('asset ownership is generation-exact', () => {expect(assets.sizes().refRows).toBe(base);});+ it('a retained receipt joins the era its source names, not the era the path resolves to', async () => {+ // Splitting ownership is not acquiring: after an invalidate the path+ // resolves to a NEW instance while the splitting owner is still bound to+ // the old one, so a re-acquire receipts something nothing is using.+ const unloaded: string[] = [];+ const assets = makeAssets(unloaded);+ const base = assets.sizes().refRows;++ const held = await assets.acquireTyped<{ id: string }>('font', 'f.ttf');+ assets.invalidate('f.ttf'); // gen1's era closes+ const fresh = await assets.acquireTyped<{ id: string }>('font', 'f.ttf');+ expect(fresh.value.id).toBe('gen2');++ const retained = held.retain()!;+ expect(retained.generation).toBe(held.generation); // the same era...+ expect(retained.value.id).toBe('gen1'); // ...and the same instance+ expect(assets.sizes().refRows).toBe(base + 3);++ held.release();+ expect(unloaded).toEqual([]); // gen1 is still owed by the retained one+ retained.release();+ expect(unloaded).toEqual(['gen1']);+ fresh.release();+ expect(unloaded).toEqual(['gen1', 'gen2']);+ expect(assets.sizes().refRows).toBe(base);+ });++ it('there is nothing to retain once the last holder let go', async () => {+ const unloaded: string[] = [];+ const assets = makeAssets(unloaded);+ const held = await assets.acquireTyped<{ id: string }>('font', 'f.ttf');+ held.release();+ // Not a resurrection door: the asset is gone, and a receipt for it would+ // be one nobody could ever give back.+ expect(held.retain()).toBeNull();+ });+it('a scope absorbs another\'s receipts, and gives back what it took', async () => {// The packaged-game shape: the loader acquires into a scope of its own// and hands the whole thing to the scene that will outlive it. Releasing