
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using Luban;


namespace Server.Config
{
public partial class CastlesConfigCategory : ConfigSingleton<CastlesConfigCategory>
{
    public const string TableName = Tables.CastlesConfigCategory;

    private readonly System.Collections.Generic.Dictionary<int, CastlesConfig> _dataMap;
    private readonly System.Collections.Generic.List<CastlesConfig> _dataList;

    public CastlesConfigCategory()
    {
        _dataMap = new System.Collections.Generic.Dictionary<int, CastlesConfig>();
        _dataList = new System.Collections.Generic.List<CastlesConfig>(); 

        this.ReLoadData();
    }

    public sealed override void ReLoadData()
    {
        // 备份旧数据，以便加载失败时回滚
        var oldDataList = new System.Collections.Generic.List<CastlesConfig>(_dataList);

        _dataMap.Clear();
        _dataList.Clear();

        try
        {
            byte[] data = TableManager.Instance.GetTableData(TableName);
            LoadData(new ByteBuf(data));
        }
        catch
        {
            // 加载失败，回滚到旧数据
            _dataMap.Clear();
            _dataList.Clear();
            foreach (var item in oldDataList)
            {
                _dataList.Add(item);
                _dataMap.Add(item.Id, item);
            }
            throw;
        }
    }

    protected sealed override void LoadData(ByteBuf _buf)
    {
        for(int n = _buf.ReadSize() ; n > 0 ; --n)
        {
            CastlesConfig _v;
            _v = CastlesConfig.DeserializeCastlesConfig(_buf);
            _dataList.Add(_v);
            _dataMap.Add(_v.Id, _v);
        }
        PostInit();
    }

    public System.Collections.Generic.Dictionary<int, CastlesConfig> DataMap => _dataMap;
    public System.Collections.Generic.List<CastlesConfig> DataList => _dataList;

    public CastlesConfig GetOrDefault(int key) => _dataMap.TryGetValue(key, out var v) ? v : null;
    public CastlesConfig Get(int key) => _dataMap[key];
    public CastlesConfig this[int key] => _dataMap[key];

    public bool Contain(int key) => _dataMap.ContainsKey(key);


    partial void PostInit();
}

}

