using System; using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using System.Collections.Generic; using UnityEngine; /// /// 游戏配置数据 /// sealed public partial class GameConfigData { #region ' 单例 ' private static GameConfigData _ins = null; public static GameConfigData Ins { get { if (null == _ins) { LogHelper.LogError("配置数据尚未初始化!"); } return _ins; } } /// /// 私有构造函数 /// /// private GameConfigData(JObject arg = null) { System.Diagnostics.Trace.Assert(null != arg, "初始化数据不可为空!"); data = arg; } private static JObject data; #endregion /// /// 配置数据是否已加载 /// public static bool IsReady => _ins != null; /// /// 初始化方法, 无法在外部通过构造函数创建实例, 只能通过init方法初始化单例. /// /// public static void Init(JObject arg = null) { _ins = new GameConfigData(arg); if (null != _ins) { Thread t = new Thread(() => { var st = DateTimeOffset.Now; LogHelper.Log(" 后台线程触发deserialize 开始"); //_ins.touchEveryProperty(); // 没用!不挣扎了...还是用反射吧,反射性能更好.--gwang var tp = typeof(GameConfigData); var pps = tp.GetProperties(); Parallel.ForEach(pps, p => // 并行实例化(反序列化到强类型)各个模块的数据 { if (p.GetCustomAttribute() != null) { _ = p.GetValue(_ins); } }); LogHelper.Log($" 后台线程触发deserialize 结束. 耗时: {(DateTimeOffset.Now - st).TotalMilliseconds:.00}ms"); }); t.IsBackground = true; t.Start(); } } /// /// 指示这是一个模块属性 /// [AttributeUsage(AttributeTargets.Property)] partial class sm_modPropAttribute : Attribute { } #region ' 辅助方法 ' private static T _moGetById(K id, Dictionary dic, string desc = "对象") where T : class { if (!dic.TryGetValue(id, out T ret)) { LogHelper.LogWarning(string.Format("未找到{0}配置【{1}】", desc, id.ToString())); return null; } return ret; } #endregion }