//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using System.Collections.Generic;
namespace GameFramework.DataNode
{
internal sealed partial class DataNodeManager : GameFrameworkModule, IDataNodeManager
{
///
/// 数据结点。
///
private sealed class DataNode : IDataNode, IReference
{
private static readonly DataNode[] EmptyDataNodeArray = new DataNode[] { };
private string m_Name;
private Variable m_Data;
private DataNode m_Parent;
private List m_Childs;
public DataNode()
{
m_Name = null;
m_Data = null;
m_Parent = null;
m_Childs = null;
}
///
/// 创建数据结点。
///
/// 数据结点名称。
/// 父数据结点。
/// 创建的数据结点。
public static DataNode Create(string name, DataNode parent)
{
if (!IsValidName(name))
{
throw new GameFrameworkException("Name of data node is invalid.");
}
DataNode node = ReferencePool.Acquire();
node.m_Name = name;
node.m_Parent = parent;
return node;
}
///
/// 获取数据结点的名称。
///
public string Name
{
get
{
return m_Name;
}
}
///
/// 获取数据结点的完整名称。
///
public string FullName
{
get
{
return m_Parent == null ? m_Name : Utility.Text.Format("{0}{1}{2}", m_Parent.FullName, PathSplitSeparator[0], m_Name);
}
}
///
/// 获取父数据结点。
///
public IDataNode Parent
{
get
{
return m_Parent;
}
}
///
/// 获取子数据结点的数量。
///
public int ChildCount
{
get
{
return m_Childs != null ? m_Childs.Count : 0;
}
}
///
/// 根据类型获取数据结点的数据。
///
/// 要获取的数据类型。
/// 指定类型的数据。
public T GetData() where T : Variable
{
return (T)m_Data;
}
///
/// 获取数据结点的数据。
///
/// 数据结点数据。
public Variable GetData()
{
return m_Data;
}
///
/// 设置数据结点的数据。
///
/// 要设置的数据类型。
/// 要设置的数据。
public void SetData(T data) where T : Variable
{
SetData((Variable)data);
}
///
/// 设置数据结点的数据。
///
/// 要设置的数据。
public void SetData(Variable data)
{
if (m_Data != null)
{
ReferencePool.Release(m_Data);
}
m_Data = data;
}
///
/// 根据索引检查是否存在子数据结点。
///
/// 子数据结点的索引。
/// 是否存在子数据结点。
public bool HasChild(int index)
{
return index >= 0 && index < ChildCount;
}
///
/// 根据名称检查是否存在子数据结点。
///
/// 子数据结点名称。
/// 是否存在子数据结点。
public bool HasChild(string name)
{
if (!IsValidName(name))
{
throw new GameFrameworkException("Name is invalid.");
}
if (m_Childs == null)
{
return false;
}
foreach (DataNode child in m_Childs)
{
if (child.Name == name)
{
return true;
}
}
return false;
}
///
/// 根据索引获取子数据结点。
///
/// 子数据结点的索引。
/// 指定索引的子数据结点,如果索引越界,则返回空。
public IDataNode GetChild(int index)
{
return index >= 0 && index < ChildCount ? m_Childs[index] : null;
}
///
/// 根据名称获取子数据结点。
///
/// 子数据结点名称。
/// 指定名称的子数据结点,如果没有找到,则返回空。
public IDataNode GetChild(string name)
{
if (!IsValidName(name))
{
throw new GameFrameworkException("Name is invalid.");
}
if (m_Childs == null)
{
return null;
}
foreach (DataNode child in m_Childs)
{
if (child.Name == name)
{
return child;
}
}
return null;
}
///
/// 根据名称获取或增加子数据结点。
///
/// 子数据结点名称。
/// 指定名称的子数据结点,如果对应名称的子数据结点已存在,则返回已存在的子数据结点,否则增加子数据结点。
public IDataNode GetOrAddChild(string name)
{
DataNode node = (DataNode)GetChild(name);
if (node != null)
{
return node;
}
node = Create(name, this);
if (m_Childs == null)
{
m_Childs = new List();
}
m_Childs.Add(node);
return node;
}
///
/// 获取所有子数据结点。
///
/// 所有子数据结点。
public IDataNode[] GetAllChild()
{
if (m_Childs == null)
{
return EmptyDataNodeArray;
}
return m_Childs.ToArray();
}
///
/// 获取所有子数据结点。
///
/// 所有子数据结点。
public void GetAllChild(List results)
{
if (results == null)
{
throw new GameFrameworkException("Results is invalid.");
}
results.Clear();
if (m_Childs == null)
{
return;
}
foreach (DataNode child in m_Childs)
{
results.Add(child);
}
}
///
/// 根据索引移除子数据结点。
///
/// 子数据结点的索引位置。
public void RemoveChild(int index)
{
DataNode node = (DataNode)GetChild(index);
if (node == null)
{
return;
}
m_Childs.Remove(node);
ReferencePool.Release(node);
}
///
/// 根据名称移除子数据结点。
///
/// 子数据结点名称。
public void RemoveChild(string name)
{
DataNode node = (DataNode)GetChild(name);
if (node == null)
{
return;
}
m_Childs.Remove(node);
ReferencePool.Release(node);
}
public void Clear()
{
if (m_Data != null)
{
ReferencePool.Release(m_Data);
m_Data = null;
}
if (m_Childs != null)
{
foreach (DataNode child in m_Childs)
{
ReferencePool.Release(child);
}
m_Childs.Clear();
}
}
///
/// 获取数据结点字符串。
///
/// 数据结点字符串。
public override string ToString()
{
return Utility.Text.Format("{0}: {1}", FullName, ToDataString());
}
///
/// 获取数据字符串。
///
/// 数据字符串。
public string ToDataString()
{
if (m_Data == null)
{
return "";
}
return Utility.Text.Format("[{0}] {1}", m_Data.Type.Name, m_Data);
}
///
/// 检测数据结点名称是否合法。
///
/// 要检测的数据结点名称。
/// 是否是合法的数据结点名称。
private static bool IsValidName(string name)
{
if (string.IsNullOrEmpty(name))
{
return false;
}
foreach (string pathSplitSeparator in PathSplitSeparator)
{
if (name.Contains(pathSplitSeparator))
{
return false;
}
}
return true;
}
void IReference.Clear()
{
m_Name = null;
m_Parent = null;
Clear();
}
}
}
}