aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_game_source_edit.py (2937B)


      1 """Prove a generated game function can be edited, rebuilt, and executed."""
      2 
      3 import hashlib
      4 import json
      5 import os
      6 import pathlib
      7 import shutil
      8 import subprocess
      9 import sys
     10 
     11 
     12 root = pathlib.Path(__file__).resolve().parents[2]
     13 work = root / 'local/client/game-14.9.1'
     14 editable = root / 'client'
     15 source = editable / 'game/hr.dll/lifted-0000.c'
     16 obj = editable / 'build/windows/game/hr.dll/lifted-0000.o'
     17 executable = editable / 'build/windows/game.exe'
     18 original_source = source.read_bytes()
     19 original_object = obj.read_bytes()
     20 source_stat, object_stat = source.stat(), obj.stat()
     21 original_hash = hashlib.sha256(executable.read_bytes()).hexdigest()
     22 backup = work / 'game-source-edit-backup.exe'
     23 assert not backup.exists(), 'Restore the existing source-edit backup before repeating this test.'
     24 shutil.copy2(executable, backup)
     25 marker = '[source-edit] generated hr.dll entry executed'
     26 scope = json.loads((editable / 'scope.json').read_text())
     27 module = next(module for module in scope['game'] if module['name'] == 'hr.dll')
     28 entry = f'void sub_{module["entry"]:08X}(void) {{'.encode('ascii')
     29 assert original_source.count(entry) == 1
     30 make = root / 'toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/mingw32-make.exe'
     31 
     32 try:
     33     source.write_bytes(original_source.replace(entry, entry +
     34         ('\n    fputs("' + marker + '\\n", stderr);').encode('ascii')))
     35     command = [str(make), '-C', str(editable), '-j4']
     36     result = subprocess.run(command, capture_output=True, text=True)
     37     (work / 'source-edit-compile.log').write_text(subprocess.list2cmdline(command) + '\n' +
     38                                                 result.stdout + result.stderr)
     39     result.check_returncode()
     40     modified_hash = hashlib.sha256(executable.read_bytes()).hexdigest()
     41     subprocess.run([sys.executable, '-u', str(root / 'tools/client/verify_game.py'),
     42                     str(executable), str(root / 'anarchy-runtime-14.9.1')], check=True)
     43     report = json.loads((work / 'verification.json').read_text())
     44 
     45     assert marker in (work / f'run-{report["pid"]}.log').read_text()
     46     assert report['running_after_120_seconds']
     47     assert report['client_exited']
     48     assert modified_hash != original_hash
     49     report['original_executable_sha256'] = original_hash
     50     report['modified_executable_sha256'] = modified_hash
     51     report['executed_marker'] = marker
     52 finally:
     53     # Preserve the production sources and executable exactly, including on failure.
     54     source.write_bytes(original_source)
     55     obj.write_bytes(original_object)
     56     os.utime(source, ns=(source_stat.st_atime_ns, source_stat.st_mtime_ns))
     57     os.utime(obj, ns=(object_stat.st_atime_ns, object_stat.st_mtime_ns))
     58     shutil.copy2(backup, executable)
     59     assert hashlib.sha256(executable.read_bytes()).hexdigest() == original_hash
     60     backup.unlink()
     61 
     62 report['production_restored'] = True
     63 (work / 'source-edit-verification.json').write_text(json.dumps(report, indent=2))
     64 print(json.dumps(report, indent=2))