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(editor): translate live asset handles to PROJECT refs, not fetch URLs

yhh69a82684a65c
diff --git a/desktop/src/engine/liveAssetRefs.ts b/desktop/src/engine/liveAssetRefs.ts
index e187d689f..a39440749 100644
--- a/desktop/src/engine/liveAssetRefs.ts
+++ b/desktop/src/engine/liveAssetRefs.ts
@@ -16,6 +16,17 @@ export interface LiveComponent {
data: Record<string, unknown>;
}
+/**
+ * A realm's Assets records the FETCHABLE form of every load path (its ref
+ * resolver returns URLs — `estella://project/assets/x.png`); the inspector
+ * speaks PROJECT-RELATIVE refs. Strip the origin so the translated value is
+ * something the editor registry can name (thumbnail, locate). Pure.
+ */
+export function projectRelative(path: string, originBase: string): string {
+ const prefix = `${originBase.replace(/\/$/, '')}/`;
+ return path.startsWith(prefix) ? path.slice(prefix.length) : path;
+}
+
/**
* Rewrite handle-valued asset fields to the load paths behind them, via
* `pathForHandle` (the realm Assets' reverse map — see Assets.pathForHandle).
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index 342bc22cb..2d9d59d63 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -18,7 +18,7 @@ import { createWebApp, setEditorMode, setPlayMode, initPlayRealmRuntime, getComp
import type { App, ESEngineModule, SceneData } from 'esengine';
import { PLAY_PROTOCOL_VERSION } from './engine/playProtocol';
import type { PlayOutbound, PlayInbound } from './engine/playProtocol';
-import { translateAssetHandles } from './engine/liveAssetRefs';
+import { translateAssetHandles, projectRelative } from './engine/liveAssetRefs';
type LiveEntity = SceneData['entities'][number];
@@ -76,8 +76,13 @@ function liveSnapshot(world: App['world'], selectedId: number | null): { tree: S
// The World stores HANDLES in asset slots; the inspector speaks REFS —
// translate at this boundary via the realm's own Assets, so the Details
// panel names the asset instead of flagging a live handle as "empty".
+ // This realm's loads resolve to fetchable estella:// URLs; strip the
+ // project origin so the editor registry can name the result.
const assets = app?.getResource(Assets) ?? null;
- const components = translateAssetHandles(raw, (kind, handle) => assets?.pathForHandle(kind, handle) ?? null);
+ const components = translateAssetHandles(raw, (kind, handle) => {
+ const p = assets?.pathForHandle(kind, handle) ?? null;
+ return p === null ? null : projectRelative(p, projectBase);
+ });
selected = { id: selectedId, name: nameOf(selectedId), parent: parentOf.get(selectedId) ?? null, children: childrenOf.get(selectedId) ?? [], components } as unknown as LiveEntity;
}
return { tree, selected };
diff --git a/desktop/tests/live-asset-refs.test.ts b/desktop/tests/live-asset-refs.test.ts
index 8d703e01e..f75eb5545 100644
--- a/desktop/tests/live-asset-refs.test.ts
+++ b/desktop/tests/live-asset-refs.test.ts
@@ -8,7 +8,7 @@
* Assets recorded — and leaves everything it can't name untouched.
*/
import { describe, it, expect } from 'vitest';
-import { translateAssetHandles } from '../src/engine/liveAssetRefs';
+import { translateAssetHandles, projectRelative } from '../src/engine/liveAssetRefs';
const resolver = (kind: string, handle: number): string | null =>
kind === 'texture' && handle === 7 ? 'assets/art/hero.png' : null;
@@ -46,3 +46,17 @@ describe('translateAssetHandles', () => {
expect(out[0].data.source).toBe('assets/maps/level1.tmj');
});
});
+
+describe('projectRelative', () => {
+ it('strips the realm origin so the editor registry can name the asset', () => {
+ expect(projectRelative('estella://project/assets/art/hero.png', 'estella://project'))
+ .toBe('assets/art/hero.png');
+ expect(projectRelative('estella://project/assets/x.png', 'estella://project/'))
+ .toBe('assets/x.png'); // trailing-slash base normalizes
+ });
+
+ it('passes through paths from other origins / already-relative paths', () => {
+ expect(projectRelative('assets/art/hero.png', 'estella://project')).toBe('assets/art/hero.png');
+ expect(projectRelative('https://cdn/x.png', 'estella://project')).toBe('https://cdn/x.png');
+ });
+});