< Summary

Class:ConfigLoader
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Config/ConfigLoader.cs
Covered lines:29
Uncovered lines:77
Coverable lines:106
Total lines:153
Line coverage:27.3% (29 of 106)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:13
Method coverage:30.7% (4 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadFromStreamingAssets(...)0%4.094082.35%
LoadSimulationConfig(...)0%2.152066.67%
LoadStaticAgentConfig(...)0%2.152066.67%
LoadSimulatorConfig()0%110100%
PrintSimulationConfig(...)0%20400%
PrintSwarmConfig(...)0%2100%
PrintDynamicAgentConfig(...)0%2100%
PrintInitialState(...)0%2100%
PrintStandardDeviation(...)0%2100%
PrintDynamicConfig(...)0%2100%
PrintPlottingConfig(...)0%2100%
PrintSubmunitionsConfig(...)0%6200%
PrintSubmunitionDynamicAgentConfig(...)0%2100%

File(s)

/github/workspace/Assets/Scripts/Config/ConfigLoader.cs

#LineLine coverage
 1using System.IO;
 2using UnityEngine;
 3using UnityEngine.Networking;
 4using Newtonsoft.Json;
 5
 6public static class ConfigLoader {
 247  public static string LoadFromStreamingAssets(string relativePath) {
 248    string filePath = Path.Combine(Application.streamingAssetsPath, relativePath);
 9
 10#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 4811    if (!filePath.StartsWith("file://")) {
 2412      filePath = "file://" + filePath;
 2413    }
 14#endif
 15
 2416    UnityWebRequest www = UnityWebRequest.Get(filePath);
 2417    www.SendWebRequest();
 18
 19    // Wait for the request to complete
 5487820    while (!www.isDone) {
 21      // You might want to yield return null here if this is called from a coroutine
 2742722    }
 23
 2424    if (www.result != UnityWebRequest.Result.Success) {
 025      Debug.LogError($"Error loading file at {filePath}: {www.error}");
 026      return null;
 27    }
 28
 2429    return www.downloadHandler.text;
 2430  }
 31
 132  public static SimulationConfig LoadSimulationConfig(string configFileName) {
 133    string relativePath = Path.Combine("Configs", configFileName);
 134    string fileContent = LoadFromStreamingAssets(relativePath);
 35
 136    if (string.IsNullOrEmpty(fileContent)) {
 037      Debug.LogError($"Failed to load SimulationConfig from {relativePath}");
 038      return null;
 39    }
 40
 141    return JsonConvert.DeserializeObject<SimulationConfig>(fileContent, new JsonSerializerSettings {
 42      Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
 43    });
 144  }
 45
 846  public static StaticAgentConfig LoadStaticAgentConfig(string configFileName) {
 847    string relativePath = Path.Combine("Configs/Models", configFileName);
 848    string fileContent = LoadFromStreamingAssets(relativePath);
 49
 850    if (string.IsNullOrEmpty(fileContent)) {
 051      Debug.LogError($"Failed to load StaticAgentConfig from {relativePath}");
 052      return null;
 53    }
 54
 855    return JsonConvert.DeserializeObject<StaticAgentConfig>(
 56        fileContent, new JsonSerializerSettings {
 57          Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
 58        });
 859  }
 60
 161  public static SimulatorConfig LoadSimulatorConfig() {
 162    string relativePath = "simulator.json";  // Path relative to StreamingAssets
 163    string fileContent = LoadFromStreamingAssets(relativePath);
 164    return JsonConvert.DeserializeObject<SimulatorConfig>(fileContent);
 165  }
 66
 067  public static void PrintSimulationConfig(SimulationConfig config) {
 068    if (config == null) {
 069      Debug.Log("SimulationConfig is null");
 070      return;
 71    }
 72
 073    Debug.Log("SimulationConfig:");
 074    Debug.Log($"Time Scale: {config.timeScale}");
 75
 076    Debug.Log("Interceptor Swarm Configurations:");
 077    for (int i = 0; i < config.interceptor_swarm_configs.Count; i++) {
 078      PrintSwarmConfig(config.interceptor_swarm_configs[i], $"Interceptor Swarm {i + 1}");
 079    }
 80
 081    Debug.Log("Threat Swarm Configurations:");
 082    for (int i = 0; i < config.threat_swarm_configs.Count; i++) {
 083      PrintSwarmConfig(config.threat_swarm_configs[i], $"Threat Swarm {i + 1}");
 084    }
 085  }
 86
 087  private static void PrintSwarmConfig(SwarmConfig swarmConfig, string swarmName) {
 088    Debug.Log($"{swarmName}:");
 089    Debug.Log($"  Number of Agents: {swarmConfig.num_agents}");
 090    PrintDynamicAgentConfig(swarmConfig.dynamic_agent_config);
 091  }
 92
 093  private static void PrintDynamicAgentConfig(DynamicAgentConfig dynamicAgentConfig) {
 094    Debug.Log("  Agent Configuration:");
 095    Debug.Log($"    Interceptor Model: {dynamicAgentConfig.agent_model}");
 096    Debug.Log($"    Threat Model: {dynamicAgentConfig.agent_model}");
 097    PrintInitialState(dynamicAgentConfig.initial_state);
 098    PrintStandardDeviation(dynamicAgentConfig.standard_deviation);
 099    PrintDynamicConfig(dynamicAgentConfig.dynamic_config);
 0100    PrintPlottingConfig(dynamicAgentConfig.plotting_config);
 0101    PrintSubmunitionsConfig(dynamicAgentConfig.submunitions_config);
 0102  }
 103
 0104  private static void PrintInitialState(InitialState initialState) {
 0105    Debug.Log("    Initial State:");
 0106    Debug.Log($"      Position: {initialState.position}");
 0107    Debug.Log($"      Rotation: {initialState.rotation}");
 0108    Debug.Log($"      Velocity: {initialState.velocity}");
 0109  }
 110
 0111  private static void PrintStandardDeviation(StandardDeviation standardDeviation) {
 0112    Debug.Log("    Standard Deviation:");
 0113    Debug.Log($"      Position: {standardDeviation.position}");
 0114    Debug.Log($"      Velocity: {standardDeviation.velocity}");
 0115  }
 116
 0117  private static void PrintDynamicConfig(DynamicConfig dynamicConfig) {
 0118    Debug.Log("    Dynamic Configuration:");
 0119    Debug.Log($"      Launch Time: {dynamicConfig.launch_config.launch_time}");
 0120    Debug.Log($"      Sensor Type: {dynamicConfig.sensor_config.type}");
 0121    Debug.Log($"      Sensor Frequency: {dynamicConfig.sensor_config.frequency}");
 0122  }
 123
 0124  private static void PrintPlottingConfig(PlottingConfig plottingConfig) {
 0125    Debug.Log("    Plotting Configuration:");
 0126    Debug.Log($"      Color: {plottingConfig.color}");
 0127    Debug.Log($"      Line Style: {plottingConfig.linestyle}");
 0128    Debug.Log($"      Marker: {plottingConfig.marker}");
 0129  }
 130
 0131  private static void PrintSubmunitionsConfig(SubmunitionsConfig submunitionsConfig) {
 0132    if (submunitionsConfig == null) {
 0133      Debug.Log("    Submunitions Configuration: None");
 0134      return;
 135    }
 136
 0137    Debug.Log("    Submunitions Configuration:");
 0138    Debug.Log($"      Number of Submunitions: {submunitionsConfig.num_submunitions}");
 0139    Debug.Log($"      Dispense Time: {submunitionsConfig.dispense_time}");
 0140    PrintSubmunitionDynamicAgentConfig(submunitionsConfig.dynamic_agent_config);
 0141  }
 142
 143  private static void PrintSubmunitionDynamicAgentConfig(
 0144      SubmunitionDynamicAgentConfig agentConfig) {
 0145    Debug.Log("      Submunition Agent Configuration:");
 0146    Debug.Log($"        Interceptor Model: {agentConfig.agent_model}");
 0147    Debug.Log($"        Threat Model: {agentConfig.agent_model}");
 0148    PrintInitialState(agentConfig.initial_state);
 0149    PrintStandardDeviation(agentConfig.standard_deviation);
 0150    PrintDynamicConfig(agentConfig.dynamic_config);
 0151    PrintPlottingConfig(agentConfig.plotting_config);
 0152  }
 153}