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.

feat(examples): editor-experienceable content hot-update (Option B)

yhh4a6a13be1cd5
diff --git a/examples/hot-update-demo/README.md b/examples/hot-update-demo/README.md
index c9a2c0f7f..f2ad5093e 100644
--- a/examples/hot-update-demo/README.md
+++ b/examples/hot-update-demo/README.md
@@ -57,11 +57,20 @@ if (plan.hasUpdate) {
}
```
-`manifestUrl` / `remoteRoot` come from `window.__estellaHotUpdate` if a host sets
-it; otherwise the demo probes a same-origin `asset-manifest.json`. In editor Play
-there is no newer build to fetch, so the console honestly reports **已是最新版本** —
-the real swap is exercised by the render verify below (and in a shipped build
-pointed at a CDN that has newer content).
+The candidate manifest defaults to a **checked-in local update channel** —
+`updates/v2-manifest.json` + `updates/art-v2.png` — so the content swap is live in
+editor Play with no CDN: press Play, watch the boot auto-check report **发现新版本 ·
+1 个文件 · 208 B**, press **下载并更新**, and the background sprite turns from green
+to red (`applyUpdate` downloads the v2 texture, verifies its `contentHash`, and the
+built-in rebinder swaps it in). `v2-manifest.json` is a full manifest that mirrors
+the running one with only the `cdn` texture bumped, so the diff is exactly one
+changed asset.
+
+The channel lives **outside `assets/`**, so a cooked build never bundles it: the
+same URL 404s and a shipped build honestly reports **已是最新版本**. A real
+deployment sets `window.__estellaHotUpdate` to point at its CDN instead — and the
+render verify below exercises that path against a genuinely cooked,
+content-addressed CDN update.
## How it works — content addressing
diff --git a/examples/hot-update-demo/src/config.ts b/examples/hot-update-demo/src/config.ts
index 87e193806..2b8fd680f 100644
--- a/examples/hot-update-demo/src/config.ts
+++ b/examples/hot-update-demo/src/config.ts
@@ -20,10 +20,19 @@ export const PACK_TILES: readonly string[] = [
/**
* Where "Check for update" looks for a candidate manifest. A shipped build (or a
- * host) can point this at a CDN by setting `window.__estellaHotUpdate` before the
- * game boots; otherwise we probe a same-origin `asset-manifest.json` and treat a
- * miss as "already up to date" (the editor Play case — there is no newer build to
- * fetch, so the update flow honestly reports it).
+ * host) can point this at a real CDN by setting `window.__estellaHotUpdate` before
+ * the game boots.
+ *
+ * The default is a **checked-in local update channel** — `updates/v2-manifest.json`
+ * plus `updates/art-v2.png`, a full manifest that mirrors the running one with the
+ * `cdn` texture bumped to a red "v2". It lives OUTSIDE `assets/`, so:
+ * - in editor Play the whole project root is served over `estella://`, so the
+ * manifest resolves → checkForUpdate finds one changed asset → you can download
+ * and apply a genuine content swap, live, without a CDN;
+ * - a cooked/shipped build never bundles `updates/`, so the same URL 404s and the
+ * console honestly reports "已是最新版本" (a real deployment would point
+ * `__estellaHotUpdate` at its CDN instead).
+ * One config, correct in both realms — no realm sniffing.
*/
export interface HotUpdateEndpoint {
manifestUrl: string;
@@ -31,11 +40,19 @@ export interface HotUpdateEndpoint {
}
export function hotUpdateEndpoint(): HotUpdateEndpoint {
- const w = globalThis as unknown as { __estellaHotUpdate?: Partial<HotUpdateEndpoint> };
+ const w = globalThis as unknown as {
+ __estellaHotUpdate?: Partial<HotUpdateEndpoint>;
+ location?: { origin?: string };
+ };
const cfg = w.__estellaHotUpdate ?? {};
+ // checkForUpdate/applyUpdate fetch through the raw backend (no asset-base
+ // prefixing), so the manifest + its assets must be addressed absolutely. The
+ // page origin IS the project root in editor Play (estella://project) and the
+ // deploy root in a shipped build, so both realms resolve correctly from it.
+ const origin = w.location?.origin ?? '';
return {
- manifestUrl: cfg.manifestUrl ?? 'asset-manifest.json',
- remoteRoot: cfg.remoteRoot ?? '',
+ manifestUrl: cfg.manifestUrl ?? `${origin}/updates/v2-manifest.json`,
+ remoteRoot: cfg.remoteRoot ?? origin,
};
}
diff --git a/examples/hot-update-demo/src/systems/build.ts b/examples/hot-update-demo/src/systems/build.ts
index c02dd3b8b..f383bb233 100644
--- a/examples/hot-update-demo/src/systems/build.ts
+++ b/examples/hot-update-demo/src/systems/build.ts
@@ -72,9 +72,10 @@ export const buildSystem = defineSystem(
state.message = '发现新版本';
}
} catch {
- // No candidate manifest reachable (e.g. editor Play): honestly "up to date".
+ // No candidate manifest reachable (e.g. a shipped build with no CDN
+ // configured, where updates/ was never bundled): honestly up to date.
state.phase = 'up-to-date';
- state.message = '已是最新版本(当前为本地内容)';
+ state.message = '已是最新版本';
}
}
diff --git a/examples/hot-update-demo/updates/art-v2.png b/examples/hot-update-demo/updates/art-v2.png
new file mode 100644
index 000000000..24a01e2c8
Binary files /dev/null and b/examples/hot-update-demo/updates/art-v2.png differ
diff --git a/examples/hot-update-demo/updates/art-v2.png.meta b/examples/hot-update-demo/updates/art-v2.png.meta
new file mode 100644
index 000000000..84301c402
--- /dev/null
+++ b/examples/hot-update-demo/updates/art-v2.png.meta
@@ -0,0 +1,12 @@
+{
+ "uuid": "a1b2c3d4-0004-4004-8004-000000000000",
+ "version": "2.0",
+ "type": "texture",
+ "importer": {
+ "maxSize": 2048,
+ "filterMode": "linear",
+ "wrapMode": "clamp",
+ "premultiplyAlpha": false,
+ "sliceBorder": { "left": 0, "right": 0, "top": 0, "bottom": 0 }
+ }
+}
diff --git a/examples/hot-update-demo/updates/v2-manifest.json b/examples/hot-update-demo/updates/v2-manifest.json
new file mode 100644
index 000000000..2294cb1b0
--- /dev/null
+++ b/examples/hot-update-demo/updates/v2-manifest.json
@@ -0,0 +1,32 @@
+{
+ "version": "2.0",
+ "revision": "v2-red",
+ "groups": {
+ "main": {
+ "bundleMode": "local",
+ "labels": [],
+ "assets": {
+ "a1b2c3d4-0001-4001-8001-000000000001": { "path": "assets/scenes/main.esscene", "type": "json", "size": 0, "labels": [] }
+ }
+ },
+ "cdn": {
+ "bundleMode": "remote",
+ "labels": [],
+ "assets": {
+ "a1b2c3d4-0002-4002-8002-000000000002": { "path": "updates/art-v2.png", "type": "texture", "size": 208, "labels": [], "contentHash": "cd75478792888fb1" }
+ }
+ },
+ "pack": {
+ "bundleMode": "remote",
+ "labels": [],
+ "assets": {
+ "a1b2c3d4-0003-4003-8003-000000000000": { "path": "assets/pack/tile0.png", "type": "texture", "size": 0, "labels": [] },
+ "a1b2c3d4-0003-4003-8003-000000000001": { "path": "assets/pack/tile1.png", "type": "texture", "size": 0, "labels": [] },
+ "a1b2c3d4-0003-4003-8003-000000000002": { "path": "assets/pack/tile2.png", "type": "texture", "size": 0, "labels": [] },
+ "a1b2c3d4-0003-4003-8003-000000000003": { "path": "assets/pack/tile3.png", "type": "texture", "size": 0, "labels": [] },
+ "a1b2c3d4-0003-4003-8003-000000000004": { "path": "assets/pack/tile4.png", "type": "texture", "size": 0, "labels": [] },
+ "a1b2c3d4-0003-4003-8003-000000000005": { "path": "assets/pack/tile5.png", "type": "texture", "size": 0, "labels": [] }
+ }
+ }
+ }
+}