/** * Development script: check every exe patch, GUI patch and native module of the patch set against * one game build and write its version record (packages/patcher/src/version-record.ts). * * node scripts/record-game-version.ts --game [--patches ] * * The game folder is only read. The record goes to /versions/.jsonc; the * patch set defaults to this repository's patches/. 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 { blockingFailures, checkGameBuild, formatVersionRecord, GAME_FILES, patchFeatures, patchSetFolders, recordFileName, type VersionRecord, } from '@wto/patcher'; const REPOSITORY = fileURLToPath(new URL('..', import.meta.url)); const USAGE = 'usage: record-game-version.ts --game [--patches ]'; function main(): number { const { values } = parseArgs({ options: { game: { type: 'string' }, patches: { type: 'string', default: join(REPOSITORY, 'patches') }, }, }); 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)), patches: values.patches, }); describe(record); const blocking = blockingFailures(unavailableFailures(record, values.patches)); if (blocking.length) { console.error(`essential patch(es) unavailable: ${blocking.map((failure) => failure.patchId).join(', ')}; nothing was written`); return 1; } const path = join(patchSetFolders(values.patches).versions, recordFileName(record.gameVersion)); writeFileSync(path, formatVersionRecord(record), 'utf-8'); console.log(`wrote ${path}`); return 0; } function describe(record: VersionRecord): void { const counts = [ `${Object.keys(record.exePatches).length} exe patch(es)`, `${Object.keys(record.guiPatches).length} GUI patch(es)`, `${Object.keys(record.nativeModules).length} native module(s)`, ]; console.log(`${record.gameVersion}: ${counts.join(', ')} apply`); for (const [id, reason] of Object.entries(record.unavailable)) console.log(` unavailable ${id}: ${reason}`); } /** The unavailable patches with their feature tags, as the essential-features check takes them. */ function unavailableFailures(record: VersionRecord, patches: string) { const features = new Map(); for (const feature of patchFeatures(patches)) { for (const id of feature.patchIds) features.set(id, [...(features.get(id) ?? []), feature.tag]); } return Object.entries(record.unavailable).map(([patchId, reason]) => ({ patchId, reason, features: features.get(patchId) ?? [] })); } process.exitCode = main();