Files
WT-Offline/packages/exepatch/src/json-fields.ts
T
N0VAN0M4LY 927201a462 feat(patches)!: support game-version variants in patch definitions
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.

BREAKING CHANGE: Each patch needs a variants list. Move executable locators into patch.jsonc and regenerate game version records with record-game-version.ts.
2026-11-29 11:47:08 +08:00

51 lines
2.4 KiB
TypeScript

/**
* Reading definition files: JSONC objects whose unknown keys are refused, so explanations stay
* in comments and in the patch notes instead of growing into data fields.
*/
import { readFileSync } from 'node:fs';
import { parseJsonc } from '@wto/jsonc';
import { DefinitionError } from './errors.ts';
export function readJsonObject(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}`);
}
return asObject(data, path);
}
export function asObject(value: unknown, where: string): Record<string, unknown> {
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new DefinitionError(`${where}: expected an object`);
return value as Record<string, unknown>;
}
export 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`);
}
}
export function requiredString(value: unknown, where: string): string {
if (typeof value !== 'string' || value === '') throw new DefinitionError(`${where} must be a non-empty string`);
return value;
}
export function stringList(value: unknown, where: string): string[] {
if (value === undefined) return [];
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) throw new DefinitionError(`${where} must be a list of strings`);
return value;
}
/** The "variants" list of a definition, each entry an object, and their written_for names unique. */
export function variantObjects(value: unknown, where: string): Record<string, unknown>[] {
if (!Array.isArray(value) || value.length === 0) throw new DefinitionError(`${where}: "variants" must be a non-empty list`);
const variants = value.map((item, index) => asObject(item, `${where} variants[${index}]`));
const names = variants.map((variant, index) => requiredString(variant.written_for, `${where} variants[${index}].written_for`));
const duplicate = names.find((name, index) => names.indexOf(name) !== index);
if (duplicate) throw new DefinitionError(`${where}: two variants are written for ${duplicate}`);
return variants;
}