< Summary

Class:ConfigLoader
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Config/ConfigLoader.cs
Covered lines:14
Uncovered lines:31
Coverable lines:45
Total lines:96
Line coverage:31.1% (14 of 45)
Covered branches:0
Total branches:0
Covered methods:2
Total methods:7
Method coverage:28.5% (2 of 7)

Metrics

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

File(s)

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

#LineLine coverage
 1using System.IO;
 2using UnityEngine;
 3using UnityEngine.Networking;
 4
 5public static class ConfigLoader {
 6  // Delegate for loading from a Protobuf text file and returning the length of the serialized
 7  // message as an output argument.
 8  private delegate Plugin.StatusCode SerializedProtobufLengthDelegate(string file,
 9                                                                      out int serializedLength);
 10
 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
 516  public static string GetStreamingAssetsFilePath(string relativePath) {
 517    return Path.Combine(Application.streamingAssetsPath, relativePath);
 518  }
 19
 520  public static string LoadFromStreamingAssets(string relativePath) {
 521    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 22
 23#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 1024    if (!streamingAssetsPath.StartsWith("file://")) {
 525      streamingAssetsPath = "file://" + streamingAssetsPath;
 526    }
 27#endif
 28
 1029    using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) {
 530      www.SendWebRequest();
 26115231      while (!www.isDone) {}
 32
 533      if (www.result != UnityWebRequest.Result.Success) {
 034        Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}.");
 035        return null;
 36      }
 537      return www.downloadHandler.text;
 38    }
 539  }
 40
 041  public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) {
 042    return LoadProtobufConfig<Configs.AttackBehaviorConfig>(
 43        Path.Combine("Configs/Attacks", configFile),
 44        Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength,
 45        Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary);
 046  }
 47
 048  public static Configs.SimulationConfig LoadSimulationConfig(string configFile) {
 049    var config = LoadProtobufConfig<Configs.SimulationConfig>(
 50        Path.Combine("Configs/Simulations", configFile),
 51        Protobuf.Protobuf_SimulationConfig_GetSerializedLength,
 52        Protobuf.Protobuf_SimulationConfig_LoadToBinary);
 053    if (config != null) {
 054      UIManager.Instance?.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}.");
 055    }
 056    return config;
 057  }
 58
 059  public static Configs.SimulatorConfig LoadSimulatorConfig(string configFile) {
 060    return LoadProtobufConfig<Configs.SimulatorConfig>(
 61        configFile, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength,
 62        Protobuf.Protobuf_SimulatorConfig_LoadToBinary);
 063  }
 64
 065  public static Configs.StaticConfig LoadStaticConfig(string configFile) {
 066    return LoadProtobufConfig<Configs.StaticConfig>(
 67        Path.Combine("Configs/Models", configFile),
 68        Protobuf.Protobuf_StaticConfig_GetSerializedLength,
 69        Protobuf.Protobuf_StaticConfig_LoadToBinary);
 070  }
 71
 72  private static T LoadProtobufConfig<T>(string relativePath,
 73                                         SerializedProtobufLengthDelegate serializedLengthFunction,
 74                                         LoadProtobufDelegate loadFunction)
 075      where T : Google.Protobuf.IMessage<T>, new() {
 076    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 77
 78    // Determine the length of the serialized Protobuf message.
 079    Plugin.StatusCode status =
 80        serializedLengthFunction(streamingAssetsPath, out int serializedLength);
 081    if (status != Plugin.StatusCode.StatusOk) {
 082      Debug.Log(
 83          $"Failed to get the length of the serialized message from the Protobuf text file {relativePath} with status co
 084      return default;
 85    }
 86
 87    // Load the Protobuf message to binary format.
 088    byte[] buffer = new byte[serializedLength];
 089    status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength);
 090    if (status != Plugin.StatusCode.StatusOk) {
 091      Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}.");
 092      return default;
 93    }
 094    return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength);
 095  }
 96}