tools/client/verify_game.py (5011B)
1 """Run a stock or rebuilt game against the local server, then always stop it.""" 2 3 import hashlib 4 import html 5 import json 6 import os 7 import pathlib 8 import re 9 import socket 10 import struct 11 import subprocess 12 import sys 13 import time 14 import urllib.parse 15 import urllib.request 16 17 18 root = pathlib.Path(__file__).resolve().parents[2] 19 sys.path.insert(0, str(root)) 20 import tools.client.server_fixture 21 admin = tools.client.server_fixture.admin_login("http://127.0.0.1:8081") 22 work = root / "local/client/game-14.9.1" 23 client = pathlib.Path(sys.argv[2]).resolve() if len(sys.argv) > 2 else root / "anarchy-vanilla-14.9.1" 24 executable = pathlib.Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else work / "game.exe" 25 26 # Verify the version and immutable input binaries before running translated code. 27 assert (client / "version.id").read_text().strip() == "14.9.1" 28 if client == root / "anarchy-vanilla-14.9.1": 29 inventory = json.loads((work / "inventory.json").read_text()) 30 for module in inventory["modules"]: 31 assert hashlib.sha256((client / module["path"]).read_bytes()).hexdigest() == module["sha256"] 32 else: 33 scope = json.loads((root / "client/scope.json").read_text()) 34 assert not any((client / module["name"]).exists() for module in scope["game"]) 35 36 # Use a real synthetic session from the running development server. 37 with socket.create_connection(("127.0.0.1", 7510), timeout=10) as connection: 38 connection.sendall(struct.pack(">HHHHIII", 1, 1, 1, 20, 0, 1, 0x25)) 39 with connection.makefile("rb") as reader: 40 header = reader.read(16) 41 length = struct.unpack_from(">H", header, 6)[0] 42 body = reader.read(((length + 3) & ~3) - 16) 43 character_id = struct.unpack_from(">I", body, 12)[0] 44 45 # Match the approved stock Bronto Burger view and suppress first-run tutorials. 46 url = f"http://127.0.0.1:8081/admin/players/{character_id}" 47 page = admin.open(url).read().decode() 48 data = html.unescape(re.search(r"<textarea[^>]*>(.*?)</textarea>", page, re.S)[1]) 49 assert "return_door: null" in data 50 data = re.sub(r"(?m)^playfield:.*$", "playfield: 705", data) 51 data = re.sub(r"(?m)^position:.*$", "position: [960, 16.5, 510]", data) 52 data = re.sub(r"(?m)^rotation:.*$", "rotation: [0, 0.9238795, 0, -0.3826834]", data) 53 admin.open(url, urllib.parse.urlencode({"data": data}).encode()).read() 54 admin.open("http://127.0.0.1:8081/admin/time", b"hour=13&minute=30").read() 55 preferences = client / "Prefs/localtest" 56 character_preferences = preferences / f"Char{character_id}" 57 character_preferences.mkdir(exist_ok=True) 58 (character_preferences / "Char.cfg").write_text("IsFirstTime 0\nIsTipEnabled 0\n", encoding="ascii") 59 login_preferences = preferences / "Login.cfg" 60 login_preferences.write_text(login_preferences.read_text().replace( 61 "ShowTipOfTheDay 1", "ShowTipOfTheDay 0").replace("3rdPersonCamera 1", "3rdPersonCamera 0"), 62 encoding="ascii") 63 environment = os.environ.copy() 64 environment.update(ANARCHY_CD="cd_image", ANARCHY_SETUP="Setupf") 65 66 # This bounded probe also runs incomplete dispatch sets: a missing function is 67 # an explicit failure, useful for finding integration problems during the build. 68 (client / "stderr.txt").unlink(missing_ok=True) 69 with (work / "run.log").open("w") as log: 70 process = subprocess.Popen([ 71 str(executable), "localtest", "localtest", str(character_id), 72 "2130706433", "7511", "0", "0", 73 ], cwd=client, env=environment, stdout=log, stderr=log) 74 report = {"pid": process.pid, "character_id": character_id, 75 "started_at": time.time(), "executable": str(executable), 76 "executable_sha256": hashlib.sha256(executable.read_bytes()).hexdigest(), 77 "client": str(client), "version": "14.9.1", "playfield": 705, 78 "position": [960, 16.5, 510], "rotation": [0, 0.9238795, 0, -0.3826834]} 79 print(f"Verifying game PID {process.pid}", flush=True) 80 try: 81 (work / "verification.json").write_text(json.dumps(report, indent=2)) 82 # Allow time to inspect the rendered world and exercise movement. 83 deadline = time.monotonic() + 120 84 while process.poll() is None and time.monotonic() < deadline: 85 time.sleep(0.1) 86 report["running_after_120_seconds"] = process.poll() is None 87 finally: 88 if process.poll() is None: 89 process.terminate() 90 process.wait(timeout=10) 91 report["exit_code"] = process.returncode 92 report["client_exited"] = True 93 if (client / "stderr.txt").exists(): 94 (work / f"stderr-{process.pid}.log").write_bytes((client / "stderr.txt").read_bytes()) 95 report["elapsed_seconds"] = time.time() - report["started_at"] 96 (work / "verification.json").write_text(json.dumps(report, indent=2)) 97 print(json.dumps(report), flush=True) 98 99 # Preserve each observed run while keeping the latest result easy to inspect. 100 (work / f"run-{process.pid}.log").write_bytes((work / "run.log").read_bytes()) 101 (work / f"verification-{process.pid}.json").write_text(json.dumps(report, indent=2))