53 lines
3.3 KiB
TypeScript
53 lines
3.3 KiB
TypeScript
// Raw frames from a running build of your game (headless Chromium, mobile viewport, touch, device scale).
|
|
// storebound shots capture [--lang en,tr] [--only hero,shop] [--device phone|tablet]
|
|
// Each entry of shots.capture.shots: { path, waitFor?: JS expression that turns true, eval?: [JS], delayMs?, device? }.
|
|
// URL = baseUrl + path + (? or &) + query, with "{lang}" replaced. Frames go to <shots.frames>/<lang>/<device>/<name>.png.
|
|
// The frame is the screen BELOW the status bar (the template draws one), so the viewport is phone.w x phone.h.
|
|
// Tip: give your game URL switches for screenshots (fixed seed, no ads, no tutorial, freeze at a moment).
|
|
import { mkdirSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import type { Ctx } from '../lib/ctx';
|
|
import { need } from '../lib/ctx';
|
|
import { done, log, warn } from '../lib/log';
|
|
import { launch } from './browser';
|
|
import { DEFAULT_PHONE, DEFAULT_TABLET } from './lib';
|
|
|
|
export async function run(ctx: Ctx): Promise<void> {
|
|
const { config, root, resolve } = ctx.loaded;
|
|
const shots = need(config.shots, 'shots');
|
|
const cap = need(shots.capture, 'shots.capture');
|
|
const a = ctx.args;
|
|
const langs = a.list('lang').length ? a.list('lang') : config.languages.map(l => l.id);
|
|
const only = a.list('only');
|
|
const devices = a.get('device') ? [a.get('device') as 'phone' | 'tablet'] : (['phone', 'tablet'] as const);
|
|
const dims = { phone: { ...DEFAULT_PHONE, ...shots.phone }, tablet: { ...DEFAULT_TABLET, ...shots.tablet } };
|
|
const browser = await launch(root, { headless: true, args: ['--mute-audio', '--ignore-gpu-blocklist', '--enable-webgl', '--enable-gpu'] });
|
|
let failed = 0;
|
|
try {
|
|
for (const lang of langs) for (const device of devices) for (const [name, s] of Object.entries(cap.shots)) {
|
|
if (only.length && !only.includes(name)) continue;
|
|
const want = s.device ?? 'phone';
|
|
if (want !== 'both' && want !== device) continue;
|
|
const d = dims[device];
|
|
const out = resolve(join(shots.frames, lang, device));
|
|
mkdirSync(out, { recursive: true });
|
|
const q = (cap.query ?? '').replaceAll('{lang}', lang);
|
|
const url = cap.baseUrl.replace(/\/$/, '') + s.path.replaceAll('{lang}', lang) + (q ? (s.path.includes('?') ? '&' : '?') + q : '');
|
|
const c = await browser.newContext({ viewport: { width: d.w, height: d.h }, deviceScaleFactor: d.dpr, hasTouch: true, isMobile: true, serviceWorkers: 'block', locale: lang });
|
|
const page = await c.newPage();
|
|
const errs: string[] = [];
|
|
page.on('pageerror', (e: Error) => errs.push(e.message));
|
|
try {
|
|
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 90000 });
|
|
if (s.waitFor) await page.waitForFunction(s.waitFor, null, { timeout: 120000, polling: 50 });
|
|
for (const js of s.eval ?? []) await page.evaluate(js);
|
|
await page.waitForTimeout(s.delayMs ?? 1000);
|
|
await page.screenshot({ path: join(out, `${name}.png`) });
|
|
log('shots', `${lang}/${device}/${name}.png${errs.length ? ' (page errors: ' + errs.join(' | ').slice(0, 200) + ')' : ''}`);
|
|
} catch (e) { failed++; warn('shots', `${lang}/${device}/${name}: ${(e as Error).message.slice(0, 300)}`); }
|
|
await c.close();
|
|
}
|
|
} finally { await browser.close(); }
|
|
if (failed) throw new Error(`${failed} captures failed`);
|
|
done('shots', 'capture done');
|
|
}
|