-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConfigParser.cs
More file actions
879 lines (814 loc) · 34.5 KB
/
ConfigParser.cs
File metadata and controls
879 lines (814 loc) · 34.5 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using RLBot.Flat;
using RLBotCS.Model;
using Tomlyn;
using Tomlyn.Model;
namespace RLBotCS.ManagerTools;
public class ConfigParser
{
public static class Fields
{
public const string RlBotTable = "rlbot";
public const string RlBotLauncher = "launcher";
public const string RlBotLauncherArg = "launcher_arg";
public const string RlBotAutoStartAgents = "auto_start_agents";
public const string RlBotWaitForAgents = "wait_for_agents";
public const string MatchTable = "match";
public const string MatchGameMode = "game_mode";
public const string MatchMapUpk = "game_map_upk";
public const string MatchSkipReplays = "skip_replays";
public const string MatchStartWithoutCountdown = "start_without_countdown";
public const string MatchExistingMatchBehavior = "existing_match_behavior";
public const string MatchRendering = "enable_rendering";
public const string MatchStateSetting = "enable_state_setting";
public const string MatchAutoSaveReplays = "auto_save_replays";
public const string MatchFreePlay = "freeplay";
public const string MutatorsTable = "mutators";
public const string MutatorsMatchLength = "match_length";
public const string MutatorsMaxScore = "max_score";
public const string MutatorsMultiBall = "multi_ball";
public const string MutatorsOvertime = "overtime";
public const string MutatorsGameSpeed = "game_speed";
public const string MutatorsBallMaxSpeed = "ball_max_speed";
public const string MutatorsBallType = "ball_type";
public const string MutatorsBallWeight = "ball_weight";
public const string MutatorsBallSize = "ball_size";
public const string MutatorsBallBounciness = "ball_bounciness";
public const string MutatorsBoostAmount = "boost_amount";
public const string MutatorsRumble = "rumble";
public const string MutatorsBoostStrength = "boost_strength";
public const string MutatorsGravity = "gravity";
public const string MutatorsDemolish = "demolish";
public const string MutatorsRespawnTime = "respawn_time";
public const string MutatorsMaxTime = "max_time";
public const string MutatorsGameEvent = "game_event";
public const string MutatorsAudio = "audio";
public const string MutatorsBallGravity = "ball_gravity";
public const string MutatorsTerritory = "territory";
public const string MutatorsStaleBall = "stale_ball";
public const string MutatorsJump = "jump";
public const string MutatorsDodgeTimer = "dodge_timer";
public const string MutatorsPossessionScore = "possession_score";
public const string MutatorsDemolishScore = "demolish_score";
public const string MutatorsNormalGoalScore = "normal_goal_score";
public const string MutatorsAerialGoalScore = "aerial_goal_score";
public const string MutatorsAssistGoalScore = "assist_goal_score";
public const string MutatorsInputRestriction = "input_restriction";
public const string CarsList = "cars";
public const string ScriptsList = "scripts";
public const string AgentTeam = "team";
public const string AgentType = "type";
public const string AgentSkill = "skill";
public const string AgentAutoStart = "auto_start";
public const string AgentName = "name";
public const string AgentLoadoutFile = "loadout_file";
public const string AgentConfigFile = "config_file";
public const string AgentSettingsTable = "settings";
public const string AgentAgentId = "agent_id";
public const string AgentRootDir = "root_dir";
public const string AgentRunCommand = "run_command";
public const string AgentRunCommandLinux = "run_command_linux";
public const string AgentHivemind = "hivemind";
public const string LoadoutBlueTable = "blue_loadout";
public const string LoadoutOrangeTable = "orange_loadout";
public const string LoadoutTeamColorId = "team_color_id";
public const string LoadoutCustomColorId = "custom_color_id";
public const string LoadoutCarId = "car_id";
public const string LoadoutDecalId = "decal_id";
public const string LoadoutWheelsId = "wheels_id";
public const string LoadoutBoostId = "boost_id";
public const string LoadoutAntennaId = "antenna_id";
public const string LoadoutHatId = "hat_id";
public const string LoadoutPaintFinishId = "paint_finish_id";
public const string LoadoutCustomFinishId = "custom_finish_id";
public const string LoadoutEngineAudioId = "engine_audio_id";
public const string LoadoutTrailsId = "trails_id";
public const string LoadoutGoalExplosionId = "goal_explosion_id";
public const string LoadoutPaintTable = "paint";
public const string LoadoutPaintCarPaintId = "car_paint_id";
public const string LoadoutPaintDecalPaintId = "decal_paint_id";
public const string LoadoutPaintWheelsPaintId = "wheels_paint_id";
public const string LoadoutPaintBoostPaintId = "boost_paint_id";
public const string LoadoutPaintAntennaPaintId = "antenna_paint_id";
public const string LoadoutPaintHatPaintId = "hat_paint_id";
public const string LoadoutPaintTrailsPaintId = "trails_paint_id";
public const string LoadoutPaintGoalExplosionPaintId = "goal_explosion_paint_id";
}
public class ConfigParserException(string? message, Exception? innerException = null)
: Exception(message, innerException);
private readonly ILogger Logger = Logging.GetLogger("ConfigParser");
/// <summary>Used to provide accurate error messages.</summary>
private readonly ConfigContextTracker _context = new();
/// <summary>Holds field names that were not present in the config. Used for debugging.</summary>
private readonly List<string> _missingValues = new();
private TomlTable LoadTomlFile(string path)
{
try
{
FileAttributes attr = File.GetAttributes(path);
if (attr.HasFlag(FileAttributes.Directory))
{
throw new ArgumentException(
$"The specified path is a directory, not a config file ({path})"
);
}
path = Path.GetFullPath(path);
return Toml.ToModel(File.ReadAllText(path), path);
}
catch (Exception e)
{
string ctx = _context.IsEmpty ? "" : $"{_context}: ";
throw new ConfigParserException($"{ctx}" + e.Message, e);
}
}
private T GetValue<T>(TomlTable table, string key, T fallback)
{
try
{
if (table.TryGetValue(key, out var res))
return (T)res;
_missingValues.Add(_context.ToStringWithEnd(key));
return fallback;
}
catch (InvalidCastException e)
{
var v = table[key];
if (v is string s)
v = $"\"{s}\"";
throw new InvalidCastException(
$"{_context.ToStringWithEnd(key)} has value {v}, but a value of type {typeof(T).Name} was expected.",
e
);
}
}
private T GetEnum<T>(TomlTable table, string key, T fallback)
where T : struct, Enum
{
if (table.TryGetValue(key, out var raw))
{
if (raw is string val)
{
if (Enum.TryParse((string)val, true, out T res))
return res;
throw new InvalidCastException(
$"{_context.ToStringWithEnd(key)} has invalid value \"{raw}\". "
+ $"Find valid values on https://wiki.rlbot.org."
);
}
else
{
throw new InvalidCastException(
$"{_context.ToStringWithEnd(key)} has value {raw}, but a value of type {typeof(T).Name} was expected."
);
}
}
_missingValues.Add(_context.ToStringWithEnd(key));
return fallback;
}
private PlayerClass GetAgentType(TomlTable table)
{
if (table.TryGetValue(Fields.AgentType, out var raw))
{
if (raw is string val)
{
switch (val.ToLower())
{
case "rlbot":
return PlayerClass.CustomBot;
case "psyonix":
return PlayerClass.PsyonixBot;
case "human":
return PlayerClass.Human;
default:
throw new InvalidCastException(
$"{_context.ToStringWithEnd(Fields.AgentType)} has invalid value \"{raw}\". "
+ $"Find valid values on https://wiki.rlbot.org."
);
}
}
else
{
throw new InvalidCastException(
$"{_context.ToStringWithEnd(Fields.AgentType)} has value {raw}, but a value of type {typeof(PlayerClass).Name} was expected."
);
}
}
_missingValues.Add(_context.ToStringWithEnd(Fields.AgentType));
return PlayerClass.CustomBot;
}
private static string? CombinePaths(string? parent, string? child)
{
if (parent == null || child == null)
return null;
return Path.Combine(parent, child);
}
private string GetRunCommand(TomlTable runnableSettings)
{
string runCommandWindows = GetValue<string>(
runnableSettings,
Fields.AgentRunCommand,
""
);
#if WINDOWS
return runCommandWindows;
#else
return GetValue(runnableSettings, Fields.AgentRunCommandLinux, runCommandWindows);
#endif
}
private ScriptConfigurationT LoadScriptConfig(string scriptConfigPath)
{
TomlTable scriptToml = LoadTomlFile(scriptConfigPath);
string tomlParent = Path.GetDirectoryName(scriptConfigPath) ?? "";
TomlTable settings = GetValue<TomlTable>(scriptToml, Fields.AgentSettingsTable, []);
using (_context.Begin(Fields.AgentSettingsTable))
{
return new ScriptConfigurationT
{
Name = GetValue(settings, Fields.AgentName, ""),
RootDir = CombinePaths(
tomlParent,
GetValue(settings, Fields.AgentRootDir, "")
),
RunCommand = GetRunCommand(settings),
AgentId = GetValue(settings, Fields.AgentAgentId, ""),
};
}
}
private uint GetTeam(TomlTable table, List<string> missingValues)
{
if (!table.TryGetValue(Fields.AgentTeam, out var raw))
{
missingValues.Add(_context.ToStringWithEnd(Fields.AgentTeam));
return 0;
}
switch (raw)
{
// Toml numbers are longs by default
case long i and >= 0 and <= 1:
return (uint)i;
case string s when s.Equals("blue", StringComparison.OrdinalIgnoreCase):
return 0;
case string s when s.Equals("orange", StringComparison.OrdinalIgnoreCase):
return 1;
default:
if (raw is string str)
raw = $"\"{str}\"";
throw new InvalidCastException(
$"{_context.ToStringWithEnd(Fields.AgentTeam)} has invalid value {raw}. "
+ $"Use 0, 1, \"blue\", or \"orange\"."
);
}
}
private PlayerConfigurationT ParseCarTable(TomlTable table, string matchConfigPath)
{
var matchConfigDir = Path.GetDirectoryName(matchConfigPath)!;
uint team = GetTeam(table, _missingValues);
string? nameOverride = GetValue<string?>(table, Fields.AgentName, null);
string? loadoutFileOverride = GetValue<string?>(table, Fields.AgentLoadoutFile, null);
if (!string.IsNullOrEmpty(loadoutFileOverride))
{
loadoutFileOverride = Path.Combine(matchConfigDir, loadoutFileOverride);
}
PlayerClass playerClass = GetAgentType(table);
if (playerClass == PlayerClass.Human)
{
return new PlayerConfigurationT
{
Variety = PlayerClassUnion.FromHuman(new HumanT()),
Team = team,
PlayerId = 0,
};
}
string configPath = GetValue(table, Fields.AgentConfigFile, "");
if (configPath != "")
{
string absoluteConfigPath = Path.Combine(matchConfigDir, configPath);
using (_context.Begin(Fields.AgentConfigFile, ConfigContextTracker.Type.Link))
{
switch (playerClass)
{
case PlayerClass.PsyonixBot:
return LoadPsyonixConfig(
absoluteConfigPath,
team,
nameOverride,
loadoutFileOverride
);
case PlayerClass.CustomBot:
return LoadPlayerConfig(
absoluteConfigPath,
team,
nameOverride,
loadoutFileOverride,
GetValue(table, Fields.AgentAutoStart, true)
);
default:
throw new ConfigParserException(
$"{_context.ToStringWithEnd(Fields.AgentType)} is out of range."
);
}
}
}
PlayerLoadoutT? loadout = null;
if (loadoutFileOverride is not null)
{
using (_context.Begin(Fields.AgentLoadoutFile, ConfigContextTracker.Type.Link))
{
loadout = LoadPlayerLoadout(loadoutFileOverride, team);
}
}
PlayerClassUnion variety;
switch (playerClass)
{
case PlayerClass.PsyonixBot:
variety = PlayerClassUnion.FromPsyonixBot(
new PsyonixBotT
{
BotSkill = GetEnum(table, Fields.AgentSkill, PsyonixSkill.AllStar),
Loadout = loadout,
Name = nameOverride,
}
);
break;
case PlayerClass.CustomBot:
variety = PlayerClassUnion.FromPsyonixBot(
new PsyonixBotT
{
BotSkill = GetEnum(table, Fields.AgentSkill, PsyonixSkill.AllStar),
Loadout = loadout,
Name = nameOverride,
}
);
break;
default:
throw new ConfigParserException(
$"{_context.ToStringWithEnd(Fields.AgentType)} is out of range."
);
}
return new PlayerConfigurationT
{
Variety = variety,
Team = team,
PlayerId = 0,
};
}
private PlayerConfigurationT LoadPsyonixConfig(
string configPath,
uint team,
string? nameOverride,
string? loadoutFileOverride
)
{
TomlTable table = LoadTomlFile(configPath);
string configDir = Path.GetDirectoryName(configPath)!;
TomlTable settings = GetValue<TomlTable>(table, Fields.AgentSettingsTable, []);
using (_context.Begin(Fields.AgentSettingsTable))
{
string rootDir = Path.Combine(
configDir,
GetValue<string>(settings, Fields.AgentRootDir, "")
);
// Override is null, "", or an absolute path.
// Null implies no override and "" implies we should not load the loadout.
string? loadoutPath = loadoutFileOverride;
if (loadoutFileOverride is null)
{
if (settings.TryGetValue(Fields.AgentLoadoutFile, out var loadoutPathRel))
{
loadoutPath = Path.Combine(configDir, (string)loadoutPathRel);
}
else
{
_missingValues.Add(_context.ToStringWithEnd(Fields.AgentLoadoutFile));
}
}
PlayerLoadoutT? loadout;
using (_context.Begin(Fields.AgentLoadoutFile, ConfigContextTracker.Type.Link))
{
loadout =
(loadoutPath ?? "") != "" ? LoadPlayerLoadout(loadoutPath!, team) : null;
}
PsyonixBotT variety = new()
{
BotSkill = GetEnum(table, Fields.AgentSkill, PsyonixSkill.AllStar),
Loadout = loadout,
Name = nameOverride ?? GetValue<string>(settings, Fields.AgentName, ""),
};
return new PlayerConfigurationT
{
Variety = PlayerClassUnion.FromPsyonixBot(variety),
Team = team,
PlayerId = 0,
};
}
}
private PlayerConfigurationT LoadPlayerConfig(
string configPath,
uint team,
string? nameOverride,
string? loadoutFileOverride,
bool autoStart
)
{
TomlTable table = LoadTomlFile(configPath);
string configDir = Path.GetDirectoryName(configPath)!;
TomlTable settings = GetValue<TomlTable>(table, Fields.AgentSettingsTable, []);
using (_context.Begin(Fields.AgentSettingsTable))
{
string rootDir = Path.Combine(
configDir,
GetValue<string>(settings, Fields.AgentRootDir, "")
);
// Override is null, "", or an absolute path.
// Null implies no override and "" implies we should not load the loadout.
string? loadoutPath = loadoutFileOverride;
if (loadoutFileOverride is null)
{
if (settings.TryGetValue(Fields.AgentLoadoutFile, out var loadoutPathRel))
{
loadoutPath = Path.Combine(configDir, (string)loadoutPathRel);
}
else
{
_missingValues.Add(_context.ToStringWithEnd(Fields.AgentLoadoutFile));
}
}
PlayerLoadoutT? loadout;
using (_context.Begin(Fields.AgentLoadoutFile, ConfigContextTracker.Type.Link))
{
loadout =
(loadoutPath ?? "") != "" ? LoadPlayerLoadout(loadoutPath!, team) : null;
}
CustomBotT variety = new()
{
AgentId = GetValue<string>(settings, Fields.AgentAgentId, ""),
Name = nameOverride ?? GetValue<string>(settings, Fields.AgentName, ""),
Loadout = loadout,
RunCommand = autoStart ? GetRunCommand(settings) : "",
Hivemind = GetValue(settings, Fields.AgentHivemind, false),
RootDir = rootDir,
};
return new PlayerConfigurationT
{
Variety = PlayerClassUnion.FromCustomBot(variety),
Team = team,
PlayerId = 0,
};
}
}
private PlayerLoadoutT LoadPlayerLoadout(string loadoutPath, uint team)
{
TomlTable loadoutToml = LoadTomlFile(loadoutPath);
string teamLoadoutString =
team == Team.Blue ? Fields.LoadoutBlueTable : Fields.LoadoutOrangeTable;
TomlTable teamLoadout = GetValue<TomlTable>(loadoutToml, teamLoadoutString, []);
using (_context.Begin(teamLoadoutString, ConfigContextTracker.Type.Link))
{
TomlTable teamPaint = GetValue<TomlTable>(
teamLoadout,
Fields.LoadoutPaintTable,
[]
);
LoadoutPaintT loadoutPaint;
using (_context.Begin(Fields.LoadoutPaintTable))
{
loadoutPaint = new LoadoutPaintT
{
// TODO - GetPrimary/Secondary color? Do any bots use this?
CarPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintCarPaintId, 0),
DecalPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintDecalPaintId, 0),
WheelsPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintWheelsPaintId, 0),
BoostPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintBoostPaintId, 0),
AntennaPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintAntennaPaintId, 0),
HatPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintHatPaintId, 0),
TrailsPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintTrailsPaintId, 0),
GoalExplosionPaintId = (uint)
GetValue<long>(teamPaint, Fields.LoadoutPaintGoalExplosionPaintId, 0),
};
}
return new PlayerLoadoutT()
{
TeamColorId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutTeamColorId, 0),
CustomColorId = (uint)
GetValue<long>(teamLoadout, Fields.LoadoutCustomColorId, 0),
CarId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutCarId, 0),
DecalId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutDecalId, 0),
WheelsId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutWheelsId, 0),
BoostId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutBoostId, 0),
AntennaId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutAntennaId, 0),
HatId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutHatId, 0),
PaintFinishId = (uint)
GetValue<long>(teamLoadout, Fields.LoadoutPaintFinishId, 0),
CustomFinishId = (uint)
GetValue<long>(teamLoadout, Fields.LoadoutCustomFinishId, 0),
EngineAudioId = (uint)
GetValue<long>(teamLoadout, Fields.LoadoutEngineAudioId, 0),
TrailsId = (uint)GetValue<long>(teamLoadout, Fields.LoadoutTrailsId, 0),
GoalExplosionId = (uint)
GetValue<long>(teamLoadout, Fields.LoadoutGoalExplosionId, 0),
LoadoutPaint = loadoutPaint,
};
}
}
private MutatorSettingsT GetMutatorSettings(TomlTable mutatorTable) =>
new MutatorSettingsT
{
MatchLength = GetEnum(
mutatorTable,
Fields.MutatorsMatchLength,
MatchLengthMutator.FiveMinutes
),
MaxScore = GetEnum(
mutatorTable,
Fields.MutatorsMaxScore,
MaxScoreMutator.Unlimited
),
MultiBall = GetEnum(mutatorTable, Fields.MutatorsMultiBall, MultiBallMutator.One),
Overtime = GetEnum(
mutatorTable,
Fields.MutatorsOvertime,
OvertimeMutator.Unlimited
),
GameSpeed = GetEnum(
mutatorTable,
Fields.MutatorsGameSpeed,
GameSpeedMutator.Default
),
BallMaxSpeed = GetEnum(
mutatorTable,
Fields.MutatorsBallMaxSpeed,
BallMaxSpeedMutator.Default
),
BallType = GetEnum(mutatorTable, Fields.MutatorsBallType, BallTypeMutator.Default),
BallWeight = GetEnum(
mutatorTable,
Fields.MutatorsBallWeight,
BallWeightMutator.Default
),
BallSize = GetEnum(mutatorTable, Fields.MutatorsBallSize, BallSizeMutator.Default),
BallBounciness = GetEnum(
mutatorTable,
Fields.MutatorsBallBounciness,
BallBouncinessMutator.Default
),
BoostAmount = GetEnum(
mutatorTable,
Fields.MutatorsBoostAmount,
BoostAmountMutator.NormalBoost
),
Rumble = GetEnum(mutatorTable, Fields.MutatorsRumble, RumbleMutator.Off),
BoostStrength = GetEnum(
mutatorTable,
Fields.MutatorsBoostStrength,
BoostStrengthMutator.One
),
Gravity = GetEnum(mutatorTable, Fields.MutatorsGravity, GravityMutator.Default),
Demolish = GetEnum(mutatorTable, Fields.MutatorsDemolish, DemolishMutator.Default),
RespawnTime = GetEnum(
mutatorTable,
Fields.MutatorsRespawnTime,
RespawnTimeMutator.ThreeSeconds
),
MaxTime = GetEnum(mutatorTable, Fields.MutatorsMaxTime, MaxTimeMutator.Unlimited),
GameEvent = GetEnum(
mutatorTable,
Fields.MutatorsGameEvent,
GameEventMutator.Default
),
Audio = GetEnum(mutatorTable, Fields.MutatorsAudio, AudioMutator.Default),
BallGravity = GetEnum(
mutatorTable,
Fields.MutatorsBallGravity,
BallGravityMutator.Default
),
Territory = GetEnum(mutatorTable, Fields.MutatorsTerritory, TerritoryMutator.Off),
StaleBall = GetEnum(
mutatorTable,
Fields.MutatorsStaleBall,
StaleBallMutator.Unlimited
),
Jump = GetEnum(mutatorTable, Fields.MutatorsJump, JumpMutator.Default),
DodgeTimer = GetEnum(
mutatorTable,
Fields.MutatorsDodgeTimer,
DodgeTimerMutator.OnePointTwentyFiveSeconds
),
PossessionScore = GetEnum(
mutatorTable,
Fields.MutatorsPossessionScore,
PossessionScoreMutator.Off
),
DemolishScore = GetEnum(
mutatorTable,
Fields.MutatorsDemolishScore,
DemolishScoreMutator.Zero
),
NormalGoalScore = GetEnum(
mutatorTable,
Fields.MutatorsNormalGoalScore,
NormalGoalScoreMutator.One
),
AerialGoalScore = GetEnum(
mutatorTable,
Fields.MutatorsAerialGoalScore,
AerialGoalScoreMutator.One
),
AssistGoalScore = GetEnum(
mutatorTable,
Fields.MutatorsAssistGoalScore,
AssistGoalScoreMutator.Zero
),
InputRestriction = GetEnum(
mutatorTable,
Fields.MutatorsInputRestriction,
InputRestrictionMutator.Default
),
};
/// <summary>
/// Loads the match configuration at the given path. Empty fields are given default values.
/// However, default values are not necessarily valid (e.g. empty agent_id).
/// Use <see cref="ConfigValidator"/> to validate the match config.
/// </summary>
/// <param name="path">Path to match configuration file.</param>
/// <param name="config">The loaded match config.</param>
/// <returns>Whether the match config was successfully loaded. Potential errors are logged.</returns>
public bool TryLoadMatchConfig(string path, out MatchConfigurationT config)
{
config = null!;
try
{
config = LoadMatchConfig(path);
return true;
}
catch (ConfigParserException e)
{
Logger.LogError(e.Message);
}
return false;
}
/// <summary>
/// Loads the match configuration at the given path. Empty fields are given default values.
/// However, default values are not necessarily valid (e.g. empty agent_id).
/// Use <see cref="ConfigValidator"/> to validate the match config.
/// </summary>
/// <param name="path">Path to match configuration file.</param>
/// <returns>The parsed MatchConfigurationT</returns>
/// <exception cref="ConfigParserException">Thrown if something went wrong. See inner exception.</exception>
public MatchConfigurationT LoadMatchConfig(string path)
{
_missingValues.Clear();
_context.Clear();
try
{
path = Path.GetFullPath(path);
TomlTable outerTable = LoadTomlFile(path);
MatchConfigurationT matchConfig = new MatchConfigurationT();
TomlTable rlbotTable = GetValue<TomlTable>(outerTable, Fields.RlBotTable, []);
using (_context.Begin(Fields.RlBotTable))
{
matchConfig.Launcher = GetEnum(
rlbotTable,
Fields.RlBotLauncher,
Launcher.Steam
);
matchConfig.LauncherArg = GetValue(rlbotTable, Fields.RlBotLauncherArg, "");
matchConfig.AutoStartAgents = GetValue(
rlbotTable,
Fields.RlBotAutoStartAgents,
true
);
matchConfig.WaitForAgents = GetValue(
rlbotTable,
Fields.RlBotWaitForAgents,
true
);
}
TomlTableArray players = GetValue<TomlTableArray>(outerTable, Fields.CarsList, []);
matchConfig.PlayerConfigurations = [];
for (var i = 0; i < players.Count; i++)
{
using (_context.Begin($"{Fields.CarsList}[{i}]"))
{
matchConfig.PlayerConfigurations.Add(ParseCarTable(players[i], path));
}
}
TomlTableArray scripts = GetValue<TomlTableArray>(
outerTable,
Fields.ScriptsList,
[]
);
matchConfig.ScriptConfigurations = [];
for (var i = 0; i < scripts.Count; i++)
{
using (_context.Begin($"{Fields.ScriptsList}[{i}]"))
{
string configPath = GetValue(scripts[i], Fields.AgentConfigFile, "");
if (configPath != "")
{
string absoluteConfigPath = Path.Combine(
Path.GetDirectoryName(path)!,
configPath
);
using (
_context.Begin(
Fields.AgentConfigFile,
ConfigContextTracker.Type.Link
)
)
{
var script = LoadScriptConfig(absoluteConfigPath);
bool autoStart = GetValue(scripts[i], Fields.AgentAutoStart, true);
if (!autoStart)
{
script.RunCommand = "";
}
matchConfig.ScriptConfigurations.Add(script);
}
}
else
{
throw new FileNotFoundException(
$"{_context.ToStringWithEnd(Fields.AgentConfigFile)} is empty. "
+ $"Scripts must specify a config file."
);
}
}
}
TomlTable mutatorTable = GetValue<TomlTable>(outerTable, Fields.MutatorsTable, []);
using (_context.Begin(Fields.MutatorsTable))
{
matchConfig.Mutators = GetMutatorSettings(mutatorTable);
}
TomlTable matchTable = GetValue<TomlTable>(outerTable, Fields.MatchTable, []);
using (_context.Begin(Fields.MatchTable))
{
matchConfig.GameMode = GetValue(matchTable, Fields.MatchGameMode, "")
.ToLower() switch
{
"soccer" => GameMode.Soccar,
_ => GetEnum(matchTable, Fields.MatchGameMode, GameMode.Soccar),
};
matchConfig.GameMapUpk = GetValue(matchTable, Fields.MatchMapUpk, "Stadium_P");
matchConfig.SkipReplays = GetValue(matchTable, Fields.MatchSkipReplays, false);
matchConfig.InstantStart = GetValue(
matchTable,
Fields.MatchStartWithoutCountdown,
false
);
matchConfig.EnableRendering = DebugRendering.OffByDefault;
if (
matchTable.TryGetValue(Fields.MatchRendering, out var raw)
&& raw is bool enableRendering
)
{
matchConfig.EnableRendering = enableRendering
? DebugRendering.OnByDefault
: DebugRendering.AlwaysOff;
}
else
{
matchConfig.EnableRendering = GetEnum(
matchTable,
Fields.MatchRendering,
DebugRendering.OffByDefault
);
}
matchConfig.EnableStateSetting = GetValue(
matchTable,
Fields.MatchStateSetting,
true
);
matchConfig.ExistingMatchBehavior = GetEnum(
matchTable,
Fields.MatchExistingMatchBehavior,
ExistingMatchBehavior.Restart
);
matchConfig.AutoSaveReplay = GetValue(
matchTable,
Fields.MatchAutoSaveReplays,
false
);
matchConfig.Freeplay = GetValue(matchTable, Fields.MatchFreePlay, false);
}
string mv = string.Join(",", _missingValues);
Logger.LogDebug($"Missing values in toml: {mv}");
Debug.Assert(_context.IsEmpty, $"Context not emptied: {_context}");
return matchConfig;
}
catch (Exception e)
{
throw new ConfigParserException(
"Failed to load match config. " + e.Message.Trim(),
e
);
}
}
}