Files
WT-Offline/packages/build/src/gui-pack.ts
T
N0VAN0M4LY 16b5d3cab1 Store patch definitions as commented JSONC with separate script fragments
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.
2026-11-28 03:24:12 +08:00

80 lines
3.2 KiB
TypeScript

/**
* 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 };
}