Skip to content

Commit edd970c

Browse files
authored
Support agent environment variables (#171)
* Add FlatBuffers generation for macOS * Support agent environment variables * Format tests * Simplify `ApplyEnvironment`
1 parent 8cf6379 commit edd970c

9 files changed

Lines changed: 91 additions & 2 deletions

File tree

RLBotCS/ManagerTools/ConfigParser.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static class Fields
7676
public const string AgentRootDir = "root_dir";
7777
public const string AgentRunCommand = "run_command";
7878
public const string AgentRunCommandLinux = "run_command_linux";
79+
public const string AgentEnvironment = "environment";
7980
public const string AgentHivemind = "hivemind";
8081

8182
public const string LoadoutBlueTable = "blue_loadout";
@@ -260,6 +261,33 @@ private string GetRunCommand(TomlTable runnableSettings)
260261
#endif
261262
}
262263

264+
private List<EnvironmentVariableT> GetEnvironment(TomlTable runnableSettings)
265+
{
266+
TomlTable environment = GetValue<TomlTable>(
267+
runnableSettings,
268+
Fields.AgentEnvironment,
269+
[]
270+
);
271+
272+
List<EnvironmentVariableT> variables = [];
273+
using (_context.Begin(Fields.AgentEnvironment))
274+
{
275+
foreach (var (key, rawValue) in environment)
276+
{
277+
if (rawValue is not string value)
278+
{
279+
throw new InvalidCastException(
280+
$"{_context.ToStringWithEnd(key)} has value {rawValue}, but a value of type String was expected."
281+
);
282+
}
283+
284+
variables.Add(new() { Name = key, Value = value });
285+
}
286+
}
287+
288+
return variables;
289+
}
290+
263291
private ScriptConfigurationT LoadScriptConfig(string scriptConfigPath)
264292
{
265293
TomlTable scriptToml = LoadTomlFile(scriptConfigPath);
@@ -276,6 +304,7 @@ private ScriptConfigurationT LoadScriptConfig(string scriptConfigPath)
276304
GetValue(settings, Fields.AgentRootDir, "")
277305
),
278306
RunCommand = GetRunCommand(settings),
307+
Environment = GetEnvironment(settings),
279308
AgentId = GetValue(settings, Fields.AgentAgentId, ""),
280309
};
281310
}
@@ -511,6 +540,7 @@ bool autoStart
511540
Name = nameOverride ?? GetValue<string>(settings, Fields.AgentName, ""),
512541
Loadout = loadout,
513542
RunCommand = autoStart ? GetRunCommand(settings) : "",
543+
Environment = GetEnvironment(settings),
514544
Hivemind = GetValue(settings, Fields.AgentHivemind, false),
515545
RootDir = rootDir,
516546
};

RLBotCS/ManagerTools/ConfigValidator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ bool surpressWarnings
101101
bot.Name ??= "";
102102
bot.RunCommand ??= "";
103103
bot.RootDir ??= "";
104+
bot.Environment ??= [];
104105
bot.Loadout ??= new();
105106
bot.Loadout.LoadoutPaint ??= new();
106107

@@ -239,6 +240,7 @@ private static bool ValidateScripts(
239240
script.Name ??= "";
240241
script.RunCommand ??= "";
241242
script.RootDir ??= "";
243+
script.Environment ??= [];
242244
script.ScriptId = $"{script.AgentId}/{Team.Scripts}/{i}".GetHashCode();
243245

244246
if (agentIdTracker.TryGetValue(script.AgentId, out var existing))

RLBotCS/ManagerTools/LaunchManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ private static Process RunCommandInShell(string command)
143143
return process;
144144
}
145145

