< Summary

Class:ConfigLoader
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Config/ConfigLoader.cs
Covered lines:18
Uncovered lines:88
Coverable lines:106
Total lines:153
Line coverage:16.9% (18 of 106)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:13
Method coverage:15.3% (2 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadFromStreamingAssets(...)0%4.094082.35%
LoadSimulationConfig(...)0%6200%
LoadStaticAgentConfig(...)0%2.152066.67%
LoadSimulatorConfig()0%2100%
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 {
 457  public static string LoadFromStreamingAssets(string relativePath) {
 458    string filePath = Path.Combine(Application.streamingAssetsPath, relativePath);
 9
 10#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 9011    if (!filePath.StartsWith("file://")) {
 4512      filePath = "file://" + filePath;
 4513    }
 14#endif
 15
 4516    UnityWebRequest www = UnityWebRequest.Get(filePath);
 4517    www.SendWebRequest();
 18
 19    // Wait for the request to complete
 9931920    while (!www.isDone) {
 21      // You might want to yield return null here if this is called from a coroutine
 4963722    }
 23
 4524    if (www.result != UnityWebRequest.Result.Success) {
 025      Debug.LogError($"Error loading file at {filePath}: {www.error}");
 026      return null;
 27    }
 28
 4529    return www.downloadHandler.text;
 4530  }
 31
 032  public static SimulationConfig LoadSimulationConfig(string configFileName) {
 033    string relativePath = Path.Combine("Configs", configFileName);
 034    string fileContent = LoadFromStreamingAssets(relativePath);
 35
 036    if (string.IsNullOrEmpty(fileContent)) {
 037      Debug.LogError($"Failed to load SimulationConfig from {relativePath}");
 038      return null;
 39    }
 40
 041    return JsonConvert.DeserializeObject<SimulationConfig>(fileContent, new JsonSerializerSettings {
 42      Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
 43    });
 044  }
 45
 1546  public static StaticAgentConfig LoadStaticAgentConfig(string configFileName) {
 1547    string relativePath = Path.Combine("Configs/Models", configFileName);
 1548    string fileContent = LoadFromStreamingAssets(relativePath);
 49
 1550    if (string.IsNullOrEmpty(fileContent)) {
 051      Debug.LogError($"Failed to load StaticAgentConfig from {relativePath}");
 052      return null;
 53    }
 54
 1555    return JsonConvert.DeserializeObject<StaticAgentConfig>(
 56        fileContent, new JsonSerializerSettings {
 57          Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
 58        });
 1559  }
 60
 061  public static SimulatorConfig LoadSimulatorConfig() {
 062    string relativePath = "simulator.json";  // Path relative to StreamingAssets
 063    string fileContent = LoadFromStreamingAssets(relativePath);
 064    return JsonConvert.DeserializeObject<SimulatorConfig>(fileContent);
 065  }
 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}