Add Quirrel script checks and a GUI build command
Compile the upstream Quirrel checker with MSVC and invoke syntax and static analysis for each input file. Treat reported errors and warnings as failures because the checker can return a zero exit code for them. Split the command line by purpose and add GUI pack construction from declared edits and injected scripts. Verify the checker against stock scripts and invalid inputs, and confirm that patch checks still run.
This commit is contained in:
1 parent
8fa50b7804
commit
b6d72f4792
8 files changed
+290
-16
No files matched your search
@@ -0,0 +1,24 @@
|
||||
"""Command line entry point for the development-side tools.
|
||||
|
||||
Each group of commands lives in its own module and registers itself here:
|
||||
- exe_commands: check-patches, patch-exe
|
||||
- build_commands: build-gui
|
||||
- script_commands: build-script-checker, check-scripts
|
||||
|
||||
Run `wtotools <command> --help` for the arguments of one command.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
from . import build_commands, exe_commands, script_commands
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="wtotools", description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
commands = parser.add_subparsers(dest="command", required=True)
|
||||
for group in (exe_commands, build_commands, script_commands):
|
||||
group.register(commands)
|
||||
args = parser.parse_args(argv)
|
||||
return args.handler(args)
|
||||
@@ -0,0 +1,3 @@
|
||||
from . import main
|
||||
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Commands that produce deployable files.
|
||||
|
||||
wtotools build-gui --stock <gui.vromfs.bin> --out <gui.vromfs.bin> [--client client] [--force]
|
||||
|
||||
Inputs, all under the client folder:
|
||||
- overrides/ edit records for stock files (see wtotools.build.overrides)
|
||||
- gui/ our own files, laid out exactly as their path inside gui.vromfs.bin
|
||||
|
||||
The stock archive is only read. The output must be a different file, and an existing output
|
||||
is replaced only with --force.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from ..build import BuildError, OverrideError, build_script_archive, load_overrides
|
||||
from ..vromfs.errors import VromfsFormatError
|
||||
|
||||
GUI_ARCHIVE = "gui.vromfs.bin"
|
||||
DEFAULT_CLIENT = Path("client")
|
||||
|
||||
|
||||
def register(commands: argparse._SubParsersAction) -> None:
|
||||
gui = commands.add_parser("build-gui", help="build gui.vromfs.bin from the stock one plus the client folder")
|
||||
gui.add_argument("--stock", type=Path, required=True, help="the stock gui.vromfs.bin to read")
|
||||
gui.add_argument("--out", type=Path, required=True, help="where to write the built archive")
|
||||
gui.add_argument("--client", type=Path, default=DEFAULT_CLIENT, help="folder with overrides/ and gui/")
|
||||
gui.add_argument("--force", action="store_true", help="overwrite an existing output file")
|
||||
gui.set_defaults(handler=_build_gui)
|
||||
|
||||
|
||||
def _build_gui(args: argparse.Namespace) -> int:
|
||||
if args.out.resolve() == args.stock.resolve():
|
||||
print("refusing to write over the stock archive; 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
|
||||
|
||||
try:
|
||||
overrides = load_overrides(args.client / "overrides")
|
||||
added = _added_files(args.client / "gui")
|
||||
built, report = build_script_archive(GUI_ARCHIVE, args.stock.read_bytes(), overrides, added)
|
||||
except (BuildError, OverrideError, VromfsFormatError) as error:
|
||||
print(f"build failed, nothing was written: {error}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
args.out.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.out.write_bytes(built)
|
||||
print(report.summary)
|
||||
for path in report.overridden:
|
||||
print(f" edited {path}")
|
||||
for path in report.added:
|
||||
print(f" added {path}")
|
||||
print(f"wrote {args.out}")
|
||||
return 0
|
||||
|
||||
|
||||
def _added_files(root: Path) -> dict[str, bytes]:
|
||||
if not root.is_dir():
|
||||
return {}
|
||||
return {path.relative_to(root).as_posix(): path.read_bytes() for path in sorted(root.rglob("*")) if path.is_file()}
|
||||
@@ -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
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Commands for syntax checking game scripts.
|
||||
|
||||
wtotools build-script-checker [--source build/quirrel-source] [--out build/quirrel-check]
|
||||
wtotools check-scripts [--checker build/quirrel-check/sq.exe] <file or folder>...
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from ..scriptcheck import CheckerBuildError, build_checker, check_scripts, find_scripts
|
||||
|
||||
DEFAULT_SOURCE = Path("build/quirrel-source")
|
||||
DEFAULT_OUT = Path("build/quirrel-check")
|
||||
|
||||
|
||||
def register(commands: argparse._SubParsersAction) -> None:
|
||||
build = commands.add_parser("build-script-checker", help="compile the Quirrel syntax checker with MSVC")
|
||||
build.add_argument("--source", type=Path, default=DEFAULT_SOURCE, help="Quirrel source checkout")
|
||||
build.add_argument("--out", type=Path, default=DEFAULT_OUT, help="where to put sq.exe")
|
||||
build.set_defaults(handler=_build)
|
||||
|
||||
check = commands.add_parser("check-scripts", help="syntax-check .nut files")
|
||||
check.add_argument("paths", type=Path, nargs="+", help=".nut files or folders containing them")
|
||||
check.add_argument("--checker", type=Path, default=DEFAULT_OUT / "sq.exe", help="the compiled checker")
|
||||
check.set_defaults(handler=_check)
|
||||
|
||||
|
||||
def _build(args: argparse.Namespace) -> int:
|
||||
try:
|
||||
checker = build_checker(args.source, args.out)
|
||||
except CheckerBuildError as error:
|
||||
print(error, file=sys.stderr)
|
||||
return 1
|
||||
print(f"built {checker}")
|
||||
return 0
|
||||
|
||||
|
||||
def _check(args: argparse.Namespace) -> int:
|
||||
if not args.checker.is_file():
|
||||
print(f"checker not found at {args.checker}; run build-script-checker first", file=sys.stderr)
|
||||
return 2
|
||||
files = find_scripts(args.paths)
|
||||
failures = check_scripts(args.checker, files)
|
||||
for failure in failures:
|
||||
print(f"FAIL {failure.path}\n {failure.message}")
|
||||
print(f"{len(files) - len(failures)}/{len(files)} file(s) pass syntax and static checks")
|
||||
return 1 if failures else 0
|
||||
Reference in new issue
Block a user