Let the patcher report unavailable patches and build in memory

The launcher has to show the player which patches do not fit their game
build, let them go on without the features those patches cover, and
refuse when a patch that keeps the copy apart from the online game is
missing. The patcher could only refuse the whole build.

- GUI patches are applied one by one. A patch with an edit whose stock
  text is not found exactly once, or whose script is missing, is left
  out whole and reported with its features, like an unavailable exe
  patch; the others are applied.
- patchExecutable always produces the executable with the available
  patches; unavailable ones and those depending on them are listed.
- essential-features.ts names the features whose patches stop an
  installation: A-离线启动, A-与正版隔离, A-不连 Gaijin (the copy does not
  start, touches the online game's data or identity, or connects to
  Gaijin without them). Patches tagged with them are 9 exe patches and
  one GUI patch today.
- buildGameCopy builds every file of the copy in memory from the stock
  files and the caller's inputs (patch folders, hook library, port,
  store pack ids) and returns them with a structured report;
  writeGameCopy writes them, replacing each file instead of writing
  through a hard link. readStockGameFiles reads the four stock files.
- assertWritableCopy takes steamLibraryConfirmed for a Steam library the
  player chose after the warning; isInSteamLibrary is exported.

scripts/build-game-copy.ts uses buildGameCopy and still writes nothing
when any patch is unavailable, unless --skip-unavailable is given and
none of the unavailable patches is essential.

Checked with the root typecheck and three runs of build-game-copy.ts
into scratch folders: all patches as they are (12 exe, 35 GUI, 496
bundle GUIDs, six files written); one ordinary exe patch and one GUI
patch broken (both reported with their features, nothing written, then
written with --skip-unavailable); the network hook patch broken (it and
the TLS patch depending on it reported as essential, nothing written
even with --skip-unavailable).
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 03:32:36 +08:00
1 parent 90cc6b0bb1
commit c8d4972cf0
8 files changed
+285 -84

No files matched your search

+60 -54
View File
@@ -4,7 +4,8 @@
* 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]
* [--native <folder with wto_ws.dll>] [--decal-cache <folder>]
* [--skip-unavailable] [--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,
@@ -13,32 +14,32 @@
* 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.
* When a patch is unavailable nothing is written, unless --skip-unavailable is given and none of
* the unavailable patches is essential (the patcher's essential-features.ts).
* 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.
* and build/native. The launcher runs the same build 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 { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
import type { PatchFailure } from '@wto/exepatch';
import {
assertWritableCopy,
buildGameCopy,
copyPlayerDecalCache,
GAME_FILES,
localEndpoint,
NETWORK_HOOK_LIBRARY,
networkHookSettings,
offlinePackBundleGuids,
patchExecutable,
playerDecalCacheProblem,
rebuildCharPack,
rebuildGuiPack,
rebuildMainPack,
readStockGameFiles,
writeGameCopy,
type GameCopyBuild,
} 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]';
+ '[--patches <exe patches>] [--gui-patches <GUI patches>] [--native <folder with wto_ws.dll>] [--decal-cache <folder>] '
+ '[--skip-unavailable] [--verbose]';
function main(): number {
const { values } = parseArgs({
@@ -51,6 +52,7 @@ function main(): number {
'gui-patches': { type: 'string', default: join(REPOSITORY, 'patches', 'gui') },
native: { type: 'string', default: join(REPOSITORY, 'build', 'native') },
'decal-cache': { type: 'string' },
'skip-unavailable': { type: 'boolean', default: false },
verbose: { type: 'boolean', default: false },
},
});
@@ -59,7 +61,6 @@ function main(): number {
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) {
@@ -72,41 +73,28 @@ function main(): number {
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');
const build = buildGameCopy({
stock: readStockGameFiles(values.game),
port: Number(values.port),
exePatches: values.patches,
guiPatches: values['gui-patches'],
networkHookLibrary: readFileSync(hookLibraryPath),
multiVehiclePackIds: multiVehiclePackIds(values.data, (line) => console.log(line)),
placeholderDecals: decalCache === undefined,
});
report(build, values.verbose);
const failures = [...build.exePatches.failures, ...build.guiPatches.failures];
if (build.blocking.length) {
console.error(`essential patch(es) unavailable: ${build.blocking.map((failure) => failure.patchId).join(', ')}; nothing was written`);
return 1;
}
if (failures.length && !values['skip-unavailable']) {
console.error('some patches are unavailable; nothing was written (--skip-unavailable writes the copy without them)');
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);
for (const path of writeGameCopy(values.out, build.files)) console.log(`wrote ${path}`);
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(', ')})`);
@@ -114,14 +102,32 @@ function main(): number {
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}`);
function report(build: GameCopyBuild, verbose: boolean): void {
const { endpoint, charPack } = build;
const target = `${endpoint.host}:${endpoint.port}`;
console.log(`exe: ${build.exePatches.applied.length} patch(es) ready, ${build.exePatches.failures.length} unavailable`);
for (const write of build.exePatches.applied) console.log(` ready ${write.patchId} at 0x${write.rva.toString(16)}`);
reportFailures(build.exePatches.failures);
console.log(`network.blk: ${charPack.network.addresses.length} address(es) now point to ${target}; ` +
`environments configured: ${charPack.network.environments.join(', ')}`);
if (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`);
console.log(`gui.blk: ${build.bundleGuids} offline vehicle pack bundle GUID(s) registered`);
console.log(`settings.blk: ${build.engineAddresses.length} address(es) now point to ${target}`);
for (const change of build.engineAddresses) console.log(` ${change.path}: ${change.before} -> ${change.after}`);
console.log(`gui.vromfs.bin: ${build.guiPatches.applied.length} GUI patch(es) ready, ${build.guiPatches.failures.length} unavailable`);
if (verbose) for (const id of build.guiPatches.applied) console.log(` ready ${id}`);
reportFailures(build.guiPatches.failures);
}
function reportFailures(failures: readonly PatchFailure[]): void {
for (const failure of failures) console.log(` FAILED ${failure.patchId}: ${failure.reason}`);
const features = [...new Set(failures.flatMap((failure) => failure.features))].sort();
if (failures.length) console.log(` features affected: ${features.join(', ') || 'none recorded'}`);
}
process.exitCode = main();