/** * GUI script patch definitions as stored on disk, one folder per change under * `gui-patches///`: * * 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 * .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; 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 { 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; } 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(); 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; } }