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(rc6): cook content-hashes staged assets; manifest carries hash+size (B2)

yhhc2ff9d4f8523
diff --git a/desktop/electron/cookAssets.ts b/desktop/electron/cookAssets.ts
index d35534cf1..71b101e5f 100644
--- a/desktop/electron/cookAssets.ts
+++ b/desktop/electron/cookAssets.ts
@@ -17,12 +17,29 @@
* native), texture atlasing, and the full web-build (html + runtime + scripts).
* This is the reachability + manifest + staging core they all build on.
*/
-import { writeFile, mkdir, copyFile } from 'node:fs/promises';
+import { writeFile, mkdir, readFile } from 'node:fs/promises';
import path from 'node:path';
import { scanAssetDatabase, type AssetEntry } from './assetDb';
+// Single-source content hash (sdk/src/asset/contentHash.ts). Imported as source —
+// no hand-mirrored copy — so the cook and the runtime agree by construction.
+import { contentHashHex } from '../../sdk/src/asset/contentHash';
const MANIFEST = 'assets.manifest.json';
+/**
+ * One cooked asset in the ship manifest. Extends the DB index entry with the
+ * physical-identity fields RC6 needs: `contentHash` (XXH64 of the staged bytes)
+ * and `size`. The runtime AssetRegistry consumes the same v1.0 manifest and
+ * simply ignores these extra fields (it maps uuid→path); the AddressableManifest
+ * and content-addressed naming (RC6 B-F) are what read them.
+ */
+export interface CookManifestEntry extends AssetEntry {
+ /** XXH64 (16 hex) of the exact bytes staged — the asset's physical identity. */
+ contentHash: string;
+ /** Staged byte length. */
+ size: number;
+}
+
export interface CookResult {
ok: boolean;
/** Absolute output dir the assets + manifest were staged into. */
@@ -75,20 +92,26 @@ export async function cookAssets(
const absOut = path.isAbsolute(opts.outDir) ? opts.outDir : path.join(root, opts.outDir);
await mkdir(absOut, { recursive: true });
- // Stage each reachable asset's file + build the ship manifest.
- const manifestEntries: AssetEntry[] = [];
+ // Stage each reachable asset's file + build the ship manifest. We read the
+ // bytes (rather than copyFile) so we can content-hash exactly what ships — the
+ // asset's physical identity (RC6 Batch B). After B4 this naturally hashes the
+ // ENCODED artifact (e.g. the .ktx2), since it hashes whatever bytes we stage.
+ const manifestEntries: CookManifestEntry[] = [];
for (const uuid of reachable) {
const entry = byUuid.get(uuid);
if (!entry) continue;
const dst = path.join(absOut, entry.path);
try {
+ const data = await readFile(path.join(root, entry.path));
await mkdir(path.dirname(dst), { recursive: true });
- await copyFile(path.join(root, entry.path), dst);
+ await writeFile(dst, data);
manifestEntries.push({
uuid: entry.uuid,
path: entry.path,
type: entry.type,
importer: entry.importer,
+ contentHash: contentHashHex(data),
+ size: data.byteLength,
});
} catch (err) {
warnings.push(`copy failed ${entry.path}: ${err instanceof Error ? err.message : String(err)}`);
diff --git a/desktop/electron/exportWeChat.ts b/desktop/electron/exportWeChat.ts
index e58d49bb6..2510edf6e 100644
--- a/desktop/electron/exportWeChat.ts
+++ b/desktop/electron/exportWeChat.ts
@@ -43,7 +43,7 @@ export interface ExportWeChatResult {
}
interface CookManifest {
- entries: { uuid: string; path: string; type: string }[];
+ entries: { uuid: string; path: string; type: string; contentHash?: string; size?: number }[];
}
// Editor asset type → AddressableAssetType (sdk/src/assetTypes.ts). The WeChat
@@ -135,14 +135,20 @@ async function scanWeChatSideModules(
return present;
}
-/** Read the web cook's flat manifest → AddressableManifest the WeChat runtime reads. */
+/** Read the web cook's flat manifest → AddressableManifest the WeChat runtime reads.
+ * Carries the cook's `contentHash` (XXH64) + `size` through so the runtime can
+ * dedupe by content and treat `<hash>.<ext>` as a permanently-cacheable CDN URL
+ * (RC6 Batch B); falls back to stat() for size only if a legacy cook omitted it. */
async function buildAddressableManifest(absOut: string): Promise<string> {
const cook = JSON.parse(await readFile(path.join(absOut, 'assets.manifest.json'), 'utf8')) as CookManifest;
- const assets: Record<string, { path: string; type: string; size: number; labels: string[] }> = {};
+ type Entry = { path: string; type: string; size: number; labels: string[]; contentHash?: string };
+ const assets: Record<string, Entry> = {};
for (const e of cook.entries) {
- let size = 0;
- try { size = (await stat(path.join(absOut, e.path))).size; } catch { /* missing file → 0 */ }
- assets[e.uuid.toLowerCase()] = { path: e.path, type: addrType(e.type), size, labels: [] };
+ let size = e.size ?? 0;
+ if (e.size == null) { try { size = (await stat(path.join(absOut, e.path))).size; } catch { /* missing file → 0 */ } }
+ const entry: Entry = { path: e.path, type: addrType(e.type), size, labels: [] };
+ if (e.contentHash) entry.contentHash = e.contentHash;
+ assets[e.uuid.toLowerCase()] = entry;
}
return JSON.stringify({ version: '2.0', groups: { main: { bundleMode: 'local', labels: [], assets } } }, null, 2) + '\n';
}
diff --git a/desktop/tests/cook-assets.test.ts b/desktop/tests/cook-assets.test.ts
index 96f4e2da4..a96216682 100644
--- a/desktop/tests/cook-assets.test.ts
+++ b/desktop/tests/cook-assets.test.ts
@@ -10,10 +10,11 @@ import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, existsSync
import { tmpdir } from 'node:os';
import path from 'node:path';
import { cookAssets } from '../electron/cookAssets';
+import { contentHashHex } from '../../sdk/src/asset/contentHash';
interface AssetManifest {
version: string;
- entries: Array<{ uuid: string; path: string; type: string }>;
+ entries: Array<{ uuid: string; path: string; type: string; contentHash?: string; size?: number }>;
}
let root: string;
@@ -84,4 +85,59 @@ describe('cookAssets (A4)', () => {
expect(res.warnings.some((w) => w.includes('missing.esscene'))).toBe(true);
expect(res.included).toEqual([]);
});
+
+ it('records a contentHash + size for each staged asset (RC6 Batch B)', async () => {
+ const res = await cookAssets(root, { entryScenes: ['assets/scenes/main.esscene'], outDir: 'build' });
+ const manifest = JSON.parse(readFileSync(res.manifestPath!, 'utf8')) as AssetManifest;
+ for (const e of manifest.entries) {
+ expect(e.contentHash, e.path).toMatch(/^[0-9a-f]{16}$/);
+ expect(e.size, e.path).toBeGreaterThan(0);
+ }
+ // The hash is over the exact staged bytes; 'USED' is the used.png body.
+ const tex = manifest.entries.find((e) => e.path === 'assets/textures/used.png')!;
+ expect(tex.contentHash).toBe(contentHashHex(new TextEncoder().encode('USED')));
+ expect(tex.size).toBe(4);
+ });
+
+ it('is deterministic: re-cooking yields identical content hashes', async () => {
+ const a = await cookAssets(root, { entryScenes: ['assets/scenes/main.esscene'], outDir: 'build' });
+ const b = await cookAssets(root, { entryScenes: ['assets/scenes/main.esscene'], outDir: 'build2' });
+ const hashes = (p: string) =>
+ JSON.parse(readFileSync(p, 'utf8')).entries
+ .map((e: { path: string; contentHash: string }) => `${e.path}=${e.contentHash}`)
+ .sort();
+ expect(hashes(a.manifestPath!)).toEqual(hashes(b.manifestPath!));
+ });
+
+ it('gives identical bytes one hash (dedup foundation), distinct bytes distinct hashes', async () => {
+ const r = mkdtempSync(path.join(tmpdir(), 'estella-cook-dedup-'));
+ try {
+ const A = '33333333-3333-4333-8333-333333333333';
+ const B = '44444444-4444-4444-8444-444444444444';
+ const C = '55555555-5555-4555-8555-555555555555';
+ const SC = '66666666-6666-4666-8666-666666666666';
+ const wa = (rel: string, type: string, uuid: string, body: string): void => {
+ const abs = path.join(r, rel);
+ mkdirSync(path.dirname(abs), { recursive: true });
+ writeFileSync(abs, body);
+ writeFileSync(`${abs}.meta`, JSON.stringify({ uuid, version: '2.0', type, importer: {} }));
+ };
+ wa('t/a.png', 'texture', A, 'SAME-BYTES');
+ wa('t/b.png', 'texture', B, 'SAME-BYTES'); // byte-identical to a, different uuid+path
+ wa('t/c.png', 'texture', C, 'OTHER-BYTES');
+ wa('s/main.esscene', 'scene', SC, JSON.stringify({
+ version: '1.0', name: 's', entities: [A, B, C].map((u, i) => ({
+ id: i + 1, name: `E${i}`, parent: null, children: [],
+ components: [{ type: 'Sprite', data: { texture: `@uuid:${u}` } }],
+ })),
+ }));
+ const res = await cookAssets(r, { entryScenes: ['s/main.esscene'], outDir: 'out' });
+ const m = JSON.parse(readFileSync(res.manifestPath!, 'utf8')) as AssetManifest;
+ const h = (p: string) => m.entries.find((e) => e.path === p)!.contentHash;
+ expect(h('t/a.png')).toBe(h('t/b.png')); // identical content → one physical identity
+ expect(h('t/a.png')).not.toBe(h('t/c.png')); // different content → different identity
+ } finally {
+ rmSync(r, { recursive: true, force: true });
+ }
+ });
});