Files
WT-Offline/scripts/build-game-copy.ts
T
N0VAN0M4LY 27fc535301 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.
2026-11-29 03:20:54 +08:00

128 lines
6.6 KiB
TypeScript

/**
* Development script: build the game copy from the stock files of a game folder.
*
* node scripts/build-game-copy.ts --game <stock game folder> --out <game copy folder> --port <n>
* --data <local server data folder>
* [--patches <exe patches>] [--gui-patches <GUI patches>]
* [--native <folder with wto_ws.dll>] [--decal-cache <folder>] [--verbose]
*
* Writes <out>/char.vromfs.bin, <out>/aces.vromfs.bin, <out>/gui.vromfs.bin, <out>/win64/aces.exe
* and the network hook library the patched executable loads (<out>/win64/wto_ws.dll from --native,
* built by native/wto-net/build.ps1, and its settings wto_ws.cfg). The stock folder is only read.
* --data is the folder the local server will run with: the store packs it sells there (its tuning
* with that folder's overrides) get bundle GUIDs in gui.blk. --decal-cache names the cache/content
* folder of the player's online installation; it is copied to <out>/cache/content, otherwise
* char.vromfs.bin gets the placeholder decal index.
* The patch folders and the hook library default to this repository's patches/exe, patches/gui
* and build/native. The launcher runs the same steps at every launch, with the port the player chose.
*/
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import {
assertWritableCopy,
copyPlayerDecalCache,
GAME_FILES,
localEndpoint,
NETWORK_HOOK_LIBRARY,
networkHookSettings,
offlinePackBundleGuids,
patchExecutable,
playerDecalCacheProblem,
rebuildCharPack,
rebuildGuiPack,
rebuildMainPack,
} from '@wto/patcher';
import { multiVehiclePackIds } from '@wto/server';
const REPOSITORY = fileURLToPath(new URL('..', import.meta.url));
const USAGE = 'usage: build-game-copy.ts --game <stock game folder> --out <game copy folder> --port <n> --data <local server data folder> '
+ '[--patches <exe patches>] [--gui-patches <GUI patches>] [--native <folder with wto_ws.dll>] [--decal-cache <folder>] [--verbose]';
function main(): number {
const { values } = parseArgs({
options: {
game: { type: 'string' },
out: { type: 'string' },
port: { type: 'string' },
data: { type: 'string' },
patches: { type: 'string', default: join(REPOSITORY, 'patches', 'exe') },
'gui-patches': { type: 'string', default: join(REPOSITORY, 'patches', 'gui') },
native: { type: 'string', default: join(REPOSITORY, 'build', 'native') },
'decal-cache': { type: 'string' },
verbose: { type: 'boolean', default: false },
},
});
if (!values.game || !values.out || !values.port || !values.data) {
console.error(USAGE);
return 2;
}
assertWritableCopy(values.game, values.out);
const endpoint = localEndpoint(Number(values.port));
const decalCache = values['decal-cache'];
const decalCacheProblem = decalCache === undefined ? undefined : playerDecalCacheProblem(decalCache);
if (decalCacheProblem) {
console.error(`--decal-cache: ${decalCacheProblem}; nothing was written`);
return 1;
}
const hookLibraryPath = join(values.native, NETWORK_HOOK_LIBRARY);
if (!existsSync(hookLibraryPath)) {
console.error(`${hookLibraryPath} not found (build it with native/wto-net/build.ps1); nothing was written`);
return 1;
}
const executable = patchExecutable(readFileSync(join(values.game, GAME_FILES.executable)), values.patches);
console.log(executable.description);
if (!executable.executable) {
console.error('some patches are unavailable; nothing was written');
return 1;
}
const stockCharPack = readFileSync(join(values.game, GAME_FILES.charPack));
const charPack = rebuildCharPack(stockCharPack, endpoint, { placeholderDecals: decalCache === undefined });
console.log(`network.blk: ${charPack.network.addresses.length} address(es) now point to ${endpoint.host}:${endpoint.port}; ` +
`environments configured: ${charPack.network.environments.join(', ')}`);
if (values.verbose) {
for (const change of charPack.network.addresses) console.log(` ${change.path}: ${change.before} -> ${change.after}`);
}
console.log(`game_settings.blk: features on for every player: ${charPack.features.join(', ') || '(already on)'}`);
console.log(`shop.blk: ${charPack.revealedVehicles} hidden vehicles shown in the tech tree`);
if (charPack.placeholderDecals !== undefined) console.log(`dldata: placeholder textures for ${charPack.placeholderDecals} decals`);
const packBundles = offlinePackBundleGuids(stockCharPack, multiVehiclePackIds(values.data, (line) => console.log(line)));
const mainPack = rebuildMainPack(readFileSync(join(values.game, GAME_FILES.mainPack)), endpoint, packBundles);
console.log(`gui.blk: ${packBundles.length} offline vehicle pack bundle GUID(s) registered`);
console.log(`settings.blk: ${mainPack.addresses.length} address(es) now point to ${endpoint.host}:${endpoint.port}`);
for (const change of mainPack.addresses) console.log(` ${change.path}: ${change.before} -> ${change.after}`);
const guiPack = rebuildGuiPack(readFileSync(join(values.game, GAME_FILES.guiPack)), endpoint, values['gui-patches']);
console.log(`gui.vromfs.bin: ${guiPack.applied.length} GUI patch(es) applied (${guiPack.applied.map((p) => p.id).join(', ')})`);
writeOutput(values.out, GAME_FILES.charPack, charPack.pack);
writeOutput(values.out, GAME_FILES.mainPack, mainPack.pack);
writeOutput(values.out, GAME_FILES.guiPack, guiPack.pack);
writeOutput(values.out, GAME_FILES.executable, executable.executable);
const executableFolder = dirname(GAME_FILES.executable);
writeOutput(values.out, join(executableFolder, NETWORK_HOOK_LIBRARY), readFileSync(hookLibraryPath));
const settings = networkHookSettings(endpoint);
writeOutput(values.out, join(executableFolder, settings.name), settings.content);
if (decalCache !== undefined) {
const copied = copyPlayerDecalCache(decalCache, values.out);
console.log(`copied the player's web cache to ${copied.copiedTo} (decal indexes: ${copied.decalIndexes.join(', ')})`);
}
return 0;
}
function writeOutput(folder: string, relativePath: string, content: Uint8Array): void {
const path = join(folder, relativePath);
mkdirSync(dirname(path), { recursive: true });
// Unlink before writing so a read-only target file or a hardlink to the stock folder is
// replaced with a fresh file instead of failing with EPERM or mutating the link target.
rmSync(path, { force: true });
writeFileSync(path, content);
console.log(`wrote ${path}`);
}
process.exitCode = main();