< Summary

Class:ConfigLoader
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Config/ConfigLoader.cs
Covered lines:28
Uncovered lines:20
Coverable lines:48
Total lines:102
Line coverage:58.3% (28 of 48)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:8
Method coverage:75% (6 of 8)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetStreamingAssetsFilePath(...)0%110100%
LoadFromStreamingAssets(...)0%30500%
LoadAttackBehaviorConfig(...)0%110100%
LoadRunConfig(...)0%2100%
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
 195016  public static string GetStreamingAssetsFilePath(string relativePath) {
 195017    return Path.Combine(Application.streamingAssetsPath, relativePath);
 195018  }
 19
 020  public static string LoadFromStreamingAssets(string relativePath) {
 021    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 22
 23#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
 024    if (!streamingAssetsPath.StartsWith("file://")) {
 025      streamingAssetsPath = "file://" + streamingAssetsPath;
 026    }
 27#endif
 28
 029    using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) {
 030      www.SendWebRequest();
 031      while (!www.isDone) {}
 32
 033      if (www.result != UnityWebRequest.Result.Success) {
 034        Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}.");
 035        return null;
 36      }
 037      return www.downloadHandler.text;
 38    }
 039  }
 40
 95541  public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) {
 95542    return LoadProtobufConfig<Configs.AttackBehaviorConfig>(
 43        Path.Combine("Configs/Attacks", configFile),
 44        Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength,
 45        Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary);
 95546  }
 47
 048  public static Configs.RunConfig LoadRunConfig(string configFile) {
 049    return LoadProtobufConfig<Configs.RunConfig>(Path.Combine("Configs/Runs", configFile),
 50                                                 Protobuf.Protobuf_RunConfig_GetSerializedLength,
 51                                                 Protobuf.Protobuf_RunConfig_LoadToBinary);
 052  }
 53
 1254  public static Configs.SimulationConfig LoadSimulationConfig(string configFile) {
 1255    var config = LoadProtobufConfig<Configs.SimulationConfig>(
 56        Path.Combine("Configs/Simulations", configFile),
 57        Protobuf.Protobuf_SimulationConfig_GetSerializedLength,
 58        Protobuf.Protobuf_SimulationConfig_LoadToBinary);
 2459    if (config != null) {
 1260      UIManager.Instance.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}.");
 1261    }
 1262    return config;
 1263  }
 64
 1265  public static Configs.SimulatorConfig LoadSimulatorConfig(string configFile) {
 1266    return LoadProtobufConfig<Configs.SimulatorConfig>(
 67        configFile, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength,
 68        Protobuf.Protobuf_SimulatorConfig_LoadToBinary);
 1269  }
 70
 96971  public static Configs.StaticConfig LoadStaticConfig(string configFile) {
 96972    return LoadProtobufConfig<Configs.StaticConfig>(
 73        Path.Combine("Configs/Models", configFile),
 74        Protobuf.Protobuf_StaticConfig_GetSerializedLength,
 75        Protobuf.Protobuf_StaticConfig_LoadToBinary);
 96976  }
 77
 78  private static T LoadProtobufConfig<T>(string relativePath,
 79                                         SerializedProtobufLengthDelegate serializedLengthFunction,
 80                                         LoadProtobufDelegate loadFunction)
 194881      where T : Google.Protobuf.IMessage<T>, new() {
 194882    string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath);
 83
 84    // Determine the length of the serialized Protobuf message.
 194885    Plugin.StatusCode status =
 86        serializedLengthFunction(streamingAssetsPath, out int serializedLength);
 194887    if (status != Plugin.StatusCode.StatusOk) {
 088      Debug.Log(
 89          $"Failed to get the length of the serialized message from the Protobuf text file {relativePath} with status co
 090      return default;
 91    }
 92
 93    // Load the Protobuf message to binary format.
 194894    byte[] buffer = new byte[serializedLength];
 194895    status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength);
 194896    if (status != Plugin.StatusCode.StatusOk) {
 097      Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}.");
 098      return default;
 99    }
 1948100    return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength);
 1948101  }
 102}