/** * Exe patch definitions as stored on disk, one directory per change to the program's behaviour, * under patches/exe///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, 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; 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(); 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(', ')}`); } }