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(video): a clip ref resolves to a URL the realm can open, so a playable plays it
esenginecd19e51f9fd4
diff --git a/sdk/src/playableRuntime.ts b/sdk/src/playableRuntime.tsindex e233c215d..659bb4a7a 100644--- a/sdk/src/playableRuntime.ts+++ b/sdk/src/playableRuntime.ts@@ -105,9 +105,10 @@ export async function initPlayableRuntime(config: PlayableRuntimeConfig): Promis// resolveRef contract; a ref with no known path (or already a path) passes// through, and the backend resolves both spellings off the same aliased map.const backend = new EmbeddedBackend(assets);+ const resolveRefToPath = (ref: string): string => keyToPath[ref] ?? ref;const source: RuntimeAssetSource = {backend,- resolveRef: (ref) => keyToPath[ref] ?? ref,+ resolveRef: resolveRefToPath,decodePixels: (path) => loadImagePixels(backend.resolveUrl(path)),// Map keys are the shippable paths (logical aliases included) — the// .eslocale discovery filters on extension, so aliases resolve fine.@@ -122,12 +123,12 @@ export async function initPlayableRuntime(config: PlayableRuntimeConfig): Promis});}- // Video source refs resolve through the SAME embedded map — the Video- // component's clip and VideoPlayer.play() both get the inlined data URL, so- // the single-file playable never fetches an external file (a `file://` page- // is a null origin and its sibling requests are CORS-blocked).+ // A single-file playable makes NO external requests, so a video's ref has to+ // come out as the inlined data URL — the path→URL step the backend owns. Same+ // composition the scene loader installs (it re-binds this per scene load); both+ // spell it out so a play() before the first scene resolves too.if (app.hasResource(VideoPlayer)) {- app.getResource(VideoPlayer).setRefResolver((ref) => assets[ref] ?? ref);+ app.getResource(VideoPlayer).setRefResolver((ref) => backend.resolveUrl(resolveRefToPath(ref)));}await initRuntime({diff --git a/sdk/src/runtimeLoader.ts b/sdk/src/runtimeLoader.tsindex caf5e97e7..95019118e 100644--- a/sdk/src/runtimeLoader.ts+++ b/sdk/src/runtimeLoader.ts@@ -271,9 +271,14 @@ export async function loadRuntimeScene(options: LoadRuntimeSceneOptions): Promisconst sceneAssets = runtimeAssets;// Video source refs resolve through the SAME channel as every other asset// (the editor's play realm wires this too) — a cooked build maps the- // authored logical ref (clip.mp4 / @uuid) to its staged .esv.+ // authored logical ref (clip.mp4 / @uuid) to its staged .esv. The staged path+ // then goes through the realm's backend, because what a video element needs is+ // a URL it can open: identity for http/filesystem realms, the inlined data URL+ // for the single-file playable (where there is no file to fetch at all).if (source.resolveRef && app.hasResource(VideoPlayer)) {- app.getResource(VideoPlayer).setRefResolver(source.resolveRef);+ const resolveRef = source.resolveRef;+ const backend = source.backend;+ app.getResource(VideoPlayer).setRefResolver((ref) => backend.resolveUrl(resolveRef(ref)));}mergeSceneTextureImportSettings(sceneAssets, sceneData, source.resolveRef ?? ((r) => r));const assetResult = await sceneAssets.preloadSceneAssets(sceneData, undefined, { skipSpine: true });diff --git a/sdk/tests/video-ref-resolution.test.ts b/sdk/tests/video-ref-resolution.test.tsnew file mode 100644index 000000000..b64c15a9a--- /dev/null+++ b/sdk/tests/video-ref-resolution.test.ts@@ -0,0 +1,70 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file A video ref must come out of the scene loader as something the video+ * backend can OPEN, which is two steps: the realm's logical→staged mapping+ * (`resolveRef`), then that path through the realm's asset backend. Installing+ * `resolveRef` alone shipped a single-file playable whose <video> pointed at a+ * sibling path the package does not contain — it played on the web (where the+ * staged path IS the URL) and drew a blank white quad everywhere else.+ */+import { describe, it, expect } from 'vitest';+import { loadRuntimeScene } from '../src/runtimeLoader';+import { VideoPlayer } from '../src/video/VideoAPI';+import { World } from '../src/world';+import type { App } from '../src/app';+import type { Backend } from '../src/asset/Backend';+import type { ESEngineModule } from '../src/wasm';+import type { SceneData } from '../src/scene';++const fakeModule = { _malloc: () => 0, _free: () => {} } as unknown as ESEngineModule;++const CLIP_DATA_URL = 'data:video/mp4;base64,AAAA';++/** Stands in for EmbeddedBackend: a known path resolves to its inlined data URL. */+const embeddedBackend: Backend = {+ resolveUrl: (p: string) => (p === 'assets/video/clip.mp4' ? CLIP_DATA_URL : p),+ fetchText: async () => '',+ fetchBinary: async () => new ArrayBuffer(0),+} as unknown as Backend;++function makeFakeApp(): { app: App; resources: Map<unknown, unknown> } {+ const resources = new Map<unknown, unknown>();+ const app = {+ world: new World(),+ hasResource: (r: unknown) => resources.has(r),+ getResource: (r: unknown) => resources.get(r),+ insertResource: (r: unknown, v: unknown) => { resources.set(r, v); },+ getPlugin: () => null,+ sideModules: undefined,+ } as unknown as App;+ return { app, resources };+}++describe('video ref resolution (scene load)', () => {+ it('composes the realm ref mapping with the backend URL', async () => {+ const { app, resources } = makeFakeApp();+ let resolver: ((ref: string) => string) | null = null;+ resources.set(VideoPlayer, { setRefResolver: (f: (ref: string) => string) => { resolver = f; } });++ await loadRuntimeScene({+ app,+ module: fakeModule,+ sceneData: { version: '1.0', name: 'main', entities: [] } as SceneData,+ source: {+ backend: embeddedBackend,+ decodePixels: () => Promise.reject(new Error('no textures in this test')),+ resolveRef: (ref) => (ref === '@uuid:clip' ? 'assets/video/clip.mp4' : ref),+ },+ spineManager: null,+ });++ expect(resolver).toBeTruthy();+ const resolve = resolver as unknown as (ref: string) => string;+ expect(resolve('@uuid:clip')).toBe(CLIP_DATA_URL);+ expect(resolve('assets/video/clip.mp4')).toBe(CLIP_DATA_URL);+ // An unknown ref passes through untouched, so the audio-sibling probe+ // (`<source>.m4a`) still reads as "no cooked track" rather than a bogus URL.+ expect(resolve('assets/video/clip.mp4.m4a')).toBe('assets/video/clip.mp4.m4a');+ });+});