| | | 1 | | using System; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.IO; |
| | | 4 | | using UnityEngine; |
| | | 5 | | |
| | | 6 | | // The run worker executes exactly one seeded simulation run launched externally, e.g., through the |
| | | 7 | | // Python batch run launcher. It applies the seed, starts the requested simulation configuration, |
| | | 8 | | // writes logs to the assigned output directory, and quits after the simulation finishes. |
| | | 9 | | |
| | | 10 | | public class RunWorker : MonoBehaviour { |
| | | 11 | | private const string _simulationConfigFlag = "--simulation_config"; |
| | | 12 | | private const string _seedFlag = "--seed"; |
| | | 13 | | private const string _outputDirFlag = "--output_dir"; |
| | | 14 | | |
| | 0 | 15 | | public static RunWorker Instance { get; private set; } |
| | | 16 | | |
| | | 17 | | // True if the run worker was launched from the CLI, e.g., as part of a batch run, rather than |
| | | 18 | | // from normal interactive mode. |
| | 54 | 19 | | public static bool IsWorkerMode { get; private set; } = false; |
| | | 20 | | |
| | | 21 | | // Simulation configuration to execute. |
| | 0 | 22 | | public static string SimulationConfigFile { get; private set; } |
| | | 23 | | |
| | | 24 | | // Simulation run seed. |
| | 0 | 25 | | public static int Seed { get; private set; } = 0; |
| | | 26 | | |
| | | 27 | | // Output directory of the simulation run. |
| | 0 | 28 | | public static string OutputDirectory { get; private set; } |
| | | 29 | | |
| | 0 | 30 | | private bool _hasStartedRun = false; |
| | 0 | 31 | | private bool _hasScheduledQuit = false; |
| | | 32 | | |
| | | 33 | | [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] |
| | 0 | 34 | | private static void OnBeforeSceneLoad() { |
| | 0 | 35 | | if (!TryGetWorkerModeArguments(Environment.GetCommandLineArgs(), |
| | | 36 | | out string simulationConfigFile, out int seed, |
| | 0 | 37 | | out string outputDirectory)) { |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | var gameObject = new GameObject("RunWorker"); |
| | 0 | 42 | | DontDestroyOnLoad(gameObject); |
| | 0 | 43 | | var runWorker = gameObject.AddComponent<RunWorker>(); |
| | 0 | 44 | | runWorker.Initialize(simulationConfigFile, seed, outputDirectory); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public static bool TryGetWorkerModeArguments(string[] args, out string simulationConfigFile, |
| | 0 | 48 | | out int seed, out string outputDirectory) { |
| | 0 | 49 | | simulationConfigFile = GetArgValue(args, _simulationConfigFlag); |
| | 0 | 50 | | string seedValue = GetArgValue(args, _seedFlag); |
| | 0 | 51 | | string rawOutputDirectory = GetArgValue(args, _outputDirFlag); |
| | 0 | 52 | | seed = 0; |
| | 0 | 53 | | outputDirectory = null; |
| | | 54 | | |
| | 0 | 55 | | if (simulationConfigFile == null && seedValue == null && rawOutputDirectory == null) { |
| | 0 | 56 | | return false; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | if (string.IsNullOrWhiteSpace(simulationConfigFile) || string.IsNullOrWhiteSpace(seedValue) || |
| | 0 | 60 | | string.IsNullOrWhiteSpace(rawOutputDirectory)) { |
| | 0 | 61 | | throw new ArgumentException("Worker mode requires simulation config, seed, and output dir."); |
| | | 62 | | } |
| | 0 | 63 | | if (!int.TryParse(seedValue, out seed)) { |
| | 0 | 64 | | throw new ArgumentException($"Failed to parse worker seed: {seedValue}."); |
| | | 65 | | } |
| | 0 | 66 | | if (!Path.IsPathRooted(rawOutputDirectory)) { |
| | 0 | 67 | | throw new ArgumentException( |
| | | 68 | | $"Worker output directory must be absolute: {rawOutputDirectory}."); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | outputDirectory = Path.GetFullPath(rawOutputDirectory); |
| | 0 | 72 | | return true; |
| | 0 | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | private void Awake() { |
| | 0 | 76 | | if (Instance != null && Instance != this) { |
| | 0 | 77 | | Destroy(gameObject); |
| | 0 | 78 | | return; |
| | | 79 | | } |
| | 0 | 80 | | Instance = this; |
| | 0 | 81 | | DontDestroyOnLoad(gameObject); |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | private void Start() { |
| | 0 | 85 | | if (!IsWorkerMode) { |
| | 0 | 86 | | Destroy(gameObject); |
| | 0 | 87 | | return; |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | Application.targetFrameRate = -1; |
| | 0 | 91 | | StartCoroutine(RunWhenReady()); |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | private void OnDestroy() { |
| | 0 | 95 | | if (SimManager.Instance != null) { |
| | 0 | 96 | | SimManager.Instance.OnSimulationEnded -= RegisterSimulationEnded; |
| | 0 | 97 | | } |
| | 0 | 98 | | if (Instance == this) { |
| | 0 | 99 | | Instance = null; |
| | 0 | 100 | | IsWorkerMode = false; |
| | 0 | 101 | | SimulationConfigFile = null; |
| | 0 | 102 | | Seed = 0; |
| | 0 | 103 | | OutputDirectory = null; |
| | 0 | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | private void Initialize(string simulationConfigFile, int seed, string outputDirectory) { |
| | 0 | 108 | | IsWorkerMode = true; |
| | 0 | 109 | | SimulationConfigFile = simulationConfigFile; |
| | 0 | 110 | | Seed = seed; |
| | 0 | 111 | | OutputDirectory = outputDirectory; |
| | 0 | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | private IEnumerator RunWhenReady() { |
| | 0 | 115 | | while (SimManager.Instance == null) { |
| | 0 | 116 | | yield return null; |
| | 0 | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | SimManager.Instance.AutoRestartOnEnd = false; |
| | 0 | 120 | | SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded; |
| | | 121 | | |
| | | 122 | | // Allow scene initialization and manager subscriptions to finish first. |
| | 0 | 123 | | yield return null; |
| | | 124 | | |
| | 0 | 125 | | PrepareOutputDirectory(); |
| | 0 | 126 | | UnityEngine.Random.InitState(Seed); |
| | 0 | 127 | | _hasStartedRun = true; |
| | 0 | 128 | | Debug.Log($"Starting run with simulation config {SimulationConfigFile} and seed {Seed}."); |
| | 0 | 129 | | SimManager.Instance.LoadNewSimulationConfig(SimulationConfigFile); |
| | 0 | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | private void PrepareOutputDirectory() { |
| | 0 | 133 | | if (Directory.Exists(OutputDirectory)) { |
| | 0 | 134 | | throw new IOException( |
| | | 135 | | $"Output directory already exists: {OutputDirectory}. Refusing to overwrite."); |
| | | 136 | | } |
| | 0 | 137 | | Directory.CreateDirectory(OutputDirectory); |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | private void RegisterSimulationEnded() { |
| | 0 | 141 | | if (!_hasStartedRun || _hasScheduledQuit) { |
| | 0 | 142 | | return; |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | _hasScheduledQuit = true; |
| | 0 | 146 | | StartCoroutine(QuitAfterCleanup()); |
| | 0 | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | private IEnumerator QuitAfterCleanup() { |
| | | 150 | | // Allow end-of-run cleanup, such as log flushing, to complete first. |
| | 0 | 151 | | yield return null; |
| | 0 | 152 | | SimManager.Instance.QuitSimulation(); |
| | 0 | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | private static string GetArgValue(string[] args, string name) { |
| | 0 | 156 | | for (int i = 0; i < args.Length; ++i) { |
| | 0 | 157 | | if (args[i].Equals(name, StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { |
| | 0 | 158 | | return args[i + 1]; |
| | | 159 | | } |
| | 0 | 160 | | if (args[i].StartsWith(name + "=", StringComparison.OrdinalIgnoreCase)) { |
| | 0 | 161 | | return args[i].Substring(name.Length + 1); |
| | | 162 | | } |
| | 0 | 163 | | } |
| | 0 | 164 | | return null; |
| | 0 | 165 | | } |
| | | 166 | | } |