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.
This commit is contained in:
1 parent
6a189513b0
commit
16b5d3cab1
185 files changed
+2676
-1487
No files matched your search
@@ -1,47 +1,23 @@
|
||||
/**
|
||||
* Rebuild gui.vromfs.bin by applying declarative script modification records from `gui-patches/`
|
||||
*.
|
||||
* Rebuild gui.vromfs.bin by applying the script patch definitions in `gui-patches/`
|
||||
* (implementation notes, format in gui-patch-definition.ts).
|
||||
*
|
||||
* Just like `exe-patches/`, every GUI script modification lives in its own folder under
|
||||
* `gui-patches/` with a `patch.json` documenting the requirement, why the local service cannot
|
||||
* handle it alone, lighter options ruled out, and the exact unique text to replace.
|
||||
* 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 { readdirSync, readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
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 GuiScriptPatch {
|
||||
readonly id: string;
|
||||
readonly title: string;
|
||||
readonly file: string;
|
||||
readonly features: readonly string[];
|
||||
readonly edit: {
|
||||
readonly find: string;
|
||||
readonly replace: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuiPackResult {
|
||||
readonly pack: Uint8Array;
|
||||
readonly applied: readonly GuiScriptPatch[];
|
||||
}
|
||||
|
||||
export function loadGuiPatches(patchesFolder: string = DEFAULT_GUI_PATCHES_DIR): readonly GuiScriptPatch[] {
|
||||
const files = collectPatchJsonPaths(patchesFolder).sort();
|
||||
return files.map((filePath) => {
|
||||
const raw = JSON.parse(readFileSync(filePath, 'utf8')) as GuiScriptPatch;
|
||||
if (!raw.id || !raw.file || !raw.edit?.find || typeof raw.edit.replace !== 'string') {
|
||||
throw new Error(`${filePath}: invalid GUI patch record`);
|
||||
}
|
||||
return raw;
|
||||
});
|
||||
}
|
||||
|
||||
export function rebuildGuiPack(
|
||||
stockPack: Uint8Array,
|
||||
endpoint: LocalEndpoint,
|
||||
@@ -52,17 +28,13 @@ export function rebuildGuiPack(
|
||||
const hostPortText = `${endpoint.host}:${endpoint.port}`;
|
||||
|
||||
for (const patch of patches) {
|
||||
const current = Buffer.from(getFile(archive, patch.file)).toString('utf8');
|
||||
const firstIndex = current.indexOf(patch.edit.find);
|
||||
if (firstIndex === -1) {
|
||||
throw new Error(`GUI patch ${patch.id}: target snippet not found in ${patch.file}; refusing to write`);
|
||||
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}`);
|
||||
}
|
||||
if (current.indexOf(patch.edit.find, firstIndex + patch.edit.find.length) !== -1) {
|
||||
throw new Error(`GUI patch ${patch.id}: target snippet appears more than once in ${patch.file}; refusing to write`);
|
||||
}
|
||||
const replacement = patch.edit.replace.replaceAll(ENDPOINT_PLACEHOLDER, hostPortText);
|
||||
const updated = current.slice(0, firstIndex) + replacement + current.slice(firstIndex + patch.edit.find.length);
|
||||
putFile(archive, patch.file, Buffer.from(updated, 'utf8'));
|
||||
putFile(archive, patch.file, Buffer.from(current, 'utf8'));
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -71,15 +43,37 @@ export function rebuildGuiPack(
|
||||
};
|
||||
}
|
||||
|
||||
function collectPatchJsonPaths(dir: string): string[] {
|
||||
const out: string[] = [];
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
const full = join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
out.push(...collectPatchJsonPaths(full));
|
||||
} else if (entry.isFile() && entry.name === 'patch.json') {
|
||||
out.push(full);
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
original.push(text.length);
|
||||
return { normalized: kept.join(''), original };
|
||||
}
|
||||
Reference in new issue
Block a user