mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
拓展自定义鲁班加载配置,支持懒加载 默认使用懒加载,未使用的配置不会载入内存
拓展自定义鲁班加载配置,支持懒加载 默认使用懒加载,未使用的配置不会载入内存
This commit is contained in:
@@ -7,57 +7,120 @@
|
||||
//------------------------------------------------------------------------------
|
||||
using Bright.Serialization;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Linq;
|
||||
|
||||
namespace GameConfig.Battle
|
||||
{
|
||||
|
||||
public partial class TbBuffAttr
|
||||
{
|
||||
private readonly Dictionary<int, Battle.BuffAttrConfig> _dataMap;
|
||||
private readonly List<Battle.BuffAttrConfig> _dataList;
|
||||
|
||||
public TbBuffAttr(ByteBuf _buf)
|
||||
public partial class TbBuffAttr
|
||||
{
|
||||
_dataMap = new Dictionary<int, Battle.BuffAttrConfig>();
|
||||
_dataList = new List<Battle.BuffAttrConfig>();
|
||||
|
||||
for(int n = _buf.ReadSize() ; n > 0 ; --n)
|
||||
public static TbBuffAttr Instance { get; private set; }
|
||||
private bool _readAll = false;
|
||||
private Dictionary<int, Battle.BuffAttrConfig> _dataMap;
|
||||
private List<Battle.BuffAttrConfig> _dataList;
|
||||
public Dictionary<int, Battle.BuffAttrConfig> DataMap
|
||||
{
|
||||
get
|
||||
{
|
||||
if(!_readAll)
|
||||
{
|
||||
ReadAll();
|
||||
_readAll = true;
|
||||
}
|
||||
return _dataMap;
|
||||
}
|
||||
}
|
||||
public List<Battle.BuffAttrConfig> DataList
|
||||
{
|
||||
get
|
||||
{
|
||||
if(!_readAll)
|
||||
{
|
||||
ReadAll();
|
||||
_readAll = true;
|
||||
}
|
||||
return _dataList;
|
||||
}
|
||||
}
|
||||
private Dictionary<int,int> _indexMap;
|
||||
public List<int> Indexes;
|
||||
private System.Func<ByteBuf> _dataLoader;
|
||||
|
||||
private void ReadAll()
|
||||
{
|
||||
_dataList.Clear();
|
||||
foreach(var index in Indexes)
|
||||
{
|
||||
var v = Get(index);
|
||||
_dataMap[index] = v;
|
||||
_dataList.Add(v);
|
||||
}
|
||||
}
|
||||
|
||||
public TbBuffAttr(ByteBuf _buf, string _tbName, System.Func<string, ByteBuf> _loader)
|
||||
{
|
||||
Instance = this;
|
||||
_dataMap = new Dictionary<int, Battle.BuffAttrConfig>();
|
||||
_dataList = new List<Battle.BuffAttrConfig>();
|
||||
_indexMap = new Dictionary<int, int>();
|
||||
_dataLoader = new System.Func<ByteBuf>(() => _loader(_tbName));
|
||||
|
||||
for (int n = _buf.ReadSize(); n > 0; --n)
|
||||
{
|
||||
int key;
|
||||
key = _buf.ReadInt();
|
||||
int index = _buf.ReadInt();
|
||||
_indexMap[key] = index;
|
||||
}
|
||||
Indexes = _indexMap.Keys.ToList();
|
||||
PostInit();
|
||||
}
|
||||
|
||||
public Battle.BuffAttrConfig this[int key] => Get(key);
|
||||
public Battle.BuffAttrConfig Get(int key)
|
||||
{
|
||||
Battle.BuffAttrConfig _v;
|
||||
if(_dataMap.TryGetValue(key, out _v))
|
||||
{
|
||||
return _v;
|
||||
}
|
||||
ResetByteBuf(_indexMap[key]);
|
||||
_v = Battle.BuffAttrConfig.DeserializeBuffAttrConfig(_buf);
|
||||
_dataList.Add(_v);
|
||||
_dataMap.Add(_v.BuffID, _v);
|
||||
_dataMap[_v.BuffID] = _v;
|
||||
_v.Resolve(tables);
|
||||
if(_indexMap.Count == _dataMap.Count)
|
||||
{
|
||||
_buf = null;
|
||||
}
|
||||
return _v;
|
||||
}
|
||||
PostInit();
|
||||
}
|
||||
|
||||
public Dictionary<int, Battle.BuffAttrConfig> DataMap => _dataMap;
|
||||
public List<Battle.BuffAttrConfig> DataList => _dataList;
|
||||
|
||||
public Battle.BuffAttrConfig GetOrDefault(int key) => _dataMap.TryGetValue(key, out var v) ? v : null;
|
||||
public Battle.BuffAttrConfig Get(int key) => _dataMap[key];
|
||||
public Battle.BuffAttrConfig this[int key] => _dataMap[key];
|
||||
|
||||
public void Resolve(Dictionary<string, object> _tables)
|
||||
{
|
||||
foreach(var v in _dataList)
|
||||
public Battle.BuffAttrConfig GetOrDefault(int key)
|
||||
{
|
||||
v.Resolve(_tables);
|
||||
if(_indexMap.TryGetValue(key,out var _))
|
||||
{
|
||||
return Get(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
PostResolve();
|
||||
}
|
||||
|
||||
public void TranslateText(System.Func<string, string, string> translator)
|
||||
{
|
||||
foreach(var v in _dataList)
|
||||
|
||||
private void ResetByteBuf(int readerInex = 0)
|
||||
{
|
||||
v.TranslateText(translator);
|
||||
if( _buf == null)
|
||||
{
|
||||
if (_buf == null)
|
||||
{
|
||||
_buf = _dataLoader();
|
||||
}
|
||||
}
|
||||
_buf.ReaderIndex = readerInex;
|
||||
}
|
||||
}
|
||||
|
||||
partial void PostInit();
|
||||
partial void PostResolve();
|
||||
}
|
||||
|
||||
}
|
||||
private ByteBuf _buf = null;
|
||||
private Dictionary<string, object> tables;
|
||||
public void CacheTables(Dictionary<string, object> _tables)
|
||||
{
|
||||
tables = _tables;
|
||||
}
|
||||
partial void PostInit();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user