更新ResDictionaryList

更新ResDictionaryList
This commit is contained in:
ALEXTANG
2022-06-16 11:42:48 +08:00
parent 4e3f87e17a
commit d43ecd1a20
3 changed files with 145 additions and 0 deletions

View File

@@ -105,10 +105,16 @@ namespace TEngine
var jsonData = JsonHelper.Instance.Deserialize<List<T>>(jsonStr.text); var jsonData = JsonHelper.Instance.Deserialize<List<T>>(jsonStr.text);
var etr = jsonData.GetEnumerator(); var etr = jsonData.GetEnumerator();
if(dic == null) if(dic == null)
{ {
dic = new Dictionary<K, T>(); dic = new Dictionary<K, T>();
} }
else
{
dic.Clear();
}
while (etr.MoveNext()) while (etr.MoveNext())
{ {
var key = convKey(etr.Current); var key = convKey(etr.Current);
@@ -123,6 +129,48 @@ namespace TEngine
return jsonData; return jsonData;
} }
public static List<T> ReadResBinDict<K, T>(Dictionary<K, List<T>> dict, ConvertDictionaryKey<K, T> convKey, string fileName = "")
{
if (string.IsNullOrEmpty(fileName))
{
fileName = typeof(T).Name;
}
string resPath = string.Format("Config/{0}.json", fileName);
TextAsset jsonStr = TResources.Load<TextAsset>(resPath);
if (jsonStr == null)
{
TLogger.LogError("读取Json配置数据失败{0}", fileName);
return null;
}
var jsonData = JsonHelper.Instance.Deserialize<List<T>>(jsonStr.text);
var etr = jsonData.GetEnumerator();
if (dict == null)
{
dict = new Dictionary<K, List<T>>();
}
else
{
dict.Clear();
}
while (etr.MoveNext())
{
var data = etr.Current;
var key = convKey(data);
List<T> listItem;
if (!dict.TryGetValue(key, out listItem))
{
listItem = new List<T>();
dict.Add(key, listItem);
}
listItem.Add(data);
}
return jsonData;
}
#endregion #endregion
public static UInt64 Make64Key(uint key1, uint key2) public static UInt64 Make64Key(uint key1, uint key2)

View File

@@ -0,0 +1,86 @@
using System.Collections.Generic;
namespace TEngine
{
class ResDictionaryList<K, T> : ResDataBase<T> where T : new()
{
private ConvertDictionaryKey<K, T> m_convKey = null;
private Dictionary<K, List<T>> m_data = null;
public Dictionary<K, List<T>> Data
{
get
{
CheckLoad();
return m_data;
}
}
public void Init(string fileName, ConvertDictionaryKey<K, T> convKey, FilterResBin<T> filter = null)
{
InitBase(fileName);
m_convKey = convKey;
}
public void Init(ConvertDictionaryKey<K, T> convKey)
{
Init(string.Empty, convKey);
}
/// <summary>
/// 构造list数据结构依赖基础的数据源
/// </summary>
/// <param name="rawList"></param>
/// <param name="convKey"></param>
public void Init(IResRawListInterface<T> sourceList, ConvertDictionaryKey<K, T> convKey, FilterResBin<T> filter = null)
{
InitBase(sourceList);
m_convKey = convKey;
}
public void Clear()
{
m_data = null;
ClearBase();
}
protected override List<T> LoadFromSourceList(IResRawListInterface<T> sourceList)
{
m_data = new Dictionary<K, List<T>>();
var rawList = sourceList.RawList;
for (int i = 0; i < rawList.Count; i++)
{
var config = rawList[i];
var key = m_convKey(config);
List<T> listData;
if (!m_data.TryGetValue(key, out listData))
{
listData = new List<T>();
m_data.Add(key, listData);
}
listData.Add(config);
}
return rawList;
}
protected override List<T> LoadFromFile(string fileName)
{
m_data = new Dictionary<K, List<T>>();
List<T> list;
if (string.IsNullOrEmpty(fileName))
{
list = ResConfigUtil.ReadResBinDict(m_data, m_convKey);
}
else
{
list = ResConfigUtil.ReadResBinDict(m_data, m_convKey,fileName);
}
return list;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b1c0c610ab7c2404992367ef40ec7a9c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: