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): deleting one file re-read the whole project, once per file
esengine2965df8ccb44
diff --git a/desktop/electron/assetDb.ts b/desktop/electron/assetDb.tsindex 10386b175..6d2d7f4ab 100644--- a/desktop/electron/assetDb.ts+++ b/desktop/electron/assetDb.ts@@ -531,7 +531,16 @@ export async function updateAssetIndex(const byPath = new Map(prev.entries.map((e) => [e.path, e] as const));const adopted: string[] = [];- let setChanged = false; // an add/remove/uuid/type change — path resolution is global+ // An ADD or a uuid/type change makes path + bare-uuid resolution global again —+ // a ref that used to dangle can now land. A REMOVAL cannot do that, so the two+ // are tracked apart; see the deps branch below for what each costs.+ let resolutionChanged = false;+ const removedUuids = new Set<string>();+ const drop = (entry: AssetEntry | undefined, content: string): void => {+ if (!entry) return;+ byPath.delete(content);+ removedUuids.add(entry.uuid);+ };for (const content of contentPaths) {const abs = path.join(root, content);@@ -540,14 +549,14 @@ export async function updateAssetIndex(// content edit, a `.meta` edit, orphan adoption, and deletion (the same states// a full scan resolves per file).if (!existsSync(abs)) {- if (before) { byPath.delete(content); setChanged = true; }+ drop(before, content);continue;}if (!existsSync(abs + META_EXT)) {// Orphan content with no sidecar: a full scan would mint one this pass// (unknown extensions are left alone). Mirror that so it enters the index now.if ((await adoptOrphan(abs)) === 'adopted') adopted.push(content);- else { if (before) { byPath.delete(content); setChanged = true; } continue; }+ else { drop(before, content); continue; }}let meta: { uuid?: unknown; type?: unknown; importer?: unknown };try {@@ -562,7 +571,7 @@ export async function updateAssetIndex(type: meta.type,importer: (meta.importer as Record<string, unknown>) ?? {},};- if (!before || before.uuid !== entry.uuid || before.type !== entry.type) setChanged = true;+ if (!before || before.uuid !== entry.uuid || before.type !== entry.type) resolutionChanged = true;byPath.set(content, entry);}@@ -572,21 +581,35 @@ export async function updateAssetIndex(// Before deps, which are keyed by uuid.const warnings: string[] = [];const reminted = await resolveDuplicateUuids(root, entries, warnings);- if (reminted.length > 0) setChanged = true;-- // Deps: when the entry SET changed, path + bare-uuid resolution is global, so- // recompute the whole graph from the new entries (still bounded to ref-carrying- // assets — far fewer than every `.meta`). When only existing assets' CONTENT- // changed, other assets' edges can't have moved: patch just the touched- // ref-assets over the previous graph.+ if (reminted.length > 0) resolutionChanged = true;++ // Deps: recomputing the whole graph means READING AND PARSING every ref-carrying+ // asset in the project — a second or more once a project has a few thousand, and+ // the main process is blocked throughout. So it is reserved for the case that+ // needs it: an ADDED entry (or a re-minted uuid, or a changed type) makes path ++ // bare-uuid resolution global again, because a reference that used to resolve to+ // nothing may now land somewhere.+ //+ // A REMOVAL cannot move an edge that isn't already pointing at what left: for+ // every other document the same paths and uuids resolve the same way. So the+ // patch set is the touched files plus whoever referenced something removed —+ // those must be re-read, since a path or bare-uuid ref to a gone asset stops+ // being an edge (an `@uuid:` one does not, and re-reading is how we tell).let deps: Record<string, string[]>;- if (setChanged) {+ if (resolutionChanged) {const r = await computeDeps(root, entries);deps = r.deps;warnings.push(...r.warnings);} else {deps = { ...prev.deps };- const touched = entries.filter((e) => contentPaths.has(e.path));+ const reresolve = new Set<string>();+ for (const uuid of removedUuids) delete deps[uuid];+ if (removedUuids.size > 0) {+ for (const [uuid, refs] of Object.entries(deps)) {+ if (refs.some((r) => removedUuids.has(r))) reresolve.add(uuid);+ }+ }+ const touched = entries.filter((e) => contentPaths.has(e.path) || reresolve.has(e.uuid));for (const e of touched) delete deps[e.uuid]; // clear stale before recomputeconst r = await computeDeps(root, entries, touched);Object.assign(deps, r.deps);diff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsxindex 8fc6423cb..a17be9ee4 100644--- a/desktop/src/panels/ContentBrowser.tsx+++ b/desktop/src/panels/ContentBrowser.tsx@@ -19,7 +19,7 @@ import { openAssetOfType } from '@/project/assetOpen';import { assetTypeRegistry } from '@/project/assetTypes';import { contributedContextRows } from '@/plugins/contextMenus';import { syncAssetPaths } from '@/project/assetPathSync';-import { findAssetUsages, type AssetUsage } from '@/project/assetUsages';+import { findAssetUsagesOfAll, type AssetUsage } from '@/project/assetUsages';import { deleteAssets } from '@/project/deleteAssets';import { FindUsagesDialog } from '@/components/FindUsagesDialog';import { NewTilesetDialog } from '@/components/NewTilesetDialog';@@ -555,14 +555,8 @@ export function ContentBrowser() {// aggregating each one's references so nothing gets deleted out from under a scene.const remove = async (paths: string[]) => {if (paths.length === 0) return;- const usages: AssetUsage[] = [];- for (const p of paths) {- try {- usages.push(...(await findAssetUsages(p)));- } catch {- // Best-effort: if the scan fails, confirm without that asset's warning.- }- }+ // Best-effort: if the scan fails, confirm without the warnings.+ const usages = await findAssetUsagesOfAll(paths).catch((): AssetUsage[] => []);setConfirmDel({ paths, names: paths.map((p) => baseName(p)), usages });};const doRemove = async () => {@@ -1257,7 +1251,11 @@ export function ContentBrowser() {><div className="th">{isImg ? (- <img src={`estella://project/${path}`} alt="" draggable={false} />+ // Deferred, because a folder is however many images the user put+ // in it: this grid mounts a tile per entry, and a full-size decode+ // per tile is how browsing an art dump costs hundreds of megabytes+ // of image memory for the twenty thumbnails actually on screen.+ <img src={`estella://project/${path}`} alt="" draggable={false} loading="lazy" decoding="async" />) : (<AssetIcon type={type} size={30} />)}diff --git a/desktop/src/project/assetRefs.ts b/desktop/src/project/assetRefs.tsindex 5a9fa915d..8471dd13c 100644--- a/desktop/src/project/assetRefs.ts+++ b/desktop/src/project/assetRefs.ts@@ -14,21 +14,52 @@ export interface AssetIndexLike {deps: Record<string, string[]>;}+/** An index inverted once, then asked about as many assets as you like. */+export interface ReverseRefs {+ /** The uuid minted for the asset at a path, or null if it isn't a tracked asset. */+ uuidOf(path: string): string | null;+ /** Project-relative paths of the documents referencing the asset at `path`. */+ referrersOf(path: string): string[];+}++/**+ * Invert the dep graph once.+ *+ * {@link referencingPaths} rebuilds a uuid→path map of the WHOLE project and+ * walks every dep edge on each call. That is the right shape for one asset and+ * quadratic for a selection: deleting a folder of sprites asked the same+ * 36k-entry question once per file.+ */+export function reverseRefs(index: AssetIndexLike): ReverseRefs {+ const pathOfUuid = new Map(index.entries.map((e) => [e.uuid, e.path] as const));+ const uuidOfPath = new Map(index.entries.map((e) => [e.path, e.uuid] as const));+ const referrers = new Map<string, string[]>();+ for (const [uuid, refs] of Object.entries(index.deps)) {+ const from = pathOfUuid.get(uuid);+ if (!from) continue;+ // A document naming the same asset twice is one referrer, not two.+ for (const ref of new Set(refs)) {+ const arr = referrers.get(ref);+ if (arr) arr.push(from);+ else referrers.set(ref, [from]);+ }+ }+ return {+ uuidOf: (path) => uuidOfPath.get(path) ?? null,+ referrersOf: (path) => {+ const target = uuidOfPath.get(path);+ if (!target) return [];+ return (referrers.get(target) ?? []).filter((p) => p !== path);+ },+ };+}+/*** Project-relative paths of the scenes/prefabs that reference the asset at `path`.* Empty if `path` isn't a tracked asset (e.g. a folder) or nothing references it.*/export function referencingPaths(index: AssetIndexLike, path: string): string[] {- const target = index.entries.find((e) => e.path === path)?.uuid;- if (!target) return [];- const byUuid = new Map(index.entries.map((e) => [e.uuid, e.path]));- const out: string[] = [];- for (const [uuid, refs] of Object.entries(index.deps)) {- if (!refs.includes(target)) continue;- const p = byUuid.get(uuid);- if (p && p !== path) out.push(p);- }- return out;+ return reverseRefs(index).referrersOf(path);}/** One asset/scene that references the asset being inspected or deleted. */@@ -49,13 +80,21 @@ export function valueReferencesAsset(value: unknown,target: { uuid: string | null; path: string },): boolean {- const uuid = target.uuid?.toLowerCase() ?? null;+ return referencesAny(+ value,+ new Set(target.uuid ? [target.uuid.toLowerCase()] : []),+ new Set([target.path]),+ );+}++/** One walk of a document against MANY targets — the batch's inner loop. */+function referencesAny(value: unknown, uuids: ReadonlySet<string>, paths: ReadonlySet<string>): boolean {const walk = (v: unknown): boolean => {if (typeof v === 'string') {- if (v === target.path) return true;- if (!uuid) return false;+ if (paths.has(v)) return true;+ if (uuids.size === 0) return false;const body = v.startsWith('@uuid:') ? v.slice('@uuid:'.length) : v;- return body.toLowerCase() === uuid;+ return uuids.has(body.toLowerCase());}if (Array.isArray(v)) return v.some(walk);if (v && typeof v === 'object') return Object.values(v as Record<string, unknown>).some(walk);@@ -75,11 +114,39 @@ export function collectAssetUsages(path: string,liveScene?: { path: string | null; data: unknown } | null,): AssetUsage[] {- const disk: AssetUsage[] = referencingPaths(index, path).map((p) => ({ path: p, unsaved: false }));- if (!liveScene?.data) return disk;- const uuid = index.entries.find((e) => e.path === path)?.uuid ?? null;- if (!valueReferencesAsset(liveScene.data, { uuid, path })) return disk;+ return collectAssetUsagesOfAll(index, [path], liveScene);+}++/**+ * The usage list for a whole SELECTION, over one inverted index and one walk of+ * the live scene — the delete-many shape.+ *+ * Rows are deduplicated: the dialog answers "what breaks if this goes", and one+ * scene that happens to use six of the selected sprites is one thing that breaks.+ */+export function collectAssetUsagesOfAll(+ index: AssetIndexLike,+ paths: readonly string[],+ liveScene?: { path: string | null; data: unknown } | null,+): AssetUsage[] {+ const refs = reverseRefs(index);+ const out: AssetUsage[] = [];+ const seen = new Set<string>();+ for (const path of paths) {+ for (const p of refs.referrersOf(path)) {+ if (seen.has(p)) continue;+ seen.add(p);+ out.push({ path: p, unsaved: false });+ }+ }+ if (!liveScene?.data) return out;// A saved copy of the same scene already reports it — one row per document.- if (liveScene.path && disk.some((d) => d.path === liveScene.path)) return disk;- return [...disk, { path: liveScene.path, unsaved: true }];+ if (liveScene.path && seen.has(liveScene.path)) return out;+ const uuids = new Set<string>();+ for (const path of paths) {+ const uuid = refs.uuidOf(path);+ if (uuid) uuids.add(uuid.toLowerCase());+ }+ if (!referencesAny(liveScene.data, uuids, new Set(paths))) return out;+ return [...out, { path: liveScene.path, unsaved: true }];}diff --git a/desktop/src/project/assetUsages.ts b/desktop/src/project/assetUsages.tsindex 56a674e31..d297f99bb 100644--- a/desktop/src/project/assetUsages.ts+++ b/desktop/src/project/assetUsages.ts@@ -8,7 +8,7 @@*/import { SceneModel } from '@/engine/SceneModel';import { ProjectStore } from './ProjectStore';-import { collectAssetUsages, type AssetUsage } from './assetRefs';+import { collectAssetUsagesOfAll, type AssetUsage } from './assetRefs';export type { AssetUsage };@@ -25,14 +25,31 @@ export async function findAssetUsages(path: string,opts?: { preferCache?: boolean },): Promise<AssetUsage[]> {+ return findAssetUsagesOfAll([path], opts);+}++/**+ * The same question asked of a whole selection, on ONE scan.+ *+ * Deleting from the Content Browser asked it per selected file, and the walk is+ * O(every file in the project) — seconds on an asset-heavy project, with the+ * main process blocked throughout. Twenty selected sprites meant twenty of those+ * back to back before the confirm dialog appeared at all, and a folder's worth+ * meant the editor did not come back.+ */+export async function findAssetUsagesOfAll(+ paths: readonly string[],+ opts?: { preferCache?: boolean },+): Promise<AssetUsage[]> {+ if (paths.length === 0) return [];const index = opts?.preferCache? (await window.estella.project.cachedAssetIndex().catch(() => null))?? (await window.estella.project.scanAssets()).index: (await window.estella.project.scanAssets()).index;const data = SceneModel.current;- return collectAssetUsages(+ return collectAssetUsagesOfAll(index,- path,+ paths,data ? { path: ProjectStore.getSnapshot()?.currentScene ?? null, data } : null,);}diff --git a/desktop/src/project/deleteAssets.ts b/desktop/src/project/deleteAssets.tsindex bd02595bf..b3964610b 100644--- a/desktop/src/project/deleteAssets.ts+++ b/desktop/src/project/deleteAssets.ts@@ -13,6 +13,10 @@* the whole batch is the difference between a delete and a freeze on a folder of sprites. A* failure on one path is reported in its own row rather than aborting the rest — a permission* error on the third file must not leave the first two deleted and unaccounted for.+ *+ * The re-scan is INCREMENTAL: we know exactly which paths went, and a full walk is O(every+ * file in the project) — seconds on an asset-heavy one. Removing a folder still falls back to+ * the full walk (its children were never named), which the incremental door decides, not us.*/import { ProjectStore } from './ProjectStore';@@ -39,6 +43,7 @@ export async function deleteAssets(paths: readonly string[]): Promise<TrashedAssresults.push({ path, token: null, error: err instanceof Error ? err.message : String(err) });}}- if (results.some((r) => r.token !== null)) await ProjectStore.refreshAssets();+ const gone = results.filter((r) => r.token !== null).map((r) => r.path);+ if (gone.length > 0) await ProjectStore.applyDiskChanges(gone);return results;}diff --git a/desktop/tests/asset-db-incremental.test.ts b/desktop/tests/asset-db-incremental.test.tsindex c5a74aaee..56ffe10d9 100644--- a/desktop/tests/asset-db-incremental.test.ts+++ b/desktop/tests/asset-db-incremental.test.ts@@ -120,6 +120,24 @@ describe('updateAssetIndex — incremental == full rescan', () => {expect(inc.index.entries.some((e) => e.uuid === FONT)).toBe(false);});+ // A removal doesn't recompute the whole graph any more — only the documents that+ // referenced what left. A PATH-ref is the case that proves the set is right: it+ // stops resolving when its target goes, so its referrer MUST be re-read (unlike+ // an `@uuid:` one, which a full scan keeps dangling).+ it('DELETING a path-ref target re-resolves its referrer, matching full', async () => {+ writeAsset('assets/materials/wall.png', 'texture', TEX2, 'WALL');+ writeAsset('assets/materials/m.esmaterial', 'material', MAT, JSON.stringify({ texture: 'wall.png' }));+ const { index: prev } = await scanAssetDatabase(root, { write: false });+ expect(prev.deps[MAT]).toEqual([TEX2]);++ rmSync(path.join(root, 'assets/materials/wall.png'));+ rmSync(path.join(root, 'assets/materials/wall.png.meta'));+ await expectIncrementalEqualsFull(prev, ['assets/materials/wall.png', 'assets/materials/wall.png.meta']);++ const inc = await updateAssetIndex(root, prev, ['assets/materials/wall.png'], { write: false });+ expect(inc.index.deps[MAT]).toBeUndefined(); // the edge is gone, not left dangling+ });+it('DELETING a .meta while the content file stays re-adopts it, like a full scan', async () => {const { index: prev } = await scanAssetDatabase(root, { write: false });rmSync(path.join(root, 'assets/textures/player.png.meta')); // orphan the png