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

+44 -59
View File
@@ -2,10 +2,13 @@
* GUI script patch definitions as stored on disk, one folder per change under
* `patches/gui/<domain>/<name>/`:
*
* patch.jsonc id, title, target script, features, the game versions it was verified on,
* and the list of edits; comments explain what the change does and why the
* local server cannot provide it
* <name>.nut the edit fragments named by `edits` (stock text to find, text to put there)
* patch.jsonc id, title, features, and the variants; comments explain what the change does
* and why the local server cannot provide it
* <name>.nut the edit fragments a variant names (stock text to find, text to put there)
*
* A variant is the change written for one game build: the target script and its edits. Most
* patches have one; when an update changes the stock text, a variant with its own fragment files
* is added, and exactly one variant must apply (the same rule as exe patches, @wto/exepatch).
*
* Fragment files hold the exact script text followed by one terminating newline, which is not
* part of the fragment. Line endings are normalised to `\n` here; applying matches regardless of
@@ -14,10 +17,12 @@
*/
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { parseJsonc } from '@wto/jsonc';
import { DefinitionError, readJsonObject, rejectUnknownKeys, requiredString, stringList, variantObjects } from '@wto/exepatch';
const PATCH_FILE = 'patch.jsonc';
const ALLOWED_KEYS = new Set(['id', 'title', 'file', 'features', 'verified_on', 'edits']);
const PATCH_KEYS = new Set(['id', 'title', 'features', 'variants']);
const VARIANT_KEYS = new Set(['written_for', 'file', 'edits']);
const EDIT_KEYS = new Set(['find', 'replace']);
export interface GuiScriptEdit {
/** Stock text that must occur exactly once in the target script, with `\n` line endings. */
@@ -26,14 +31,19 @@ export interface GuiScriptEdit {
readonly replace: string;
}
export interface GuiScriptVariant {
/** The game version this variant was written against; names the variant. */
readonly writtenFor: string;
/** Script path inside gui.vromfs.bin. */
readonly file: string;
readonly edits: readonly GuiScriptEdit[];
}
export interface GuiScriptPatch {
readonly id: string;
readonly title: string;
/** Script path inside gui.vromfs.bin. */
readonly file: string;
readonly features: readonly string[];
readonly verifiedOn: readonly string[];
readonly edits: readonly GuiScriptEdit[];
readonly variants: readonly GuiScriptVariant[];
readonly directory: string;
}
@@ -57,76 +67,51 @@ function findDefinitionDirectories(root: string): string[] {
function readDefinition(directory: string): GuiScriptPatch {
const where = join(directory, PATCH_FILE);
const raw = readObject(where);
const unknown = Object.keys(raw).filter((key) => !ALLOWED_KEYS.has(key));
if (unknown.length) {
throw new Error(`${where}: unknown key(s) ${unknown.join(', ')}; explanations belong in comments or the patch notes`);
}
const raw = readJsonObject(where);
rejectUnknownKeys(raw, PATCH_KEYS, where);
return {
id: requiredString(raw.id, where, 'id'),
title: requiredString(raw.title, where, 'title'),
file: requiredString(raw.file, where, 'file'),
features: stringList(raw.features, where, 'features'),
verifiedOn: nonEmptyStringList(raw.verified_on, where, 'verified_on'),
edits: readEdits(raw.edits, directory, where),
id: requiredString(raw.id, `${where} id`),
title: requiredString(raw.title, `${where} title`),
features: stringList(raw.features, `${where} features`),
variants: variantObjects(raw.variants, where).map((variant) => readVariant(variant, directory, `${where} variant ${String(variant.written_for)}`)),
directory,
};
}
function readVariant(variant: Record<string, unknown>, directory: string, where: string): GuiScriptVariant {
rejectUnknownKeys(variant, VARIANT_KEYS, where);
return {
writtenFor: variant.written_for as string,
file: requiredString(variant.file, `${where} file`),
edits: readEdits(variant.edits, directory, where),
};
}
function readEdits(value: unknown, directory: string, where: string): GuiScriptEdit[] {
if (!Array.isArray(value) || value.length === 0) throw new Error(`${where}: "edits" must be a non-empty list`);
if (!Array.isArray(value) || value.length === 0) throw new DefinitionError(`${where}: "edits" must be a non-empty list`);
return value.map((item, index) => {
if (!item || typeof item !== 'object') throw new Error(`${where}: edits[${index}] must be an object`);
const label = `${where} edits[${index}]`;
if (!item || typeof item !== 'object') throw new DefinitionError(`${label} must be an object`);
const edit = item as Record<string, unknown>;
const find = readFragment(directory, requiredString(edit.find, where, `edits[${index}].find`));
if (find === '') throw new Error(`${where}: edits[${index}].find is empty`);
const replace = readFragment(directory, requiredString(edit.replace, where, `edits[${index}].replace`));
return { find, replace };
rejectUnknownKeys(edit, EDIT_KEYS, label);
const find = readFragment(directory, requiredString(edit.find, `${label}.find`));
if (find === '') throw new DefinitionError(`${label}.find is empty`);
return { find, replace: readFragment(directory, requiredString(edit.replace, `${label}.replace`)) };
});
}
/** Fragment text with `\n` line endings and the file's terminating newline removed. */
export function readFragment(directory: string, name: string): string {
if (name.includes('/') || name.includes('\\')) throw new Error(`${directory}: fragment "${name}" must be a file in the patch folder`);
if (name.includes('/') || name.includes('\\')) throw new DefinitionError(`${directory}: fragment "${name}" must be a file in the patch folder`);
const text = readFileSync(join(directory, name), 'utf8').replace(/^/, '').replace(/\r\n/g, '\n');
return text.endsWith('\n') ? text.slice(0, -1) : text;
}
function readObject(path: string): Record<string, unknown> {
let data: unknown;
try {
data = parseJsonc(readFileSync(path, 'utf8'));
} catch (error) {
throw new Error(`${path}: ${(error as Error).message}`);
}
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error(`${path}: expected an object`);
return data as Record<string, unknown>;
}
function requiredString(value: unknown, where: string, key: string): string {
if (typeof value !== 'string' || value === '') throw new Error(`${where}: "${key}" must be a non-empty string`);
return value;
}
function stringList(value: unknown, where: string, key: string): string[] {
if (value === undefined) return [];
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) {
throw new Error(`${where}: "${key}" must be a list of strings`);
}
return value;
}
function nonEmptyStringList(value: unknown, where: string, key: string): string[] {
const list = stringList(value, where, key);
if (list.length === 0) throw new Error(`${where}: "${key}" must name at least one game version`);
return list;
}
function rejectDuplicateIds(patches: readonly GuiScriptPatch[]): void {
const seen = new Map<string, string>();
for (const patch of patches) {
const previous = seen.get(patch.id);
if (previous) throw new Error(`two GUI patches use id "${patch.id}": ${previous} and ${patch.directory}`);
if (previous) throw new DefinitionError(`two GUI patches use id "${patch.id}": ${previous} and ${patch.directory}`);
seen.set(patch.id, patch.directory);
}
}