-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.py
More file actions
350 lines (301 loc) · 12.4 KB
/
config.py
File metadata and controls
350 lines (301 loc) · 12.4 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import tomllib
from pathlib import Path
from typing import Any, Literal
import rlbot.flat as flat
from rlbot.utils.logging import DEFAULT_LOGGER as logger
from rlbot.utils.os_detector import CURRENT_OS, OS
class ConfigParsingException(Exception):
pass
def __enum(table: dict[str, str | Any], key: str, enum: Any, default: int = 0) -> Any:
if key not in table:
return enum(default)
try:
for i in range(100000):
if str(enum(i)).split(".")[-1].lower() == table[key].lower():
return enum(i)
except ValueError:
raise ConfigParsingException(
f"Invalid value {repr(table[key])} for key '{key}'."
)
def __str(table: dict[str, str | Any], key: str, default: str = "") -> str:
v = table.get(key, default)
if isinstance(v, str):
return v
raise ConfigParsingException(f"'{key}' has value {repr(v)}. Expected a string.")
def __bool(table: dict[str, bool | Any], key: str, default: bool = False) -> bool:
v = table.get(key, default)
if isinstance(v, bool):
return v
raise ConfigParsingException(f"'{key}' has value {repr(v)}. Expected a bool.")
def __int(table: dict[str, int | Any], key: str, default: int = 0) -> int:
v = table.get(key, default)
if isinstance(v, int):
return v
raise ConfigParsingException(f"'{key}' has value {repr(v)}. Expected an int.")
def __table(table: dict[str, dict[str, Any] | Any], key: str) -> dict[str, Any]:
v = table.get(key, dict())
if isinstance(v, dict):
return v # type: ignore
raise ConfigParsingException(f"'{key}' has value {repr(v)}. Expected a table.")
def __team(table: dict[str, Literal["blue", "orange", 0, 1] | Any]) -> int:
if "team" not in table:
return 0
v = table["team"]
if isinstance(v, str):
if v.lower() == "blue":
return 0
if v.lower() == "orange":
return 1
if isinstance(v, int):
if 0 <= v <= 1:
return v
raise ConfigParsingException(
f'\'team\' has value {repr(v)}. Expected a 0, 1, "blue", or "orange".'
)
def load_match_config(config_path: Path | str) -> flat.MatchConfiguration:
"""
Reads the match toml file at the provided path and creates the corresponding MatchConfiguration.
"""
config_path = Path(config_path)
with open(config_path, "rb") as f:
config = tomllib.load(f)
rlbot_table = __table(config, "rlbot")
match_table = __table(config, "match")
mutator_table = __table(config, "mutators")
players: list[flat.PlayerConfiguration] = []
for car_table in config.get("cars", []):
car_config = __str(car_table, "config_file")
name = __str(car_table, "name")
team = __team(car_table)
loadout_file = __str(car_table, "loadout_file") or None
skill = __enum(
car_table, "skill", flat.PsyonixSkill, int(flat.PsyonixSkill.AllStar)
)
variant = __str(car_table, "type", "rlbot").lower()
match variant:
case "rlbot":
abs_config_path = (config_path.parent / car_config).resolve()
players.append(
load_player_config(abs_config_path, team, name, loadout_file)
)
case "psyonix":
abs_config_path = (
(config_path.parent / car_config).resolve() if car_config else None
)
players.append(
load_psyonix_config(
team,
skill,
name,
loadout_file,
abs_config_path,
)
)
case "human":
players.append(flat.PlayerConfiguration(flat.Human(), team, 0))
case t:
raise ConfigParsingException(
f"Invalid player type {repr(t)} for player {len(players)}."
)
scripts: list[flat.ScriptConfiguration] = []
for script_table in config.get("scripts", []):
if script_config := __str(script_table, "config_file"):
abs_config_path = (config_path.parent / script_config).resolve()
scripts.append(load_script_config(abs_config_path))
else:
scripts.append(flat.ScriptConfiguration())
mutators = flat.MutatorSettings(
match_length=__enum(mutator_table, "match_length", flat.MatchLengthMutator),
max_score=__enum(mutator_table, "max_score", flat.MaxScoreMutator),
multi_ball=__enum(mutator_table, "multi_ball", flat.MultiBallMutator),
overtime=__enum(mutator_table, "overtime", flat.OvertimeMutator),
series_length=__enum(mutator_table, "series_length", flat.SeriesLengthMutator),
game_speed=__enum(mutator_table, "game_speed", flat.GameSpeedMutator),
ball_max_speed=__enum(
mutator_table, "ball_max_speed", flat.BallMaxSpeedMutator
),
ball_type=__enum(mutator_table, "ball_type", flat.BallTypeMutator),
ball_weight=__enum(mutator_table, "ball_weight", flat.BallWeightMutator),
ball_size=__enum(mutator_table, "ball_size", flat.BallSizeMutator),
ball_bounciness=__enum(
mutator_table, "ball_bounciness", flat.BallBouncinessMutator
),
boost_amount=__enum(mutator_table, "boost_amount", flat.BoostAmountMutator),
rumble=__enum(mutator_table, "rumble", flat.RumbleMutator),
boost_strength=__enum(
mutator_table, "boost_strength", flat.BoostStrengthMutator
),
gravity=__enum(mutator_table, "gravity", flat.GravityMutator),
demolish=__enum(mutator_table, "demolish", flat.DemolishMutator),
respawn_time=__enum(mutator_table, "respawn_time", flat.RespawnTimeMutator),
max_time=__enum(mutator_table, "max_time", flat.MaxTimeMutator),
game_event=__enum(mutator_table, "game_event", flat.GameEventMutator),
audio=__enum(mutator_table, "audio", flat.AudioMutator),
)
try:
enable_rendering = (
flat.DebugRendering.OnByDefault
if __bool(match_table, "enable_rendering")
else flat.DebugRendering.OffByDefault
)
except ConfigParsingException:
enable_rendering = __enum(
match_table, "enable_rendering", flat.DebugRendering.AlwaysOff
)
return flat.MatchConfiguration(
launcher=__enum(rlbot_table, "launcher", flat.Launcher),
launcher_arg=__str(rlbot_table, "launcher_arg"),
auto_start_agents=__bool(rlbot_table, "auto_start_agents", True),
wait_for_agents=__bool(rlbot_table, "wait_for_agents", True),
game_map_upk=__str(match_table, "game_map_upk"),
player_configurations=players,
script_configurations=scripts,
game_mode=__enum(match_table, "game_mode", flat.GameMode),
skip_replays=__bool(match_table, "skip_replays"),
instant_start=__bool(match_table, "instant_start"),
mutators=mutators,
existing_match_behavior=__enum(
match_table, "existing_match_behavior", flat.ExistingMatchBehavior
),
enable_rendering=enable_rendering,
enable_state_setting=__bool(match_table, "enable_state_setting"),
freeplay=__bool(match_table, "freeplay"),
)
def load_player_loadout(path: Path | str, team: int) -> flat.PlayerLoadout:
"""
Reads the loadout toml file at the provided path and extracts the `PlayerLoadout` for the given team.
"""
with open(path, "rb") as f:
config = tomllib.load(f)
table_name = "blue_loadout" if team == 0 else "orange_loadout"
loadout = __table(config, table_name)
paint = None
if paint_table := __table(loadout, "paint"):
paint = flat.LoadoutPaint(
car_paint_id=__int(paint_table, "car_paint_id"),
decal_paint_id=__int(paint_table, "decal_paint_id"),
wheels_paint_id=__int(paint_table, "wheels_paint_id"),
boost_paint_id=__int(paint_table, "boost_paint_id"),
antenna_paint_id=__int(paint_table, "antenna_paint_id"),
hat_paint_id=__int(paint_table, "hat_paint_id"),
trails_paint_id=__int(paint_table, "trails_paint_id"),
goal_explosion_paint_id=__int(paint_table, "goal_explosion_paint_id"),
)
return flat.PlayerLoadout(
team_color_id=__int(loadout, "team_color_id"),
custom_color_id=__int(loadout, "custom_color_id"),
car_id=__int(loadout, "car_id"),
decal_id=__int(loadout, "decal_id"),
wheels_id=__int(loadout, "wheels_id"),
boost_id=__int(loadout, "boost_id"),
antenna_id=__int(loadout, "antenna_id"),
hat_id=__int(loadout, "hat_id"),
paint_finish_id=__int(loadout, "paint_finish_id"),
custom_finish_id=__int(loadout, "custom_finish_id"),
engine_audio_id=__int(loadout, "engine_audio_id"),
trails_id=__int(loadout, "trails_id"),
goal_explosion_id=__int(loadout, "goal_explosion_id"),
loadout_paint=paint,
)
def load_player_config(
path: Path | str,
team: int,
name_override: str | None = None,
loadout_override: Path | str | None = None,
) -> flat.PlayerConfiguration:
"""
Reads the bot toml file at the provided path and
creates a `PlayerConfiguration` of the given type for the given team.
"""
path = Path(path)
with open(path, "rb") as f:
config = tomllib.load(f)
settings = __table(config, "settings")
root_dir = path.parent.absolute()
if "root_dir" in settings:
root_dir /= Path(__str(settings, "root_dir"))
run_command = __str(settings, "run_command")
if CURRENT_OS == OS.LINUX and "run_command_linux" in settings:
run_command = __str(settings, "run_command_linux")
loadout_path = (
path.parent / Path(__str(settings, "loadout_file"))
if "loadout_file" in settings
else None
)
loadout_path = loadout_override or loadout_path
loadout = (
load_player_loadout(loadout_path, team) if loadout_path is not None else None
)
return flat.PlayerConfiguration(
flat.CustomBot(
name_override or __str(settings, "name"),
str(root_dir),
run_command,
loadout,
__str(settings, "agent_id"),
__bool(settings, "hivemind"),
),
team,
0,
)
def load_psyonix_config(
team: int,
skill_level: flat.PsyonixSkill,
name_override: str | None = None,
loadout_override: Path | str | None = None,
path: Path | str | None = None,
) -> flat.PlayerConfiguration:
"""
Creates a `PlayerConfiguration` for a Psyonix bot of the given team and skill.
If a path is provided, it will be used override the default name and loadout.
"""
name = name_override
loadout_path = loadout_override
# Don't parse the toml file if we have no data we need to extract,
# even if a path to a toml file is provided.
if path is not None and (name is None or loadout_path is None):
path = Path(path)
with open(path, "rb") as f:
config = tomllib.load(f)
settings = __table(config, "settings")
if name is None:
name = __str(settings, "name")
if loadout_path is None:
loadout_path = (
path.parent / Path(__str(settings, "loadout_file"))
if "loadout_file" in settings
else None
)
loadout = (
load_player_loadout(loadout_path, team) if loadout_path is not None else None
)
return flat.PlayerConfiguration(
flat.PsyonixBot(
name or "",
loadout,
skill_level,
),
team,
0,
)
def load_script_config(path: Path | str) -> flat.ScriptConfiguration:
"""
Reads the script toml file at the provided path and creates a `ScriptConfiguration` from it.
"""
path = Path(path)
with open(path, "rb") as f:
config = tomllib.load(f)
settings: dict[str, Any] = config["settings"]
root_dir = path.parent
if "root_dir" in settings:
root_dir /= Path(__str(settings, "root_dir"))
run_command = __str(settings, "run_command")
if CURRENT_OS == OS.LINUX and "run_command_linux" in settings:
run_command = __str(settings, "run_command_linux")
return flat.ScriptConfiguration(
__str(settings, "name"),
str(root_dir),
run_command,
0,
__str(settings, "agent_id"),
)