refactor(patches)!: migrate definitions to JSONC and script fragments

Patch definitions under exe-patches/ and gui-patches/ move from
patch.json/locate.json to patch.jsonc/locate.jsonc. Each file now holds
only the data needed to apply the patch plus a short English comment
stating what it changes, why the local service cannot provide it, and
what happens without it. Every definition records the game versions it
was verified on (verified_on, required).

GUI patches keep their stock text and replacement in find.nut and
replace.nut next to patch.jsonc instead of escaped JSON strings, so the
script edits are readable, diffable and can carry Quirrel comments.
Matching treats CRLF and LF as the same line break because stock
scripts mix both; only the matched span is rewritten.

Loaders on both sides (packages/build, packages/exepatch, wtotools)
reject unknown keys so analysis text cannot creep back into the data.
JSONC parsing moves into a shared @wto/jsonc package; the build now
parses tuning/store.jsonc instead of scanning it with a regex.

Verified: npm run typecheck; check-patches (10/10 ready on 2.61.0.37);
check-gui (33 patches, 24 scripts compile); the TypeScript and Python
appliers produce identical scripts, byte-identical to the previous
definitions except one stock script with mixed line endings.

The 15 battle patches are migrated mechanically and still carry the
pending-review comment; they are rewritten by the LocalMP review.

BREAKING CHANGE: Patch loaders require JSONC definitions. Migrate patch.json and locate.json and move embedded GUI edits to separate Quirrel files.
This commit is contained in:
N0VAN0M4LY committed 2026-11-28 03:24:12 +08:00
1 parent b260d2899e
commit db981ab834
185 files changed
+2676 -1487

No files matched your search

+31 -10
View File
@@ -2,18 +2,24 @@
* Patch definitions as stored on disk, one directory per change to the program's behaviour,
* under exe-patches/<domain>/<name>/:
*
* patch.json what the program does, what it does after the patch, why the executable
* has to change, which features depend on it, and the bytes to write
* locate.json how to find the site
* 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
* service cannot provide it
* locate.jsonc how to find the site
*
* This code reads only what applying needs; the explanatory fields are for people.
* Unknown keys are rejected so that explanations stay in comments and in the patch notes
* instead of growing into data fields.
*/
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { parseJsonc } from '@wto/jsonc';
import { DefinitionError } from './errors.ts';
const PATCH_FILE = 'patch.json';
const LOCATE_FILE = 'locate.json';
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']);
export interface PatchEdit {
/** Distance from the located site to the first byte written. */
@@ -32,6 +38,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 directory: string;
@@ -57,16 +64,22 @@ function findDefinitionDirectories(root: string): string[] {
}
function readDefinition(directory: string): PatchDefinition {
const patch = readJson(join(directory, PATCH_FILE));
const locate = readJson(join(directory, LOCATE_FILE));
const where = join(directory, PATCH_FILE);
const locateWhere = join(directory, LOCATE_FILE);
const patch = readJson(where);
const locate = readJson(locateWhere);
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(`${join(directory, LOCATE_FILE)}: needs a string "method"`);
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,
directory,
@@ -76,6 +89,7 @@ function readDefinition(directory: string): PatchDefinition {
function readEdit(raw: unknown, where: string): PatchEdit {
if (!raw || typeof raw !== 'object') throw new DefinitionError(`${where}: "edit" 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) {
@@ -100,7 +114,7 @@ function stringList(value: unknown, where: string, key: string): string[] {
function readJson(path: string): Record<string, unknown> {
let data: unknown;
try {
data = JSON.parse(readFileSync(path, 'utf-8'));
data = parseJsonc(readFileSync(path, 'utf-8'));
} catch (error) {
throw new DefinitionError(`${path}: ${(error as Error).message}`);
}
@@ -108,6 +122,13 @@ function readJson(path: string): Record<string, unknown> {
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`);
}
}
function isFile(path: string): boolean {
try {
return statSync(path).isFile();