feat(patcher): support additive mod pack overlays, addon mounts, and protected exe patches

This commit is contained in:
N0VAN0M4LY committed 2026-12-02 23:48:34 +08:00
1 parent 8fbaeb4228
commit fd9d25d346
11 files changed
+1088 -24

No files matched your search

+94 -5
View File
@@ -1,10 +1,21 @@
/**
* Patch a copy of aces.exe with every definition under the exe patch folder. A patch that cannot
* be applied (site missing or ambiguous, original bytes differ, or a patch it requires failed) is
* left out and listed with the features it covers; whether a copy with
* patches missing may be installed is the caller's decision (essential-features.ts).
* 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 { applyWrites, describePlan, loadPatches, PeImage, planPatches, type PatchPlan } from '@wto/exepatch';
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. */
@@ -13,7 +24,85 @@ export interface ExecutableResult {
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,
};
}