< 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:95
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%330100%
LoadSimulatorConfig(...)0%110100%
LoadStaticConfig(...)0%110100%
LoadProtobufConfig[T](...)0%5.024060%

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  // Delegate for loading from a Protobuf text file to binary format and returning the length of the
 11  // serialized message as an output argument.
 12  private delegate Plugin.StatusCode LoadProtobufDelegate(string file, byte[] buffer,
 13                                                          int bufferSize, out int serializedLength);
 14
 196215  public static string GetStreamingAssetsFilePath(string relativePath) {
 196216    return Path.Combine(Application.streamingAssetsPath, relativePath);
 196217  }
 18
 019  public static string LoadFromStreamingAssets(string relativePath) {
 020    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 21
 22#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 023    if (!streamingAssetsPath.StartsWith("file://")) {
 024      streamingAssetsPath = "file://" + streamingAssetsPath;
 025    }
 26#endif
 27
 028    using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) {
 029      www.SendWebRequest();
 030      while (!www.isDone) {}
 31
 032      if (www.result != UnityWebRequest.Result.Success) {
 033        Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}.");
 034        return null;
 35      }
 036      return www.downloadHandler.text;
 37    }
 038  }
 39
 95540  public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) {
 95541    return LoadProtobufConfig<Configs.AttackBehaviorConfig>(
 42        Path.Combine("Configs/Attacks", configFile),
 43        Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength,
 44        Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary);
 95545  }
 46
 1247  public static Configs.SimulationConfig LoadSimulationConfig(string configFile) {
 1248    var config = LoadProtobufConfig<Configs.SimulationConfig>(
 49        Path.Combine("Configs/Simulations", configFile),
 50        Protobuf.Protobuf_SimulationConfig_GetSerializedLength,
 51        Protobuf.Protobuf_SimulationConfig_LoadToBinary);
 2452    if (config != null) {
 1253      UIManager.Instance?.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}.");
 1254    }
 1255    return config;
 1256  }
 57
 1258  public static Configs.SimulatorConfig LoadSimulatorConfig(string configFile) {
 1259    return LoadProtobufConfig<Configs.SimulatorConfig>(
 60        configFile, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength,
 61        Protobuf.Protobuf_SimulatorConfig_LoadToBinary);
 1262  }
 63
 98164  public static Configs.StaticConfig LoadStaticConfig(string configFile) {
 98165    return LoadProtobufConfig<Configs.StaticConfig>(
 66        Path.Combine("Configs/Models", configFile),
 67        Protobuf.Protobuf_StaticConfig_GetSerializedLength,
 68        Protobuf.Protobuf_StaticConfig_LoadToBinary);
 98169  }
 70
 71  private static T LoadProtobufConfig<T>(string relativePath,
 72                                         SerializedProtobufLengthDelegate serializedLengthFunction,
 73                                         LoadProtobufDelegate loadFunction)
 196074      where T : Google.Protobuf.IMessage<T>, new() {
 196075    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 76
 77    // Determine the length of the serialized Protobuf message.
 196078    Plugin.StatusCode status =
 79        serializedLengthFunction(streamingAssetsPath, out int serializedLength);
 196080    if (status != Plugin.StatusCode.StatusOk) {
 081      Debug.Log(
 82          $"Failed to get the length of the serialized message from the Protobuf text file {relativePath} with status co
 083      return default;
 84    }
 85
 86    // Load the Protobuf message to binary format.
 196087    byte[] buffer = new byte[serializedLength];
 196088    status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength);
 196089    if (status != Plugin.StatusCode.StatusOk) {
 090      Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}.");
 091      return default;
 92    }
 196093    return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength);
 196094  }
 95}