zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Module/Config/ConfigHelper.cs

139 lines
4.6 KiB
C#

using System;
using System.IO;
using System.Text;
#if UNITY
using UnityEngine;
#endif
namespace ET
{
public static class ConfigHelper
{
public static async ETTask<byte[]> GetBytes(string key)
{
try
{
#if SERVER
string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}../DataTable/{key}.json";
var buffer = await File.ReadAllBytesAsync(path);
return buffer;
#elif UNITY
string path = $"Assets/Download/DataTable/{key}.json";
TextAsset text = await ResourceHelper.LoadAssetAsync<TextAsset>(path);
byte[] buffer = text.bytes;
return buffer;
#else
return null;
#endif
}
catch (Exception e)
{
throw new Exception($"load data file fail, key: {key}", e);
}
}
public static async ETTask<string> GetTextAsync(string key)
{
try
{
#if SERVER
string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}../DataTable/{key}.json";
if(!File.Exists(path))
{
path = $"{System.AppDomain.CurrentDomain.BaseDirectory}../Config/{key}.json";
var str = await File.ReadAllTextAsync(path);
return str;
}
else
{
var str = await File.ReadAllTextAsync(path);
var bytes = ZipHelper.Decompress(Convert.FromBase64String(str));
str = Encoding.UTF8.GetString(bytes);
return str;
}
#else
string path = $"Assets/Download/DataTable/{key}.json";
#if UNITY_EDITOR
string str = File.ReadAllText(path);
#elif UNITY
TextAsset text = await ResourceHelper.LoadAssetAsync<TextAsset>(path);
var str = text.text;
#else
string str = File.ReadAllText(@$"E:\Unity\CTT\DataTable/{key}.json");
#endif
byte[] bytes = ZipHelper.Decompress(Convert.FromBase64String(str));
str = Encoding.UTF8.GetString(bytes);
return str;
#endif
}
catch (Exception e)
{
throw new Exception($"load data file fail, key: {key}", e);
}
}
public static string GetText(string key)
{
try
{
#if SERVER
string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}../DataTable/{key}.json";
if (!File.Exists(path))
{
path = $"{System.AppDomain.CurrentDomain.BaseDirectory}../Config/{key}.json";
var str = File.ReadAllText(path);
return str;
}
else
{
var str = File.ReadAllText(path);
var bytes = ZipHelper.Decompress(Convert.FromBase64String(str));
str = Encoding.UTF8.GetString(bytes);
return str;
}
#else
string path = $"Assets/Download/DataTable/{key}.json";
#if UNITY_EDITOR
string str = File.ReadAllText(path);
#elif UNITY
TextAsset text = ResourceHelper.LoadAsset<TextAsset>(path);
var str = text.text;
#else
string str = File.ReadAllText(@$"E:\Unity\CTT\DataTable/{key}.json");
#endif
byte[] bytes = ZipHelper.Decompress(Convert.FromBase64String(str));
str = Encoding.UTF8.GetString(bytes);
return str;
#endif
}
catch (Exception e)
{
throw new Exception($"load data file fail, key: {key}", e);
}
}
public static string GetGlobal()
{
try
{
#if SERVER
return null;
#elif UNITY
GameObject config = (GameObject)ResourcesHelper.Load("KV");
string configStr = config.Get<TextAsset>("GlobalProto").text;
return configStr;
#else
string path = @$"E:\Unity\CTT\Unity\Assets/Res/Config/GlobalProto.txt";
string str = File.ReadAllText(path);
return str;
#endif
}
catch (Exception e)
{
throw new Exception($"load global config file fail", e);
}
}
public static T ToObject<T>(string str)
{
return MongoHelper.FromJson<T>(str);
}
}
}