using System; using System.IO; using System.Text; using UnityEngine; 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; #else string path = $"Assets/Download/DataTable/{key}.json"; TextAsset text = await ResourceHelper.LoadAssetAsync(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 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(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(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("GlobalProto").text; return configStr; #endif } catch (Exception e) { throw new Exception($"load global config file fail", e); } } public static T ToObject(string str) { return MongoHelper.FromJson(str); } } }