diff --git a/Assets/TEngine/Runtime/Config/ResConfigUtil.cs b/Assets/TEngine/Runtime/Config/ResConfigUtil.cs index 17374218..b3e1de91 100644 --- a/Assets/TEngine/Runtime/Config/ResConfigUtil.cs +++ b/Assets/TEngine/Runtime/Config/ResConfigUtil.cs @@ -105,10 +105,16 @@ namespace TEngine var jsonData = JsonHelper.Instance.Deserialize>(jsonStr.text); var etr = jsonData.GetEnumerator(); + if(dic == null) { dic = new Dictionary(); } + else + { + dic.Clear(); + } + while (etr.MoveNext()) { var key = convKey(etr.Current); @@ -123,6 +129,48 @@ namespace TEngine return jsonData; } + + public static List ReadResBinDict(Dictionary> dict, 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 (dict == null) + { + dict = new Dictionary>(); + } + else + { + dict.Clear(); + } + while (etr.MoveNext()) + { + var data = etr.Current; + var key = convKey(data); + List listItem; + if (!dict.TryGetValue(key, out listItem)) + { + listItem = new List(); + dict.Add(key, listItem); + } + listItem.Add(data); + } + + return jsonData; + } #endregion public static UInt64 Make64Key(uint key1, uint key2) diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionaryList.cs b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionaryList.cs new file mode 100644 index 00000000..a5b30b34 --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionaryList.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; + +namespace TEngine +{ + class ResDictionaryList : ResDataBase where T : new() + { + 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, FilterResBin filter = null) + { + InitBase(fileName); + m_convKey = convKey; + } + + public void Init(ConvertDictionaryKey convKey) + { + Init(string.Empty, convKey); + } + + /// + /// 构造list数据结构,依赖基础的数据源 + /// + /// + /// + public void Init(IResRawListInterface sourceList, ConvertDictionaryKey convKey, FilterResBin filter = null) + { + InitBase(sourceList); + m_convKey = convKey; + } + + 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]; + var key = m_convKey(config); + List listData; + if (!m_data.TryGetValue(key, out listData)) + { + listData = new List(); + m_data.Add(key, listData); + } + + listData.Add(config); + } + + return rawList; + } + + protected override List LoadFromFile(string fileName) + { + m_data = new Dictionary>(); + + List list; + if (string.IsNullOrEmpty(fileName)) + { + list = ResConfigUtil.ReadResBinDict(m_data, m_convKey); + } + else + { + list = ResConfigUtil.ReadResBinDict(m_data, m_convKey,fileName); + } + + return list; + } + } +} diff --git a/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionaryList.cs.meta b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionaryList.cs.meta new file mode 100644 index 00000000..dcd96cce --- /dev/null +++ b/Assets/TEngine/Runtime/Config/ResDataBase/ResDictionaryList.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b1c0c610ab7c2404992367ef40ec7a9c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: