/** * Work out every write first; nothing is written here. * * Per patch: find the site, require exactly one match, check the bytes there are the ones the * definition expects. A patch failing any of these gets a reason instead of a write, and so * does every patch that depends on it, so a game update yields a list of unavailable patches * and the features they cover rather than a broken executable. */ import type { PatchDefinition } from './definition.ts'; import { DefinitionError, LocateError, PatchError, VerifyError } from './errors.ts'; import { locatorFor } from './locate/index.ts'; import type { PeImage } from './pe-image.ts'; export interface PlannedWrite { readonly patchId: string; readonly rva: number; readonly fileOffset: number; readonly payload: Uint8Array; readonly expectOriginal: Uint8Array; } export interface PatchFailure { readonly patchId: string; readonly features: readonly string[]; readonly reason: string; } export interface PatchPlan { readonly writes: readonly PlannedWrite[]; readonly failures: readonly PatchFailure[]; } export function planPatches(image: PeImage, definitions: readonly PatchDefinition[]): PatchPlan { const planned = new Map(); const failed = new Map(); for (const definition of definitions) { try { planned.set(definition.id, planOne(image, definition)); } catch (error) { if (!(error instanceof PatchError)) throw error; failed.set(definition.id, error.message); } } propagateFailures(definitions, failed); const writes = definitions.filter((item) => !failed.has(item.id)).map((item) => planned.get(item.id)!); rejectOverlaps(writes); 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 }; } 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 0x${write.rva.toString(16)}`); for (const failure of plan.failures) lines.push(` FAILED ${failure.patchId}: ${failure.reason}`); const features = [...new Set(plan.failures.flatMap((failure) => failure.features))].sort(); if (plan.failures.length) lines.push(` features affected: ${features.join(', ') || 'none recorded'}`); return lines.join('\n'); } function planOne(image: PeImage, definition: PatchDefinition): PlannedWrite { const hits = locatorFor(definition.locate.method)(image, definition.locate); if (!hits.length) throw new LocateError('site not found: this game build differs from the verified ones, or the executable is already patched'); if (hits.length > 1) throw new LocateError(`site found in ${hits.length} places, so it is ambiguous: ${hits.map(hex).join(', ')}`); const rva = hits[0]! + definition.edit.offset; const actual = image.read(rva, definition.edit.expectOriginal.length); if (!Buffer.from(actual).equals(definition.edit.expectOriginal)) { throw new VerifyError(`bytes at ${hex(rva)} are ${Buffer.from(actual).toString('hex')}, expected ` + `${Buffer.from(definition.edit.expectOriginal).toString('hex')} (different game build, or already patched)`); } return { patchId: definition.id, rva, fileOffset: image.fileOffset(rva), payload: definition.edit.payload, expectOriginal: definition.edit.expectOriginal }; } /** A patch whose prerequisite is unavailable cannot be applied either. */ function propagateFailures(definitions: readonly PatchDefinition[], failed: Map): void { for (let changed = true; changed;) { changed = false; for (const definition of definitions) { if (failed.has(definition.id)) continue; const blocking = definition.requires.filter((id) => failed.has(id)); if (blocking.length) { failed.set(definition.id, `depends on unavailable patch(es): ${blocking.join(', ')}`); changed = true; } } } } function rejectOverlaps(writes: readonly PlannedWrite[]): void { const sorted = [...writes].sort((left, right) => left.fileOffset - right.fileOffset); for (let index = 1; index < sorted.length; index++) { const earlier = sorted[index - 1]!; const later = sorted[index]!; if (later.fileOffset < earlier.fileOffset + earlier.payload.length) { throw new DefinitionError(`${earlier.patchId} and ${later.patchId} write overlapping bytes at ${hex(earlier.rva)} and ${hex(later.rva)}`); } } } function hex(value: number): string { return `0x${value.toString(16)}`; }