
//------------------------------------------------------------------------------
// <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 NCMJGameConfigCategory : ConfigSingleton<NCMJGameConfigCategory>
{
    public const string TableName = Tables.NCMJGameConfigCategory;

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

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

        this.ReLoadData();
    }

    public sealed override void ReLoadData()
    {
        // 备份旧数据，以便加载失败时回滚
        var oldDataList = new System.Collections.Generic.List<NCMJGameConfig>(_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)
        {
            NCMJGameConfig _v;
            _v = NCMJGameConfig.DeserializeNCMJGameConfig(_buf);
            _dataList.Add(_v);
            _dataMap.Add(_v.Id, _v);
        }
        PostInit();
    }

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

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

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


    partial void PostInit();
}

}

