/** * Rebuild gui.vromfs.bin by applying the script patch definitions in the GUI patch folder * (format in gui-patch-definition.ts). * * A variant applies when every one of its edits finds its stock text exactly once in the current * script; exactly one variant of a patch must apply. A patch with no applicable variant is left * out whole, never half applied, and listed with the features it covers, the same way an * unavailable exe patch is; the other patches are applied. */ import { chooseVariant, PatchError, type PatchFailure } from '@wto/exepatch'; import { getFile, putFile, readArchive, writeArchive, type Archive } from '@wto/vromfs'; import { loadGuiPatches, type GuiScriptPatch, type GuiScriptVariant } from './gui-patch-definition.ts'; import type { LocalEndpoint } from './network/local-endpoint.ts'; const ENDPOINT_PLACEHOLDER = '{{LOCAL_ENDPOINT}}'; export interface AppliedGuiPatch { readonly patch: GuiScriptPatch; readonly variant: GuiScriptVariant; } export interface GuiPackResult { /** The data pack with every applicable patch written. */ readonly pack: Uint8Array; readonly applied: readonly AppliedGuiPatch[]; readonly failures: readonly PatchFailure[]; } /** The stock text of an edit was not found exactly once, or the target script is missing. */ class GuiPatchMismatch extends PatchError { override name = 'GuiPatchMismatch'; } export function rebuildGuiPack( stockPack: Uint8Array, endpoint: LocalEndpoint, patchesFolder: string, ): GuiPackResult { const patches = loadGuiPatches(patchesFolder); const archive = readArchive(stockPack); const hostPortText = `${endpoint.host}:${endpoint.port}`; const applied: AppliedGuiPatch[] = []; const failures: PatchFailure[] = []; for (const patch of patches) { try { const chosen = chooseVariant(patch.variants, (variant) => patchedScript(archive, variant, hostPortText)); putFile(archive, chosen.variant.file, Buffer.from(chosen.value, 'utf8')); applied.push({ patch, variant: chosen.variant }); } catch (error) { if (!(error instanceof PatchError)) throw error; failures.push({ patchId: patch.id, features: patch.features, reason: error.message }); } } return { pack: writeArchive(archive), applied, failures }; } /** The target script with every edit of `variant` made; throws GuiPatchMismatch before any change. */ function patchedScript(archive: Archive, variant: GuiScriptVariant, hostPortText: string): string { if (!archive.files.has(variant.file)) throw new GuiPatchMismatch(`the data pack has no script ${variant.file}`); let current = Buffer.from(getFile(archive, variant.file)).toString('utf8'); const newline = current.includes('\r\n') ? '\r\n' : '\n'; for (const [index, edit] of variant.edits.entries()) { const replace = edit.replace.replaceAll('\n', newline).replaceAll(ENDPOINT_PLACEHOLDER, hostPortText); current = replaceUnique(current, edit.find, replace, `edit ${index + 1} in ${variant.file}`); } return current; } /** * 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 GuiPatchMismatch(`${label}: stock text not found`); if (normalized.indexOf(find, first + find.length) !== -1) throw new GuiPatchMismatch(`${label}: stock text appears more than once`); 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 }; }