Files
WT-Offline/packages/jsonc/src/jsonc.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

45 lines
1.4 KiB
TypeScript

/**
* Parse JSON with comments (// line comments and block comments), the format of the tuning
* files and of the patch definitions. Comments are blanked out, outside string literals only;
* the rest goes to JSON.parse, so the error positions still match the file.
*/
export function parseJsonc(text: string): unknown {
return JSON.parse(stripComments(text));
}
function stripComments(text: string): string {
let out = '';
let index = 0;
while (index < text.length) {
const ch = text[index]!;
const next = text[index + 1];
if (ch === '"') {
const end = stringEnd(text, index);
out += text.slice(index, end);
index = end;
} else if (ch === '/' && next === '/') {
while (index < text.length && text[index] !== '\n') index++;
} else if (ch === '/' && next === '*') {
const end = text.indexOf('*/', index + 2);
const stop = end < 0 ? text.length : end + 2;
out += text.slice(index, stop).replace(/[^\n]/g, ' ');
index = stop;
} else {
out += ch;
index++;
}
}
return out;
}
/** Index just past the closing quote of the string literal starting at `start`. */
function stringEnd(text: string, start: number): number {
let index = start + 1;
while (index < text.length) {
if (text[index] === '\\') index += 2;
else if (text[index] === '"') return index + 1;
else index++;
}
return text.length;
}