拓展自定义鲁班加载配置,支持懒加载 默认使用懒加载,未使用的配置不会载入内存

拓展自定义鲁班加载配置,支持懒加载 默认使用懒加载,未使用的配置不会载入内存
This commit is contained in:
ALEXTANG
2023-08-17 23:07:07 +08:00
parent bf73ce333b
commit e071c20214
17 changed files with 1123 additions and 216 deletions

View File

@@ -22,7 +22,6 @@ public sealed partial class Item : Bright.Config.BeanBase
UpgradeToItemId = _buf.ReadInt();
if(_buf.ReadBool()){ ExpireTime = _buf.ReadInt(); } else { ExpireTime = null; }
BatchUseable = _buf.ReadBool();
Quality = (item.EQuality)_buf.ReadInt();
ExchangeStream = item.ItemExchange.DeserializeItemExchange(_buf);
{int n0 = System.Math.Min(_buf.ReadSize(), _buf.Size);ExchangeList = new System.Collections.Generic.List<item.ItemExchange>(n0);for(var i0 = 0 ; i0 < n0 ; i0++) { item.ItemExchange _e0; _e0 = item.ItemExchange.DeserializeItemExchange(_buf); ExchangeList.Add(_e0);}}
ExchangeColumn = item.ItemExchange.DeserializeItemExchange(_buf);
@@ -64,10 +63,6 @@ public sealed partial class Item : Bright.Config.BeanBase
/// </summary>
public bool BatchUseable { get; private set; }
/// <summary>
/// 品质
/// </summary>
public item.EQuality Quality { get; private set; }
/// <summary>
/// 道具兑换配置
/// </summary>
public item.ItemExchange ExchangeStream { get; private set; }
@@ -106,7 +101,6 @@ public sealed partial class Item : Bright.Config.BeanBase
+ "UpgradeToItemId:" + UpgradeToItemId + ","
+ "ExpireTime:" + ExpireTime + ","
+ "BatchUseable:" + BatchUseable + ","
+ "Quality:" + Quality + ","
+ "ExchangeStream:" + ExchangeStream + ","
+ "ExchangeList:" + Bright.Common.StringUtil.CollectionToString(ExchangeList) + ","
+ "ExchangeColumn:" + ExchangeColumn + ","

View File

@@ -7,57 +7,120 @@
//------------------------------------------------------------------------------
using Bright.Serialization;
using System.Collections.Generic;
using System.Linq;
namespace GameConfig.item
{
public partial class TbItem
{
private readonly Dictionary<int, item.Item> _dataMap;
private readonly List<item.Item> _dataList;
public TbItem(ByteBuf _buf)
public partial class TbItem
{
_dataMap = new Dictionary<int, item.Item>();
_dataList = new List<item.Item>();
for(int n = _buf.ReadSize() ; n > 0 ; --n)
public static TbItem Instance { get; private set; }
private bool _readAll = false;
private Dictionary<int, item.Item> _dataMap;
private List<item.Item> _dataList;
public Dictionary<int, item.Item> DataMap
{
get
{
if(!_readAll)
{
ReadAll();
_readAll = true;
}
return _dataMap;
}
}
public List<item.Item> 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 TbItem(ByteBuf _buf, string _tbName, System.Func<string, ByteBuf> _loader)
{
Instance = this;
_dataMap = new Dictionary<int, item.Item>();
_dataList = new List<item.Item>();
_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 item.Item this[int key] => Get(key);
public item.Item Get(int key)
{
item.Item _v;
if(_dataMap.TryGetValue(key, out _v))
{
return _v;
}
ResetByteBuf(_indexMap[key]);
_v = item.Item.DeserializeItem(_buf);
_dataList.Add(_v);
_dataMap.Add(_v.Id, _v);
_dataMap[_v.Id] = _v;
_v.Resolve(tables);
if(_indexMap.Count == _dataMap.Count)
{
_buf = null;
}
return _v;
}
PostInit();
}
public Dictionary<int, item.Item> DataMap => _dataMap;
public List<item.Item> DataList => _dataList;
public item.Item GetOrDefault(int key) => _dataMap.TryGetValue(key, out var v) ? v : null;
public item.Item Get(int key) => _dataMap[key];
public item.Item this[int key] => _dataMap[key];
public void Resolve(Dictionary<string, object> _tables)
{
foreach(var v in _dataList)
public item.Item 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();
}
}