Files
WT-Offline/packages/patcher/src/executable.ts
T

109 lines
3.4 KiB
TypeScript

/**
* Patch a copy of aces.exe with every definition under the core exe patch folder, and optionally
* apply declarative mod exe patches on top while keeping every core patch byte range locked.
*/
import { join } from 'node:path';
import {
applyWrites,
describePlan,
loadPatches,
PeImage,
planAdditionalPatches,
planPatches,
type PatchDefinition,
type PatchFailure,
type PatchPlan,
type PlannedWrite,
} from '@wto/exepatch';
import type { DiscoveredMod } from '@wto/mods';
export interface ExecutableResult {
/** The executable with every available patch written. */
readonly executable: Uint8Array;
readonly plan: PatchPlan;
readonly description: string;
}
export interface ModExePatchFailure extends PatchFailure {
readonly modId: string;
}
export interface ModExecutableResult extends ExecutableResult {
readonly modApplied: readonly { readonly modId: string; readonly patchId: string; readonly rva: number }[];
readonly modFailures: readonly ModExePatchFailure[];
}
export function patchExecutable(stockExecutable: Uint8Array, patchesRoot: string): ExecutableResult {
const plan = planPatches(new PeImage(stockExecutable), loadPatches(patchesRoot));
return { executable: applyWrites(stockExecutable, plan.writes), plan, description: describePlan(plan) };
}
export function patchExecutableWithMods(
stockExecutable: Uint8Array,
corePatchesRoot: string,
mods: readonly DiscoveredMod[],
): ModExecutableResult {
const image = new PeImage(stockExecutable);
const corePlan = planPatches(image, loadPatches(corePatchesRoot));
const modsWithExe = mods.filter((mod) => mod.content.exePatchDirs.length > 0);
if (modsWithExe.length === 0) {
return {
executable: applyWrites(stockExecutable, corePlan.writes),
plan: corePlan,
description: describePlan(corePlan),
modApplied: [],
modFailures: [],
};
}
const allModDefs: PatchDefinition[] = [];
const ownerById = new Map<string, string>();
const loadFailures: ModExePatchFailure[] = [];
for (const mod of modsWithExe) {
const exeRoot = join(mod.rootDir, 'client', 'exe-patches');
try {
const defs = loadPatches(exeRoot);
for (const def of defs) {
const namespacedId = `${mod.manifest.id}/${def.id}`;
ownerById.set(namespacedId, mod.manifest.id);
allModDefs.push({
...def,
id: namespacedId,
requires: def.requires.map((req) => (req.includes('/') ? req : `${mod.manifest.id}/${req}`)),
});
}
} catch (error) {
loadFailures.push({
modId: mod.manifest.id,
patchId: `${mod.manifest.id}:exe-patches`,
features: [],
reason: error instanceof Error ? error.message : String(error),
});
}
}
const modPlan = planAdditionalPatches(image, allModDefs, corePlan.writes);
const combinedWrites: readonly PlannedWrite[] = [...corePlan.writes, ...modPlan.writes];
const modApplied = modPlan.writes.map((write) => ({
modId: ownerById.get(write.patchId) ?? 'unknown',
patchId: write.patchId,
rva: write.rva,
}));
const modFailures: ModExePatchFailure[] = [
...loadFailures,
...modPlan.failures.map((failure) => ({
...failure,
modId: ownerById.get(failure.patchId) ?? 'unknown',
})),
];
return {
executable: applyWrites(stockExecutable, combinedWrites),
plan: corePlan,
description: describePlan(corePlan),
modApplied,
modFailures,
};
}