< Summary

Class:ConfigLoader
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Config/ConfigLoader.cs
Covered lines:28
Uncovered lines:17
Coverable lines:45
Total lines:99
Line coverage:62.2% (28 of 45)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:7
Method coverage:85.7% (6 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetStreamingAssetsFilePath(...)0%110100%
LoadFromStreamingAssets(...)0%30500%
LoadAttackBehaviorConfig(...)0%110100%
LoadSimulationConfig(...)0%220100%
LoadSimulatorConfig()0%110100%
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
 153219  public static string GetStreamingAssetsFilePath(string relativePath) {
 153220    return Path.Combine(Application.streamingAssetsPath, relativePath);
 153221  }
 22
 023  public static string LoadFromStreamingAssets(string relativePath) {
 024    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 25
 26#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 027    if (!streamingAssetsPath.StartsWith("file://")) {
 028      streamingAssetsPath = "file://" + streamingAssetsPath;
 029    }
 30#endif
 31
 032    using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) {
 033      www.SendWebRequest();
 034      while (!www.isDone) {}
 35
 036      if (www.result != UnityWebRequest.Result.Success) {
 037        Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}.");
 038        return null;
 39      }
 040      return www.downloadHandler.text;
 41    }
 042  }
 43
 75544  public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) {
 75545    return LoadProtobufConfig<Configs.AttackBehaviorConfig>(
 46        Path.Combine("Configs/Attacks", configFile),
 47        Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength,
 48        Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary);
 75549  }
 50
 1051  public static Configs.SimulationConfig LoadSimulationConfig(string configFile) {
 1052    var config = LoadProtobufConfig<Configs.SimulationConfig>(
 53        Path.Combine("Configs/Simulations", configFile),
 54        Protobuf.Protobuf_SimulationConfig_GetSerializedLength,
 55        Protobuf.Protobuf_SimulationConfig_LoadToBinary);
 2056    if (config != null) {
 1057      UIManager.Instance.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}.");
 1058    }
 1059    return config;
 1060  }
 61
 1062  public static Configs.SimulatorConfig LoadSimulatorConfig() {
 1063    return LoadProtobufConfig<Configs.SimulatorConfig>(
 64        SimulatorConfigRelativePath, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength,
 65        Protobuf.Protobuf_SimulatorConfig_LoadToBinary);
 1066  }
 67
 75568  public static Configs.StaticConfig LoadStaticConfig(string configFile) {
 75569    return LoadProtobufConfig<Configs.StaticConfig>(
 70        Path.Combine("Configs/Models", configFile),
 71        Protobuf.Protobuf_StaticConfig_GetSerializedLength,
 72        Protobuf.Protobuf_StaticConfig_LoadToBinary);
 75573  }
 74
 75  private static T LoadProtobufConfig<T>(string relativePath,
 76                                         SerializedProtobufLengthDelegate serializedLengthFunction,
 77                                         LoadProtobufDelegate loadFunction)
 153078      where T : Google.Protobuf.IMessage<T>, new() {
 153079    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 80
 81    // Determine the length of the serialized Protobuf message.
 153082    Plugin.StatusCode status =
 83        serializedLengthFunction(streamingAssetsPath, out int serializedLength);
 153084    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.
 153091    byte[] buffer = new byte[serializedLength];
 153092    status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength);
 153093    if (status != Plugin.StatusCode.StatusOk) {
 094      Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}.");
 095      return default;
 96    }
 153097    return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength);
 153098  }
 99}