/** * Development script: check every exe and GUI patch against one game build and write its version * record (packages/patcher/src/version-record.ts). * * node scripts/record-game-version.ts --game [--out ] * [--patches ] [--gui-patches ] * * The game folder is only read. The record goes to /.jsonc (default * patches/versions). Nothing is written when an essential patch is unavailable * (essential-features.ts), since such a build cannot be adapted by a record alone. */ import { readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseArgs } from 'node:util'; import { loadPatches } from '@wto/exepatch'; import { blockingFailures, checkGameBuild, formatVersionRecord, GAME_FILES, loadGuiPatches, recordFileName } from '@wto/patcher'; const REPOSITORY = fileURLToPath(new URL('..', import.meta.url)); const USAGE = 'usage: record-game-version.ts --game [--out ] [--patches ] [--gui-patches ]'; function main(): number { const { values } = parseArgs({ options: { game: { type: 'string' }, out: { type: 'string', default: join(REPOSITORY, 'patches', 'versions') }, patches: { type: 'string', default: join(REPOSITORY, 'patches', 'exe') }, 'gui-patches': { type: 'string', default: join(REPOSITORY, 'patches', 'gui') }, }, }); if (!values.game) { console.error(USAGE); return 2; } const record = checkGameBuild({ executable: readFileSync(join(values.game, GAME_FILES.executable)), guiPack: readFileSync(join(values.game, GAME_FILES.guiPack)), exePatches: values.patches, guiPatches: values['gui-patches'], }); const exeCount = Object.keys(record.exePatches).length; const guiCount = Object.keys(record.guiPatches).length; console.log(`${record.gameVersion}: ${exeCount} exe patch(es) and ${guiCount} GUI patch(es) apply`); for (const [id, reason] of Object.entries(record.unavailable)) console.log(` unavailable ${id}: ${reason}`); const blocking = blockingFailures(unavailableFailures(record.unavailable, values.patches, values['gui-patches'])); if (blocking.length) { console.error(`essential patch(es) unavailable: ${blocking.map((failure) => failure.patchId).join(', ')}; nothing was written`); return 1; } const path = join(values.out, recordFileName(record.gameVersion)); writeFileSync(path, formatVersionRecord(record), 'utf-8'); console.log(`wrote ${path}`); return 0; } /** The unavailable patches with their feature tags, as the essential-features check takes them. */ function unavailableFailures(unavailable: Readonly>, exePatches: string, guiPatches: string) { const features = new Map([...loadPatches(exePatches), ...loadGuiPatches(guiPatches)].map((patch) => [patch.id, patch.features])); return Object.entries(unavailable).map(([patchId, reason]) => ({ patchId, reason, features: features.get(patchId) ?? [] })); } process.exitCode = main();