| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | |
|
| | 6 | | public 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 | |
|
| 1532 | 19 | | public static string GetStreamingAssetsFilePath(string relativePath) { |
| 1532 | 20 | | return Path.Combine(Application.streamingAssetsPath, relativePath); |
| 1532 | 21 | | } |
| | 22 | |
|
| 0 | 23 | | public static string LoadFromStreamingAssets(string relativePath) { |
| 0 | 24 | | string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath); |
| | 25 | |
|
| | 26 | | #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS |
| 0 | 27 | | if (!streamingAssetsPath.StartsWith("file://")) { |
| 0 | 28 | | streamingAssetsPath = "file://" + streamingAssetsPath; |
| 0 | 29 | | } |
| | 30 | | #endif |
| | 31 | |
|
| 0 | 32 | | using (UnityWebRequest www = UnityWebRequest.Get(streamingAssetsPath)) { |
| 0 | 33 | | www.SendWebRequest(); |
| 0 | 34 | | while (!www.isDone) {} |
| | 35 | |
|
| 0 | 36 | | if (www.result != UnityWebRequest.Result.Success) { |
| 0 | 37 | | Debug.LogError($"Failed to load {streamingAssetsPath}: {www.error}."); |
| 0 | 38 | | return null; |
| | 39 | | } |
| 0 | 40 | | return www.downloadHandler.text; |
| | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| 755 | 44 | | public static Configs.AttackBehaviorConfig LoadAttackBehaviorConfig(string configFile) { |
| 755 | 45 | | return LoadProtobufConfig<Configs.AttackBehaviorConfig>( |
| | 46 | | Path.Combine("Configs/Attacks", configFile), |
| | 47 | | Protobuf.Protobuf_AttackBehaviorConfig_GetSerializedLength, |
| | 48 | | Protobuf.Protobuf_AttackBehaviorConfig_LoadToBinary); |
| 755 | 49 | | } |
| | 50 | |
|
| 10 | 51 | | public static Configs.SimulationConfig LoadSimulationConfig(string configFile) { |
| 10 | 52 | | var config = LoadProtobufConfig<Configs.SimulationConfig>( |
| | 53 | | Path.Combine("Configs/Simulations", configFile), |
| | 54 | | Protobuf.Protobuf_SimulationConfig_GetSerializedLength, |
| | 55 | | Protobuf.Protobuf_SimulationConfig_LoadToBinary); |
| 20 | 56 | | if (config != null) { |
| 10 | 57 | | UIManager.Instance.LogActionMessage($"[SIM] Loaded simulation configuration: {configFile}."); |
| 10 | 58 | | } |
| 10 | 59 | | return config; |
| 10 | 60 | | } |
| | 61 | |
|
| 10 | 62 | | public static Configs.SimulatorConfig LoadSimulatorConfig() { |
| 10 | 63 | | return LoadProtobufConfig<Configs.SimulatorConfig>( |
| | 64 | | SimulatorConfigRelativePath, Protobuf.Protobuf_SimulatorConfig_GetSerializedLength, |
| | 65 | | Protobuf.Protobuf_SimulatorConfig_LoadToBinary); |
| 10 | 66 | | } |
| | 67 | |
|
| 755 | 68 | | public static Configs.StaticConfig LoadStaticConfig(string configFile) { |
| 755 | 69 | | return LoadProtobufConfig<Configs.StaticConfig>( |
| | 70 | | Path.Combine("Configs/Models", configFile), |
| | 71 | | Protobuf.Protobuf_StaticConfig_GetSerializedLength, |
| | 72 | | Protobuf.Protobuf_StaticConfig_LoadToBinary); |
| 755 | 73 | | } |
| | 74 | |
|
| | 75 | | private static T LoadProtobufConfig<T>(string relativePath, |
| | 76 | | SerializedProtobufLengthDelegate serializedLengthFunction, |
| | 77 | | LoadProtobufDelegate loadFunction) |
| 1530 | 78 | | where T : Google.Protobuf.IMessage<T>, new() { |
| 1530 | 79 | | string streamingAssetsPath = GetStreamingAssetsFilePath(relativePath); |
| | 80 | |
|
| | 81 | | // Determine the length of the serialized Protobuf message. |
| 1530 | 82 | | Plugin.StatusCode status = |
| | 83 | | serializedLengthFunction(streamingAssetsPath, out int serializedLength); |
| 1530 | 84 | | if (status != Plugin.StatusCode.StatusOk) { |
| 0 | 85 | | Debug.Log( |
| | 86 | | $"Failed to get the length of the serialized message from the Protobuf text file {relativePath} with status co |
| 0 | 87 | | return default; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // Load the Protobuf message to binary format. |
| 1530 | 91 | | byte[] buffer = new byte[serializedLength]; |
| 1530 | 92 | | status = loadFunction(streamingAssetsPath, buffer, serializedLength, out int loadedLength); |
| 1530 | 93 | | if (status != Plugin.StatusCode.StatusOk) { |
| 0 | 94 | | Debug.Log($"Failed to load the Protobuf text file {relativePath} with status code {status}."); |
| 0 | 95 | | return default; |
| | 96 | | } |
| 1530 | 97 | | return new Google.Protobuf.MessageParser<T>(() => new T()).ParseFrom(buffer, 0, loadedLength); |
| 1530 | 98 | | } |
| | 99 | | } |