Let each patch carry variants written for different game builds

A game update used to fail a patch whenever any byte it compared moved, even
when only an address in the instruction changed. Patch definitions now say
what a change needs and read the rest from the code:

- patch.jsonc holds a list of variants, each written for one game build
  (written_for) with its own locate block and edit; locate.jsonc is merged
  into it. Exactly one variant must apply, otherwise the patch is left out.
  GUI script patches use the same variants rule.
- expect_original and the bytes to write use the signature syntax, so
  addresses that differ between builds can be wildcards.
- A locate block can capture values at the site (rel32 targets, immediates,
  displacements); a rel32 fixup fills a hole in the written bytes from a
  capture. allow-local-host-bot-spawns now jumps to the target of the
  conditional jump it replaces instead of a stored distance, which makes it
  apply to 2.61.0.43 as well.
- verified_on is gone from the definitions. patches/versions/<version>.jsonc
  records, per checked game build, which variant of every patch applies and
  where; scripts/record-game-version.ts writes it and the launcher reads the
  recorded versions from there.

The Python tools (check-patches, patch-exe, check-gui) read the same format;
check-patches assembles a variant whose assembly names captures at the site
it was located at.

Checked: root and launcher type checks; check-patches and check-gui on
2.61.0.37 and 2.61.0.43 (12 exe and 35 GUI patches each); check-patches and
check-gui report a deliberately broken patch with a non-zero exit;
build-game-copy from 2.61.0.43 into a temporary folder. The computed bytes of
allow-local-host-bot-spawns on 2.61.0.37 equal the bytes it stored before.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 11:47:08 +08:00
1 parent 3f366651bd
commit b819912733
94 files changed
+1884 -933

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
/**
* 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 <stock game folder> [--out <records folder>]
* [--patches <exe patches>] [--gui-patches <GUI patches>]
*
* The game folder is only read. The record goes to <out>/<game version>.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 <stock game folder> [--out <records folder>] [--patches <exe patches>] [--gui-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<Record<string, string>>, 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();