forked from flyinghead/flycast
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
219 lines (171 loc) · 5.77 KB
/
run.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import functools
import os
import subprocess
import shutil
import sys
import glob
import random
import time
import threading
import urllib.request
import shutil
from typing import List
from os.path import exists
from pathlib import Path
print = functools.partial(print, flush=True)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
N = int(os.getenv("N", 4))
V = int(os.getenv("V", 3))
TIMEOUT = int(os.getenv("TIMEOUT", 3600))
ITERATION = int(os.getenv("ITERATION", 1))
WIDE = int(os.getenv("WIDE", 0))
ROM = os.getenv("ROM", r"C:\rom\gdx-disc2\gdx-disc2.gdi")
FLYCAST = os.getenv("FLYCAST", r"R:\Temp\flycast.exe")
GDXSV = os.getenv("GDXSV", r"127.0.0.1")
REPLAY = os.getenv("REPLAY", "")
FLYCAST_NAME = Path(FLYCAST).name
X_OFFSET = 0
Y_OFFSET = 50
W = 854 if WIDE else 640
H = 480
def download_state():
os.makedirs(f"work/state", exist_ok=True)
if not os.path.isfile(f"work/state/gdx-disc1_99.state"):
urllib.request.urlretrieve("https://storage.googleapis.com/gdxsv/misc/gdx-disc1_99.state", f"work/state/gdx-disc1_99.state")
if not os.path.isfile(f"work/state/gdx-disc2_99.state"):
urllib.request.urlretrieve("https://storage.googleapis.com/gdxsv/misc/gdx-disc2_99.state", f"work/state/gdx-disc2_99.state")
def download_flycast(url: str):
cwd = os.getcwd()
os.makedirs(f"work", exist_ok=True)
os.chdir("work")
if exists("flycast.exe"):
os.remove("flycast.exe")
save_name = os.path.basename(url)
urllib.request.urlretrieve(url, save_name)
if save_name.endswith("zip"):
shutil.unpack_archive(save_name, ".")
if exists("flycast-gdxsv.exe"):
os.rename("flycast-gdxsv.exe", "flycast.exe")
assert exists("flycast.exe")
global FLYCAST
global FLYCAST_NAME
FLYCAST = "work/flycast.exe"
FLYCAST_NAME = "flycast.exe"
os.chdir(cwd)
def prepare_workdir(idx: int):
os.makedirs(f"work/flycast{idx}/data", exist_ok=True)
shutil.copy(Path(FLYCAST), f"work/flycast{idx}/")
for file in glob.glob(os.path.join("work/state", "*.state")):
if os.path.isfile(file):
shutil.copy(file, f"work/flycast{idx}/data")
return f"work/flycast{idx}"
def conf_log(idx: int):
return f"--config log:Verbosity={V} --config log:LogToFile=1"
def conf_volume(idx: int):
return f"--config config:aica.Volume=20"
def conf_gdxsv(idx: int):
return f"--config gdxsv:server={GDXSV}"
def conf_window_layout(idx: int):
x = X_OFFSET + W * (idx % 2)
y = Y_OFFSET + H * (idx // 2)
wide = "yes" if WIDE else "no"
return f"--config window:top={y} --config window:left={x} --config window:width={W} --config window:height={H} --config config:rend.WideScreen={wide} --config config:rend.WidescreenGameHacks={wide}"
def run(idx, *arg_list) -> subprocess.Popen:
cmd = " ".join(arg_list)
print(cmd)
new_env = os.environ.copy()
new_env["GGPO_NETWORK_DELAY"] = "16"
# new_env["GGPO_OOP_PERCENT"] = "1"
if idx == 0:
new_env["GGPO_NETWORK_JAM_DELAY"] = "500"
# else: new_env["GGPO_NETWORK_DELAY"] = "100"
return subprocess.Popen(cmd, shell=True, env=new_env)
def run_rom(idx: int) -> subprocess.Popen:
return run(idx,
FLYCAST_NAME, ROM,
conf_gdxsv(idx),
conf_volume(idx),
conf_log(idx),
conf_window_layout(idx),
)
def run_replay(idx: int) -> subprocess.Popen:
return run(idx,
FLYCAST_NAME, ROM,
conf_gdxsv(idx),
conf_volume(idx),
conf_window_layout(idx),
conf_log(idx),
f"--config gdxsv:replay={REPLAY}"
)
def run_rbk_test(idx: int) -> subprocess.Popen:
return run(idx,
FLYCAST_NAME, ROM,
conf_gdxsv(idx),
conf_volume(idx),
conf_window_layout(idx),
conf_log(idx),
f"--config gdxsv:rbk_test={idx+1}/{N}"
)
def run_rbk_test_random(idx: int) -> subprocess.Popen:
seed = random.randint(1, 99999)
return run(idx,
FLYCAST_NAME, ROM,
conf_gdxsv(idx),
conf_volume(idx),
conf_window_layout(idx),
conf_log(idx),
f"--config gdxsv:rbk_test={idx+1}/{N} --config gdxsv:rand_input={seed}"
)
def truncate(path: str):
with open(path, "w") as f:
f.truncate(0)
def tail(parent: subprocess.Popen, path: str):
with open(path, "r", encoding='utf-8') as f:
f.seek(0, 2)
while parent.poll() == None:
print(f.readline(), end="")
def exec_func(func_name: str):
start_time = time.time()
cwd = os.getcwd()
print(cwd)
func = {f.__name__[4:]: f for f in [
run_rom,
run_replay,
run_rbk_test,
run_rbk_test_random,
]}[func_name]
popens: List[subprocess.Popen] = []
try:
for i in reversed(range(N)):
wdir = prepare_workdir(i+1)
os.chdir(wdir)
truncate("flycast.log")
p = func(i)
if i == 0:
threading.Thread(target=tail, args=(p, "flycast.log"), name="tail", daemon=True).start()
popens.append(p)
os.chdir(cwd)
while popens[-1].poll() == None:
time.sleep(1)
if TIMEOUT < time.time() - start_time:
print("timeout")
break
finally:
for p in popens:
if os.name == 'nt':
subprocess.run(['taskkill', '/F', '/T', '/PID', str(p.pid)])
else:
p.kill()
for p in popens:
print(f"exit {p.poll()} {p.args}")
def main():
if FLYCAST.startswith("http"):
download_flycast(FLYCAST)
download_state()
for t in range(ITERATION):
print(f"===== ITERATION {t + 1}/{ITERATION} START =====")
exec_func(sys.argv[1])
print(f"===== ITERATION {t + 1}/{ITERATION} END =====")
if __name__ == "__main__":
main()