Use WebRequests to get StreamingAssets files
parent
c0b95b2fa7
commit
33b4deec0c
|
@ -1,45 +1,72 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Networking;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
public static class ConfigLoader {
|
public static class ConfigLoader {
|
||||||
private static string NormalizePath(string path) {
|
|
||||||
if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.LinuxPlayer) {
|
private static string LoadFromStreamingAssets(string relativePath)
|
||||||
if (!path.StartsWith("file://")) {
|
{
|
||||||
return "file://" + path;
|
string filePath = Path.Combine(Application.streamingAssetsPath, relativePath);
|
||||||
|
|
||||||
|
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX || UNITY_IOS
|
||||||
|
if (!filePath.StartsWith("file://"))
|
||||||
|
{
|
||||||
|
filePath = "file://" + filePath;
|
||||||
}
|
}
|
||||||
}
|
#endif
|
||||||
return path;
|
|
||||||
|
UnityWebRequest www = UnityWebRequest.Get(filePath);
|
||||||
|
www.SendWebRequest();
|
||||||
|
|
||||||
|
// Wait for the request to complete
|
||||||
|
while (!www.isDone)
|
||||||
|
{
|
||||||
|
// You might want to yield return null here if this is called from a coroutine
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SimulationConfig LoadSimulationConfig(string configFileName) {
|
if (www.result != UnityWebRequest.Result.Success)
|
||||||
string configFilePath = NormalizePath(Path.Combine(Application.streamingAssetsPath, "Configs", configFileName));
|
{
|
||||||
if (File.Exists(configFilePath)) {
|
Debug.LogError($"Error loading file at {filePath}: {www.error}");
|
||||||
string json = File.ReadAllText(configFilePath);
|
|
||||||
SimulationConfig config = JsonConvert.DeserializeObject<SimulationConfig>(json, new JsonSerializerSettings {
|
|
||||||
Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
|
|
||||||
});
|
|
||||||
return config;
|
|
||||||
} else {
|
|
||||||
Debug.LogError($"Configuration file not found at path: {configFilePath}");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return www.downloadHandler.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static StaticConfig LoadStaticConfig(string configFileName) {
|
public static SimulationConfig LoadSimulationConfig(string configFileName)
|
||||||
string configFilePath = NormalizePath(Path.Combine(Application.streamingAssetsPath, "Configs/Models", configFileName));
|
{
|
||||||
if (File.Exists(configFilePath)) {
|
string relativePath = Path.Combine("Configs", configFileName);
|
||||||
string json = File.ReadAllText(configFilePath);
|
string fileContent = LoadFromStreamingAssets(relativePath);
|
||||||
StaticConfig config = JsonConvert.DeserializeObject<StaticConfig>(json, new JsonSerializerSettings {
|
|
||||||
Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
|
if (string.IsNullOrEmpty(fileContent))
|
||||||
});
|
{
|
||||||
return config;
|
Debug.LogError($"Failed to load SimulationConfig from {relativePath}");
|
||||||
} else {
|
|
||||||
Debug.LogError($"Static configuration file not found at path: {configFilePath}");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<SimulationConfig>(fileContent, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static StaticConfig LoadStaticConfig(string configFileName)
|
||||||
|
{
|
||||||
|
string relativePath = Path.Combine("Configs/Models", configFileName);
|
||||||
|
string fileContent = LoadFromStreamingAssets(relativePath);
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(fileContent))
|
||||||
|
{
|
||||||
|
Debug.LogError($"Failed to load StaticConfig from {relativePath}");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<StaticConfig>(fileContent, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static void PrintSimulationConfig(SimulationConfig config)
|
public static void PrintSimulationConfig(SimulationConfig config)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue