Files
WT-Offline/tools/src/wtotools/exepatch/bytepattern.py
T
N0VAN0M4LY b819912733 Let each patch carry variants written for different game builds
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.
2026-11-29 11:47:08 +08:00

60 lines
1.9 KiB
Python

"""Byte patterns as written in patch definitions: hex bytes separated by spaces, "??" for a byte
that is not fixed, e.g. "48 83 3d ?? ?? ?? ?? 00". Signatures, the expected original bytes of an
edit and the bytes an edit writes all use this form (packages/exepatch/src/byte-pattern.ts).
In a signature or an expected original a "??" matches any byte; in the bytes to write it is a hole
that a fixup fills (fixups.py).
"""
from __future__ import annotations
import re
from dataclasses import dataclass
from .errors import DefinitionError
WILDCARD = "??"
@dataclass(frozen=True)
class BytePattern:
data: bytes # 0 where the pattern has a wildcard
fixed: tuple[bool, ...]
def __len__(self) -> int:
return len(self.data)
@property
def complete(self) -> bool:
return all(self.fixed)
def matches(self, actual: bytes) -> bool:
return len(actual) == len(self.data) and all(
not fixed or byte == expected for byte, expected, fixed in zip(actual, self.data, self.fixed))
def __str__(self) -> str:
return " ".join(f"{byte:02x}" if fixed else WILDCARD for byte, fixed in zip(self.data, self.fixed))
def parse(text: object, where: str) -> BytePattern:
if not isinstance(text, str):
raise DefinitionError(f"{where} must be a string of hex bytes")
tokens = text.split()
if not tokens:
raise DefinitionError(f"{where} is empty")
data = bytearray()
fixed = []
for token in tokens:
if token == WILDCARD:
data.append(0)
fixed.append(False)
elif re.fullmatch(r"[0-9a-fA-F]{2}", token):
data.append(int(token, 16))
fixed.append(True)
else:
raise DefinitionError(f"{where}: token must be a hex byte or {WILDCARD!r}, got {token!r}")
return BytePattern(bytes(data), tuple(fixed))
def spaced(data: bytes) -> str:
return " ".join(f"{byte:02x}" for byte in data)