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

+88
View File
@@ -0,0 +1,88 @@
/**
* RFC 1459 + Gaijin IRC wire parser and line formatters used by the local chat server.
*/
export const IRC_SERVER_HOST = 'wt.local';
export interface IrcCommand {
readonly verb: string;
readonly params: readonly string[];
}
const HANDSHAKE_PREFIX = /^(?:PASS |NICK |USER |CAP |PING |QUIT(?: |\r|\n|$))/i;
export function startsIrcHandshake(firstChunk: Buffer): boolean {
if (firstChunk.length < 4) return false;
const head = firstChunk.subarray(0, Math.min(firstChunk.length, 16)).toString('latin1');
return HANDSHAKE_PREFIX.test(head);
}
export function parseIrcLine(rawLine: string): IrcCommand | undefined {
let rest = rawLine.replace(/[\r\n]+$/, '');
if (rest.length === 0) return undefined;
if (rest.startsWith(':')) {
const space = rest.indexOf(' ');
if (space < 0) return undefined;
rest = rest.slice(space + 1).trimStart();
}
if (rest.length === 0) return undefined;
const verbEnd = rest.indexOf(' ');
if (verbEnd < 0) {
return { verb: rest.toUpperCase(), params: [] };
}
const verb = rest.slice(0, verbEnd).toUpperCase();
let cursor = rest.slice(verbEnd + 1);
const params: string[] = [];
while (cursor.length > 0) {
while (cursor.startsWith(' ')) cursor = cursor.slice(1);
if (cursor.length === 0) break;
if (cursor.startsWith(':')) {
params.push(cursor.slice(1));
break;
}
const nextSpace = cursor.indexOf(' ');
if (nextSpace < 0) {
params.push(cursor);
break;
}
params.push(cursor.slice(0, nextSpace));
cursor = cursor.slice(nextSpace + 1);
}
return { verb, params };
}
export function formatServerNumeric(code: string, nick: string, params: readonly string[], trailing?: string): string {
const target = sanitizeToken(nick || '*');
const middle = params.length > 0 ? ` ${params.map(sanitizeToken).join(' ')}` : '';
const suffix = trailing !== undefined ? ` :${sanitizeTrailing(trailing)}` : '';
return `:${IRC_SERVER_HOST} ${code} ${target}${middle}${suffix}\r\n`;
}
export function formatServerCommand(verb: string, params: readonly string[], trailing?: string): string {
const middle = params.length > 0 ? ` ${params.map(sanitizeToken).join(' ')}` : '';
const suffix = trailing !== undefined ? ` :${sanitizeTrailing(trailing)}` : '';
return `:${IRC_SERVER_HOST} ${verb}${middle}${suffix}\r\n`;
}
export function formatUserEvent(nick: string, user: string, verb: string, params: readonly string[], trailing?: string): string {
const safeNick = sanitizeToken(nick || 'Player');
const safeUser = sanitizeToken(user || '0');
const middle = params.length > 0 ? ` ${params.map(sanitizeToken).join(' ')}` : '';
const suffix = trailing !== undefined ? ` :${sanitizeTrailing(trailing)}` : '';
return `:${safeNick}!${safeUser}@127.0.0.1 ${verb}${middle}${suffix}\r\n`;
}
export function sanitizeChannelName(raw: string): string {
const trimmed = sanitizeToken(raw);
if (trimmed.length === 0) return '#general';
return trimmed.startsWith('#') || trimmed.startsWith('&') ? trimmed : `#${trimmed}`;
}
function sanitizeToken(text: string): string {
return text.replace(/[\r\n\0 ]+/g, '');
}
function sanitizeTrailing(text: string): string {
return text.replace(/[\r\n\0]+/g, ' ');
}
+284
View File
@@ -0,0 +1,284 @@
/**
* In-process RFC 1459 + Gaijin IRC chat server (`IRCClient` in `aces.exe` 0x133c5e0).
*
* Serves hangar general rooms, custom player rooms, discussion threads (`XTJOIN` / `XTMETA`),
* private messages, and session presence so `gchat_is_connected()` and `gchat_is_enabled()`
* evaluate `true` natively in both hangar and battle.
*/
import type { Duplex } from 'node:stream';
import {
formatServerCommand,
formatServerNumeric,
formatUserEvent,
IRC_SERVER_HOST,
parseIrcLine,
sanitizeChannelName,
type IrcCommand,
} from './irc-protocol.ts';
interface IrcSession {
readonly socket: Duplex;
nick: string;
user: string;
realName: string;
registered: boolean;
buffer: string;
readonly channels: Set<string>;
}
interface IrcChannel {
readonly name: string;
topic: string;
readonly members: Set<IrcSession>;
}
const MAX_BUFFER_CHARS = 65_536;
export class IrcServer {
readonly #sessions = new Set<IrcSession>();
readonly #channels = new Map<string, IrcChannel>();
#nextThreadId = 1;
accept(socket: Duplex): void {
const session: IrcSession = {
socket, nick: '', user: '', realName: '', registered: false, buffer: '', channels: new Set(),
};
this.#sessions.add(session);
socket.on('data', (chunk: Buffer | string) => {
session.buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
if (session.buffer.length > MAX_BUFFER_CHARS) {
socket.destroy();
return;
}
let nl = session.buffer.indexOf('\n');
while (nl >= 0) {
const rawLine = session.buffer.slice(0, nl);
session.buffer = session.buffer.slice(nl + 1);
const cmd = parseIrcLine(rawLine);
if (cmd) this.#handleCommand(session, cmd);
nl = session.buffer.indexOf('\n');
}
});
socket.once('close', () => this.#removeSession(session, 'Client disconnected'));
socket.once('error', () => socket.destroy());
}
#handleCommand(session: IrcSession, cmd: IrcCommand): void {
const [p0 = '', p1 = ''] = cmd.params;
switch (cmd.verb) {
case 'CAP': if (p0.toUpperCase() === 'LS') this.#send(session, formatServerCommand('CAP', ['*', 'LS'], '')); return;
case 'PASS':
case 'PONG': return;
case 'NICK': this.#handleNick(session, p0); return;
case 'USER':
session.user = p0 || '0';
session.realName = cmd.params[3] ?? session.user;
this.#tryCompleteRegistration(session);
return;
case 'PING': this.#send(session, formatServerCommand('PONG', [IRC_SERVER_HOST], p0 || IRC_SERVER_HOST)); return;
case 'JOIN': this.#handleJoin(session, p0); return;
case 'PART': this.#handlePart(session, p0, p1); return;
case 'PRIVMSG':
case 'NOTICE': this.#handleMessage(session, cmd.verb, p0, p1); return;
case 'NAMES': this.#sendNames(session, sanitizeChannelName(p0 || '#general')); return;
case 'LIST': this.#handleList(session); return;
case 'TOPIC': this.#handleTopic(session, cmd.params); return;
case 'MODE': this.#handleMode(session, p0); return;
case 'WHO': this.#handleWho(session, p0); return;
case 'WHOIS': this.#handleWhois(session, p0); return;
case 'XTJOIN': this.#handleXtJoin(session, cmd.params); return;
case 'XTMETA': this.#handleXtMeta(session, cmd.params); return;
case 'QUIT':
this.#removeSession(session, p0 || 'Quit');
session.socket.end();
return;
default: return;
}
}
#handleNick(session: IrcSession, rawNick: string): void {
const nextNick = rawNick.trim() || `Player${this.#sessions.size}`;
const prevNick = session.nick;
session.nick = nextNick;
if (session.registered && prevNick && prevNick !== nextNick) {
this.#send(session, formatUserEvent(prevNick, session.user, 'NICK', [], nextNick));
return;
}
this.#tryCompleteRegistration(session);
}
#tryCompleteRegistration(session: IrcSession): void {
if (session.registered || !session.nick || !session.user) return;
session.registered = true;
const { nick, user } = session;
this.#send(session, formatServerNumeric('001', nick, [], `Welcome to the War Thunder Local Chat Network ${nick}!${user}@127.0.0.1`));
this.#send(session, formatServerNumeric('002', nick, [], `Your host is ${IRC_SERVER_HOST}, running version 1.0`));
this.#send(session, formatServerNumeric('003', nick, [], 'This server was created locally'));
this.#send(session, formatServerNumeric('004', nick, [IRC_SERVER_HOST, '1.0', 'iow', 'ntov']));
this.#send(session, formatServerNumeric('375', nick, [], `- ${IRC_SERVER_HOST} Message of the Day -`));
this.#send(session, formatServerNumeric('372', nick, [], '- Local offline chat service active'));
this.#send(session, formatServerNumeric('376', nick, [], 'End of /MOTD command.'));
}
#handleJoin(session: IrcSession, rawTargets: string): void {
if (!rawTargets) return;
if (rawTargets === '0') {
for (const chan of [...session.channels]) this.#partOne(session, chan, '');
return;
}
for (const raw of rawTargets.split(',')) {
if (!raw.trim()) continue;
this.#joinChannel(session, sanitizeChannelName(raw), '');
}
}
#joinChannel(session: IrcSession, channelName: string, initialTopic: string): void {
let channel = this.#channels.get(channelName);
if (!channel) {
channel = { name: channelName, topic: initialTopic, members: new Set() };
this.#channels.set(channelName, channel);
} else if (initialTopic && !channel.topic) {
channel.topic = initialTopic;
}
channel.members.add(session);
session.channels.add(channelName);
const joinLine = formatUserEvent(session.nick, session.user, 'JOIN', [], channelName);
for (const member of channel.members) this.#send(member, joinLine);
if (channel.topic) {
this.#send(session, formatServerNumeric('332', session.nick, [channelName], channel.topic));
} else {
this.#send(session, formatServerNumeric('331', session.nick, [channelName], 'No topic is set'));
}
this.#sendNames(session, channelName);
}
#handlePart(session: IrcSession, rawTargets: string, reason: string): void {
for (const raw of rawTargets.split(',')) {
if (!raw.trim()) continue;
this.#partOne(session, sanitizeChannelName(raw), reason);
}
}
#partOne(session: IrcSession, channelName: string, reason: string): void {
const channel = this.#channels.get(channelName);
if (!channel || !channel.members.has(session)) return;
const partLine = formatUserEvent(session.nick, session.user, 'PART', [channelName], reason || undefined);
for (const member of channel.members) this.#send(member, partLine);
channel.members.delete(session);
session.channels.delete(channelName);
if (channel.members.size === 0) this.#channels.delete(channelName);
}
#handleMessage(session: IrcSession, verb: 'PRIVMSG' | 'NOTICE', target: string, text: string): void {
if (!target) return;
if (target.startsWith('#') || target.startsWith('&')) {
const channel = this.#channels.get(sanitizeChannelName(target));
if (!channel) {
if (verb === 'PRIVMSG') this.#send(session, formatServerNumeric('403', session.nick, [target], 'No such channel'));
return;
}
const line = formatUserEvent(session.nick, session.user, verb, [channel.name], text);
for (const member of channel.members) {
if (member !== session) this.#send(member, line);
}
return;
}
const recipient = [...this.#sessions].find((peer) => peer.registered && peer.nick === target);
if (!recipient) {
if (verb === 'PRIVMSG') this.#send(session, formatServerNumeric('401', session.nick, [target], 'No such nick/channel'));
return;
}
this.#send(recipient, formatUserEvent(session.nick, session.user, verb, [recipient.nick], text));
}
#sendNames(session: IrcSession, channelName: string): void {
const channel = this.#channels.get(channelName);
const nicks = channel ? [...channel.members].map((m) => m.nick).filter(Boolean).join(' ') : session.nick;
this.#send(session, formatServerNumeric('353', session.nick, ['=', channelName], nicks));
this.#send(session, formatServerNumeric('366', session.nick, [channelName], 'End of /NAMES list.'));
}
#handleList(session: IrcSession): void {
this.#send(session, formatServerNumeric('321', session.nick, ['Channel'], 'Users Name'));
for (const channel of this.#channels.values()) {
if (channel.name === '#___empty___') continue;
this.#send(session, formatServerNumeric('322', session.nick, [channel.name, String(channel.members.size)], channel.topic));
}
this.#send(session, formatServerNumeric('323', session.nick, [], 'End of /LIST'));
}
#handleTopic(session: IrcSession, params: readonly string[]): void {
const channelName = sanitizeChannelName(params[0] ?? '');
const channel = this.#channels.get(channelName);
if (!channel) {
this.#send(session, formatServerNumeric('403', session.nick, [channelName], 'No such channel'));
return;
}
if (params.length >= 2) {
channel.topic = params[1] ?? '';
const line = formatUserEvent(session.nick, session.user, 'TOPIC', [channelName], channel.topic);
for (const member of channel.members) this.#send(member, line);
return;
}
if (channel.topic) this.#send(session, formatServerNumeric('332', session.nick, [channelName], channel.topic));
else this.#send(session, formatServerNumeric('331', session.nick, [channelName], 'No topic is set'));
}
#handleMode(session: IrcSession, target: string): void {
if (target.startsWith('#') || target.startsWith('&')) {
this.#send(session, formatServerNumeric('324', session.nick, [sanitizeChannelName(target), '+nt']));
} else this.#send(session, formatServerNumeric('221', session.nick, ['+i']));
}
#handleWho(session: IrcSession, target: string): void {
const channel = target.startsWith('#') ? this.#channels.get(sanitizeChannelName(target)) : undefined;
for (const m of channel?.members ?? []) {
this.#send(session, formatServerNumeric('352', session.nick, [channel!.name, m.user, '127.0.0.1', IRC_SERVER_HOST, m.nick, 'H'], `0 ${m.realName}`));
}
this.#send(session, formatServerNumeric('315', session.nick, [target || '*'], 'End of /WHO list.'));
}
#handleWhois(session: IrcSession, targetNick: string): void {
const found = [...this.#sessions].find((peer) => peer.nick === targetNick);
if (found) this.#send(session, formatServerNumeric('311', session.nick, [found.nick, found.user, '127.0.0.1', '*'], found.realName));
this.#send(session, formatServerNumeric('318', session.nick, [targetNick || '*'], 'End of /WHOIS list.'));
}
#handleXtJoin(session: IrcSession, params: readonly string[]): void {
this.#joinChannel(session, `#___xthread_${this.#nextThreadId++}`, params[1] ?? params[0] ?? 'Discussion');
}
#handleXtMeta(session: IrcSession, params: readonly string[]): void {
const channelName = sanitizeChannelName(params[0] ?? '');
const channel = this.#channels.get(channelName);
if (!channel || params[1]?.toLowerCase() !== 'topic') return;
channel.topic = params[2] ?? '';
const line = formatUserEvent(session.nick, session.user, 'TOPIC', [channelName], channel.topic);
for (const member of channel.members) this.#send(member, line);
}
#removeSession(session: IrcSession, reason: string): void {
if (!this.#sessions.delete(session)) return;
const quitLine = session.registered ? formatUserEvent(session.nick, session.user, 'QUIT', [], reason) : '';
const notified = new Set<IrcSession>();
for (const channelName of session.channels) {
const channel = this.#channels.get(channelName);
if (!channel) continue;
channel.members.delete(session);
for (const peer of quitLine ? channel.members : []) {
if (!notified.has(peer)) {
notified.add(peer);
this.#send(peer, quitLine);
}
}
if (channel.members.size === 0) this.#channels.delete(channelName);
}
session.channels.clear();
}
#send(session: IrcSession, line: string): void {
if (!session.socket.destroyed && session.socket.writable) session.socket.write(line);
}
}