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

+126
View File
@@ -0,0 +1,126 @@
/**
* Record of everything the game sent to the local server, so that what the client actually
* asks for can be read off instead of guessed (implementation notes, milestone 1 steps 3 and 4).
*
* <data>/logs/<start time>/requests.jsonl one JSON line per HTTP exchange or raw connection
* <data>/logs/<start time>/bodies/ request and response bodies, plus readable text
* (decoded BLK) next to them
*
* Each exchange also prints one line to the console.
*/
import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs';
import type { IncomingHttpHeaders } from 'node:http';
import { join } from 'node:path';
import type { ServiceRequest, ServiceResponse } from '../transport/exchange.ts';
import type { FirstBytesInfo } from './first-bytes.ts';
export interface MatchingLogEntry {
readonly direction: 'in' | 'out';
/** First byte of the message (0x1a request, 0x1b answer, other: notification). */
readonly kind: number;
readonly summary: string;
readonly unhandled: boolean;
readonly text: string;
}
export class TrafficLog {
readonly folder: string;
readonly #linesPath: string;
readonly #bodiesFolder: string;
#sequence = 0;
constructor(dataFolder: string) {
this.folder = join(dataFolder, 'logs', new Date().toISOString().replace(/[:.]/g, '-'));
this.#linesPath = join(this.folder, 'requests.jsonl');
this.#bodiesFolder = join(this.folder, 'bodies');
mkdirSync(this.#bodiesFolder, { recursive: true });
}
exchange(request: ServiceRequest, service: string, response: ServiceResponse, connectionId: number): void {
const id = this.#next();
const notes = response.logNotes ?? {};
const files = {
request: this.#writeBody(id, 'req.bin', notes.redactedRequestBody ?? request.body),
requestText: this.#writeBody(id, 'req.txt', notes.requestText),
response: notes.omitResponseBody ? null : this.#writeBody(id, 'resp.bin', response.body),
responseText: this.#writeBody(id, 'resp.txt', notes.responseText),
};
this.#append({
id, time: new Date().toISOString(), kind: 'http', connection: connectionId,
method: request.method, host: request.originalHost ?? null, path: request.path, query: request.query.toString(),
headers: plainHeaders(request.headers), requestBytes: request.body.length,
service, summary: notes.summary ?? null, unhandled: notes.unhandled ?? false,
status: response.status, responseBytes: byteLength(response.body), files,
});
const flag = notes.unhandled ? ' [UNHANDLED]' : '';
console.log(`#${id} ${request.method} ${request.originalHost ?? '(no host)'}${request.path} -> ${service}` +
`${notes.summary ? ` ${notes.summary}` : ''} = ${response.status}${flag}`);
}
connectionOpened(connectionId: number, remotePort: number | undefined): void {
this.#append({ id: this.#next(), time: new Date().toISOString(), kind: 'connection-open', connection: connectionId, remotePort: remotePort ?? null });
}
connectionClosed(connectionId: number, bytesRead: number, bytesWritten: number): void {
this.#append({ id: this.#next(), time: new Date().toISOString(), kind: 'connection-close', connection: connectionId, bytesRead, bytesWritten });
}
nonHttpConnection(connectionId: number, info: FirstBytesInfo, error: string): void {
const id = this.#next();
const { kind, ...details } = info;
this.#append({ id, time: new Date().toISOString(), kind: `non-http-${kind}`, connection: connectionId, ...details, parserError: error });
console.log(`#${id} non-HTTP connection (${info.kind}${info.serverName ? `, asks for ${info.serverName}` : ''}): ${info.textHead}`);
}
/** One message of the matching protocol; the text is already redacted by the sender. */
matchingMessage(connectionId: number, entry: MatchingLogEntry): void {
const id = this.#next();
const text = this.#writeBody(id, `matching-${entry.direction}.json`, entry.text);
this.#append({ id, time: new Date().toISOString(), kind: 'matching', connection: connectionId, direction: entry.direction,
messageKind: entry.kind, summary: entry.summary, unhandled: entry.unhandled, files: { text } });
const flag = entry.unhandled ? ' [UNHANDLED]' : '';
console.log(`#${id} matching ${entry.direction === 'in' ? '<-' : '->'} ${entry.summary}${flag}`);
}
matchingError(connectionId: number, message: string): void {
const id = this.#next();
this.#append({ id, time: new Date().toISOString(), kind: 'matching-error', connection: connectionId, message });
console.log(`#${id} matching connection ${connectionId} closed: ${message}`);
}
webSocket(connectionId: number, request: ServiceRequest): void {
const id = this.#next();
this.#append({ id, time: new Date().toISOString(), kind: 'websocket', connection: connectionId, host: request.originalHost ?? null,
path: request.path, query: request.query.toString(), headers: plainHeaders(request.headers) });
console.log(`#${id} WebSocket ${request.originalHost ?? '(no host)'}${request.path} (refused)`);
}
#next(): number {
this.#sequence += 1;
return this.#sequence;
}
#append(line: object): void {
appendFileSync(this.#linesPath, JSON.stringify(line) + '\n');
}
#writeBody(id: number, suffix: string, content: Uint8Array | string | undefined): string | null {
if (content === undefined || byteLength(content) === 0) return null;
const name = `${String(id).padStart(6, '0')}.${suffix}`;
writeFileSync(join(this.#bodiesFolder, name), content);
return name;
}
}
function byteLength(body: Uint8Array | string | undefined): number {
if (body === undefined) return 0;
return typeof body === 'string' ? Buffer.byteLength(body) : body.length;
}
function plainHeaders(headers: IncomingHttpHeaders): Record<string, string> {
const result: Record<string, string> = {};
for (const [name, value] of Object.entries(headers)) {
if (value !== undefined) result[name] = Array.isArray(value) ? value.join(', ') : value;
}
return result;
}