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. BREAKING CHANGE: Each patch needs a variants list. Move executable locators into patch.jsonc and regenerate game version records with record-game-version.ts.
126 lines
5.3 KiB
TypeScript
126 lines
5.3 KiB
TypeScript
/**
|
||
* GUI script patch definitions as stored on disk, one folder per change under
|
||
* `patches/gui/<domain>/<name>/`:
|
||
*
|
||
* 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
|
||
* the script's line endings (stock scripts mix `\r\n` and `\n`), so fragments survive editors
|
||
* and git newline conversion.
|
||
*/
|
||
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
||
import { join } from 'node:path';
|
||
import { DefinitionError, readJsonObject, rejectUnknownKeys, requiredString, stringList, variantObjects } from '@wto/exepatch';
|
||
|
||
const PATCH_FILE = 'patch.jsonc';
|
||
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. */
|
||
readonly find: string;
|
||
/** Text written in its place, with `\n` line endings. */
|
||
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;
|
||
readonly features: readonly string[];
|
||
readonly variants: readonly GuiScriptVariant[];
|
||
readonly directory: string;
|
||
}
|
||
|
||
/** Every definition under `root`, sorted by id. */
|
||
export function loadGuiPatches(root: string): readonly GuiScriptPatch[] {
|
||
const patches = findDefinitionDirectories(root).map(readDefinition);
|
||
rejectDuplicateIds(patches);
|
||
return patches.sort((left, right) => left.id.localeCompare(right.id));
|
||
}
|
||
|
||
function findDefinitionDirectories(root: string): string[] {
|
||
const found: string[] = [];
|
||
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
||
if (!entry.isDirectory()) continue;
|
||
const directory = join(root, entry.name);
|
||
if (isFile(join(directory, PATCH_FILE))) found.push(directory);
|
||
else found.push(...findDefinitionDirectories(directory));
|
||
}
|
||
return found;
|
||
}
|
||
|
||
function readDefinition(directory: string): GuiScriptPatch {
|
||
const where = join(directory, PATCH_FILE);
|
||
const raw = readJsonObject(where);
|
||
rejectUnknownKeys(raw, PATCH_KEYS, where);
|
||
return {
|
||
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 DefinitionError(`${where}: "edits" must be a non-empty list`);
|
||
return value.map((item, index) => {
|
||
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>;
|
||
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 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 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 DefinitionError(`two GUI patches use id "${patch.id}": ${previous} and ${patch.directory}`);
|
||
seen.set(patch.id, patch.directory);
|
||
}
|
||
}
|
||
|
||
function isFile(path: string): boolean {
|
||
try {
|
||
return statSync(path).isFile();
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|