refactor(repo)!: move apps, shared packages, and patch definitions

Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:

  apps/server        the local server (was packages/local-service),
                     package @wto/server
  packages/patcher   builds the game copy (was packages/build),
                     package @wto/patcher
  patches/exe        exe patch definitions (was exe-patches)
  patches/gui        GUI script patches (was gui-patches)

Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".

The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.

The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.


Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.

BREAKING CHANGE: Use @wto/server and @wto/patcher from the new workspace paths. Run the development build with scripts/build-game-copy.ts and pass --data.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 03:20:54 +08:00
1 parent 9c813ccf7d
commit 930ea07630
476 files changed
+275 -251

No files matched your search

@@ -0,0 +1,140 @@
/**
* GUI script patch definitions as stored on disk, one folder per change under
* `patches/gui/<domain>/<name>/`:
*
* patch.jsonc id, title, target script, features, the game versions it was verified on,
* and the list of edits; comments explain what the change does and why the
* local server cannot provide it
* <name>.nut the edit fragments named by `edits` (stock text to find, text to put there)
*
* Fragment files hold the exact script text followed by one terminating newline, which is not
* part of the fragment. Line endings are normalised to `\n` here; applying matches regardless of
* the script's line endings (stock scripts mix `\r\n` and `\n`), so fragments survive editors
* and git newline conversion.
*/
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { parseJsonc } from '@wto/jsonc';
const PATCH_FILE = 'patch.jsonc';
const ALLOWED_KEYS = new Set(['id', 'title', 'file', 'features', 'verified_on', 'edits']);
export interface GuiScriptEdit {
/** Stock text that must occur exactly once in the target script, with `\n` line endings. */
readonly find: string;
/** Text written in its place, with `\n` line endings. */
readonly replace: string;
}
export interface GuiScriptPatch {
readonly id: string;
readonly title: string;
/** Script path inside gui.vromfs.bin. */
readonly file: string;
readonly features: readonly string[];
readonly verifiedOn: readonly string[];
readonly edits: readonly GuiScriptEdit[];
readonly directory: string;
}
/** Every definition under `root`, sorted by id. */
export function loadGuiPatches(root: string): readonly GuiScriptPatch[] {
const patches = findDefinitionDirectories(root).map(readDefinition);
rejectDuplicateIds(patches);
return patches.sort((left, right) => left.id.localeCompare(right.id));
}
function findDefinitionDirectories(root: string): string[] {
const found: string[] = [];
for (const entry of readdirSync(root, { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const directory = join(root, entry.name);
if (isFile(join(directory, PATCH_FILE))) found.push(directory);
else found.push(...findDefinitionDirectories(directory));
}
return found;
}
function readDefinition(directory: string): GuiScriptPatch {
const where = join(directory, PATCH_FILE);
const raw = readObject(where);
const unknown = Object.keys(raw).filter((key) => !ALLOWED_KEYS.has(key));
if (unknown.length) {
throw new Error(`${where}: unknown key(s) ${unknown.join(', ')}; explanations belong in comments or the patch notes`);
}
return {
id: requiredString(raw.id, where, 'id'),
title: requiredString(raw.title, where, 'title'),
file: requiredString(raw.file, where, 'file'),
features: stringList(raw.features, where, 'features'),
verifiedOn: nonEmptyStringList(raw.verified_on, where, 'verified_on'),
edits: readEdits(raw.edits, directory, where),
directory,
};
}
function readEdits(value: unknown, directory: string, where: string): GuiScriptEdit[] {
if (!Array.isArray(value) || value.length === 0) throw new Error(`${where}: "edits" must be a non-empty list`);
return value.map((item, index) => {
if (!item || typeof item !== 'object') throw new Error(`${where}: edits[${index}] must be an object`);
const edit = item as Record<string, unknown>;
const find = readFragment(directory, requiredString(edit.find, where, `edits[${index}].find`));
if (find === '') throw new Error(`${where}: edits[${index}].find is empty`);
const replace = readFragment(directory, requiredString(edit.replace, where, `edits[${index}].replace`));
return { find, replace };
});
}
/** Fragment text with `\n` line endings and the file's terminating newline removed. */
export function readFragment(directory: string, name: string): string {
if (name.includes('/') || name.includes('\\')) throw new Error(`${directory}: fragment "${name}" must be a file in the patch folder`);
const text = readFileSync(join(directory, name), 'utf8').replace(/^/, '').replace(/\r\n/g, '\n');
return text.endsWith('\n') ? text.slice(0, -1) : text;
}
function readObject(path: string): Record<string, unknown> {
let data: unknown;
try {
data = parseJsonc(readFileSync(path, 'utf8'));
} catch (error) {
throw new Error(`${path}: ${(error as Error).message}`);
}
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error(`${path}: expected an object`);
return data as Record<string, unknown>;
}
function requiredString(value: unknown, where: string, key: string): string {
if (typeof value !== 'string' || value === '') throw new Error(`${where}: "${key}" must be a non-empty string`);
return value;
}
function stringList(value: unknown, where: string, key: string): string[] {
if (value === undefined) return [];
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) {
throw new Error(`${where}: "${key}" must be a list of strings`);
}
return value;
}
function nonEmptyStringList(value: unknown, where: string, key: string): string[] {
const list = stringList(value, where, key);
if (list.length === 0) throw new Error(`${where}: "${key}" must name at least one game version`);
return list;
}
function rejectDuplicateIds(patches: readonly GuiScriptPatch[]): void {
const seen = new Map<string, string>();
for (const patch of patches) {
const previous = seen.get(patch.id);
if (previous) throw new Error(`two GUI patches use id "${patch.id}": ${previous} and ${patch.directory}`);
seen.set(patch.id, patch.directory);
}
}
function isFile(path: string): boolean {
try {
return statSync(path).isFile();
} catch {
return false;
}
}