using System; using System.IO; using System.Text; #if UNITY using UnityEngine; #endif namespace ET { public static class ConfigHelper { public static async ETTask 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(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 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(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(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("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(string str) { return MongoHelper.FromJson(str); } } }