146+
private static void ApplyEnvironment(
147+
ProcessStartInfo startInfo,
148+
List<RLBot.Flat.EnvironmentVariableT> environment
149+
)
150+
{
151+
foreach (var variable in environment)
152+
{
153+
startInfo.EnvironmentVariables[variable.Name] = variable.Value;
154+
}
155+
}
156+
146157
private static void LaunchGameViaLegendary()
147158
{
148159
Process legendary = RunCommandInShell(
@@ -186,6 +197,7 @@ int rlbotSocketsPort
186197
Process botProcess = RunCommandInShell(details.RunCommand);
187198

188199
botProcess.StartInfo.WorkingDirectory = details.RootDir;
200+
ApplyEnvironment(botProcess.StartInfo, details.Environment);
189201
botProcess.StartInfo.EnvironmentVariables["RLBOT_AGENT_ID"] = details.AgentId;
190202
botProcess.StartInfo.EnvironmentVariables["RLBOT_SERVER_PORT"] =
191203
rlbotSocketsPort.ToString();
@@ -233,6 +245,7 @@ int rlbotSocketsPort
233245
if (script.RootDir != "")
234246
scriptProcess.StartInfo.WorkingDirectory = script.RootDir;
235247

248+
ApplyEnvironment(scriptProcess.StartInfo, script.Environment);
236249
scriptProcess.StartInfo.EnvironmentVariables["RLBOT_AGENT_ID"] = script.AgentId;
237250
scriptProcess.StartInfo.EnvironmentVariables["RLBOT_SERVER_PORT"] =
238251
rlbotSocketsPort.ToString();

RLBotCS/RLBotCS.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
>
8585
<Exec Command="../generate-flatbuffers.sh" />
8686
</Target>
87+
<Target
88+
Name="GenerateFlatBuffersMac"
89+
BeforeTargets="PreBuildEvent"
90+
Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'"
91+
>
92+
<Exec Command="../generate-flatbuffers-mac.sh" />
93+
</Target>
8794
<ItemGroup>
8895
<Compile Include="..\FlatBuffer\RLBot.cs" />
8996
</ItemGroup>

RLBotCSTests/ConfigParserTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45
using RLBot.Flat;
56
using RLBotCS.ManagerTools;
@@ -179,10 +180,24 @@ public void Overrides()
179180

180181
PlayerConfigurationT player = mc.PlayerConfigurations[0];
181182
Assert.AreEqual("New Bot Name", player.Variety.AsCustomBot().Name);
182-
Assert.IsNull(player.Variety.AsCustomBot().Loadout);
183+
CustomBotT bot = player.Variety.AsCustomBot();
184+
Assert.IsNull(bot.Loadout);
185+
Assert.AreEqual("bot-value", bot.Environment.Single(e => e.Name == "BOT_ENV").Value);
186+
Assert.AreEqual(
187+
"bot-shared",
188+
bot.Environment.Single(e => e.Name == "SHARED_ENV").Value
189+
);
183190

184191
ScriptConfigurationT script = mc.ScriptConfigurations[0];
185192
Assert.AreEqual("Normal Test Script", script.Name); // Not overriden
193+
Assert.AreEqual(
194+
"script-value",
195+
script.Environment.Single(e => e.Name == "SCRIPT_ENV").Value
196+
);
197+
Assert.AreEqual(
198+
"script-shared",
199+
script.Environment.Single(e => e.Name == "SHARED_ENV").Value
200+
);
186201
}
187202

188203
[TestMethod]

RLBotCSTests/TestTomls/normal.bot.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ run_command_linux = "python bot.py"
66
loadout_file = "loadout.toml"
77
logo_file = "normal_logo.png"
88

9+
[settings.environment]
10+
BOT_ENV = "bot-value"
11+
SHARED_ENV = "bot-shared"
12+
913
[details]
1014
description = "This is a normal test bot"
1115
fun_fact = "knock knock ..."

RLBotCSTests/TestTomls/normal.script.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ run_command_linux = "python script.py"
66
loadout_file = "loadout.toml"
77
logo_file = "normal_logo.png"
88

9+
[settings.environment]
10+
SCRIPT_ENV = "script-value"
11+
SHARED_ENV = "script-shared"
12+
913
[details]
1014
description = "This is a normal test script"
1115
fun_fact = "knock knock ..."

flatbuffers-schema

generate-flatbuffers-mac.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
cd "$(dirname "$0")"
4+
5+
echo Generating flatbuffers header file...
6+
7+
./flatbuffers-schema/binaries/flatc_mac --gen-all --csharp --gen-object-api --gen-onefile -o ./FlatBuffer ./flatbuffers-schema/schema/rlbot.fbs
8+
9+
# the file produced is called rlbot_generated.cs, rename it to RLBot.cs after removing the old one
10+
rm -f ./FlatBuffer/RLBot.cs
11+
mv ./FlatBuffer/rlbot_generated.cs ./FlatBuffer/RLBot.cs
12+
sed -i '' 's/rlbot\.flat/RLBot.Flat/g' ./FlatBuffer/RLBot.cs
13+
14+
echo Done.

0 commit comments

Comments
 (0)