mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
更新ResDictionaryList
更新ResDictionaryList
This commit is contained in:
@@ -105,10 +105,16 @@ namespace TEngine
|
||||
var jsonData = JsonHelper.Instance.Deserialize<List<T>>(jsonStr.text);
|
||||
|
||||
var etr = jsonData.GetEnumerator();
|
||||
|
||||
if(dic == null)
|
||||
{
|
||||
dic = new Dictionary<K, T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
dic.Clear();
|
||||
}
|
||||
|
||||
while (etr.MoveNext())
|
||||
{
|
||||
var key = convKey(etr.Current);
|
||||
@@ -123,6 +129,48 @@ namespace TEngine
|
||||
|
||||
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
|
||||
|
||||
public static UInt64 Make64Key(uint key1, uint key2)
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1c0c610ab7c2404992367ef40ec7a9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user