vc-online/tools/thumbs.ts

39 lines
2.5 KiB
TypeScript

// Takes the vehicle pictures for the menu from the running game (photo mode), for every vehicle in menu.json.
// Needs: bun tools/serve.ts web/dist <game dir> 8790 and bun server/main.ts on 8791.
// Usage: bun tools/thumbs.ts <game dir>
import { chromium } from "playwright-core";
import { mkdirSync, readFileSync } from "node:fs";
const dir = process.argv[2];
if (!dir) throw new Error("usage: bun tools/thumbs.ts <game dir>");
const { cars, weapons } = JSON.parse(readFileSync(`${dir}/online/menu.json`, "utf8"));
const out = `${dir}/online/thumbs`;
mkdirSync(out, { recursive: true });
const w = 1600, h = 900, k = w / 1280;
const b = await chromium.launch({ channel: "chrome", headless: true, args: ["--mute-audio"] });
const p = await (await b.newContext({ viewport: { width: w, height: h } })).newPage();
p.on("console", (m) => { if (/error|abort|cannot|failed/i.test(m.text())) console.log("console:", m.text().slice(0, 200)); });
p.on("pageerror", (e) => console.log("pageerror", String(e).slice(0, 300)));
await p.addInitScript(() => { try { localStorage.setItem("vcNamed", "1"); } catch (e) {} });
await p.goto("http://127.0.0.1:8790/?mute=1&server=ws://127.0.0.1:8791/ws");
// Photo mode wears the engine out after a few dozen vehicles: a fresh page for each batch.
async function ready() {
await p.waitForFunction(() => (window as any).vcPlaying, null, { timeout: 180000, polling: 500 });
await p.addStyleTag({ content: ".ui { display: none !important; }" });
await new Promise((r) => setTimeout(r, 6000));
}
await ready();
let shot = 0;
for (const g of weapons) {
await p.evaluate((t) => { const M = (window as any).Module; (M.vcCmds || (M.vcCmds = [])).push("weapon " + t); }, g.type);
await new Promise((r) => setTimeout(r, 1200));
await p.screenshot({ path: `${out}/w${g.type}.jpg`, type: "jpeg", quality: 85, timeout: 90000, clip: { x: 1132 * k, y: 38 * k, width: 96 * k, height: 96 * k } });
}
for (const c of cars) {
if (shot++ % 12 === 0) { await p.reload(); await ready(); }
await p.evaluate((m) => { const M = (window as any).Module; (M.vcCmds || (M.vcCmds = [])).push("photo " + m); }, c.model);
await new Promise((r) => setTimeout(r, 2500));
await p.screenshot({ path: `${out}/${c.model}.jpg`, type: "jpeg", quality: 80, timeout: 90000, clip: c.bike ? { x: 531 * k, y: 280 * k, width: 220 * k, height: 124 * k } : { x: 451 * k, y: 268 * k, width: 330 * k, height: 186 * k } });
console.log(c.model);
}
await Promise.race([b.close(), new Promise((r) => setTimeout(r, 4000))]);
process.exit(0);