123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- using System.Collections.Generic;
- using XGame.Framework.Asyncs;
- using XGame.Framework.Serialization;
- namespace XGame.Framework.Database
- {
- public abstract class TableRepository<TTable, TRepository> : ISerializable, IDisposable where TTable : class, ITable where TRepository : TableRepository<TTable, TRepository>
- {
- protected TTable[] _tables;
- protected Dictionary<long, TTable> _tableMap;
- void ISerializable.Deserialize(IReader reader)
- {
- _tables = reader.ReadEnumerable<TTable[]>();
- _tableMap = new Dictionary<long, TTable>();
- foreach(var table in _tables)
- {
- #if UNITY_EDITOR
- if (_tableMap.ContainsKey(table.Key))
- {
- Log.Error($"TableRepository 重复数据. Name:{typeof(TTable)} Id:{table.Key}");
- continue;
- }
- #endif
- _tableMap.Add(table.Key, table);
- }
- }
- void ISerializable.Serialize(IWriter writer)
- {
- writer.Write(_tables);
- }
- void IDisposable.Dispose()
- {
- _tables = null;
- _tableMap?.Clear();
- _tableMap = null;
- }
- public TTable GetByKey(long key)
- {
- if (_tableMap == null)
- {
- return default;
- }
- _tableMap.TryGetValue(key, out var table);
- return table;
- }
- /// <summary>
- /// 根据索引取值
- /// index == -1 表示取最后一个
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- public TTable GetByIndex(int index)
- {
- var count = _tables?.Length ?? 0;
- if (count == 0)
- {
- return default;
- }
- if (index == -1)
- index = count - 1;
- else if (index < 0 || index >= count)
- {
- Log.Error($"配置表索引越界. Name:{typeof(TTable)} Count:{count} Index:{index}");
- return default;
- }
- return _tables[index];
- }
- public TTable[] GetAllTables()
- {
- return _tables;
- }
- #region 静态方法
- public static string TableName => typeof(TTable).Name;
- /// <summary>
- /// 配置表数量
- /// </summary>
- public static int Count => GetAll()?.Length ?? 0;
- /// <summary>
- /// 配置表是否为空
- /// </summary>
- public static bool IsEmpty => Count == 0;
- public static TTable Get(long key)
- {
- return DatabaseModule.Instance.GetByKey<TTable, TRepository>(key);
- }
- /// <summary>
- /// 配置表第一个数据
- /// </summary>
- /// <returns></returns>
- public static TTable First()
- {
- return DatabaseModule.Instance.GetByIndex<TTable, TRepository>(0);
- }
- /// <summary>
- /// 配置表最后一个数据
- /// </summary>
- /// <returns></returns>
- public static TTable Last()
- {
- return DatabaseModule.Instance.GetByIndex<TTable, TRepository>(-1);
- }
- public static TTable[] GetAll()
- {
- return DatabaseModule.Instance.GetAll<TTable, TRepository>();
- }
- public static IAsync LoadAsync()
- {
- return DatabaseModule.Instance.LoadAsync<TTable, TRepository>(TableName);
- }
- public static void Unload()
- {
- DatabaseModule.Instance.Unload(TableName);
- }
- //public static implicit operator TableRepository<TTable>(TableRepository<ITable> v)
- //{
- // return v as TableRepository<TTable>;
- //}
- #endregion
- }
- }
|