//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using GameFramework.Resource;
using System;
using System.Collections.Generic;
namespace GameFramework.DataTable
{
///
/// 数据表管理器。
///
internal sealed partial class DataTableManager : GameFrameworkModule, IDataTableManager
{
private readonly Dictionary m_DataTables;
private IResourceManager m_ResourceManager;
private IDataProviderHelper m_DataProviderHelper;
private IDataTableHelper m_DataTableHelper;
///
/// 初始化数据表管理器的新实例。
///
public DataTableManager()
{
m_DataTables = new Dictionary();
m_ResourceManager = null;
m_DataProviderHelper = null;
m_DataTableHelper = null;
}
///
/// 获取数据表数量。
///
public int Count
{
get
{
return m_DataTables.Count;
}
}
///
/// 获取缓冲二进制流的大小。
///
public int CachedBytesSize
{
get
{
return DataProvider.CachedBytesSize;
}
}
///
/// 数据表管理器轮询。
///
/// 逻辑流逝时间,以秒为单位。
/// 真实流逝时间,以秒为单位。
internal override void Update(float elapseSeconds, float realElapseSeconds)
{
}
///
/// 关闭并清理数据表管理器。
///
internal override void Shutdown()
{
foreach (KeyValuePair dataTable in m_DataTables)
{
dataTable.Value.Shutdown();
}
m_DataTables.Clear();
}
///
/// 设置资源管理器。
///
/// 资源管理器。
public void SetResourceManager(IResourceManager resourceManager)
{
if (resourceManager == null)
{
throw new GameFrameworkException("Resource manager is invalid.");
}
m_ResourceManager = resourceManager;
}
///
/// 设置数据表数据提供者辅助器。
///
/// 数据表数据提供者辅助器。
public void SetDataProviderHelper(IDataProviderHelper dataProviderHelper)
{
if (dataProviderHelper == null)
{
throw new GameFrameworkException("Data provider helper is invalid.");
}
m_DataProviderHelper = dataProviderHelper;
}
///
/// 设置数据表辅助器。
///
/// 数据表辅助器。
public void SetDataTableHelper(IDataTableHelper dataTableHelper)
{
if (dataTableHelper == null)
{
throw new GameFrameworkException("Data table helper is invalid.");
}
m_DataTableHelper = dataTableHelper;
}
///
/// 确保二进制流缓存分配足够大小的内存并缓存。
///
/// 要确保二进制流缓存分配内存的大小。
public void EnsureCachedBytesSize(int ensureSize)
{
DataProvider.EnsureCachedBytesSize(ensureSize);
}
///
/// 释放缓存的二进制流。
///
public void FreeCachedBytes()
{
DataProvider.FreeCachedBytes();
}
///
/// 是否存在数据表。
///
/// 数据表行的类型。
/// 是否存在数据表。
public bool HasDataTable() where T : IDataRow
{
return InternalHasDataTable(new TypeNamePair(typeof(T)));
}
///
/// 是否存在数据表。
///
/// 数据表行的类型。
/// 是否存在数据表。
public bool HasDataTable(Type dataRowType)
{
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
return InternalHasDataTable(new TypeNamePair(dataRowType));
}
///
/// 是否存在数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 是否存在数据表。
public bool HasDataTable(string name) where T : IDataRow
{
return InternalHasDataTable(new TypeNamePair(typeof(T), name));
}
///
/// 是否存在数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 是否存在数据表。
public bool HasDataTable(Type dataRowType, string name)
{
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
return InternalHasDataTable(new TypeNamePair(dataRowType, name));
}
///
/// 获取数据表。
///
/// 数据表行的类型。
/// 要获取的数据表。
public IDataTable GetDataTable() where T : IDataRow
{
return (IDataTable)InternalGetDataTable(new TypeNamePair(typeof(T)));
}
///
/// 获取数据表。
///
/// 数据表行的类型。
/// 要获取的数据表。
public DataTableBase GetDataTable(Type dataRowType)
{
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
return InternalGetDataTable(new TypeNamePair(dataRowType));
}
///
/// 获取数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 要获取的数据表。
public IDataTable GetDataTable(string name) where T : IDataRow
{
return (IDataTable)InternalGetDataTable(new TypeNamePair(typeof(T), name));
}
///
/// 获取数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 要获取的数据表。
public DataTableBase GetDataTable(Type dataRowType, string name)
{
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
return InternalGetDataTable(new TypeNamePair(dataRowType, name));
}
///
/// 获取所有数据表。
///
/// 所有数据表。
public DataTableBase[] GetAllDataTables()
{
int index = 0;
DataTableBase[] results = new DataTableBase[m_DataTables.Count];
foreach (KeyValuePair dataTable in m_DataTables)
{
results[index++] = dataTable.Value;
}
return results;
}
///
/// 获取所有数据表。
///
/// 所有数据表。
public void GetAllDataTables(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
foreach (KeyValuePair dataTable in m_DataTables)
{
results.Add(dataTable.Value);
}
}
///
/// 创建数据表。
///
/// 数据表行的类型。
/// 要创建的数据表。
public IDataTable CreateDataTable() where T : class, IDataRow, new()
{
return CreateDataTable(string.Empty);
}
///
/// 创建数据表。
///
/// 数据表行的类型。
/// 要创建的数据表。
public DataTableBase CreateDataTable(Type dataRowType)
{
return CreateDataTable(dataRowType, string.Empty);
}
///
/// 创建数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 要创建的数据表。
public IDataTable CreateDataTable(string name) where T : class, IDataRow, new()
{
if (m_ResourceManager == null)
{
throw new GameFrameworkException("You must set resource manager first.");
}
if (m_DataProviderHelper == null)
{
throw new GameFrameworkException("You must set data provider helper first.");
}
TypeNamePair typeNamePair = new TypeNamePair(typeof(T), name);
if (HasDataTable(name))
{
throw new GameFrameworkException(Utility.Text.Format("Already exist data table '{0}'.", typeNamePair));
}
DataTable dataTable = new DataTable(name);
dataTable.SetResourceManager(m_ResourceManager);
dataTable.SetDataProviderHelper(m_DataProviderHelper);
m_DataTables.Add(typeNamePair, dataTable);
return dataTable;
}
///
/// 创建数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 要创建的数据表。
public DataTableBase CreateDataTable(Type dataRowType, string name)
{
if (m_ResourceManager == null)
{
throw new GameFrameworkException("You must set resource manager first.");
}
if (m_DataProviderHelper == null)
{
throw new GameFrameworkException("You must set data provider helper first.");
}
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
TypeNamePair typeNamePair = new TypeNamePair(dataRowType, name);
if (HasDataTable(dataRowType, name))
{
throw new GameFrameworkException(Utility.Text.Format("Already exist data table '{0}'.", typeNamePair));
}
Type dataTableType = typeof(DataTable<>).MakeGenericType(dataRowType);
DataTableBase dataTable = (DataTableBase)Activator.CreateInstance(dataTableType, name);
dataTable.SetResourceManager(m_ResourceManager);
dataTable.SetDataProviderHelper(m_DataProviderHelper);
m_DataTables.Add(typeNamePair, dataTable);
return dataTable;
}
///
/// 销毁数据表。
///
/// 数据表行的类型。
public bool DestroyDataTable() where T : IDataRow
{
return InternalDestroyDataTable(new TypeNamePair(typeof(T)));
}
///
/// 销毁数据表。
///
/// 数据表行的类型。
/// 是否销毁数据表成功。
public bool DestroyDataTable(Type dataRowType)
{
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
return InternalDestroyDataTable(new TypeNamePair(dataRowType));
}
///
/// 销毁数据表。
///
/// 数据表行的类型。
/// 数据表名称。
public bool DestroyDataTable(string name) where T : IDataRow
{
return InternalDestroyDataTable(new TypeNamePair(typeof(T), name));
}
///
/// 销毁数据表。
///
/// 数据表行的类型。
/// 数据表名称。
/// 是否销毁数据表成功。
public bool DestroyDataTable(Type dataRowType, string name)
{
if (dataRowType == null)
{
throw new GameFrameworkException("Data row type is invalid.");
}
if (!typeof(IDataRow).IsAssignableFrom(dataRowType))
{
throw new GameFrameworkException(Utility.Text.Format("Data row type '{0}' is invalid.", dataRowType.FullName));
}
return InternalDestroyDataTable(new TypeNamePair(dataRowType, name));
}
///
/// 销毁数据表。
///
/// 数据表行的类型。
/// 要销毁的数据表。
/// 是否销毁数据表成功。
public bool DestroyDataTable(IDataTable dataTable) where T : IDataRow
{
if (dataTable == null)
{
throw new GameFrameworkException("Data table is invalid.");
}
return InternalDestroyDataTable(new TypeNamePair(typeof(T), dataTable.Name));
}
///
/// 销毁数据表。
///
/// 要销毁的数据表。
/// 是否销毁数据表成功。
public bool DestroyDataTable(DataTableBase dataTable)
{
if (dataTable == null)
{
throw new GameFrameworkException("Data table is invalid.");
}
return InternalDestroyDataTable(new TypeNamePair(dataTable.Type, dataTable.Name));
}
private bool InternalHasDataTable(TypeNamePair typeNamePair)
{
return m_DataTables.ContainsKey(typeNamePair);
}
private DataTableBase InternalGetDataTable(TypeNamePair typeNamePair)
{
DataTableBase dataTable = null;
if (m_DataTables.TryGetValue(typeNamePair, out dataTable))
{
return dataTable;
}
return null;
}
private bool InternalDestroyDataTable(TypeNamePair typeNamePair)
{
DataTableBase dataTable = null;
if (m_DataTables.TryGetValue(typeNamePair, out dataTable))
{
dataTable.Shutdown();
return m_DataTables.Remove(typeNamePair);
}
return false;
}
}
}