Files
WT-Offline/packages/exepatch/src/variants.ts
T
N0VAN0M4LY 927201a462 feat(patches)!: support game-version variants in patch definitions
A game update used to fail a patch whenever any byte it compared moved, even
when only an address in the instruction changed. Patch definitions now say
what a change needs and read the rest from the code:

- patch.jsonc holds a list of variants, each written for one game build
  (written_for) with its own locate block and edit; locate.jsonc is merged
  into it. Exactly one variant must apply, otherwise the patch is left out.
  GUI script patches use the same variants rule.
- expect_original and the bytes to write use the signature syntax, so
  addresses that differ between builds can be wildcards.
- A locate block can capture values at the site (rel32 targets, immediates,
  displacements); a rel32 fixup fills a hole in the written bytes from a
  capture. allow-local-host-bot-spawns now jumps to the target of the
  conditional jump it replaces instead of a stored distance, which makes it
  apply to 2.61.0.43 as well.
- verified_on is gone from the definitions. patches/versions/<version>.jsonc
  records, per checked game build, which variant of every patch applies and
  where; scripts/record-game-version.ts writes it and the launcher reads the
  recorded versions from there.

The Python tools (check-patches, patch-exe, check-gui) read the same format;
check-patches assembles a variant whose assembly names captures at the site
it was located at.

Checked: root and launcher type checks; check-patches and check-gui on
2.61.0.37 and 2.61.0.43 (12 exe and 35 GUI patches each); check-patches and
check-gui report a deliberately broken patch with a non-zero exit;
build-game-copy from 2.61.0.43 into a temporary folder. The computed bytes of
allow-local-host-bot-spawns on 2.61.0.37 equal the bytes it stored before.

BREAKING CHANGE: Each patch needs a variants list. Move executable locators into patch.jsonc and regenerate game version records with record-game-version.ts.
2026-11-29 11:47:08 +08:00

38 lines
1.5 KiB
TypeScript

/**
* Choosing the variant of a patch that fits the game files at hand. Every variant is tried; the
* patch applies only when exactly one of them does. None applying means the build differs from
* every build a variant was written for; two applying means the variants do not tell builds
* apart, which is a definition bug, so the patch is refused rather than guessed.
*
* Used for exe patches here and for GUI script patches in @wto/patcher.
*/
import { LocateError, PatchError } from './errors.ts';
export interface Variant {
readonly writtenFor: string;
}
export interface Chosen<V extends Variant, T> {
readonly variant: V;
readonly value: T;
}
/** `attempt` returns the variant's result, or throws a PatchError saying why it does not apply. */
export function chooseVariant<V extends Variant, T>(variants: readonly V[], attempt: (variant: V) => T): Chosen<V, T> {
const applied: Chosen<V, T>[] = [];
const reasons: string[] = [];
for (const variant of variants) {
try {
applied.push({ variant, value: attempt(variant) });
} catch (error) {
if (!(error instanceof PatchError)) throw error;
reasons.push(variants.length === 1 ? error.message : `variant ${variant.writtenFor}: ${error.message}`);
}
}
if (applied.length > 1) {
throw new LocateError(`variants ${applied.map((item) => item.variant.writtenFor).join(' and ')} all apply, so the choice is ambiguous`);
}
if (!applied.length) throw new LocateError(reasons.join('; '));
return applied[0]!;
}