Add the executable patch engine and local archive compatibility

Keep patch definitions and dependencies separate from the patch engine.
Resolve all patches before writing, verify expected bytes, and refuse to
overwrite the input executable. Include a read-only check command for
comparing definitions with their assembly source.

Allow locally rebuilt script archives to load in the offline client.
Verify the patch against the 2.61.0.37 executable and confirm that the
patched copy remains readable as PE.
This commit is contained in:
N0VAN0M4LY committed 2026-11-23 13:32:36 +08:00
1 parent c9e10b0db7
commit 8fa50b7804
12 files changed
+701

No files matched your search

@@ -0,0 +1,27 @@
"""Ways to find a patch site in an executable.
One method per module, each exposing `locate(image, spec) -> list[int]` returning the
RVAs it matched. A method never picks a winner among several matches; the caller
requires exactly one and refuses otherwise.
"""
from __future__ import annotations
from collections.abc import Callable, Mapping
from ..errors import DefinitionError
from . import signature
Locator = Callable[..., list[int]]
_METHODS: Mapping[str, Locator] = {
signature.METHOD: signature.locate,
}
def method(name: str) -> Locator:
if name not in _METHODS:
raise DefinitionError(f"unknown locate method {name!r}; known methods: {', '.join(sorted(_METHODS))}")
return _METHODS[name]
__all__ = ["Locator", "method", "signature"]
@@ -0,0 +1,57 @@
"""Find a patch site by scanning for a byte pattern with wildcards.
A pattern is a hex string where "??" stands for any byte, for example:
"41 57 41 56 48 81 ec ?? ?? ?? ??"
Wildcards cover the bytes that legitimately differ between builds: addresses inside
rip-relative operands and the targets of relative calls and jumps. What is left is the
shape of the code, which is what identifies the function across versions.
This module only matches patterns; it needs no disassembler, which is why it is the part
that runs on the player's machine. Patterns are generated once on the development side
(see authoring.py) and stored in the patch definition.
"""
from __future__ import annotations
import re
from ..errors import DefinitionError
METHOD = "signature"
WILDCARD = "??"
def locate(image, *, pattern: str, section: str = ".text", limit: int = 8, **_unused) -> list[int]:
"""RVAs in `section` where `pattern` matches, at most `limit` of them."""
expression = compile_pattern(pattern)
data = image.section_bytes(section)
base = image.section(section).rva
hits = []
for match in expression.finditer(data):
hits.append(base + match.start())
if len(hits) >= limit:
break
return hits
def compile_pattern(pattern: str) -> re.Pattern[bytes]:
return re.compile(b"".join(_token_to_regex(token) for token in _tokens(pattern)), re.DOTALL)
def pattern_length(pattern: str) -> int:
return len(_tokens(pattern))
def _tokens(pattern: str) -> list[str]:
tokens = pattern.split()
if not tokens:
raise DefinitionError("signature pattern is empty")
for token in tokens:
if token != WILDCARD and not re.fullmatch(r"[0-9a-fA-F]{2}", token):
raise DefinitionError(f"signature pattern token must be a hex byte or {WILDCARD!r}, got {token!r}")
return tokens
def _token_to_regex(token: str) -> bytes:
return b"." if token == WILDCARD else re.escape(bytes.fromhex(token))