/** * Rebuild gui.vromfs.bin by applying the script patch definitions in `gui-patches/` * (implementation notes, format in gui-patch-definition.ts). * * Every edit must find its stock text exactly once in the current script; anything else * refuses the whole build rather than writing a half-patched archive. */ import { fileURLToPath } from 'node:url'; import { getFile, putFile, readArchive, writeArchive } from '@wto/vromfs'; import { loadGuiPatches, type GuiScriptPatch } from './gui-patch-definition.ts'; import type { LocalEndpoint } from './network/local-endpoint.ts'; const DEFAULT_GUI_PATCHES_DIR = fileURLToPath(new URL('../../../gui-patches', import.meta.url)); const ENDPOINT_PLACEHOLDER = '{{LOCAL_ENDPOINT}}'; export interface GuiPackResult { readonly pack: Uint8Array; readonly applied: readonly GuiScriptPatch[]; } export function rebuildGuiPack( stockPack: Uint8Array, endpoint: LocalEndpoint, patchesFolder: string = DEFAULT_GUI_PATCHES_DIR, ): GuiPackResult { const patches = loadGuiPatches(patchesFolder); const archive = readArchive(stockPack); const hostPortText = `${endpoint.host}:${endpoint.port}`; for (const patch of patches) { let current = Buffer.from(getFile(archive, patch.file)).toString('utf8'); const newline = current.includes('\r\n') ? '\r\n' : '\n'; for (const [index, edit] of patch.edits.entries()) { const replace = edit.replace.replaceAll('\n', newline).replaceAll(ENDPOINT_PLACEHOLDER, hostPortText); current = replaceUnique(current, edit.find, replace, `GUI patch ${patch.id} edit ${index + 1} in ${patch.file}`); } putFile(archive, patch.file, Buffer.from(current, 'utf8')); } return { pack: writeArchive(archive), applied: patches, }; } /** * Replace the single occurrence of `find` (written with `\n` line endings) in `text`, treating * `\r\n` and `\n` in the script as the same line break; stock scripts mix both. Only the matched * span changes, every other byte of the script is kept. */ function replaceUnique(text: string, find: string, replace: string, label: string): string { const { normalized, original } = withoutCarriageReturns(text); const first = normalized.indexOf(find); if (first === -1) throw new Error(`${label}: stock text not found; refusing to write`); if (normalized.indexOf(find, first + find.length) !== -1) throw new Error(`${label}: stock text appears more than once; refusing to write`); return text.slice(0, original[first]!) + replace + text.slice(original[first + find.length]!); } /** * `text` with `\r\n` turned into `\n`, and for every normalized index the index in `text` where * that character's original sequence starts (a `\r\n` maps to its `\r`). */ function withoutCarriageReturns(text: string): { readonly normalized: string; readonly original: readonly number[] } { const kept: string[] = []; const original: number[] = []; let index = 0; while (index < text.length) { original.push(index); if (text[index] === '\r' && text[index + 1] === '\n') { kept.push('\n'); index += 2; } else { kept.push(text[index]!); index += 1; } } original.push(text.length); return { normalized: kept.join(''), original }; }