Scan locally cached .ddsx files under cache/content (<first2>/<rest>.ddsx) when building char.vromfs.bin from player decal snapshots or regional.vromfs.bin. Repair single-LOD cache hits by falling back missing lodN/hdrN pairs to available LODs, fall back uncached stock decals to built-in placeholders, and drop uncached extra decals and incomplete downloadable skins.
269 lines
13 KiB
TypeScript
269 lines
13 KiB
TypeScript
/**
|
|
* Build the files of the game copy in memory: the rebuilt data packs (with optional additive mod
|
|
* overlays), the patched executable (with protected core patches + optional declarative mod exe
|
|
* patches), 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, the multi-vehicle pack ids the local server will sell, and
|
|
* optionally the ordered list of enabled mods with any additional stock packs they touch.
|
|
*/
|
|
import { readFileSync } from 'node:fs';
|
|
import { dirname, join } from 'node:path';
|
|
import { PeImage, type PatchFailure } from '@wto/exepatch';
|
|
import { touchedClientPacks, type DiscoveredMod, type ModdableClientPack } from '@wto/mods';
|
|
import { rebuildCharPack, type CharPackResult } from './char-pack.ts';
|
|
import { blockingFailures } from './essential-features.ts';
|
|
import { patchExecutableWithMods, type ModExePatchFailure } from './executable.ts';
|
|
import { GAME_FILES } from './game-copy.ts';
|
|
import {
|
|
GAME_PACK_FILE,
|
|
rebuildGamePack,
|
|
validateGroundAiGamePack,
|
|
type GamePackResult,
|
|
type GroundAiBuildInputs,
|
|
} from './ground-ai/game-pack.ts';
|
|
import { groundAiOwnsMovement, isGroundAiActive } from './ground-ai/ground-ai-config.ts';
|
|
import { rebuildGuiPack } from './gui-pack.ts';
|
|
import { rebuildMainPack } from './main-pack.ts';
|
|
import { collectModAddonMounts, type ModAddonMount } from './mods/addon-register.ts';
|
|
import { CORE_BUILD_PACKS } from './mods/deploy-external.ts';
|
|
import { applyModGuiPatches } from './mods/gui-overlay.ts';
|
|
import { applyModPackOverlays, type PackOverlayReport } from './mods/pack-overlay.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 type { AddedFile } from './pack-edit.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;
|
|
/** Real dldata/downloadable_decals.blk and downloadable_skins.blk extracted from the player's cache. */
|
|
readonly decalIndexFiles?: readonly AddedFile[];
|
|
/** Optional player decal cache folder used to filter cached .ddsx textures when embedding indexes. */
|
|
readonly decalCache?: string;
|
|
/** Optional pre-scanned set of cached .ddsx relative paths ("<first2>/<rest>.ddsx", lowercased). */
|
|
readonly cachedDdsx?: ReadonlySet<string>;
|
|
/** Whether wto_ws.dll writes win64/wto_ws.log; defaults to true. */
|
|
readonly hookLogging?: boolean;
|
|
/** Ordered list of enabled additive mods to overlay onto this build. */
|
|
readonly mods?: readonly DiscoveredMod[];
|
|
/** Stock bytes of optional packs (lang/mis/game/ui/*) touched by enabled mods or ground AI. */
|
|
readonly extraStockPacks?: Partial<Record<ModdableClientPack, Uint8Array>>;
|
|
/** Optional modular ground AI scripts and tuning compiled into `game.vromfs.bin` when active. */
|
|
readonly groundAi?: GroundAiBuildInputs;
|
|
}
|
|
|
|
/** 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 ModBuildReport {
|
|
readonly enabledModIds: readonly string[];
|
|
readonly packOverlays: readonly PackOverlayReport[];
|
|
readonly addonMounts: readonly ModAddonMount[];
|
|
readonly guiPatches: {
|
|
readonly applied: readonly { readonly modId: string; readonly patchId: string }[];
|
|
readonly failures: readonly (PatchFailure & { readonly modId: string })[];
|
|
};
|
|
readonly exePatches: {
|
|
readonly applied: readonly { readonly modId: string; readonly patchId: string; readonly rva: number }[];
|
|
readonly failures: readonly ModExePatchFailure[];
|
|
};
|
|
}
|
|
|
|
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;
|
|
/** Injected ground AI modules when `groundAi` is active; undefined when stock AI is kept. */
|
|
readonly gamePack?: Omit<GamePackResult, 'pack'>;
|
|
/** Details of additive mod overlays applied during this build. */
|
|
readonly mods: ModBuildReport;
|
|
}
|
|
|
|
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 enabledMods = inputs.mods ?? [];
|
|
const packOverlays: PackOverlayReport[] = [];
|
|
const groundAiActive = inputs.groundAi !== undefined && isGroundAiActive(inputs.groundAi.config);
|
|
|
|
const executable = patchExecutableWithMods(inputs.stock.executable, folders.exe, enabledMods);
|
|
const stockImage = new PeImage(inputs.stock.executable);
|
|
const customGroundDrive = groundAiActive && inputs.groundAi !== undefined && groundAiOwnsMovement(inputs.groundAi.config) &&
|
|
inputs.groundAi.config.navigation.driveMode === 'vehicle_phys';
|
|
const nativeDefinitions = loadNativeModules(folders.native)
|
|
.filter((module) => customGroundDrive || module.module !== 'local_mp_ground_ai');
|
|
const native = resolveNativeModules(stockImage, nativeDefinitions);
|
|
const core = buildCorePacks(inputs, endpoint, folders.gui, enabledMods, packOverlays);
|
|
|
|
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: core.charPack },
|
|
{ path: GAME_FILES.mainPack, content: core.mainPack },
|
|
{ path: GAME_FILES.guiPack, content: core.guiPack },
|
|
{ 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 gamePackReport = appendOptionalPackFiles(files, packOverlays, inputs, enabledMods, groundAiActive);
|
|
const failures = [...executable.plan.failures, ...core.guiFailures, ...native.failures];
|
|
return {
|
|
files,
|
|
endpoint,
|
|
exePatches: { applied: executable.plan.writes.map((write) => ({ patchId: write.patchId, rva: write.rva })), failures: executable.plan.failures },
|
|
guiPatches: { applied: core.appliedGuiPatches, failures: core.guiFailures },
|
|
nativeModules: { available: [...native.available.keys()], failures: native.failures },
|
|
blocking: blockingFailures(failures),
|
|
charPack: core.charReport,
|
|
engineAddresses: core.engineAddresses,
|
|
bundleGuids: core.bundleCount,
|
|
...(gamePackReport ? { gamePack: gamePackReport } : {}),
|
|
mods: {
|
|
enabledModIds: enabledMods.map((mod) => mod.manifest.id),
|
|
packOverlays,
|
|
addonMounts: core.addonMounts,
|
|
guiPatches: { applied: core.modGuiApplied, failures: core.modGuiFailures },
|
|
exePatches: { applied: executable.modApplied, failures: executable.modFailures },
|
|
},
|
|
};
|
|
}
|
|
|
|
function buildCorePacks(
|
|
inputs: GameCopyInputs,
|
|
endpoint: LocalEndpoint,
|
|
guiPatchFolder: string,
|
|
enabledMods: readonly DiscoveredMod[],
|
|
packOverlays: PackOverlayReport[],
|
|
) {
|
|
const coreGuiPack = rebuildGuiPack(inputs.stock.guiPack, endpoint, guiPatchFolder);
|
|
const appliedGuiPatches = coreGuiPack.applied.map((item) => item.patch.id);
|
|
const guiPackFileOverlay = applyModPackOverlays(coreGuiPack.pack, 'gui.vromfs.bin', enabledMods);
|
|
if (guiPackFileOverlay.report.operationsApplied > 0) packOverlays.push(guiPackFileOverlay.report);
|
|
const modGuiResult = applyModGuiPatches(guiPackFileOverlay.pack, endpoint, enabledMods);
|
|
|
|
const localRegistration = appliedGuiPatches.includes('registration/open-local-page') &&
|
|
appliedGuiPatches.includes('registration/keep-local-page-embedded');
|
|
const { pack: coreCharPack, ...charReport } = rebuildCharPack(inputs.stock.charPack, endpoint, {
|
|
placeholderDecals: inputs.placeholderDecals,
|
|
...(inputs.decalIndexFiles ? { decalIndexFiles: inputs.decalIndexFiles } : {}),
|
|
...(inputs.decalCache ? { decalCache: inputs.decalCache } : {}),
|
|
...(inputs.cachedDdsx ? { cachedDdsx: inputs.cachedDdsx } : {}),
|
|
localRegistration,
|
|
});
|
|
const charOverlay = applyModPackOverlays(coreCharPack, 'char.vromfs.bin', enabledMods);
|
|
if (charOverlay.report.operationsApplied > 0) packOverlays.push(charOverlay.report);
|
|
|
|
const bundles = offlinePackBundleGuids(charOverlay.pack, inputs.multiVehiclePackIds);
|
|
const addonMounts = collectModAddonMounts(enabledMods);
|
|
const coreMainPack = rebuildMainPack(
|
|
inputs.stock.mainPack,
|
|
endpoint,
|
|
bundles,
|
|
addonMounts.map((mount) => mount.mountFolder),
|
|
);
|
|
const mainOverlay = applyModPackOverlays(coreMainPack.pack, 'aces.vromfs.bin', enabledMods);
|
|
if (mainOverlay.report.operationsApplied > 0) packOverlays.push(mainOverlay.report);
|
|
|
|
return {
|
|
charPack: charOverlay.pack,
|
|
mainPack: mainOverlay.pack,
|
|
guiPack: modGuiResult.pack,
|
|
charReport,
|
|
appliedGuiPatches,
|
|
guiFailures: coreGuiPack.failures,
|
|
modGuiApplied: modGuiResult.applied,
|
|
modGuiFailures: modGuiResult.failures,
|
|
engineAddresses: coreMainPack.addresses,
|
|
bundleCount: bundles.length,
|
|
addonMounts,
|
|
};
|
|
}
|
|
|
|
function appendOptionalPackFiles(
|
|
files: GameCopyFile[],
|
|
packOverlays: PackOverlayReport[],
|
|
inputs: GameCopyInputs,
|
|
enabledMods: readonly DiscoveredMod[],
|
|
groundAiActive: boolean,
|
|
): Omit<GamePackResult, 'pack'> | undefined {
|
|
let gamePackReport: Omit<GamePackResult, 'pack'> | undefined;
|
|
if (groundAiActive && inputs.groundAi) {
|
|
const stockGameBytes = inputs.extraStockPacks?.[GAME_PACK_FILE];
|
|
if (!stockGameBytes) {
|
|
throw new Error(`ground AI requires stock pack "${GAME_PACK_FILE}" in extraStockPacks`);
|
|
}
|
|
const { pack: aiGamePack, ...report } = rebuildGamePack(stockGameBytes, inputs.groundAi);
|
|
const overlay = applyModPackOverlays(aiGamePack, GAME_PACK_FILE, enabledMods);
|
|
if (overlay.report.operationsApplied > 0) {
|
|
validateGroundAiGamePack(overlay.pack, report.modules);
|
|
packOverlays.push(overlay.report);
|
|
}
|
|
files.push({ path: GAME_PACK_FILE, content: overlay.pack });
|
|
gamePackReport = report;
|
|
}
|
|
|
|
for (const pack of touchedClientPacks(enabledMods)) {
|
|
if (CORE_BUILD_PACKS.has(pack)) continue;
|
|
if (groundAiActive && pack === GAME_PACK_FILE) continue;
|
|
const stockBytes = inputs.extraStockPacks?.[pack];
|
|
if (!stockBytes) {
|
|
throw new Error(`mod overlay requires stock pack "${pack}" in extraStockPacks`);
|
|
}
|
|
const overlay = applyModPackOverlays(stockBytes, pack, enabledMods);
|
|
if (overlay.report.operationsApplied > 0) {
|
|
packOverlays.push(overlay.report);
|
|
files.push({ path: join(...pack.split('/')), content: overlay.pack });
|
|
}
|
|
}
|
|
return gamePackReport;
|
|
}
|