//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using System;
namespace GameFramework.DataNode
{
///
/// 数据结点管理器。
///
internal sealed partial class DataNodeManager : GameFrameworkModule, IDataNodeManager
{
private static readonly string[] EmptyStringArray = new string[] { };
private static readonly string[] PathSplitSeparator = new string[] { ".", "/", "\\" };
private const string RootName = "";
private DataNode m_Root;
///
/// 初始化数据结点管理器的新实例。
///
public DataNodeManager()
{
m_Root = DataNode.Create(RootName, null);
}
///
/// 获取根数据结点。
///
public IDataNode Root
{
get
{
return m_Root;
}
}
///
/// 数据结点管理器轮询。
///
/// 逻辑流逝时间,以秒为单位。
/// 真实流逝时间,以秒为单位。
internal override void Update(float elapseSeconds, float realElapseSeconds)
{
}
///
/// 关闭并清理数据结点管理器。
///
internal override void Shutdown()
{
ReferencePool.Release(m_Root);
m_Root = null;
}
///
/// 根据类型获取数据结点的数据。
///
/// 要获取的数据类型。
/// 相对于 node 的查找路径。
/// 指定类型的数据。
public T GetData(string path) where T : Variable
{
return GetData(path, null);
}
///
/// 获取数据结点的数据。
///
/// 相对于 node 的查找路径。
/// 数据结点的数据。
public Variable GetData(string path)
{
return GetData(path, null);
}
///
/// 根据类型获取数据结点的数据。
///
/// 要获取的数据类型。
/// 相对于 node 的查找路径。
/// 查找起始结点。
/// 指定类型的数据。
public T GetData(string path, IDataNode node) where T : Variable
{
IDataNode current = GetNode(path, node);
if (current == null)
{
throw new GameFrameworkException(Utility.Text.Format("Data node is not exist, path '{0}', node '{1}'.", path, node != null ? node.FullName : string.Empty));
}
return current.GetData();
}
///
/// 获取数据结点的数据。
///
/// 相对于 node 的查找路径。
/// 查找起始结点。
/// 数据结点的数据。
public Variable GetData(string path, IDataNode node)
{
IDataNode current = GetNode(path, node);
if (current == null)
{
throw new GameFrameworkException(Utility.Text.Format("Data node is not exist, path '{0}', node '{1}'.", path, node != null ? node.FullName : string.Empty));
}
return current.GetData();
}
///
/// 设置数据结点的数据。
///
/// 要设置的数据类型。
/// 相对于 node 的查找路径。
/// 要设置的数据。
public void SetData(string path, T data) where T : Variable
{
SetData(path, data, null);
}
///
/// 设置数据结点的数据。
///
/// 相对于 node 的查找路径。
/// 要设置的数据。
public void SetData(string path, Variable data)
{
SetData(path, data, null);
}
///
/// 设置数据结点的数据。
///
/// 要设置的数据类型。
/// 相对于 node 的查找路径。
/// 要设置的数据。
/// 查找起始结点。
public void SetData(string path, T data, IDataNode node) where T : Variable
{
IDataNode current = GetOrAddNode(path, node);
current.SetData(data);
}
///
/// 设置数据结点的数据。
///
/// 相对于 node 的查找路径。
/// 要设置的数据。
/// 查找起始结点。
public void SetData(string path, Variable data, IDataNode node)
{
IDataNode current = GetOrAddNode(path, node);
current.SetData(data);
}
///
/// 获取数据结点。
///
/// 相对于 node 的查找路径。
/// 指定位置的数据结点,如果没有找到,则返回空。
public IDataNode GetNode(string path)
{
return GetNode(path, null);
}
///
/// 获取数据结点。
///
/// 相对于 node 的查找路径。
/// 查找起始结点。
/// 指定位置的数据结点,如果没有找到,则返回空。
public IDataNode GetNode(string path, IDataNode node)
{
IDataNode current = node ?? m_Root;
string[] splitedPath = GetSplitedPath(path);
foreach (string i in splitedPath)
{
current = current.GetChild(i);
if (current == null)
{
return null;
}
}
return current;
}
///
/// 获取或增加数据结点。
///
/// 相对于 node 的查找路径。
/// 指定位置的数据结点,如果没有找到,则创建相应的数据结点。
public IDataNode GetOrAddNode(string path)
{
return GetOrAddNode(path, null);
}
///
/// 获取或增加数据结点。
///
/// 相对于 node 的查找路径。
/// 查找起始结点。
/// 指定位置的数据结点,如果没有找到,则增加相应的数据结点。
public IDataNode GetOrAddNode(string path, IDataNode node)
{
IDataNode current = node ?? m_Root;
string[] splitedPath = GetSplitedPath(path);
foreach (string i in splitedPath)
{
current = current.GetOrAddChild(i);
}
return current;
}
///
/// 移除数据结点。
///
/// 相对于 node 的查找路径。
public void RemoveNode(string path)
{
RemoveNode(path, null);
}
///
/// 移除数据结点。
///
/// 相对于 node 的查找路径。
/// 查找起始结点。
public void RemoveNode(string path, IDataNode node)
{
IDataNode current = node ?? m_Root;
IDataNode parent = current.Parent;
string[] splitedPath = GetSplitedPath(path);
foreach (string i in splitedPath)
{
parent = current;
current = current.GetChild(i);
if (current == null)
{
return;
}
}
if (parent != null)
{
parent.RemoveChild(current.Name);
}
}
///
/// 移除所有数据结点。
///
public void Clear()
{
m_Root.Clear();
}
///
/// 数据结点路径切分工具函数。
///
/// 要切分的数据结点路径。
/// 切分后的字符串数组。
private static string[] GetSplitedPath(string path)
{
if (string.IsNullOrEmpty(path))
{
return EmptyStringArray;
}
return path.Split(PathSplitSeparator, StringSplitOptions.RemoveEmptyEntries);
}
}
}