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): what must be rebuilt when a file changes, as a walk of the eras
esengine3209820d0c09
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex e0d30c8c2..7d3fd6409 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -765,6 +765,7 @@ ResourceDef<AssetsClass>```@internal appScope: AssetScope@internal dependenciesOf: (type: string, ref: string) => readonly DependencyReceipt[]+@internal dependentsOf: (ref: string) => AssetIdentity[]@internal entityScopes: EntityAssetScopes@internal publishedRegistryAssets: <T>(type: string) => T[]@internal releaseTextureLease: (lease: AssetRefLease<number>) => voiddiff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex d70dae3d4..fca7120ea 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -765,6 +765,7 @@ ResourceDef<AssetsClass>```@internal appScope: AssetScope@internal dependenciesOf: (type: string, ref: string) => readonly DependencyReceipt[]+@internal dependentsOf: (ref: string) => AssetIdentity[]@internal entityScopes: EntityAssetScopes@internal publishedRegistryAssets: <T>(type: string) => T[]@internal releaseTextureLease: (lease: AssetRefLease<number>) => voiddiff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 3feb6d1f1..72120faba 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -760,6 +760,7 @@ ResourceDef<AssetsClass>```@internal appScope: AssetScope@internal dependenciesOf: (type: string, ref: string) => readonly DependencyReceipt[]+@internal dependentsOf: (ref: string) => AssetIdentity[]@internal entityScopes: EntityAssetScopes@internal publishedRegistryAssets: <T>(type: string) => T[]@internal releaseTextureLease: (lease: AssetRefLease<number>) => voiddiff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 0eda6fd1f..f01a6ab4d 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -760,6 +760,7 @@ ResourceDef<AssetsClass>```@internal appScope: AssetScope@internal dependenciesOf: (type: string, ref: string) => readonly DependencyReceipt[]+@internal dependentsOf: (ref: string) => AssetIdentity[]@internal entityScopes: EntityAssetScopes@internal publishedRegistryAssets: <T>(type: string) => T[]@internal releaseTextureLease: (lease: AssetRefLease<number>) => voiddiff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex 20915f1ac..77b5e65a5 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -760,6 +760,7 @@ ResourceDef<AssetsClass>```@internal appScope: AssetScope@internal dependenciesOf: (type: string, ref: string) => readonly DependencyReceipt[]+@internal dependentsOf: (ref: string) => AssetIdentity[]@internal entityScopes: EntityAssetScopes@internal publishedRegistryAssets: <T>(type: string) => T[]@internal releaseTextureLease: (lease: AssetRefLease<number>) => voiddiff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.tsindex e7cf29aef..8209bd943 100644--- a/sdk/src/asset/Assets.ts+++ b/sdk/src/asset/Assets.ts@@ -22,7 +22,8 @@ import { AssetRefLedger, type AssetRefLease } from './AssetRefLedger';import { AssetScope, type AssetLease } from './AssetLease';import { EntityAssetScopes } from './entityAssetScopes';import { RegistryAssetSlots, type RegistryAssetKind } from './registryAssets';-import { DependencyRecorder, type DependencyReceipt, type Preparation, type PreparedLoad } from './dependencies';+import { DependencyRecorder, isInvalidatable, type AssetIdentity, type DependencyReceipt,+ type Preparation, type PreparedLoad } from './dependencies';import { SpineAssetLoader } from './loaders/SpineAssetLoader';import { MaterialAssetLoader } from './loaders/MaterialAssetLoader';import { MeshAssetLoader } from './loaders/MeshAssetLoader';@@ -2076,6 +2077,43 @@ export class Assets {return this.genericCache_.get(type)?.get(path)?.edges ?? [];}+ /**+ * Every asset of this realm whose CURRENT era took `ref` — the reverse of+ * {@link dependenciesOf}, walked from the eras so there is no table to keep.+ * By canonical identity: a receipt says what the acquisition ASKED for (a+ * `@uuid:` ref, a document-relative path) and a change names a file.+ *+ * @internal+ */+ dependentsOf(ref: string): AssetIdentity[] {+ const canonical = new Map<string, string>();+ const identify = (path: string): string => {+ let known = canonical.get(path);+ if (known === undefined) {+ known = this.resolveLoadPath_(path);+ canonical.set(path, known);+ }+ return known;+ };+ const target = identify(ref);+ const out: AssetIdentity[] = [];+ for (const era of this.eras_()) {+ const took = era.edges.some((e) => isInvalidatable(e) && identify(e.path) === target);+ if (took) out.push({ type: era.type, path: era.path });+ }+ return out;+ }++ /** Every live era of this realm, published under a name or held by handle. */+ private *eras_(): Generator<{ type: string; path: string; edges: readonly DependencyReceipt[] }> {+ for (const era of this.registrySlots_.eras()) {+ yield { type: era.type, path: era.key, edges: era.edges };+ }+ for (const [type, cache] of this.genericCache_) {+ for (const [path, era] of cache.entries()) yield { type, path, edges: era.edges };+ }+ }+/*** Everything this realm publishes of one type. What a schedule analysis* reads to learn what the graphs THIS app loaded reach for.diff --git a/sdk/src/asset/dependencies.ts b/sdk/src/asset/dependencies.tsindex 3fb35cf24..32a65d572 100644--- a/sdk/src/asset/dependencies.ts+++ b/sdk/src/asset/dependencies.ts@@ -46,6 +46,23 @@ export interface DependencyReceipt {readonly path: string;}+/** One asset of a realm: what kind it is, and the name its era is filed under. */+export interface AssetIdentity {+ readonly type: string;+ readonly path: string;+}++/**+ * Whether a change to what this edge names can invalidate the era that recorded+ * it. A read is causality by definition; an acquisition only when it names an+ * addressable asset, since a composed resource has no content anyone can change+ * and its `composed:<handle>` identity is a debug name, not a lookup key.+ */+export function isInvalidatable(edge: DependencyReceipt): boolean {+ if (edge.kind === 'source') return true;+ return edge.type !== undefined;+}+/** What one preparation took, sealed onto what it produced. */export interface Preparation {/** Everything it acquired; released when what it produced retires. */diff --git a/sdk/src/asset/registryAssets.ts b/sdk/src/asset/registryAssets.tsindex e57f14add..487ce5650 100644--- a/sdk/src/asset/registryAssets.ts+++ b/sdk/src/asset/registryAssets.ts@@ -162,6 +162,16 @@ export class RegistryAssetSlots {return this.byName_.get(lookupKey(type, name))?.era?.edges ?? [];}+ /** Every live era, by the key its slot is filed under. What a reverse walk+ * reads: who depends on what is computed from the eras themselves. */+ eras(): Array<{ type: string; key: string; edges: readonly DependencyReceipt[] }> {+ const out: Array<{ type: string; key: string; edges: readonly DependencyReceipt[] }> = [];+ for (const slot of new Set(this.byName_.values())) {+ if (slot.era) out.push({ type: slot.type, key: slot.key, edges: slot.era.edges });+ }+ return out;+ }+/*** What this realm publishes for (type, name) right now — the answer a* runtime lookup reads, and the only copy of it.diff --git a/sdk/tests/asset-dependencies.test.ts b/sdk/tests/asset-dependencies.test.tsindex 3a71fa253..9c360374e 100644--- a/sdk/tests/asset-dependencies.test.ts+++ b/sdk/tests/asset-dependencies.test.ts@@ -385,3 +385,65 @@ describe('a material instance acquires its parent', () => {expect(pool.liveTextures(), 'the texture the PARENT bound').toBe(0);});});++describe('what must be rebuilt when this changes', () => {+ beforeEach(() => { pool = createPoolFake(); materialFake.reset(); });++ it('the map that folded a .tsj in is named by the file it read', async () => {+ const assets = realm({ 'level.tmj': MAP_WITH_EXTERNAL, 'terrain.tsj': EXTERNAL_TILESET });+ await assets.acquireTyped('tilemap', 'maps/level.tmj');++ expect(assets.dependentsOf('maps/terrain.tsj'))+ .toEqual([{ type: 'tilemap', path: 'maps/level.tmj' }]);+ });++ it('a texture names every era that took it, of any kind', async () => {+ const assets = realm({ 'hero.esmaterial': MATERIAL });+ await assets.acquireTyped('material', 'materials/hero.esmaterial');++ expect(assets.dependentsOf('materials/wall.png'))+ .toEqual([{ type: 'material', path: 'materials/hero.esmaterial' }]);+ });++ it('a composed resource names nobody', async () => {+ // Its `composed:<handle>` identity is a debug name, not a lookup key —+ // in an invalidation namespace it would collide with whatever handle+ // number a file happened to be given.+ const assets = realm({ 'level.tmj': MAP_WITH_COLLECTION });+ await assets.acquireTyped('tilemap', 'maps/level.tmj');+ const composed = assets.dependenciesOf('tilemap', 'maps/level.tmj')+ .find((e) => e.path.startsWith('composed:'))!;++ expect(assets.dependentsOf(composed.path)).toEqual([]);+ });++ it('a receipt spelled as a uuid answers to the file it resolves to', async () => {+ // A receipt says what the acquisition ASKED for; a change names a file.+ // Comparing the two spellings directly is how a graph misses every edge+ // a scene serialized.+ const assets = realm({});+ assets.setAssetRefResolver((ref) => (ref === '@uuid:skin' ? 'art/skin.png' : ref));+ assets.register({+ type: 'widget', extensions: ['.widget'],+ registry: {+ prepare: async (path, ctx) => {+ await ctx.acquireTexture('@uuid:skin');+ return { published: { path }, value: { id: path } };+ },+ },+ });+ await assets.acquireTyped('widget', 'x.widget');++ expect(assets.dependentsOf('art/skin.png')).toEqual([{ type: 'widget', path: 'x.widget' }]);+ expect(assets.dependentsOf('@uuid:skin'), 'the same asset, spelled the other way')+ .toEqual([{ type: 'widget', path: 'x.widget' }]);+ });++ it('an era that retired takes its edges with it', async () => {+ const assets = realm({ 'level.tmj': MAP_WITH_EXTERNAL, 'terrain.tsj': EXTERNAL_TILESET });+ const held = await assets.acquireTyped('tilemap', 'maps/level.tmj');++ held.release();+ expect(assets.dependentsOf('maps/terrain.tsj')).toEqual([]);+ });+});