File size: 2,539 Bytes
5463b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Read a saved park through the pinned engine in an isolated verifier profile."""
import argparse
import json
import os
from pathlib import Path
import selectors
import shutil
import subprocess
import tempfile
import time


def reopen(park, engine="openrct2", rct2="/opt/rct2-assets"):
    with tempfile.TemporaryDirectory(prefix="t33-reopen-") as temp:
        profile=Path(temp)
        (profile/"plugin").mkdir()
        observer=Path(__file__).with_name("native-inspect.js")
        shutil.copy2(observer,profile/"plugin/observer.js")
        # The seed park is a converted copy; no RCT1 installation is needed.
        command=[engine,str(park.resolve()),"--headless","--rct2-data-path",rct2,
                 "--user-data-path",temp]
        child=subprocess.Popen(command,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,
                               bufsize=0)
        output=b""
        try:
            selector=selectors.DefaultSelector()
            selector.register(child.stdout,selectors.EVENT_READ)
            deadline=time.monotonic()+90
            while time.monotonic()<deadline:
                if not selector.select(timeout=1):
                    if child.poll() is not None:
                        break
                    continue
                block=os.read(child.stdout.fileno(),65536)
                if not block:
                    break
                output+=block
                for line in output.decode(errors="replace").splitlines():
                    if "T37JSON " in line:
                        try:
                            data,_=json.JSONDecoder().raw_decode(line.split("T37JSON ",1)[1])
                            if data.get("kind") == "load":
                                return data
                        except json.JSONDecodeError:
                            pass
            raise RuntimeError("Native park inspection failed: "+output.decode(errors="replace")[-2000:])
        finally:
            child.terminate()
            try:
                child.wait(timeout=5)
            except subprocess.TimeoutExpired:
                child.kill()
                child.wait()


if __name__ == "__main__":
    parser=argparse.ArgumentParser()
    parser.add_argument("park",type=Path)
    parser.add_argument("output",type=Path)
    parser.add_argument("--engine",default="openrct2")
    parser.add_argument("--rct2",default="/opt/rct2-assets")
    args=parser.parse_args()
    args.output.write_text(json.dumps(reopen(args.park,args.engine,args.rct2),indent=2)+"\n")