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:
1 parent
3f366651bd
commit
b819912733
94 files changed
+1884
-933
No files matched your search
@@ -1,36 +1,46 @@
|
||||
/**
|
||||
* Patch definitions as stored on disk, one directory per change to the program's behaviour,
|
||||
* under patches/exe/<domain>/<name>/:
|
||||
* Exe patch definitions as stored on disk, one directory per change to the program's behaviour,
|
||||
* under patches/exe/<domain>/<name>/patch.jsonc:
|
||||
*
|
||||
* patch.jsonc id, title, features, required patches, the game versions it was verified
|
||||
* on, and the bytes to write; comments explain the change and why the local
|
||||
* server cannot provide it
|
||||
* locate.jsonc how to find the site
|
||||
* 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")
|
||||
*
|
||||
* Unknown keys are rejected so that explanations stay in comments and in the patch notes
|
||||
* instead of growing into data fields.
|
||||
* 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, readFileSync, statSync } from 'node:fs';
|
||||
import { readdirSync, statSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { parseJsonc } from '@wto/jsonc';
|
||||
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 LOCATE_FILE = 'locate.jsonc';
|
||||
const PATCH_KEYS = new Set(['id', 'title', 'features', 'requires', 'verified_on', 'edit']);
|
||||
const EDIT_KEYS = new Set(['offset', 'asm', 'data', 'bytes', 'expect_original']);
|
||||
const LOCATE_KEYS = new Set(['method', 'section', 'pattern']);
|
||||
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;
|
||||
readonly payload: Uint8Array;
|
||||
readonly expectOriginal: Uint8Array;
|
||||
/** 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 LocateSpec {
|
||||
readonly method: string;
|
||||
readonly [key: string]: unknown;
|
||||
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 {
|
||||
@@ -38,9 +48,7 @@ export interface PatchDefinition {
|
||||
readonly title: string;
|
||||
readonly features: readonly string[];
|
||||
readonly requires: readonly string[];
|
||||
readonly verifiedOn: readonly string[];
|
||||
readonly edit: PatchEdit;
|
||||
readonly locate: LocateSpec;
|
||||
readonly variants: readonly PatchVariant[];
|
||||
readonly directory: string;
|
||||
}
|
||||
|
||||
@@ -65,68 +73,37 @@ function findDefinitionDirectories(root: string): string[] {
|
||||
|
||||
function readDefinition(directory: string): PatchDefinition {
|
||||
const where = join(directory, PATCH_FILE);
|
||||
const locateWhere = join(directory, LOCATE_FILE);
|
||||
const patch = readJson(where);
|
||||
const locate = readJson(locateWhere);
|
||||
const patch = readJsonObject(where);
|
||||
rejectUnknownKeys(patch, PATCH_KEYS, where);
|
||||
rejectUnknownKeys(locate, LOCATE_KEYS, locateWhere);
|
||||
if (typeof patch.id !== 'string' || typeof patch.title !== 'string') throw new DefinitionError(`${where}: needs string "id" and "title"`);
|
||||
if (typeof locate.method !== 'string') throw new DefinitionError(`${locateWhere}: needs a string "method"`);
|
||||
const verifiedOn = stringList(patch.verified_on, where, 'verified_on');
|
||||
if (verifiedOn.length === 0) throw new DefinitionError(`${where}: "verified_on" must name at least one game version`);
|
||||
return {
|
||||
id: patch.id,
|
||||
title: patch.title,
|
||||
features: stringList(patch.features, where, 'features'),
|
||||
requires: stringList(patch.requires, where, 'requires'),
|
||||
verifiedOn,
|
||||
edit: readEdit(patch.edit, where),
|
||||
locate: locate as LocateSpec,
|
||||
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 readEdit(raw: unknown, where: string): PatchEdit {
|
||||
if (!raw || typeof raw !== 'object') throw new DefinitionError(`${where}: "edit" must be an object`);
|
||||
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} edit`);
|
||||
const payload = hexBytes(edit.bytes, where, 'bytes');
|
||||
const expectOriginal = hexBytes(edit.expect_original, where, 'expect_original');
|
||||
if (payload.length !== expectOriginal.length) {
|
||||
throw new DefinitionError(`${where}: bytes is ${payload.length} bytes but expect_original is ${expectOriginal.length}`);
|
||||
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}: edit.offset must be an integer`);
|
||||
return { offset, payload, expectOriginal };
|
||||
}
|
||||
|
||||
function hexBytes(value: unknown, where: string, key: string): Uint8Array {
|
||||
if (typeof value !== 'string' || !/^([0-9a-fA-F]{2})*$/.test(value)) throw new DefinitionError(`${where}: edit.${key} must be a hex string`);
|
||||
return Buffer.from(value, 'hex');
|
||||
}
|
||||
|
||||
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 DefinitionError(`${where}: "${key}" must be a list of strings`);
|
||||
return value;
|
||||
}
|
||||
|
||||
function readJson(path: string): Record<string, unknown> {
|
||||
let data: unknown;
|
||||
try {
|
||||
data = parseJsonc(readFileSync(path, 'utf-8'));
|
||||
} catch (error) {
|
||||
throw new DefinitionError(`${path}: ${(error as Error).message}`);
|
||||
}
|
||||
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new DefinitionError(`${path}: expected a JSON object`);
|
||||
return data as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function rejectUnknownKeys(object: Record<string, unknown>, allowed: ReadonlySet<string>, where: string): void {
|
||||
const unknown = Object.keys(object).filter((key) => !allowed.has(key));
|
||||
if (unknown.length) {
|
||||
throw new DefinitionError(`${where}: unknown key(s) ${unknown.join(', ')}; explanations belong in comments or the patch notes`);
|
||||
}
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user