/** * Development script: build the game copy from the stock files of a game folder. * * node scripts/build-game-copy.ts --game --out --port * --data * [--patches ] [--gui-patches ] * [--native ] [--decal-cache ] [--verbose] * * Writes /char.vromfs.bin, /aces.vromfs.bin, /gui.vromfs.bin, /win64/aces.exe * and the network hook library the patched executable loads (/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 /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 --out --port --data ' + '[--patches ] [--gui-patches ] [--native ] [--decal-cache ] [--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();