// Records what the game reads from the world image at the spawn point and writes web/start-ranges.json. // Headless, muted, and not connected to the game server. Usage: bun tools/trace-start.ts import { chromium } from "playwright-core"; import { join } from "node:path"; const url = process.argv[2]; const br = await chromium.launch({ channel: "chrome", headless: true, args: ["--mute-audio"] }); const p = await (await br.newContext({ viewport: { width: 1280, height: 720 } })).newPage(); await p.addInitScript(() => { try { localStorage.setItem("vcNamed", "1"); } catch (e) {} }); await p.goto(`${url}?mute=1&imgdelay=900&readlog=1&server=ws://127.0.0.1:9/ws`); await p.waitForFunction(() => (window as any).vcPlaying, null, { timeout: 240000, polling: 500 }); await p.waitForTimeout(30000); // the first seconds of play load what stands around the player const log: string[] = await p.evaluate(() => (window as any).Module.vcReadLog); await Promise.race([br.close(), new Promise((r) => setTimeout(r, 4000))]); const reads = log.map((l) => l.split(":").map(Number)).sort((a, b) => a[0] - b[0]); const ranges: number[][] = []; for (const [at, size] of reads) { const last = ranges[ranges.length - 1]; if (last && at <= last[0] + last[1]) last[1] = Math.max(last[1], at + size - last[0]); else ranges.push([at, size]); } const total = ranges.reduce((n, r) => n + r[1], 0); await Bun.write(join(import.meta.dir, "../web/start-ranges.json"), JSON.stringify({ key: "models/gta3.img", ranges })); console.log(`${log.length} reads, ${ranges.length} ranges, ${(total / 1048576).toFixed(1)} MB`); process.exit(0);