build(repo): restore build tools and define the workspace layout
Restore the reviewed pack, PE, and patch tooling. Specify a TypeScript build pipeline, a type-preserving BLK codec, and a configurable loopback port selected at launch. Document the package boundaries.
This commit is contained in:
1 parent
7b6d8973d1
commit
89aa5198c3
25 files changed
+1155
No files matched your search
@@ -0,0 +1,80 @@
|
||||
"""Commands that work on the game executable.
|
||||
|
||||
wtotools check-patches --exe <aces.exe> [--patches exe-patches]
|
||||
wtotools patch-exe --exe <aces.exe> --out <aces.exe> [--patches exe-patches] [--force]
|
||||
|
||||
The input executable is only ever read. `patch-exe` refuses to write over its input and
|
||||
refuses an existing output unless --force is given, so it cannot modify a game install by
|
||||
accident.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from ..exepatch import apply_writes, changed_ranges, load_patches, plan_patches
|
||||
from ..exepatch.assemble import disagreement
|
||||
from ..pe import PeImage
|
||||
|
||||
DEFAULT_PATCHES = Path("exe-patches")
|
||||
|
||||
|
||||
def register(commands: argparse._SubParsersAction) -> None:
|
||||
check = commands.add_parser("check-patches", help="locate and verify every patch, write nothing")
|
||||
_add_common(check)
|
||||
check.set_defaults(handler=_check_patches)
|
||||
|
||||
patch = commands.add_parser("patch-exe", help="apply every patch to a copy of the executable")
|
||||
_add_common(patch)
|
||||
patch.add_argument("--out", type=Path, required=True, help="where to write the patched copy")
|
||||
patch.add_argument("--force", action="store_true", help="overwrite an existing output file")
|
||||
patch.set_defaults(handler=_patch_exe)
|
||||
|
||||
|
||||
def _add_common(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument("--exe", type=Path, required=True, help="the game executable to read")
|
||||
parser.add_argument("--patches", type=Path, default=DEFAULT_PATCHES, help="patch definition directory")
|
||||
|
||||
|
||||
def _check_patches(args: argparse.Namespace) -> int:
|
||||
definitions = load_patches(args.patches)
|
||||
print(f"{len(definitions)} patch definition(s) in {args.patches}")
|
||||
|
||||
mismatched = 0
|
||||
for definition in definitions:
|
||||
problem = disagreement(definition)
|
||||
if problem:
|
||||
mismatched += 1
|
||||
print(f" assembly disagrees with stored bytes: {definition.identifier}: {problem}")
|
||||
if not mismatched:
|
||||
print(" every patch's assembly source compiles to its stored bytes")
|
||||
|
||||
plan = plan_patches(PeImage(args.exe.read_bytes()), definitions)
|
||||
print(plan.describe())
|
||||
return 0 if plan.complete and not mismatched else 1
|
||||
|
||||
|
||||
def _patch_exe(args: argparse.Namespace) -> int:
|
||||
if args.out.resolve() == args.exe.resolve():
|
||||
print("refusing to write over the input executable; choose a different --out", file=sys.stderr)
|
||||
return 2
|
||||
if args.out.exists() and not args.force:
|
||||
print(f"{args.out} already exists; pass --force to overwrite", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
original = args.exe.read_bytes()
|
||||
plan = plan_patches(PeImage(original), load_patches(args.patches))
|
||||
print(plan.describe())
|
||||
if not plan.complete:
|
||||
print("some patches are unavailable; nothing was written", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
patched = apply_writes(original, plan.writes)
|
||||
for start, end in changed_ranges(original, patched):
|
||||
print(f" changed {end - start} byte(s) at file offset {start:#x}: "
|
||||
f"{original[start:end].hex()} -> {patched[start:end].hex()}")
|
||||
args.out.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.out.write_bytes(patched)
|
||||
print(f"wrote {args.out}")
|
||||
return 0
|
||||
Reference in new issue
Block a user