Split the repository into apps, packages and patches

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.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 03:20:54 +08:00
1 parent f6cdab7ec7
commit 27fc535301
476 files changed
+275 -251

No files matched your search

+136
View File
@@ -0,0 +1,136 @@
/**
* Protocol-layer wire codecs for `online_storage` (`cln_set_general_blk` and `PBBD` batch
* streams on `cln_bulk_set_gblk`; implementation notes; aces.exe 0x4662a70..0x466b2ec,
* 0x41537d0..0x4154020).
*/
import { createHash } from 'node:crypto';
import { gunzipSync, inflateSync, zstdDecompressSync } from 'node:zlib';
import { decodeBlk, emptyBlock, encodeBlk, findBlock, setParam, type BlkBlock } from '@wto/blk';
import type { ParsedGblkUpload } from '../cloud-settings/cloud-settings-store.ts';
import { intField, stringField } from '../profile/blk-fields.ts';
import { unpackMarked } from './compression-marker.ts';
export const PBBD_MAGIC = 0x44424250; // "PBBD" little-endian (0x4663427: 0x144424250)
export function isPbbdStream(bytes: Uint8Array): boolean {
if (bytes.byteLength < 8) return false;
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
return view.getUint32(0, true) === PBBD_MAGIC && view.getUint16(4, true) === 1;
}
export function unpackCloudBlk(bytes: Uint8Array, comprHeader: string | undefined): BlkBlock | undefined {
if (bytes.byteLength === 0) return undefined;
const raw = decompressCloudBytes(bytes, comprHeader);
if (!raw || raw.byteLength === 0) return undefined;
try {
return decodeBlk(raw);
} catch {
return undefined;
}
}
export function parsePbbdBulkUploads(bytes: Uint8Array): ReadonlyArray<ParsedGblkUpload> {
if (!isPbbdStream(bytes)) return [];
const chunks = readPbbdChunks(bytes);
const uploads: ParsedGblkUpload[] = [];
for (let i = 0; i < chunks.length; i++) {
const meta = chunks[i];
if (!meta) continue;
const desc = stringField(meta, 'desc');
if (desc === 'store_gblk') {
const containerCandidate = chunks[i + 1];
i += 1;
const blkType = stringField(meta, 'blkType');
if (!blkType || !containerCandidate) continue;
const fallbackVersion = intField(meta, 'version', 1) || 1;
const extracted = normalizeContainerBlk(containerCandidate, fallbackVersion);
const contentHash = stringField(meta, 'contentHash') || computeContentHash(extracted.content);
uploads.push({ blkType, version: extracted.version, contentHash, container: extracted.container });
} else if (desc === 'bigquery' || desc === 'logout') {
i += 1;
}
}
return uploads;
}
export function normalizeContainerBlk(
incoming: BlkBlock,
fallbackVersion: number,
): { readonly version: number; readonly content: BlkBlock; readonly container: BlkBlock } {
const existingContent = findBlock(incoming, 'content');
const rawVersion = intField(incoming, 'version', fallbackVersion);
const version = rawVersion > 0 ? rawVersion : Math.max(1, fallbackVersion);
const content = cloneAsContent(existingContent ?? incoming);
return { version, content, container: buildContainerBlk(version, content) };
}
export function buildContainerBlk(version: number, content: BlkBlock): BlkBlock {
const container = emptyBlock();
setParam(container, 'version', { type: 'int', value: Math.max(1, Math.trunc(version)) });
container.blocks.push(cloneAsContent(content));
return container;
}
export function computeContentHash(content: BlkBlock): string {
const rootCopy = emptyBlock();
rootCopy.params.push(...content.params);
rootCopy.blocks.push(...content.blocks);
return createHash('md5').update(encodeBlk(rootCopy)).digest('hex');
}
export function describePbbdForLog(bytes: Uint8Array): string {
const uploads = parsePbbdBulkUploads(bytes);
if (uploads.length === 0) return 'PBBD stream (0 store_gblk items)';
return uploads.map((u) => `${u.blkType} v${u.version} (${u.contentHash})`).join(', ');
}
function readPbbdChunks(bytes: Uint8Array): Array<BlkBlock | undefined> {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const chunkCount = view.getUint16(6, true);
const chunks: Array<BlkBlock | undefined> = [];
let offset = 8;
for (let i = 0; i < chunkCount; i++) {
if (offset + 5 > bytes.byteLength) break;
const payloadLen = view.getUint32(offset, true);
const marker = bytes[offset + 4]!;
offset += 5;
if (offset + payloadLen > bytes.byteLength) break;
const slice = bytes.subarray(offset, offset + payloadLen);
offset += payloadLen;
chunks.push(decodeMarkedChunk(marker, slice));
}
return chunks;
}
function cloneAsContent(source: BlkBlock): BlkBlock {
return { name: 'content', params: [...source.params], blocks: [...source.blocks] };
}
function decompressCloudBytes(bytes: Uint8Array, comprHeader: string | undefined): Uint8Array | undefined {
const method = comprHeader?.trim().toLowerCase();
try {
if (method === 'zstd') return zstdDecompressSync(bytes);
if (method === 'zlib') return bytes.byteLength > 4 ? inflateSync(bytes.subarray(4)) : undefined;
if (method === 'gzip') return gunzipSync(bytes);
if (!method || method === 'none') return unpackMarked(bytes)?.data ?? bytes;
return unpackMarked(bytes)?.data;
} catch {
return unpackMarked(bytes)?.data;
}
}
function decodeMarkedChunk(marker: number, payload: Uint8Array): BlkBlock | undefined {
if (payload.byteLength === 0) return emptyBlock();
try {
let raw: Uint8Array | undefined;
if (marker === 0x69) raw = payload;
else if (marker === 0x5a) raw = zstdDecompressSync(payload);
else if (marker === 0x7a) raw = payload.byteLength > 4 ? inflateSync(payload.subarray(4)) : undefined;
else if (marker === 0x1f) raw = gunzipSync(payload);
return raw ? decodeBlk(raw) : undefined;
} catch {
return undefined;
}
}