diff --git a/Assets/TEngine/Runtime/Config/ResConfigUtil.cs b/Assets/TEngine/Runtime/Config/ResConfigUtil.cs index 41bf936d..17374218 100644 --- a/Assets/TEngine/Runtime/Config/ResConfigUtil.cs +++ b/Assets/TEngine/Runtime/Config/ResConfigUtil.cs @@ -5,14 +5,34 @@ using UnityEngine; namespace TEngine { + /// + /// 过滤配置 + /// + /// + /// + /// + public delegate bool FilterResBin(TType val); + /// + /// 计算拼接Key + /// + /// + /// + /// + /// + public delegate KeyType ConvertDictionaryKey(TType val); + public class ResConfigUtil { private static StringBuilder m_strBuilder = new StringBuilder(); private static readonly string m_split = "_"; #region 读取接口 - public static List ReadConfigListRes(string fileName) + public static List ReadConfigListRes(string fileName = "") { + if (string.IsNullOrEmpty(fileName)) + { + fileName = typeof(T).Name; + } string resPath = string.Format("Config/{0}.json",fileName); TextAsset jsonStr = TResources.Load(resPath); if (jsonStr == null) @@ -27,8 +47,12 @@ namespace TEngine return list; } - public static Dictionary ReadConfigRes(string fileName) + public static Dictionary ReadConfigRes(string fileName = "") { + if (string.IsNullOrEmpty(fileName)) + { + fileName = typeof(T).Name; + } string resPath = string.Format("Config/{0}.json", fileName); TextAsset jsonStr = TResources.Load(resPath); if (jsonStr == null) @@ -43,8 +67,12 @@ namespace TEngine return dic; } - public static Dictionary ReadConfigResIntKey(string fileName) + public static Dictionary ReadConfigResIntKey(string fileName = "") { + if (string.IsNullOrEmpty(fileName)) + { + fileName = typeof(T).Name; + } string resPath = string.Format("Config/{0}.json", fileName); TextAsset jsonStr = TResources.Load(resPath); if (jsonStr == null) @@ -59,6 +87,42 @@ namespace TEngine return dic; } + public static List ReadResBinDict(Dictionary dic,ConvertDictionaryKey convKey ,string fileName = "") + { + if (string.IsNullOrEmpty(fileName)) + { + fileName = typeof(T).Name; + } + + string resPath = string.Format("Config/{0}.json", fileName); + TextAsset jsonStr = TResources.Load(resPath); + if (jsonStr == null) + { + TLogger.LogError("读取Json配置数据失败:{0}", fileName); + return null; + } + + var jsonData = JsonHelper.Instance.Deserialize>(jsonStr.text); + + var etr = jsonData.GetEnumerator(); + if(dic == null) + { + dic = new Dictionary(); + } + while (etr.MoveNext()) + { + var key = convKey(etr.Current); + { + if (dic.ContainsKey(key)) + { + TLogger.LogError("Config {0} Load Error, Repeat config {1}",typeof(T).ToString(),key.ToString()); + } + dic.Add(key, etr.Current); + } + } + + return jsonData; + } #endregion public static UInt64 Make64Key(uint key1, uint key2) diff --git a/Assets/TEngine/Runtime/Config/ResDataBase.meta b/Assets/TEngine/Runtime/Config/ResDataBase.meta new file mode 100644 index 00000000..8e064e2b --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ddadc0410f1c53a46b575aee186bc96a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResDataBase.cs b/Assets/TEngine/Runtime/Config/ResDataBase/ResDataBase.cs new file mode 100644 index 00000000..2f891260 --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResDataBase.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TEngine +{ + public interface IResRawListInterface + { + List RawList { get; } + } + + public class ResDataBase : IResRawListInterface + { + private IResRawListInterface m_sourceRawList = null; + + private string m_fileName = null; + private List m_rawList = null; + private bool m_loaded = false; + + public List RawList + { + get + { + CheckLoad(); + return m_rawList; + } + } + + protected void InitBase(string fileName) + { + m_fileName = fileName; + } + + protected void InitBase(IResRawListInterface sourceList) + { + m_sourceRawList = sourceList; + } + + protected void ClearBase() + { + m_loaded = false; + m_rawList = null; + } + + protected void CheckLoad() + { + if (m_loaded) + { + return; + } + +#if UNITY_EDITOR + GameTickWatcher tickWatcher = new GameTickWatcher(); +#endif + + m_loaded = true; + if (m_sourceRawList != null) + { + m_rawList = LoadFromSourceList(m_sourceRawList); + } + else + { + m_rawList = LoadFromFile(m_fileName); + } + + if (m_rawList == null) + { + m_rawList = new List(); + } + +#if UNITY_EDITOR + TLogger.LogInfo("read config {0} used time: {1}", typeof(T).ToString(), tickWatcher.ElapseTime()); +#endif + } + + + #region 处理载入 + + protected virtual List LoadFromFile(string fileName) + { + return null; + } + + protected virtual List LoadFromSourceList(IResRawListInterface sourceList) + { + return null; + } + + + #endregion + } +} diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResDataBase.cs.meta b/Assets/TEngine/Runtime/Config/ResDataBase/ResDataBase.cs.meta new file mode 100644 index 00000000..14d9c380 --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResDataBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f66e08f903a6801468e66394daca48d0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionary.cs b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionary.cs new file mode 100644 index 00000000..6e3d6796 --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionary.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TEngine +{ + public class ResDictionary : ResDataBase where T : new() + { + private FilterResBin m_filter = null; + private ConvertDictionaryKey m_convKey = null; + private Dictionary m_data = null; + + public Dictionary Data + { + get + { + CheckLoad(); + return m_data; + } + } + + public void Init(string fileName, ConvertDictionaryKey convKey) + { + InitBase(fileName); + m_convKey = convKey; + m_filter = null; + } + + public void Init(ConvertDictionaryKey convKey) + { + Init(string.Empty, convKey); + } + + public void Init(IResRawListInterface rawList, ConvertDictionaryKey convKey, FilterResBin filter = null) + { + InitBase(rawList); + m_convKey = convKey; + m_filter = filter; + } + + public void Clear() + { + m_data = null; + ClearBase(); + } + + protected override List LoadFromSourceList(IResRawListInterface sourceList) + { + m_data = new Dictionary(); + var rawList = sourceList.RawList; + for (int i = 0; i < rawList.Count; i++) + { + var config = rawList[i]; + if (m_filter != null && !m_filter(config)) + { + continue; + } + + var key = m_convKey(config); + if (m_data.ContainsKey(key)) + { + TLogger.LogError("Config {0} load error, repeat config: {0}", typeof(T).ToString(), key.ToString()); + continue; + } + + m_data.Add(key, config); + } + + return rawList; + } + + protected override List LoadFromFile(string fileName) + { + m_data = new Dictionary(); + + ///读取文件不支持filter,实际也没这个需求 + TLogger.LogAssert(m_filter == null); + + List rawList; + if (string.IsNullOrEmpty(fileName)) + { + rawList = ResConfigUtil.ReadResBinDict(m_data, m_convKey); + } + else + { + rawList = ResConfigUtil.ReadResBinDict(m_data, m_convKey,fileName); + } + + return rawList; + } + + public bool TryGetValue(K key, out T itemData, bool showLog = false) + { + if (Data.TryGetValue(key, out itemData)) + { + return true; + } + + if (showLog) + { + TLogger.LogError("get config {0} failed, key: {1}!", typeof(T), key); + } + return false; + } + } +} diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionary.cs.meta b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionary.cs.meta new file mode 100644 index 00000000..400ef588 --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionary.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: efcf1d5d517018a4795f384a746dcb5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResList.cs b/Assets/TEngine/Runtime/Config/ResDataBase/ResList.cs new file mode 100644 index 00000000..993bd4dd --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResList.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TEngine +{ + public class ResList : ResDataBase where T : new() + { + private Comparison m_comparer; + + public List Data + { + get { return RawList; } + } + + /// + /// 初始化配置 + /// + /// 如果需要排序,那么传入排序函数 + public void Init(Comparison comparer = null) + { + InitBase(string.Empty); + m_comparer = comparer; + } + + /// + /// 初始化配置 + /// + /// 指定配置文件名 + /// 如果需要排序,那么传入排序函数 + public void Init(string fileName, Comparison comparer = null) + { + InitBase(fileName); + m_comparer = comparer; + } + + public void Clear() + { + ClearBase(); + } + + protected override List LoadFromFile(string fileName) + { + List listData; + if (string.IsNullOrEmpty(fileName)) + { + listData = ResConfigUtil.ReadConfigListRes(); + } + else + { + listData = ResConfigUtil.ReadConfigListRes(fileName); + } + + ///增加排序功能 + if (m_comparer != null) + { + listData.Sort(m_comparer); + } + + return listData; + } + + protected override List LoadFromSourceList(IResRawListInterface sourceList) + { + if (m_comparer != null) + { + List listSorted = new List(); + + var list = sourceList.RawList; + for (int i = 0; i < list.Count; i++) + { + listSorted.Add(list[i]); + } + + listSorted.Sort(m_comparer); + + return listSorted; + } + + return sourceList.RawList; + } + } +} diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResList.cs.meta b/Assets/TEngine/Runtime/Config/ResDataBase/ResList.cs.meta new file mode 100644 index 00000000..a8841852 --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResList.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e387a6e0873c0724981315547f5d83c1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/Core/GameTickWatcher.cs b/Assets/TEngine/Runtime/Core/GameTickWatcher.cs new file mode 100644 index 00000000..00c57544 --- /dev/null +++ b/Assets/TEngine/Runtime/Core/GameTickWatcher.cs @@ -0,0 +1,30 @@ +namespace TEngine +{ + /// + /// 用来在多线程下检测耗时 + /// + public class GameTickWatcher + { + private long m_startTick = 0; + + public GameTickWatcher() + { + Refresh(); + } + + public void Refresh() + { + m_startTick = System.DateTime.Now.Ticks; + } + + /// + /// 用时 + /// + /// + public float ElapseTime() + { + long endTick = System.DateTime.Now.Ticks; + return (float)((endTick - m_startTick) / 10000) / 1000.0f; + } + } +} diff --git a/Assets/TEngine/Runtime/Core/GameTickWatcher.cs.meta b/Assets/TEngine/Runtime/Core/GameTickWatcher.cs.meta new file mode 100644 index 00000000..f7f94d47 --- /dev/null +++ b/Assets/TEngine/Runtime/Core/GameTickWatcher.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e6ceaf7f190a870479bfba811f443ab5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: