using System; using System.Collections.Generic; using System.IO; using System.Reflection; using Fantasy.Assembly; using Fantasy.Helper; using Fantasy.Platform.Net; using Fantasy.Serialize; using Newtonsoft.Json.Linq; // ReSharper disable SuspiciousTypeConversion.Global namespace Fantasy.ConfigTable { /// /// 配置表帮助类 /// public static class ConfigTableHelper { private static readonly Dictionary _dictionary = new Dictionary(); /// /// 初始化ConfigTableHelper /// public static void Initialize(string json, params System.Reflection.Assembly[] assemblies) { var jsonObj = JObject.Parse(json); Dictionary tokens = new(); foreach (var item in jsonObj) { try { var name = item.Key; var value = item.Value; if (value is JArray jArray) { tokens[name] = jArray; } } catch (Exception e) { Log.Error($"读表异常,请检查,name={item.Key} ex={e}"); } } foreach (var type in GetAllConfigTableTypes(assemblies)) { var name = type.Name; if (tokens.TryGetValue(name, out var jArray)) { // 通过反射调用 ParseJson 方法 var parseMethod = type.GetMethod("ParseJson", BindingFlags.Public | BindingFlags.Static); parseMethod?.Invoke(null, [jArray]); } } var d = _dictionary; } public static ConfigContext Table() where T : IConfigTable { var type = typeof(T); if (_dictionary.TryGetValue(type, out var context)) { return context as ConfigContext; } var jsonContext = new ConfigContext(); _dictionary[type] = jsonContext; return jsonContext; } public static List ParseLine(JArray arr) where T : IConfigTable, new() { List list = new List(); foreach (var jToken in arr) { T instance = jToken.ToObject(); if (instance != null) { list.Add(instance); } } var context = Table(); context.Association(list); return list; } /// /// 获取所有实现了 IConfigTable 接口的非抽象类 /// /// 所有非抽象的配置对象类 private static List GetAllConfigTableTypes(params System.Reflection.Assembly[] assemblies) { var types = new List(); var interfaceType = typeof(IConfigTable); // 遍历当前程序集中的所有类型 foreach (var assembly in assemblies) { foreach (var type in assembly.GetTypes()) { // 检查是否实现了 IConfigTable 接口,并且不是抽象类 if (interfaceType.IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface) { types.Add(type); } } } return types; } // private static string _configTableBinaryDirectory; // private static readonly object Lock = new object(); // // 配置表数据缓存字典 // private static readonly Dictionary ConfigDic = new (); // /// // /// 初始化ConfigTableHelper // /// // /// // public static void Initialize(string configTableBinaryDirectory) // { // _configTableBinaryDirectory = configTableBinaryDirectory; // } // /// // /// 加载配置表数据 // /// // /// 配置表类型 // /// 配置表数据 // public static T Load() where T : ASerialize // { // lock (Lock) // { // try // { // var dataConfig = typeof(T).Name; // // if (ConfigDic.TryGetValue(dataConfig, out var aProto)) // { // return (T)aProto; // } // // var configFile = GetConfigPath(dataConfig); // var bytes = File.ReadAllBytes(configFile); // // Log.Debug($"dataConfig:{dataConfig} {bytes.Length}"); // var data = SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf).Deserialize(bytes); // ConfigDic[dataConfig] = data; // return data; // } // catch (Exception ex) // { // throw new Exception($"ConfigTableManage:{typeof(T).Name} 数据表加载之后反序列化时出错:{ex}"); // } // } // } // // /// // /// 获取配置表文件路径 // /// // /// 配置表名称 // /// 配置表文件路径 // private static string GetConfigPath(string name) // { // var configFile = Path.Combine(_configTableBinaryDirectory, $"{name}.bytes"); // // if (File.Exists(configFile)) // { // return configFile; // } // // throw new FileNotFoundException($"{name}.byte not found: {configFile}"); // } // // /// // /// 重新加载配置表数据 // /// // public static void ReLoadConfigTable() // { // lock (Lock) // { // foreach (var (_, aProto) in ConfigDic) // { // ((IDisposable) aProto).Dispose(); // } // // ConfigDic.Clear(); // } // } } }