< 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:95
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  // 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
 515  public static string GetStreamingAssetsFilePath(string relativePath) {
 516    return Path.Combine(Application.streamingAssetsPath, relativePath);
 517  }
 18
 519  public static string LoadFromStreamingAssets(string relativePath) {
 520    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 21
 22#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 1023    if (!streamingAssetsPath.StartsWith("file://")) {
 524      streamingAssetsPath = "file://" + streamingAssetsPath;
 525    }
 26#endif
 27
 1028    using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) {
 529      www.SendWebRequest();
 1487630      while (!www.isDone) {}
 31
 532      if (www.result != UnityWebRequest.Result.Success) {
 033        Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}.");
 034        return null;
 35      }
 536      return www.downloadHandler.text;
 37    }
 538  }
 39
 040  public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) {
 041    return LoadProtobufConfig<Configs.AttackBehaviorConfig>(
 42        Path.Combine("Configs/Attacks", configFile),
 43        Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength,
 44        Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary);
 045  }
 46
 047  public static Configs.SimulationConfig LoadSimulationConfig(string configFile) {
 048    var config = LoadProtobufConfig<Configs.SimulationConfig>(
 49        Path.Combine("Configs/Simulations", configFile),
 50        Protobuf.Protobuf_SimulationConfig_GetSerializedLength,
 51        Protobuf.Protobuf_SimulationConfig_LoadToBinary);
 052    if (config != null) {
 053      UIManager.Instance?.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}.");
 054    }
 055    return config;
 056  }
 57
 058  public static Configs.SimulatorConfig LoadSimulatorConfig(string configFile) {
 059    return LoadProtobufConfig<Configs.SimulatorConfig>(
 60        configFile, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength,
 61        Protobuf.Protobuf_SimulatorConfig_LoadToBinary);
 062  }
 63
 064  public static Configs.StaticConfig LoadStaticConfig(string configFile) {
 065    return LoadProtobufConfig<Configs.StaticConfig>(
 66        Path.Combine("Configs/Models", configFile),
 67        Protobuf.Protobuf_StaticConfig_GetSerializedLength,
 68        Protobuf.Protobuf_StaticConfig_LoadToBinary);
 069  }
 70
 71  private static T LoadProtobufConfig<T>(string relativePath,
 72                                         SerializedProtobufLengthDelegate serializedLengthFunction,
 73                                         LoadProtobufDelegate loadFunction)
 074      where T : Google.Protobuf.IMessage<T>, new() {
 075    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 76
 77    // Determine the length of the serialized Protobuf message.
 078    Plugin.StatusCode status =
 79        serializedLengthFunction(streamingAssetsPath, out int serializedLength);
 080    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.
 087    byte[] buffer = new byte[serializedLength];
 088    status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength);
 089    if (status != Plugin.StatusCode.StatusOk) {
 090      Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}.");
 091      return default;
 92    }
 093    return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength);
 094  }
 95}