/** * GUI script patch definitions as stored on disk, one folder per change under * `patches/gui///`: * * patch.jsonc id, title, features, and the variants; comments explain what the change does * and why the local server cannot provide it * .nut the edit fragments a variant names (stock text to find, text to put there) * * A variant is the change written for one game build: the target script and its edits. Most * patches have one; when an update changes the stock text, a variant with its own fragment files * is added, and exactly one variant must apply (the same rule as exe patches, @wto/exepatch). * * 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 { DefinitionError, readJsonObject, rejectUnknownKeys, requiredString, stringList, variantObjects } from '@wto/exepatch'; const PATCH_FILE = 'patch.jsonc'; const PATCH_KEYS = new Set(['id', 'title', 'features', 'variants']); const VARIANT_KEYS = new Set(['written_for', 'file', 'edits']); const EDIT_KEYS = new Set(['find', 'replace']); 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 GuiScriptVariant { /** The game version this variant was written against; names the variant. */ readonly writtenFor: string; /** Script path inside gui.vromfs.bin. */ readonly file: string; readonly edits: readonly GuiScriptEdit[]; } export interface GuiScriptPatch { readonly id: string; readonly title: string; readonly features: readonly string[]; readonly variants: readonly GuiScriptVariant[]; 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 = readJsonObject(where); rejectUnknownKeys(raw, PATCH_KEYS, where); return { id: requiredString(raw.id, `${where} id`), title: requiredString(raw.title, `${where} title`), features: stringList(raw.features, `${where} features`), variants: variantObjects(raw.variants, where).map((variant) => readVariant(variant, directory, `${where} variant ${String(variant.written_for)}`)), directory, }; } function readVariant(variant: Record, directory: string, where: string): GuiScriptVariant { rejectUnknownKeys(variant, VARIANT_KEYS, where); return { writtenFor: variant.written_for as string, file: requiredString(variant.file, `${where} file`), edits: readEdits(variant.edits, directory, where), }; } function readEdits(value: unknown, directory: string, where: string): GuiScriptEdit[] { if (!Array.isArray(value) || value.length === 0) throw new DefinitionError(`${where}: "edits" must be a non-empty list`); return value.map((item, index) => { const label = `${where} edits[${index}]`; if (!item || typeof item !== 'object') throw new DefinitionError(`${label} must be an object`); const edit = item as Record; rejectUnknownKeys(edit, EDIT_KEYS, label); const find = readFragment(directory, requiredString(edit.find, `${label}.find`)); if (find === '') throw new DefinitionError(`${label}.find is empty`); return { find, replace: readFragment(directory, requiredString(edit.replace, `${label}.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 DefinitionError(`${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 rejectDuplicateIds(patches: readonly GuiScriptPatch[]): void { const seen = new Map(); for (const patch of patches) { const previous = seen.get(patch.id); if (previous) throw new DefinitionError(`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; } }