Files
WT-Offline/packages/patcher/src/build-game-copy.ts
T
N0VAN0M4LY fb7d07f4f9
Launcher release / release (push) Waiting to run
feat(launcher): add About log export shortcut and runtime logging toggle
Add a quick ZIP log export button to the About page (also bundling the latest embedded browser -browser.log while excluding player saves) and add a runtime logging toggle under Settings -> Diagnostics that controls launcher file logs, local server traffic logs, and wto_ws.log via log=0 in wto_ws.cfg. Update the v0.1.2 release note accordingly.
2026-12-02 17:41:49 +08:00

126 lines
6.2 KiB
TypeScript

/**
* Build the files of the game copy in memory: the three rebuilt data packs,
* the patched executable, the network hook library and its settings. Nothing is written here
* (write-game-copy.ts does that), so a caller can show the report first and let the player decide.
*
* Everything the build needs from outside comes in as an input: the stock files, the patch set
* (patch-set.ts), the hook library, and the multi-vehicle pack ids the local server will sell from its
* data folder.
*/
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { PeImage, type PatchFailure } from '@wto/exepatch';
import { rebuildCharPack, type CharPackResult } from './char-pack.ts';
import { blockingFailures } from './essential-features.ts';
import { patchExecutable } from './executable.ts';
import { GAME_FILES } from './game-copy.ts';
import { rebuildGuiPack } from './gui-pack.ts';
import { rebuildMainPack } from './main-pack.ts';
import type { AddressChange } from './network/address-rewrite.ts';
import { localEndpoint, type LocalEndpoint } from './network/local-endpoint.ts';
import { NETWORK_HOOK_LIBRARY, networkHookSettings } from './network/network-hook.ts';
import { loadNativeModules } from './native/native-definition.ts';
import { resolveNativeModules } from './native/native-resolve.ts';
import { patchSetFolders } from './patch-set.ts';
import { offlinePackBundleGuids } from './store/offline-bundles.ts';
/** The stock files the build changes. */
export interface StockGameFiles {
readonly charPack: Uint8Array;
readonly mainPack: Uint8Array;
readonly guiPack: Uint8Array;
readonly executable: Uint8Array;
}
export interface GameCopyInputs {
readonly stock: StockGameFiles;
/** The local server's port, written into the data packs and the hook settings. */
readonly port: number;
/** Root of the patch set: exe, GUI and native definitions (patch-set.ts). */
readonly patches: string;
/** wto_ws.dll, built from native/wto-net. */
readonly networkHookLibrary: Uint8Array;
/** Ids of the multi-vehicle packs the local server sells (@wto/server multiVehiclePackIds). */
readonly multiVehiclePackIds: readonly string[];
/** Store the placeholder decal index; false when the player's own web cache is copied. */
readonly placeholderDecals: boolean;
/** Whether wto_ws.dll writes win64/wto_ws.log; defaults to true. */
readonly hookLogging?: boolean;
}
/** One file of the game copy, by its path relative to the copy folder. */
export interface GameCopyFile {
readonly path: string;
readonly content: Uint8Array;
}
export interface GameCopyBuild {
/** Every file to write, with unavailable patches left out. */
readonly files: readonly GameCopyFile[];
readonly endpoint: LocalEndpoint;
readonly exePatches: { readonly applied: readonly { readonly patchId: string; readonly rva: number }[]; readonly failures: readonly PatchFailure[] };
readonly guiPatches: { readonly applied: readonly string[]; readonly failures: readonly PatchFailure[] };
/** Native modules the hook library can run on this executable, and the ones it cannot. */
readonly nativeModules: { readonly available: readonly string[]; readonly failures: readonly PatchFailure[] };
/** Unavailable patches that must stop an installation (essential-features.ts). */
readonly blocking: readonly PatchFailure[];
readonly charPack: Omit<CharPackResult, 'pack'>;
/** Addresses in aces.vromfs.bin config/settings.blk now pointing at the local server. */
readonly engineAddresses: readonly AddressChange[];
/** Store bundle GUIDs registered in config/gui.blk. */
readonly bundleGuids: number;
}
export function readStockGameFiles(folder: string): StockGameFiles {
return {
charPack: readFileSync(join(folder, GAME_FILES.charPack)),
mainPack: readFileSync(join(folder, GAME_FILES.mainPack)),
guiPack: readFileSync(join(folder, GAME_FILES.guiPack)),
executable: readFileSync(join(folder, GAME_FILES.executable)),
};
}
export function buildGameCopy(inputs: GameCopyInputs): GameCopyBuild {
const endpoint = localEndpoint(inputs.port);
const folders = patchSetFolders(inputs.patches);
const executable = patchExecutable(inputs.stock.executable, folders.exe);
const stockImage = new PeImage(inputs.stock.executable);
const native = resolveNativeModules(stockImage, loadNativeModules(folders.native));
const guiPack = rebuildGuiPack(inputs.stock.guiPack, endpoint, folders.gui);
const appliedGuiPatches = guiPack.applied.map((item) => item.patch.id);
const localRegistration = appliedGuiPatches.includes('registration/open-local-page') &&
appliedGuiPatches.includes('registration/keep-local-page-embedded');
const { pack: charPack, ...charReport } = rebuildCharPack(inputs.stock.charPack, endpoint, {
placeholderDecals: inputs.placeholderDecals,
localRegistration,
});
const bundles = offlinePackBundleGuids(inputs.stock.charPack, inputs.multiVehiclePackIds);
const mainPack = rebuildMainPack(inputs.stock.mainPack, endpoint, bundles);
const executableFolder = dirname(GAME_FILES.executable);
const hookSettings = networkHookSettings(endpoint, stockImage, native.values, {
loggingEnabled: inputs.hookLogging ?? true,
});
const files: GameCopyFile[] = [
{ path: GAME_FILES.charPack, content: charPack },
{ path: GAME_FILES.mainPack, content: mainPack.pack },
{ path: GAME_FILES.guiPack, content: guiPack.pack },
{ path: GAME_FILES.executable, content: executable.executable },
{ path: join(executableFolder, NETWORK_HOOK_LIBRARY), content: inputs.networkHookLibrary },
{ path: join(executableFolder, hookSettings.name), content: hookSettings.content },
];
const failures = [...executable.plan.failures, ...guiPack.failures, ...native.failures];
return {
files,
endpoint,
exePatches: { applied: executable.plan.writes.map((write) => ({ patchId: write.patchId, rva: write.rva })), failures: executable.plan.failures },
guiPatches: { applied: appliedGuiPatches, failures: guiPack.failures },
nativeModules: { available: [...native.available.keys()], failures: native.failures },
blocking: blockingFailures(failures),
charPack: charReport,
engineAddresses: mainPack.addresses,
bundleGuids: bundles.length,
};
}