| | 1 | | using System.IO; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Networking; |
| | 4 | | using Newtonsoft.Json; |
| | 5 | |
|
| | 6 | | public static class ConfigLoader { |
| 24 | 7 | | public static string LoadFromStreamingAssets(string relativePath) { |
| 24 | 8 | | string filePath = Path.Combine(Application.streamingAssetsPath, relativePath); |
| | 9 | |
|
| | 10 | | #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS |
| 48 | 11 | | if (!filePath.StartsWith("file://")) { |
| 24 | 12 | | filePath = "file://" + filePath; |
| 24 | 13 | | } |
| | 14 | | #endif |
| | 15 | |
|
| 24 | 16 | | UnityWebRequest www = UnityWebRequest.Get(filePath); |
| 24 | 17 | | www.SendWebRequest(); |
| | 18 | |
|
| | 19 | | // Wait for the request to complete |
| 54878 | 20 | | while (!www.isDone) { |
| | 21 | | // You might want to yield return null here if this is called from a coroutine |
| 27427 | 22 | | } |
| | 23 | |
|
| 24 | 24 | | if (www.result != UnityWebRequest.Result.Success) { |
| 0 | 25 | | Debug.LogError($"Error loading file at {filePath}: {www.error}"); |
| 0 | 26 | | return null; |
| | 27 | | } |
| | 28 | |
|
| 24 | 29 | | return www.downloadHandler.text; |
| 24 | 30 | | } |
| | 31 | |
|
| 1 | 32 | | public static SimulationConfig LoadSimulationConfig(string configFileName) { |
| 1 | 33 | | string relativePath = Path.Combine("Configs", configFileName); |
| 1 | 34 | | string fileContent = LoadFromStreamingAssets(relativePath); |
| | 35 | |
|
| 1 | 36 | | if (string.IsNullOrEmpty(fileContent)) { |
| 0 | 37 | | Debug.LogError($"Failed to load SimulationConfig from {relativePath}"); |
| 0 | 38 | | return null; |
| | 39 | | } |
| | 40 | |
|
| 1 | 41 | | return JsonConvert.DeserializeObject<SimulationConfig>(fileContent, new JsonSerializerSettings { |
| | 42 | | Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() } |
| | 43 | | }); |
| 1 | 44 | | } |
| | 45 | |
|
| 8 | 46 | | public static StaticAgentConfig LoadStaticAgentConfig(string configFileName) { |
| 8 | 47 | | string relativePath = Path.Combine("Configs/Models", configFileName); |
| 8 | 48 | | string fileContent = LoadFromStreamingAssets(relativePath); |
| | 49 | |
|
| 8 | 50 | | if (string.IsNullOrEmpty(fileContent)) { |
| 0 | 51 | | Debug.LogError($"Failed to load StaticAgentConfig from {relativePath}"); |
| 0 | 52 | | return null; |
| | 53 | | } |
| | 54 | |
|
| 8 | 55 | | return JsonConvert.DeserializeObject<StaticAgentConfig>( |
| | 56 | | fileContent, new JsonSerializerSettings { |
| | 57 | | Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() } |
| | 58 | | }); |
| 8 | 59 | | } |
| | 60 | |
|
| 1 | 61 | | public static SimulatorConfig LoadSimulatorConfig() { |
| 1 | 62 | | string relativePath = "simulator.json"; // Path relative to StreamingAssets |
| 1 | 63 | | string fileContent = LoadFromStreamingAssets(relativePath); |
| 1 | 64 | | return JsonConvert.DeserializeObject<SimulatorConfig>(fileContent); |
| 1 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | public static void PrintSimulationConfig(SimulationConfig config) { |
| 0 | 68 | | if (config == null) { |
| 0 | 69 | | Debug.Log("SimulationConfig is null"); |
| 0 | 70 | | return; |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | Debug.Log("SimulationConfig:"); |
| 0 | 74 | | Debug.Log($"Time Scale: {config.timeScale}"); |
| | 75 | |
|
| 0 | 76 | | Debug.Log("Interceptor Swarm Configurations:"); |
| 0 | 77 | | for (int i = 0; i < config.interceptor_swarm_configs.Count; i++) { |
| 0 | 78 | | PrintSwarmConfig(config.interceptor_swarm_configs[i], $"Interceptor Swarm {i + 1}"); |
| 0 | 79 | | } |
| | 80 | |
|
| 0 | 81 | | Debug.Log("Threat Swarm Configurations:"); |
| 0 | 82 | | for (int i = 0; i < config.threat_swarm_configs.Count; i++) { |
| 0 | 83 | | PrintSwarmConfig(config.threat_swarm_configs[i], $"Threat Swarm {i + 1}"); |
| 0 | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| 0 | 87 | | private static void PrintSwarmConfig(SwarmConfig swarmConfig, string swarmName) { |
| 0 | 88 | | Debug.Log($"{swarmName}:"); |
| 0 | 89 | | Debug.Log($" Number of Agents: {swarmConfig.num_agents}"); |
| 0 | 90 | | PrintDynamicAgentConfig(swarmConfig.dynamic_agent_config); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | private static void PrintDynamicAgentConfig(DynamicAgentConfig dynamicAgentConfig) { |
| 0 | 94 | | Debug.Log(" Agent Configuration:"); |
| 0 | 95 | | Debug.Log($" Interceptor Model: {dynamicAgentConfig.agent_model}"); |
| 0 | 96 | | Debug.Log($" Threat Model: {dynamicAgentConfig.agent_model}"); |
| 0 | 97 | | PrintInitialState(dynamicAgentConfig.initial_state); |
| 0 | 98 | | PrintStandardDeviation(dynamicAgentConfig.standard_deviation); |
| 0 | 99 | | PrintDynamicConfig(dynamicAgentConfig.dynamic_config); |
| 0 | 100 | | PrintPlottingConfig(dynamicAgentConfig.plotting_config); |
| 0 | 101 | | PrintSubmunitionsConfig(dynamicAgentConfig.submunitions_config); |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | private static void PrintInitialState(InitialState initialState) { |
| 0 | 105 | | Debug.Log(" Initial State:"); |
| 0 | 106 | | Debug.Log($" Position: {initialState.position}"); |
| 0 | 107 | | Debug.Log($" Rotation: {initialState.rotation}"); |
| 0 | 108 | | Debug.Log($" Velocity: {initialState.velocity}"); |
| 0 | 109 | | } |
| | 110 | |
|
| 0 | 111 | | private static void PrintStandardDeviation(StandardDeviation standardDeviation) { |
| 0 | 112 | | Debug.Log(" Standard Deviation:"); |
| 0 | 113 | | Debug.Log($" Position: {standardDeviation.position}"); |
| 0 | 114 | | Debug.Log($" Velocity: {standardDeviation.velocity}"); |
| 0 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | private static void PrintDynamicConfig(DynamicConfig dynamicConfig) { |
| 0 | 118 | | Debug.Log(" Dynamic Configuration:"); |
| 0 | 119 | | Debug.Log($" Launch Time: {dynamicConfig.launch_config.launch_time}"); |
| 0 | 120 | | Debug.Log($" Sensor Type: {dynamicConfig.sensor_config.type}"); |
| 0 | 121 | | Debug.Log($" Sensor Frequency: {dynamicConfig.sensor_config.frequency}"); |
| 0 | 122 | | } |
| | 123 | |
|
| 0 | 124 | | private static void PrintPlottingConfig(PlottingConfig plottingConfig) { |
| 0 | 125 | | Debug.Log(" Plotting Configuration:"); |
| 0 | 126 | | Debug.Log($" Color: {plottingConfig.color}"); |
| 0 | 127 | | Debug.Log($" Line Style: {plottingConfig.linestyle}"); |
| 0 | 128 | | Debug.Log($" Marker: {plottingConfig.marker}"); |
| 0 | 129 | | } |
| | 130 | |
|
| 0 | 131 | | private static void PrintSubmunitionsConfig(SubmunitionsConfig submunitionsConfig) { |
| 0 | 132 | | if (submunitionsConfig == null) { |
| 0 | 133 | | Debug.Log(" Submunitions Configuration: None"); |
| 0 | 134 | | return; |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | Debug.Log(" Submunitions Configuration:"); |
| 0 | 138 | | Debug.Log($" Number of Submunitions: {submunitionsConfig.num_submunitions}"); |
| 0 | 139 | | Debug.Log($" Dispense Time: {submunitionsConfig.dispense_time}"); |
| 0 | 140 | | PrintSubmunitionDynamicAgentConfig(submunitionsConfig.dynamic_agent_config); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | private static void PrintSubmunitionDynamicAgentConfig( |
| 0 | 144 | | SubmunitionDynamicAgentConfig agentConfig) { |
| 0 | 145 | | Debug.Log(" Submunition Agent Configuration:"); |
| 0 | 146 | | Debug.Log($" Interceptor Model: {agentConfig.agent_model}"); |
| 0 | 147 | | Debug.Log($" Threat Model: {agentConfig.agent_model}"); |
| 0 | 148 | | PrintInitialState(agentConfig.initial_state); |
| 0 | 149 | | PrintStandardDeviation(agentConfig.standard_deviation); |
| 0 | 150 | | PrintDynamicConfig(agentConfig.dynamic_config); |
| 0 | 151 | | PrintPlottingConfig(agentConfig.plotting_config); |
| 0 | 152 | | } |
| | 153 | | } |