Files
WT-Offline/packages/exepatch/src/definition.ts
T
N0VAN0M4LY b819912733 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.
2026-11-29 11:47:08 +08:00

133 lines
5.9 KiB
TypeScript

/**
* Exe patch definitions as stored on disk, one directory per change to the program's behaviour,
* under patches/exe/<domain>/<name>/patch.jsonc:
*
* id, title, features, requires what the patch is; comments explain the change and why the
* local server cannot provide it
* variants ways to make the change, each written for one game build:
* where it goes ("locate") and what to write ("edit")
*
* Most patches have one variant. When a game update changes the code a variant relies on, a
* new variant is added for that patch alone; the engine tries every variant and requires that
* exactly one applies. Which builds a variant was checked on is recorded per game version in
* patches/versions/, not here.
*/
import { readdirSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { parseBytePattern, type BytePattern } from './byte-pattern.ts';
import { DefinitionError } from './errors.ts';
import { readFixupSpecs, type FixupSpec } from './fixups.ts';
import { readJsonObject, rejectUnknownKeys, requiredString, stringList, variantObjects } from './json-fields.ts';
import { readSiteSpec, type SiteSpec } from './site.ts';
const PATCH_FILE = 'patch.jsonc';
const PATCH_KEYS = new Set(['id', 'title', 'features', 'requires', 'variants']);
const VARIANT_KEYS = new Set(['written_for', 'locate', 'edit']);
/** asm and data are the human-readable source of `bytes`; tools/ checks they agree. */
const EDIT_KEYS = new Set(['offset', 'asm', 'data', 'bytes', 'expect_original', 'fixups']);
export interface PatchEdit {
/** Distance from the located site to the first byte written. */
readonly offset: number;
/** The bytes to write; holes are filled by `fixups`. */
readonly payload: BytePattern;
readonly fixups: readonly FixupSpec[];
/** The bytes that must be there; wildcards accept any byte. */
readonly expectOriginal: BytePattern;
}
export interface PatchVariant {
/** The game version this variant was written against; names the variant. */
readonly writtenFor: string;
readonly locate: SiteSpec;
readonly edit: PatchEdit;
}
export interface PatchDefinition {
readonly id: string;
readonly title: string;
readonly features: readonly string[];
readonly requires: readonly string[];
readonly variants: readonly PatchVariant[];
readonly directory: string;
}
/** Every definition under `root`, sorted by id. */
export function loadPatches(root: string): PatchDefinition[] {
const definitions = findDefinitionDirectories(root).map(readDefinition);
rejectDuplicateIds(definitions);
rejectUnknownRequirements(definitions);
return definitions.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): PatchDefinition {
const where = join(directory, PATCH_FILE);
const patch = readJsonObject(where);
rejectUnknownKeys(patch, PATCH_KEYS, where);
return {
id: requiredString(patch.id, `${where} id`),
title: requiredString(patch.title, `${where} title`),
features: stringList(patch.features, `${where} features`),
requires: stringList(patch.requires, `${where} requires`),
variants: variantObjects(patch.variants, where).map((variant) => readVariant(variant, `${where} variant ${String(variant.written_for)}`)),
directory,
};
}
function readVariant(variant: Record<string, unknown>, where: string): PatchVariant {
rejectUnknownKeys(variant, VARIANT_KEYS, where);
const locate = readSiteSpec(variant.locate, `${where} locate`);
return { writtenFor: variant.written_for as string, locate, edit: readEdit(variant.edit, locate, `${where} edit`) };
}
function readEdit(raw: unknown, locate: SiteSpec, where: string): PatchEdit {
if (!raw || typeof raw !== 'object') throw new DefinitionError(`${where} must be an object`);
const edit = raw as Record<string, unknown>;
rejectUnknownKeys(edit, EDIT_KEYS, where);
const payload = parseBytePattern(edit.bytes, `${where} bytes`);
const expectOriginal = parseBytePattern(edit.expect_original, `${where} expect_original`);
if (payload.bytes.length !== expectOriginal.bytes.length) {
throw new DefinitionError(`${where}: bytes is ${payload.bytes.length} bytes but expect_original is ${expectOriginal.bytes.length}`);
}
const offset = edit.offset ?? 0;
if (typeof offset !== 'number' || !Number.isInteger(offset)) throw new DefinitionError(`${where}: offset must be an integer`);
const captureNames = new Set(locate.captures.map((capture) => capture.name));
return { offset, payload, fixups: readFixupSpecs(edit.fixups, payload, captureNames, where), expectOriginal };
}
function isFile(path: string): boolean {
try {
return statSync(path).isFile();
} catch {
return false;
}
}
function rejectDuplicateIds(definitions: readonly PatchDefinition[]): void {
const seen = new Map<string, string>();
for (const definition of definitions) {
const previous = seen.get(definition.id);
if (previous) throw new DefinitionError(`two definitions use id "${definition.id}": ${previous} and ${definition.directory}`);
seen.set(definition.id, definition.directory);
}
}
function rejectUnknownRequirements(definitions: readonly PatchDefinition[]): void {
const known = new Set(definitions.map((definition) => definition.id));
for (const definition of definitions) {
const unknown = definition.requires.filter((id) => !known.has(id));
if (unknown.length) throw new DefinitionError(`${definition.id} requires unknown patch(es): ${unknown.join(', ')}`);
}
}