CTT/Unity/Assets/Model/Module/Config/ConfigHelper.cs

127 lines
4.1 KiB
C#

using System;
using System.IO;
using System.Text;
using UnityEngine;
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;
#else
string path = $"Assets/Download/DataTable/{key}.json";
TextAsset text = await ResourceHelper.LoadAssetAsync<TextAsset>(path);
byte[] buffer = text.bytes;
return buffer;
#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
var str = File.ReadAllText(path);
#else
TextAsset text = await ResourceHelper.LoadAssetAsync<TextAsset>(path);
var str = text.text;
#endif
var 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
var str = File.ReadAllText(path);
#else
TextAsset text = ResourceHelper.LoadAsset<TextAsset>(path);
var str = text.text;
#endif
var 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;
#else
GameObject config = (GameObject)ResourcesHelper.Load("KV");
string configStr = config.Get<TextAsset>("GlobalProto").text;
return configStr;
#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);
}
}
}