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.
141 lines
5.7 KiB
TypeScript
141 lines
5.7 KiB
TypeScript
/**
|
||
* GUI script patch definitions as stored on disk, one folder per change under
|
||
* `gui-patches/<domain>/<name>/`:
|
||
*
|
||
* patch.jsonc id, title, target script, features, the game versions it was verified on,
|
||
* and the list of edits; comments explain what the change does and why the
|
||
* local service cannot provide it
|
||
* <name>.nut the edit fragments named by `edits` (stock text to find, text to put there)
|
||
*
|
||
* Fragment files hold the exact script text followed by one terminating newline, which is not
|
||
* part of the fragment. Line endings are normalised to `\n` here; applying matches regardless of
|
||
* the script's line endings (stock scripts mix `\r\n` and `\n`), so fragments survive editors
|
||
* and git newline conversion.
|
||
*/
|
||
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
||
import { join } from 'node:path';
|
||
import { parseJsonc } from '@wto/jsonc';
|
||
|
||
const PATCH_FILE = 'patch.jsonc';
|
||
const ALLOWED_KEYS = new Set(['id', 'title', 'file', 'features', 'verified_on', 'edits']);
|
||
|
||
export interface GuiScriptEdit {
|
||
/** Stock text that must occur exactly once in the target script, with `\n` line endings. */
|
||
readonly find: string;
|
||
/** Text written in its place, with `\n` line endings. */
|
||
readonly replace: string;
|
||
}
|
||
|
||
export interface GuiScriptPatch {
|
||
readonly id: string;
|
||
readonly title: string;
|
||
/** Script path inside gui.vromfs.bin. */
|
||
readonly file: string;
|
||
readonly features: readonly string[];
|
||
readonly verifiedOn: readonly string[];
|
||
readonly edits: readonly GuiScriptEdit[];
|
||
readonly directory: string;
|
||
}
|
||
|
||
/** Every definition under `root`, sorted by id. */
|
||
export function loadGuiPatches(root: string): readonly GuiScriptPatch[] {
|
||
const patches = findDefinitionDirectories(root).map(readDefinition);
|
||
rejectDuplicateIds(patches);
|
||
return patches.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): GuiScriptPatch {
|
||
const where = join(directory, PATCH_FILE);
|
||
const raw = readObject(where);
|
||
const unknown = Object.keys(raw).filter((key) => !ALLOWED_KEYS.has(key));
|
||
if (unknown.length) {
|
||
throw new Error(`${where}: unknown key(s) ${unknown.join(', ')}; explanations belong in comments or the patch notes`);
|
||
}
|
||
return {
|
||
id: requiredString(raw.id, where, 'id'),
|
||
title: requiredString(raw.title, where, 'title'),
|
||
file: requiredString(raw.file, where, 'file'),
|
||
features: stringList(raw.features, where, 'features'),
|
||
verifiedOn: nonEmptyStringList(raw.verified_on, where, 'verified_on'),
|
||
edits: readEdits(raw.edits, directory, where),
|
||
directory,
|
||
};
|
||
}
|
||
|
||
function readEdits(value: unknown, directory: string, where: string): GuiScriptEdit[] {
|
||
if (!Array.isArray(value) || value.length === 0) throw new Error(`${where}: "edits" must be a non-empty list`);
|
||
return value.map((item, index) => {
|
||
if (!item || typeof item !== 'object') throw new Error(`${where}: edits[${index}] must be an object`);
|
||
const edit = item as Record<string, unknown>;
|
||
const find = readFragment(directory, requiredString(edit.find, where, `edits[${index}].find`));
|
||
if (find === '') throw new Error(`${where}: edits[${index}].find is empty`);
|
||
const replace = readFragment(directory, requiredString(edit.replace, where, `edits[${index}].replace`));
|
||
return { find, replace };
|
||
});
|
||
}
|
||
|
||
/** Fragment text with `\n` line endings and the file's terminating newline removed. */
|
||
export function readFragment(directory: string, name: string): string {
|
||
if (name.includes('/') || name.includes('\\')) throw new Error(`${directory}: fragment "${name}" must be a file in the patch folder`);
|
||
const text = readFileSync(join(directory, name), 'utf8').replace(/^/, '').replace(/\r\n/g, '\n');
|
||
return text.endsWith('\n') ? text.slice(0, -1) : text;
|
||
}
|
||
|
||
function readObject(path: string): Record<string, unknown> {
|
||
let data: unknown;
|
||
try {
|
||
data = parseJsonc(readFileSync(path, 'utf8'));
|
||
} catch (error) {
|
||
throw new Error(`${path}: ${(error as Error).message}`);
|
||
}
|
||
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error(`${path}: expected an object`);
|
||
return data as Record<string, unknown>;
|
||
}
|
||
|
||
function requiredString(value: unknown, where: string, key: string): string {
|
||
if (typeof value !== 'string' || value === '') throw new Error(`${where}: "${key}" must be a non-empty string`);
|
||
return value;
|
||
}
|
||
|
||
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 Error(`${where}: "${key}" must be a list of strings`);
|
||
}
|
||
return value;
|
||
}
|
||
|
||
function nonEmptyStringList(value: unknown, where: string, key: string): string[] {
|
||
const list = stringList(value, where, key);
|
||
if (list.length === 0) throw new Error(`${where}: "${key}" must name at least one game version`);
|
||
return list;
|
||
}
|
||
|
||
function rejectDuplicateIds(patches: readonly GuiScriptPatch[]): void {
|
||
const seen = new Map<string, string>();
|
||
for (const patch of patches) {
|
||
const previous = seen.get(patch.id);
|
||
if (previous) throw new Error(`two GUI patches use id "${patch.id}": ${previous} and ${patch.directory}`);
|
||
seen.set(patch.id, patch.directory);
|
||
}
|
||
}
|
||
|
||
function isFile(path: string): boolean {
|
||
try {
|
||
return statSync(path).isFile();
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|