//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
namespace GameFramework.DataTable
{
internal sealed partial class DataTableManager : GameFrameworkModule, IDataTableManager
{
///
/// 数据表。
///
/// 数据表行的类型。
private sealed class DataTable : DataTableBase, IDataTable where T : class, IDataRow, new()
{
private readonly Dictionary m_DataSet;
private T m_MinIdDataRow;
private T m_MaxIdDataRow;
///
/// 初始化数据表的新实例。
///
/// 数据表名称。
public DataTable(string name)
: base(name)
{
m_DataSet = new Dictionary();
m_MinIdDataRow = null;
m_MaxIdDataRow = null;
}
///
/// 获取数据表行的类型。
///
public override Type Type
{
get
{
return typeof(T);
}
}
///
/// 获取数据表行数。
///
public override int Count
{
get
{
return m_DataSet.Count;
}
}
///
/// 获取数据表行。
///
/// 数据表行的编号。
/// 数据表行。
public T this[int id]
{
get
{
return GetDataRow(id);
}
}
///
/// 获取编号最小的数据表行。
///
public T MinIdDataRow
{
get
{
return m_MinIdDataRow;
}
}
///
/// 获取编号最大的数据表行。
///
public T MaxIdDataRow
{
get
{
return m_MaxIdDataRow;
}
}
///
/// 检查是否存在数据表行。
///
/// 数据表行的编号。
/// 是否存在数据表行。
public override bool HasDataRow(int id)
{
return m_DataSet.ContainsKey(id);
}
///
/// 检查是否存在数据表行。
///
/// 要检查的条件。
/// 是否存在数据表行。
public bool HasDataRow(Predicate condition)
{
if (condition == null)
{
throw new GameFrameworkException("Condition is invalid.");
}
foreach (KeyValuePair dataRow in m_DataSet)
{
if (condition(dataRow.Value))
{
return true;
}
}
return false;
}
///
/// 获取数据表行。
///
/// 数据表行的编号。
/// 数据表行。
public T GetDataRow(int id)
{
T dataRow = null;
if (m_DataSet.TryGetValue(id, out dataRow))
{
return dataRow;
}
return null;
}
///
/// 获取符合条件的数据表行。
///
/// 要检查的条件。
/// 符合条件的数据表行。
/// 当存在多个符合条件的数据表行时,仅返回第一个符合条件的数据表行。
public T GetDataRow(Predicate condition)
{
if (condition == null)
{
throw new GameFrameworkException("Condition is invalid.");
}
foreach (KeyValuePair dataRow in m_DataSet)
{
if (condition(dataRow.Value))
{
return dataRow.Value;
}
}
return null;
}
///
/// 获取符合条件的数据表行。
///
/// 要检查的条件。
/// 符合条件的数据表行。
public T[] GetDataRows(Predicate condition)
{
if (condition == null)
{
throw new GameFrameworkException("Condition is invalid.");
}
List results = new List();
foreach (KeyValuePair dataRow in m_DataSet)
{
if (condition(dataRow.Value))
{
results.Add(dataRow.Value);
}
}
return results.ToArray();
}
///
/// 获取符合条件的数据表行。
///
/// 要检查的条件。
/// 符合条件的数据表行。
public void GetDataRows(Predicate condition, List results)
{
if (condition == null)
{
throw new GameFrameworkException("Condition is invalid.");
}
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair dataRow in m_DataSet)
{
if (condition(dataRow.Value))
{
results.Add(dataRow.Value);
}
}
}
///
/// 获取排序后的数据表行。
///
/// 要排序的条件。
/// 排序后的数据表行。
public T[] GetDataRows(Comparison comparison)
{
if (comparison == null)
{
throw new GameFrameworkException("Comparison is invalid.");
}
List results = new List();
foreach (KeyValuePair dataRow in m_DataSet)
{
results.Add(dataRow.Value);
}
results.Sort(comparison);
return results.ToArray();
}
///
/// 获取排序后的数据表行。
///
/// 要排序的条件。
/// 排序后的数据表行。
public void GetDataRows(Comparison comparison, List results)
{
if (comparison == null)
{
throw new GameFrameworkException("Comparison is invalid.");
}
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair dataRow in m_DataSet)
{
results.Add(dataRow.Value);
}
results.Sort(comparison);
}
///
/// 获取排序后的符合条件的数据表行。
///
/// 要检查的条件。
/// 要排序的条件。
/// 排序后的符合条件的数据表行。
public T[] GetDataRows(Predicate condition, Comparison comparison)
{
if (condition == null)
{
throw new GameFrameworkException("Condition is invalid.");
}
if (comparison == null)
{
throw new GameFrameworkException("Comparison is invalid.");
}
List results = new List();
foreach (KeyValuePair dataRow in m_DataSet)
{
if (condition(dataRow.Value))
{
results.Add(dataRow.Value);
}
}
results.Sort(comparison);
return results.ToArray();
}
///
/// 获取排序后的符合条件的数据表行。
///
/// 要检查的条件。
/// 要排序的条件。
/// 排序后的符合条件的数据表行。
public void GetDataRows(Predicate condition, Comparison comparison, List results)
{
if (condition == null)
{
throw new GameFrameworkException("Condition is invalid.");
}
if (comparison == null)
{
throw new GameFrameworkException("Comparison is invalid.");
}
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair dataRow in m_DataSet)
{
if (condition(dataRow.Value))
{
results.Add(dataRow.Value);
}
}
results.Sort(comparison);
}
///
/// 获取所有数据表行。
///
/// 所有数据表行。
public T[] GetAllDataRows()
{
int index = 0;
T[] results = new T[m_DataSet.Count];
foreach (KeyValuePair dataRow in m_DataSet)
{
results[index++] = dataRow.Value;
}
return results;
}
///
/// 获取所有数据表行。
///
/// 所有数据表行。
public void GetAllDataRows(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair dataRow in m_DataSet)
{
results.Add(dataRow.Value);
}
}
///
/// 增加数据表行。
///
/// 要解析的数据表行字符串。
/// 用户自定义数据。
/// 是否增加数据表行成功。
public override bool AddDataRow(string dataRowString, object userData)
{
try
{
T dataRow = new T();
if (!dataRow.ParseDataRow(dataRowString, userData))
{
return false;
}
InternalAddDataRow(dataRow);
return true;
}
catch (Exception exception)
{
if (exception is GameFrameworkException)
{
throw;
}
throw new GameFrameworkException(Utility.Text.Format("Can not parse data row string for data table '{0}' with exception '{1}'.", new TypeNamePair(typeof(T), Name), exception), exception);
}
}
///
/// 增加数据表行。
///
/// 要解析的数据表行二进制流。
/// 数据表行二进制流的起始位置。
/// 数据表行二进制流的长度。
/// 用户自定义数据。
/// 是否增加数据表行成功。
public override bool AddDataRow(byte[] dataRowBytes, int startIndex, int length, object userData)
{
try
{
T dataRow = new T();
if (!dataRow.ParseDataRow(dataRowBytes, startIndex, length, userData))
{
return false;
}
InternalAddDataRow(dataRow);
return true;
}
catch (Exception exception)
{
if (exception is GameFrameworkException)
{
throw;
}
throw new GameFrameworkException(Utility.Text.Format("Can not parse data row bytes for data table '{0}' with exception '{1}'.", new TypeNamePair(typeof(T), Name), exception), exception);
}
}
///
/// 移除指定数据表行。
///
/// 要移除数据表行的编号。
/// 是否移除数据表行成功。
public override bool RemoveDataRow(int id)
{
if (!HasDataRow(id))
{
return false;
}
if (!m_DataSet.Remove(id))
{
return false;
}
if (m_MinIdDataRow != null && m_MinIdDataRow.Id == id || m_MaxIdDataRow != null && m_MaxIdDataRow.Id == id)
{
m_MinIdDataRow = null;
m_MaxIdDataRow = null;
foreach (KeyValuePair dataRow in m_DataSet)
{
if (m_MinIdDataRow == null || m_MinIdDataRow.Id > dataRow.Key)
{
m_MinIdDataRow = dataRow.Value;
}
if (m_MaxIdDataRow == null || m_MaxIdDataRow.Id < dataRow.Key)
{
m_MaxIdDataRow = dataRow.Value;
}
}
}
return true;
}
///
/// 清空所有数据表行。
///
public override void RemoveAllDataRows()
{
m_DataSet.Clear();
m_MinIdDataRow = null;
m_MaxIdDataRow = null;
}
///
/// 返回循环访问集合的枚举数。
///
/// 循环访问集合的枚举数。
public IEnumerator GetEnumerator()
{
return m_DataSet.Values.GetEnumerator();
}
///
/// 返回循环访问集合的枚举数。
///
/// 循环访问集合的枚举数。
IEnumerator IEnumerable.GetEnumerator()
{
return m_DataSet.Values.GetEnumerator();
}
///
/// 关闭并清理数据表。
///
internal override void Shutdown()
{
m_DataSet.Clear();
}
private void InternalAddDataRow(T dataRow)
{
if (HasDataRow(dataRow.Id))
{
throw new GameFrameworkException(Utility.Text.Format("Already exist '{0}' in data table '{1}'.", dataRow.Id, new TypeNamePair(typeof(T), Name)));
}
m_DataSet.Add(dataRow.Id, dataRow);
if (m_MinIdDataRow == null || m_MinIdDataRow.Id > dataRow.Id)
{
m_MinIdDataRow = dataRow;
}
if (m_MaxIdDataRow == null || m_MaxIdDataRow.Id < dataRow.Id)
{
m_MaxIdDataRow = dataRow;
}
}
}
}
}