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