// Writes manifest.json into a game folder: the eager files (path + size) the browser // loads before start-up and the ranged image files it streams, chosen by web/data-pack.json. The data host serves // every file under its lower-case path, so the manifest uses lower case too. // Usage: bun tools/make-manifest.ts import { Glob } from "bun"; import { createHash } from "node:crypto"; import { join, relative } from "node:path"; import { existsSync, statSync, writeFileSync, readFileSync } from "node:fs"; const gameDir = process.argv[2]; if (!gameDir) throw new Error("usage: bun tools/make-manifest.ts "); const pack = JSON.parse(readFileSync(join(import.meta.dir, "../web/data-pack.json"), "utf8")); const lower = (p: string) => p.replaceAll("\\", "/").toLowerCase(); const matches = (patterns: string[], path: string) => patterns.some((g) => new Glob(lower(g)).match(path)); const files: { path: string; size: number }[] = []; const diskPath = new Map(); // lower-case path -> real path on disk const ranged: { path: string; size: number; memory?: boolean }[] = []; for (const path of new Glob("**/*").scanSync({ cwd: gameDir, onlyFiles: true })) { const rel = lower(path); const size = statSync(join(gameDir, path)).size; diskPath.set(rel, path); if (matches(pack.skip, rel)) continue; // A file named exactly in "eager" beats a "ranged" folder pattern. const named = pack.eager.some((g: string) => !g.includes("*") && lower(g) === rel); if (!named && matches(pack.ranged, rel)) ranged.push(matches(pack.memory ?? [], rel) ? { path: rel, size, memory: true } : { path: rel, size }); else if (matches(pack.eager, rel)) files.push({ path: rel, size }); } files.sort((a, b) => a.path.localeCompare(b.path)); const total = files.reduce((n, f) => n + f.size, 0); // All eager files in one blob, in manifest order: the browser fetches one file, not hundreds. const bundle = Buffer.concat(files.map((f) => readFileSync(join(gameDir, diskPath.get(f.path)!)))); writeFileSync(join(gameDir, "eager.bin"), bundle); // The start pack: the pieces of the world image the game reads at the spawn point (web/start-ranges.json). let start: any; const rangesFile = join(import.meta.dir, "../web/start-ranges.json"); if (existsSync(rangesFile)) { const { key, ranges } = JSON.parse(readFileSync(rangesFile, "utf8")); const image = readFileSync(join(gameDir, diskPath.get(key)!)); const pack = Buffer.concat(ranges.map(([at, size]: number[]) => image.subarray(at, at + size))); writeFileSync(join(gameDir, "start.bin"), pack); start = { path: "start.bin", key, ranges, hash: createHash("sha256").update(pack).digest("hex").slice(0, 16) }; console.log(`start pack ${(pack.length / 1048576).toFixed(1)} MB in ${ranges.length} ranges`); } writeFileSync(join(gameDir, "manifest.json"), JSON.stringify({ start, files, ranged, bundle: { path: "eager.bin", size: bundle.length, hash: createHash("sha256").update(bundle).digest("hex").slice(0, 16) } }, null, 0)); console.log(`${files.length} files, ${(total / 1048576).toFixed(1)} MB eager; ${ranged.length} ranged`);