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

+16
View File
@@ -0,0 +1,16 @@
/** Typed content of tuning/limits.jsonc: limits the offline version sets where the original server would. */
import type { TuningReader } from './tuning-reader.ts';
export interface LimitsTuning {
readonly itemPiecesPerPurchase: number;
readonly userLogsKept: number;
readonly battleSessionsKept: number;
}
export function readLimitsTuning(file: TuningReader): LimitsTuning {
return {
itemPiecesPerPurchase: file.int('itemPiecesPerPurchase', { min: 1 }),
userLogsKept: file.int('userLogsKept', { min: 1 }),
battleSessionsKept: file.int('battleSessionsKept', { min: 1 }),
};
}
+51
View File
@@ -0,0 +1,51 @@
/**
* Load one tuning file: the defaults shipped in
* apps/server/tuning/<name>.jsonc, with <data folder>/tuning/<name>.jsonc laid
* over them field by field when the player or a developer put one there. Objects merge key
* by key; any other value (numbers, texts, lists) in the override replaces the default.
*/
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseJsonc } from '@wto/jsonc';
import { TuningError, TuningReader } from './tuning-reader.ts';
const DEFAULTS_FOLDER = fileURLToPath(new URL('../../tuning/', import.meta.url));
export interface TuningFiles {
/** The merged content of <name>.jsonc, ready for typed reads. */
read(name: string): TuningReader;
}
export function tuningFiles(dataFolder: string, report: (line: string) => void): TuningFiles {
const overridesFolder = join(dataFolder, 'tuning');
return {
read(name: string): TuningReader {
const fileName = `${name}.jsonc`;
const defaults = parseFile(join(DEFAULTS_FOLDER, fileName));
const overridePath = join(overridesFolder, fileName);
if (!existsSync(overridePath)) return new TuningReader(fileName, defaults);
report(`tuning ${fileName}: using overrides from ${overridePath}`);
return new TuningReader(fileName, mergeOverride(defaults, parseFile(overridePath)));
},
};
}
function parseFile(path: string): unknown {
try {
return parseJsonc(readFileSync(path, 'utf8'));
} catch (error) {
throw new TuningError(`tuning file ${path} cannot be read: ${error instanceof Error ? error.message : String(error)}`);
}
}
function mergeOverride(base: unknown, override: unknown): unknown {
if (!isObject(base) || !isObject(override)) return override;
const merged: Record<string, unknown> = { ...base };
for (const [key, value] of Object.entries(override)) merged[key] = key in base ? mergeOverride(base[key], value) : value;
return merged;
}
function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
+120
View File
@@ -0,0 +1,120 @@
/**
* Typed, validating reads of one tuning file. Every read names the field
* it wants; a missing field or a value of the wrong type or range stops start-up with the
* file, the field path and what was expected, so a typo in a tuning file never turns into a
* silent default.
*/
export class TuningError extends Error {}
export interface IntRange {
readonly min?: number;
readonly max?: number;
}
export class TuningReader {
readonly #file: string;
readonly #path: string;
readonly #value: Readonly<Record<string, unknown>>;
constructor(file: string, value: unknown, path = '') {
this.#file = file;
this.#path = path;
if (!isPlainObject(value)) throw this.#error(path || '(root)', 'an object', value);
this.#value = value;
}
has(key: string): boolean {
return this.#value[key] !== undefined;
}
keys(): string[] {
return Object.keys(this.#value);
}
int(key: string, range: IntRange = {}): number {
const value = this.#field(key);
if (typeof value !== 'number' || !Number.isSafeInteger(value)) throw this.#error(key, 'a whole number', value);
return this.#inRange(key, value, range);
}
/** A number with a fractional part allowed (weights, multipliers). */
number(key: string, range: IntRange = {}): number {
const value = this.#field(key);
if (typeof value !== 'number' || !Number.isFinite(value)) throw this.#error(key, 'a number', value);
return this.#inRange(key, value, range);
}
/** A GJN amount with at most two decimals (12.99), returned in hundredths (1299). */
money(key: string, range: IntRange = {}): number {
const value = this.number(key, range);
const cents = Math.round(value * 100);
if (Math.abs(cents - value * 100) > 1e-6) throw this.#error(key, 'an amount with at most two decimals', value);
return cents;
}
string(key: string): string {
const value = this.#field(key);
if (typeof value !== 'string' || value === '') throw this.#error(key, 'a non-empty text', value);
return value;
}
bool(key: string): boolean {
const value = this.#field(key);
if (typeof value !== 'boolean') throw this.#error(key, 'true or false', value);
return value;
}
stringList(key: string): string[] {
const value = this.#field(key);
if (!Array.isArray(value) || value.some((entry) => typeof entry !== 'string' || entry === '')) throw this.#error(key, 'a list of texts', value);
return value as string[];
}
intList(key: string, range: IntRange = {}): number[] {
const value = this.#field(key);
if (!Array.isArray(value) || value.some((entry) => typeof entry !== 'number' || !Number.isSafeInteger(entry))) throw this.#error(key, 'a list of whole numbers', value);
return (value as number[]).map((entry) => this.#inRange(key, entry, range));
}
object(key: string): TuningReader {
return new TuningReader(this.#file, this.#field(key), this.#join(key));
}
/** A list of objects; `read` turns each into a typed value. */
list<T>(key: string, read: (entry: TuningReader) => T): T[] {
const value = this.#field(key);
if (!Array.isArray(value)) throw this.#error(key, 'a list', value);
return value.map((entry, index) => read(new TuningReader(this.#file, entry, `${this.#join(key)}[${index}]`)));
}
/** An object used as a table: every key maps to an entry `read` turns into a typed value. */
table<T>(key: string, read: (entry: TuningReader, name: string) => T): Map<string, T> {
const table = this.object(key);
return new Map(table.keys().map((name) => [name, read(table.object(name), name)]));
}
#field(key: string): unknown {
const value = this.#value[key];
if (value === undefined) throw this.#error(key, 'a value', undefined);
return value;
}
#inRange(key: string, value: number, { min, max }: IntRange): number {
if (min !== undefined && value < min) throw this.#error(key, `at least ${min}`, value);
if (max !== undefined && value > max) throw this.#error(key, `at most ${max}`, value);
return value;
}
#join(key: string): string {
return this.#path ? `${this.#path}.${key}` : key;
}
#error(key: string, expected: string, got: unknown): TuningError {
const field = key === this.#path || key === '(root)' ? key : this.#join(key);
return new TuningError(`tuning file ${this.#file}: ${field} must be ${expected} (found ${JSON.stringify(got) ?? 'nothing'})`);
}
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
+52
View File
@@ -0,0 +1,52 @@
/**
* All tuning values of the offline version, read once at start-up (implementation notes; user
* quote 119): values the offline version decides itself (prices, rewards, drop weights,
* limits, preset content). Values the game data already has are read from the game files
* instead, and protocol constants stay in the code next to their source.
*/
import { readBattleEconomyTuning, type BattleEconomyTuning } from '../battle/battle-economy-tuning.ts';
import { readBattleTuning, type BattleTuning } from '../battle/battle-tuning.ts';
import { readClanTuning, type ClanTuning } from '../clans/clan-tuning.ts';
import { readMarketplaceTuning, type MarketplaceTuning } from '../inventory/marketplace-tuning.ts';
import { readRecyclingTuning, type RecyclingTuning } from '../inventory/recycling-tuning.ts';
import { readSupplyChestTuning, type SupplyChestTuning } from '../inventory/supply-chest-tuning.ts';
import { readTrophyTuning, type TrophyTuning } from '../inventory/trophy-tuning.ts';
import { readNewProfileTuning, type NewProfileTuning } from '../profile/new-profile-tuning.ts';
import { readResearchTuning, type ResearchTuning } from '../research/research-tuning.ts';
import { readRedeemTuning, type RedeemTuning } from '../store/redeem-tuning.ts';
import { readStoreTuning, type StoreTuning } from '../store/store-tuning.ts';
import { readLimitsTuning, type LimitsTuning } from './limits-tuning.ts';
import { tuningFiles } from './tuning-loader.ts';
export interface Tuning {
readonly store: StoreTuning;
readonly redeemCodes: RedeemTuning;
readonly newProfile: NewProfileTuning;
readonly research: ResearchTuning;
readonly marketplace: MarketplaceTuning;
readonly supplyChests: SupplyChestTuning;
readonly recycling: RecyclingTuning;
readonly trophies: TrophyTuning;
readonly clans: ClanTuning;
readonly battle: BattleTuning;
readonly battleEconomy: BattleEconomyTuning;
readonly limits: LimitsTuning;
}
export function loadTuning(dataFolder: string, report: (line: string) => void): Tuning {
const files = tuningFiles(dataFolder, report);
return {
store: readStoreTuning(files.read('store')),
redeemCodes: readRedeemTuning(files.read('redeem-codes')),
newProfile: readNewProfileTuning(files.read('new-profile')),
research: readResearchTuning(files.read('research')),
marketplace: readMarketplaceTuning(files.read('marketplace')),
supplyChests: readSupplyChestTuning(files.read('supply-chests')),
recycling: readRecyclingTuning(files.read('recycling')),
trophies: readTrophyTuning(files.read('trophies')),
clans: readClanTuning(files.read('clans')),
battle: readBattleTuning(files.read('battle')),
battleEconomy: readBattleEconomyTuning(files.read('battle-economy')),
limits: readLimitsTuning(files.read('limits')),
};
}