Store patch definitions as commented JSONC with separate 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.
This commit is contained in:
N0VAN0M4LY committed 2026-11-28 03:24:12 +08:00
1 parent 6a189513b0
commit 16b5d3cab1
185 files changed
+2676 -1487

No files matched your search

+31 -19
View File
@@ -2,10 +2,12 @@
One directory per change to the program's behaviour, under exe-patches/<domain>/<name>/:
patch.json what the program does now, what it does after the patch, why the
executable has to change at all, which features depend on it, and the
bytes to write
locate.json how to find the site, and how that was established
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
Unknown keys are rejected so that explanations stay in comments and in the patch notes.
The engine reads these; it holds no knowledge about any particular patch. Keeping the
definitions as data is what lets the installer report exactly which changes did not apply
@@ -13,14 +15,17 @@ and which features are affected.
"""
from __future__ import annotations
import json
from dataclasses import dataclass, field
from dataclasses import dataclass
from pathlib import Path
from .. import jsonc
from .errors import DefinitionError
PATCH_FILE = "patch.json"
LOCATE_FILE = "locate.json"
PATCH_FILE = "patch.jsonc"
LOCATE_FILE = "locate.jsonc"
PATCH_KEYS = frozenset({"id", "title", "features", "requires", "verified_on", "edit"})
EDIT_KEYS = frozenset({"offset", "asm", "data", "bytes", "expect_original"})
LOCATE_KEYS = frozenset({"method", "section", "pattern"})
@dataclass(frozen=True)
@@ -47,10 +52,10 @@ class PatchDefinition:
title: str
features: tuple[str, ...]
requires: tuple[str, ...]
verified_on: tuple[str, ...]
edit: PatchEdit
locate: dict
directory: Path | None = None
notes: dict = field(default_factory=dict)
@property
def locate_method(self) -> str:
@@ -68,27 +73,39 @@ def load_patches(root: Path) -> list[PatchDefinition]:
def _read(directory: Path) -> PatchDefinition:
patch = _read_json(directory / PATCH_FILE)
locate = _read_json(directory / LOCATE_FILE)
_reject_unknown_keys(patch, PATCH_KEYS, directory / PATCH_FILE)
_reject_unknown_keys(locate, LOCATE_KEYS, directory / LOCATE_FILE)
for key in ("id", "title", "edit"):
if key not in patch:
raise DefinitionError(f"{directory / PATCH_FILE}: missing key {key!r}")
if "method" not in locate:
raise DefinitionError(f"{directory / LOCATE_FILE}: missing key 'method'")
verified_on = tuple(str(item) for item in patch.get("verified_on", ()))
if not verified_on:
raise DefinitionError(f"{directory / PATCH_FILE}: 'verified_on' must name at least one game version")
return PatchDefinition(
identifier=str(patch["id"]),
title=str(patch["title"]),
features=tuple(str(item) for item in patch.get("features", ())),
requires=tuple(str(item) for item in patch.get("requires", ())),
verified_on=verified_on,
edit=_read_edit(directory, patch["edit"]),
locate=locate,
directory=directory,
notes={key: value for key, value in patch.items()
if key not in ("id", "title", "features", "requires", "edit")},
)
def _reject_unknown_keys(data: dict, allowed: frozenset[str], where: Path) -> None:
unknown = sorted(set(data) - allowed)
if unknown:
raise DefinitionError(f"{where}: unknown key(s) {', '.join(unknown)}; "
"explanations belong in comments or the patch notes")
def _read_edit(directory: Path, raw: object) -> PatchEdit:
if not isinstance(raw, dict):
raise DefinitionError(f"{directory / PATCH_FILE}: 'edit' must be an object")
_reject_unknown_keys(raw, EDIT_KEYS, directory / PATCH_FILE)
for key in ("bytes", "expect_original"):
if not isinstance(raw.get(key), str):
raise DefinitionError(f"{directory / PATCH_FILE}: edit needs a hex string {key!r}")
@@ -109,15 +126,10 @@ def _hex(directory: Path, key: str, text: str) -> bytes:
def _read_json(path: Path) -> dict:
if not path.is_file():
raise DefinitionError(f"{path}: file not found")
try:
data = json.loads(path.read_text("utf-8"))
except json.JSONDecodeError as error:
raise DefinitionError(f"{path}: invalid JSON: {error}") from error
if not isinstance(data, dict):
raise DefinitionError(f"{path}: expected a JSON object")
return data
return jsonc.load_object(path)
except ValueError as error:
raise DefinitionError(str(error)) from error
def _reject_duplicate_ids(definitions: list[PatchDefinition]) -> None: