-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConfigValidator.cs
More file actions
268 lines (240 loc) · 10.4 KB
/
ConfigValidator.cs
File metadata and controls
268 lines (240 loc) · 10.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
using Microsoft.Extensions.Logging;
using RLBot.Flat;
using RLBotCS.Conversion;
using RLBotCS.Model;
namespace RLBotCS.ManagerTools;
using Fields = ConfigParser.Fields;
public static class ConfigValidator
{
private static readonly ILogger Logger = Logging.GetLogger("ConfigValidator");
/// <summary>
/// Validates the given match config.
/// The validation may modify fields (e.g. turning null or unused strings into empty strings).
/// Psyonix bots will be given Psyonix preset loadouts as fitting.
/// If the config is invalid, the reasons are logged.
/// </summary>
/// <returns>Whether the given match is valid and can be started without issues.</returns>
public static bool Validate(MatchConfigurationT config, bool surpressWarnings = false)
{
bool valid = true;
PsyonixLoadouts.Reset();
ConfigContextTracker ctx = new();
using (ctx.Begin(Fields.RlBotTable))
{
if (config.Launcher == Launcher.Custom)
{
config.LauncherArg = (config.LauncherArg ?? "").ToLower();
if (config.LauncherArg != "legendary" && config.LauncherArg != "heroic")
{
Logger.LogError(
$"Invalid {ctx.ToStringWithEnd(Fields.RlBotLauncherArg)} value \"{config.LauncherArg}\". "
+ $"\"legendary\" and \"heroic\" are the only Custom launchers supported currently."
);
valid = false;
}
}
else
{
config.LauncherArg = "";
}
}
config.Mutators ??= new();
config.PlayerConfigurations ??= new();
config.ScriptConfigurations ??= new();
Dictionary<string, (string rootDir, string runCmd)> agentIdTracker = new();
valid =
ValidatePlayers(ctx, config.PlayerConfigurations, agentIdTracker, surpressWarnings)
&& valid;
valid = ValidateScripts(ctx, config.ScriptConfigurations, agentIdTracker) && valid;
Logger.LogDebug(valid ? "Match config is valid." : "Match config is invalid!");
return valid;
}
private static bool ValidatePlayers(
ConfigContextTracker ctx,
List<PlayerConfigurationT> players,
Dictionary<string, (string rootDir, string runCmd)> agentIdTracker,
bool surpressWarnings
)
{
bool valid = true;
int humanCount = 0;
int humanIndex = -1;
for (int i = 0; i < players.Count; i++)
{
using var _ = ctx.Begin($"{Fields.CarsList}[{i}]");
var player = players[i];
if (player.Team != Team.Blue && player.Team != Team.Orange)
{
Logger.LogError(
$"Invalid {ctx.ToStringWithEnd(Fields.AgentTeam)} of '{player.Team}'. "
+ $"Must be 0 (blue) or 1 (orange)."
);
valid = false;
}
switch (player.Variety.Value)
{
case CustomBotT bot:
bot.AgentId ??= "";
bot.AgentId = bot.AgentId.Trim();
if (bot.AgentId == "")
{
Logger.LogError(
$"{ctx.ToStringWithEnd(Fields.AgentType)} is \"rlbot\" "
+ $"but {ctx.ToStringWithEnd(Fields.AgentAgentId)} is empty. "
+ $"RLBot bots must have an agent ID. "
+ $"We recommend the format \"<developer>/<botname>/<version>\"."
);
valid = false;
}
bot.Name ??= "";
bot.RunCommand ??= "";
bot.RootDir ??= "";
bot.Loadout ??= new();
bot.Loadout.LoadoutPaint ??= new();
player.PlayerId = $"{bot.AgentId}/{player.Team}/{i}".GetHashCode();
// Dont validate agent id for bots that will be manually started
if (!surpressWarnings && !string.IsNullOrEmpty(bot.RunCommand))
{
// Reduce user confusion around how agent ids should be used
// Same bot == same agent id, different bot == different agent id
// This is not a hard requirement, so we just log a warning
// We check for "same bot" by comparing RootDir and RunCommand
if (agentIdTracker.TryGetValue(bot.AgentId, out var existing))
{
if (
existing.rootDir != bot.RootDir
|| existing.runCmd != bot.RunCommand
)
{
string errorStr;
if (existing.rootDir != bot.RootDir)
{
errorStr =
existing.runCmd != bot.RunCommand
? "RootDirs and RunCommands"
: "RootDirs";
}
else
{
errorStr = "RunCommands";
}
Logger.LogWarning(
$"Potential agent ID conflict: \"{bot.AgentId}\" is used by multiple bots with different {errorStr}.\n"
+ "Agent configs using the same ID may get used interchangeably. Agents that behave differently should have unique IDs."
);
}
}
else
{
agentIdTracker[bot.AgentId] = (bot.RootDir, bot.RunCommand);
}
}
break;
case PsyonixBotT bot:
string skill = bot.BotSkill switch
{
PsyonixSkill.Beginner => "beginner",
PsyonixSkill.Rookie => "rookie",
PsyonixSkill.Pro => "pro",
PsyonixSkill.AllStar => "allstar",
_ => HandleOutOfRange(
out valid,
"",
$"{ctx.ToStringWithEnd(Fields.AgentSkill)} is out of range."
),
};
// Apply Psyonix preset loadouts
if (string.IsNullOrEmpty(bot.Name))
{
(bot.Name, var preset) = PsyonixLoadouts.GetNext((int)player.Team);
string andPreset = bot.Loadout == null ? " and preset" : "";
bot.Loadout ??= preset;
Logger.LogDebug(
$"Gave unnamed Psyonix bot {ctx} a name{andPreset} ({bot.Name})"
);
}
else if (bot.Loadout == null)
{
bot.Loadout = PsyonixLoadouts.GetFromName(bot.Name, (int)player.Team);
Logger.LogDebug(
bot.Loadout == null
? $"Failed to find a preset loadout for Psyonix bot {ctx} named \"{bot.Name}\"."
: $"Found preset loadout for Psyonix bot {ctx} named \"{bot.Name}\"."
);
}
// Fallback if above fails or user didn't include paints
bot.Loadout ??= new();
bot.Loadout.LoadoutPaint ??= new();
player.PlayerId =
$"psyonix/{bot.BotSkill}/{player.Team}/{i}".GetHashCode();
break;
case HumanT:
humanCount++;
humanIndex = i;
player.PlayerId = 0;
break;
}
}
if (humanCount > 1)
{
Logger.LogError("Only one player can be of type \"Human\".");
valid = false;
}
if (humanIndex != -1)
{
// Move human to last index
var tmp = players[humanIndex];
players[humanIndex] = players.Last();
players[players.Count - 1] = tmp;
}
return valid;
}
private static bool ValidateScripts(
ConfigContextTracker ctx,
List<ScriptConfigurationT> scripts,
Dictionary<string, (string rootDir, string runCmd)> agentIdTracker
)
{
bool valid = true;
for (int i = 0; i < scripts.Count; i++)
{
using var _ = ctx.Begin($"{Fields.ScriptsList}[{i}]");
var script = scripts[i];
script.AgentId ??= "";
script.AgentId = script.AgentId.Trim();
if (script.AgentId == "")
{
Logger.LogError(
$"{ctx.ToStringWithEnd(Fields.AgentAgentId)} is empty. "
+ $"Scripts must have an agent ID. "
+ $"We recommend the format \"<developer>/<scriptname>/<version>\"."
);
valid = false;
}
script.Name ??= "";
script.RunCommand ??= "";
script.RootDir ??= "";
script.ScriptId = $"{script.AgentId}/{Team.Scripts}/{i}".GetHashCode();
if (agentIdTracker.TryGetValue(script.AgentId, out var existing))
{
Logger.LogError(
$"{ctx.ToStringWithEnd(Fields.AgentAgentId)} \"{script.AgentId}\" is already in use. "
+ "Each script must have a unique agent ID."
);
valid = false;
}
else
{
agentIdTracker[script.AgentId] = (script.RootDir, script.RunCommand);
}
}
return valid;
}
/// <summary>Helder function to handle enums out of range inline</summary>
private static T HandleOutOfRange<T>(out bool valid, T fallback, string message)
{
valid = false;
Logger.LogError(message);
return fallback;
}
}