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

+1 -1
View File
@@ -13,6 +13,6 @@ export { DefinitionError, LocateError, PatchError, PeFormatError, VerifyError }
export { asObject, readJsonObject, rejectUnknownKeys, requiredString, stringList, variantObjects } from './json-fields.ts';
export { NO_CONTEXT, type LocateContext } from './locate/index.ts';
export { PeImage, type FunctionRange, type Section } from './pe-image.ts';
export { describePlan, planPatches, type PatchFailure, type PatchPlan, type PlannedWrite } from './plan.ts';
export { describePlan, planAdditionalPatches, planPatches, type PatchFailure, type PatchPlan, type PlannedWrite } from './plan.ts';
export { findSite, readSiteSpec, type FoundSite, type SiteSpec } from './site.ts';
export { chooseVariant, type Chosen, type Variant } from './variants.ts';
+61
View File
@@ -58,6 +58,67 @@ export function planPatches(image: PeImage, definitions: readonly PatchDefinitio
return { writes, failures };
}
/**
* Plan additive patches (such as declarative mod exe patches) on top of locked core writes.
* Any patch that fails to locate/verify, depends on a failed patch, overlaps a protected core
* write, or overlaps an earlier accepted additive write is recorded in `failures` rather than
* throwing, so core offline isolation patches are never overwritten or broken.
*/
export function planAdditionalPatches(
image: PeImage,
definitions: readonly PatchDefinition[],
protectedWrites: readonly PlannedWrite[],
): PatchPlan {
const planned = new Map<string, PlannedWrite>();
const failed = new Map<string, string>();
for (const definition of definitions) {
try {
const chosen = chooseVariant(definition.variants, (variant) => planVariant(image, variant));
planned.set(definition.id, { patchId: definition.id, variant: chosen.variant.writtenFor, ...chosen.value });
} catch (error) {
if (!(error instanceof PatchError)) throw error;
failed.set(definition.id, error.message);
}
}
propagateFailures(definitions, failed);
const accepted: PlannedWrite[] = [];
for (const definition of definitions) {
if (failed.has(definition.id)) continue;
const candidate = planned.get(definition.id)!;
const coreConflict = findOverlappingWrite(candidate, protectedWrites);
if (coreConflict) {
failed.set(definition.id, `overlaps protected core patch "${coreConflict.patchId}" at ${hex(candidate.rva)}`);
continue;
}
const earlierConflict = findOverlappingWrite(candidate, accepted);
if (earlierConflict) {
failed.set(definition.id, `overlaps earlier patch "${earlierConflict.patchId}" at ${hex(candidate.rva)}`);
continue;
}
accepted.push(candidate);
}
// Re-propagate if an overlap rejection broke a downstream dependency.
const beforeSecondPass = failed.size;
propagateFailures(definitions, failed);
const writes = failed.size === beforeSecondPass
? accepted
: accepted.filter((write) => !failed.has(write.patchId));
const failures = definitions.filter((item) => failed.has(item.id))
.map((item) => ({ patchId: item.id, features: item.features, reason: failed.get(item.id)! }));
return { writes, failures };
}
function findOverlappingWrite(candidate: PlannedWrite, existing: readonly PlannedWrite[]): PlannedWrite | undefined {
const cStart = candidate.fileOffset;
const cEnd = cStart + candidate.payload.length;
return existing.find((write) => {
const wStart = write.fileOffset;
const wEnd = wStart + write.payload.length;
return cStart < wEnd && wStart < cEnd;
});
}
export function describePlan(plan: PatchPlan): string {
const lines = [`${plan.writes.length} patch(es) ready, ${plan.failures.length} unavailable`];
for (const write of plan.writes) lines.push(` ready ${write.patchId} at ${hex(write.rva)} (variant ${write.variant})`);