< Summary

Class:ConfigLoader
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Config/ConfigLoader.cs
Covered lines:29
Uncovered lines:16
Coverable lines:45
Total lines:99
Line coverage:64.4% (29 of 45)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:7
Method coverage:71.4% (5 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetStreamingAssetsFilePath(...)0%110100%
LoadFromStreamingAssets(...)0%5.125083.33%
LoadAttackBehaviorConfig(...)0%110100%
LoadSimulationConfig(...)0%6200%
LoadSimulatorConfig()0%2100%
LoadStaticConfig(...)0%110100%
LoadProtobufConfig[T](...)0%5.024060%

File(s)

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

#LineLine coverage
 1using System;
 2using System.IO;
 3using UnityEngine;
 4using UnityEngine.Networking;
 5
 6public static class ConfigLoader {
 7  // Delegate for loading from a Protobuf text file and returning the length of the serialized
 8  // message as an output argument.
 9  private delegate Plugin.StatusCode SerializedProtobufLengthDelegate(string file,
 10                                                                      out int serializedLength);
 11  // Delegate for loading from a Protobuf text file to binary format and returning the length of the
 12  // serialized message as an output argument.
 13  private delegate Plugin.StatusCode LoadProtobufDelegate(string file, byte[] buffer,
 14                                                          int bufferSize, out int serializedLength);
 15
 16  // Relative path to the default simulator configuration.
 17  private const string SimulatorConfigRelativePath = "simulator.pbtxt";
 18
 5319  public static string GetStreamingAssetsFilePath(string relativePath) {
 5320    return Path.Combine(Application.streamingAssetsPath, relativePath);
 5321  }
 22
 223  public static string LoadFromStreamingAssets(string relativePath) {
 224    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 25
 26#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 427    if (!streamingAssetsPath.StartsWith("file://")) {
 228      streamingAssetsPath = "file://" + streamingAssetsPath;
 229    }
 30#endif
 31
 432    using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) {
 233      www.SendWebRequest();
 369234      while (!www.isDone) {}
 35
 236      if (www.result != UnityWebRequest.Result.Success) {
 037        Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}.");
 038        return null;
 39      }
 240      return www.downloadHandler.text;
 41    }
 242  }
 43
 1544  public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) {
 1545    return LoadProtobufConfig<Configs.AttackBehaviorConfig>(
 46        Path.Combine("Configs/Attacks", configFile),
 47        Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength,
 48        Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary);
 1549  }
 50
 051  public static Configs.SimulationConfig LoadSimulationConfig(string configFile) {
 052    var config = LoadProtobufConfig<Configs.SimulationConfig>(
 53        Path.Combine("Configs/Simulations", configFile),
 54        Protobuf.Protobuf_SimulationConfig_GetSerializedLength,
 55        Protobuf.Protobuf_SimulationConfig_LoadToBinary);
 056    if (config != null) {
 057      UIManager.Instance.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}.");
 058    }
 059    return config;
 060  }
 61
 062  public static Configs.SimulatorConfig LoadSimulatorConfig() {
 063    return LoadProtobufConfig<Configs.SimulatorConfig>(
 64        SimulatorConfigRelativePath, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength,
 65        Protobuf.Protobuf_SimulatorConfig_LoadToBinary);
 066  }
 67
 2268  public static Configs.StaticConfig LoadStaticConfig(string configFile) {
 2269    return LoadProtobufConfig<Configs.StaticConfig>(
 70        Path.Combine("Configs/Models", configFile),
 71        Protobuf.Protobuf_StaticConfig_GetSerializedLength,
 72        Protobuf.Protobuf_StaticConfig_LoadToBinary);
 2273  }
 74
 75  private static T LoadProtobufConfig<T>(string relativePath,
 76                                         SerializedProtobufLengthDelegate serializedLengthFunction,
 77                                         LoadProtobufDelegate loadFunction)
 3778      where T : Google.Protobuf.IMessage<T>, new() {
 3779    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 80
 81    // Determine the length of the serialized Protobuf message.
 3782    Plugin.StatusCode status =
 83        serializedLengthFunction(streamingAssetsPath, out int serializedLength);
 3784    if (status != Plugin.StatusCode.StatusOk) {
 085      Debug.Log(
 86          $"Failed to get the length of the serialized message from the Protobuf text file {relativePath} with status co
 087      return default;
 88    }
 89
 90    // Load the Protobuf message to binary format.
 3791    byte[] buffer = new byte[serializedLength];
 3792    status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength);
 3793    if (status != Plugin.StatusCode.StatusOk) {
 094      Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}.");
 095      return default;
 96    }
 3797    return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength);
 3798  }
 99}