-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathmanager_gen.py
More file actions
190 lines (142 loc) · 6.53 KB
/
manager_gen.py
File metadata and controls
190 lines (142 loc) · 6.53 KB
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
#
# License: See LICENSE.md file
# GitHub: https://github.com/Baekalfen/PyBoy
#
import re
# Plugins and priority!
# E.g. DisableInput first
windows = ["WindowSDL2", "WindowOpenGL", "WindowNull", "Debug"]
game_wrappers = [
"GameWrapperSuperMarioLand", "GameWrapperTetris", "GameWrapperKirbyDreamLand", "GameWrapperPokemonGen1",
"GameWrapperPokemonPinball", "GameWrapperCastlevaniaTheAdventure"
]
plugins = [
"DisableInput", "AutoPause", "RecordReplay", "Rewind", "ScreenRecorder", "ScreenshotRecorder", "DebugPrompt"
] + game_wrappers
all_plugins = windows + plugins
def to_snake_case(s):
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", s)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
def skip_lines(iterator, stop):
# Skip old lines
while True:
if next(line_iter).strip().startswith(stop):
break
if __name__ == "__main__":
out_lines = []
with open("manager.py", "r") as f:
line_iter = iter(f.readlines())
while True:
line = next(line_iter, None)
if line is None:
break
# Find place to inject
if line.strip().startswith("# foreach"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# foreach")
skip_lines(line_iter, "# foreach end")
_, foreach, plugin_type, fun = line.strip().split(" ", 3)
for p in eval(plugin_type):
p_name = to_snake_case(p)
lines.append(f"if self.{p_name}_enabled:\n")
# lines.append(f" {var_name} = self.{p_name}\n")
for sub_fun in fun.split(", "):
sub_fun = sub_fun.replace("[]", f"self.{p_name}")
lines.append(f" {sub_fun}\n")
lines.append("# foreach end\n")
out_lines.extend([indentation + l for l in lines])
elif line.strip().startswith("# plugins_enabled"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# plugins_enabled")
skip_lines(line_iter, "# plugins_enabled end")
for p in all_plugins:
p_name = to_snake_case(p)
lines.append(f"self.{p_name} = {p}(pyboy, mb, pyboy_argv)\n")
lines.append(f"self.{p_name}_enabled = self.{p_name}.enabled()\n")
lines.append("# plugins_enabled end\n")
out_lines.extend([indentation + l for l in lines])
elif line.strip().startswith("# yield_plugins"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# yield_plugins")
skip_lines(line_iter, "# yield_plugins end")
for p in all_plugins:
p_name = to_snake_case(p)
lines.append(f"yield {p}.argv\n")
lines.append("# yield_plugins end\n")
out_lines.extend([indentation + l for l in lines])
elif line.strip().startswith("# imports"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# imports")
skip_lines(line_iter, "# imports end")
for p in all_plugins:
p_name = to_snake_case(p)
lines.append(f"from pyboy.plugins.{p_name} import {p} # isort:skip\n")
lines.append("# imports end\n")
out_lines.extend([indentation + l for l in lines])
elif line.strip().startswith("# gamewrapper"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# gamewrapper")
skip_lines(line_iter, "# gamewrapper end")
for p in game_wrappers:
p_name = to_snake_case(p)
lines.append(f"if self.{p_name}_enabled: return self.{p_name}\n")
lines.append("# gamewrapper end\n")
out_lines.extend([indentation + l for l in lines])
else:
out_lines.append(line)
with open("manager.py", "w") as f:
f.writelines(out_lines)
out_lines = []
with open("manager.pxd", "r") as f:
line_iter = iter(f.readlines())
while True:
line = next(line_iter, None)
if line is None:
break
# Find place to inject
if line.strip().startswith("# plugin_cdef"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# plugin_cdef")
skip_lines(line_iter, "# plugin_cdef end")
for p in all_plugins:
p_name = to_snake_case(p)
lines.append(f"cdef public {p} {p_name}\n")
for p in all_plugins:
p_name = to_snake_case(p)
lines.append(f"cdef bint {p_name}_enabled\n")
lines.append("# plugin_cdef end\n")
out_lines.extend([indentation + l for l in lines])
elif line.strip().startswith("# imports"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# imports")
skip_lines(line_iter, "# imports end")
for p in all_plugins:
p_name = to_snake_case(p)
lines.append(f"from pyboy.plugins.{p_name} cimport {p}\n")
lines.append("# imports end\n")
out_lines.extend([indentation + l for l in lines])
else:
out_lines.append(line)
with open("manager.pxd", "w") as f:
f.writelines(out_lines)
out_lines = []
with open("__init__.py", "r") as f:
line_iter = iter(f.readlines())
while True:
line = next(line_iter, None)
if line is None:
break
# Find place to inject
if line.strip().startswith("# docs exclude"):
lines = [line.strip() + "\n"]
indentation = " " * line.index("# docs exclude")
skip_lines(line_iter, "# docs exclude end")
for p in (set(all_plugins) - set(game_wrappers)) | set(["manager", "manager_gen"]):
p_name = to_snake_case(p)
lines.append(f"\"{p_name}\": False,\n")
lines.append("# docs exclude end\n")
out_lines.extend([indentation + l for l in lines])
else:
out_lines.append(line)
with open("__init__.py", "w") as f:
f.writelines(out_lines)