DataNodeManager.DataNode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System.Collections.Generic;
  8. namespace GameFramework.DataNode
  9. {
  10. internal sealed partial class DataNodeManager : GameFrameworkModule, IDataNodeManager
  11. {
  12. /// <summary>
  13. /// 数据结点。
  14. /// </summary>
  15. private sealed class DataNode : IDataNode, IReference
  16. {
  17. private static readonly DataNode[] EmptyDataNodeArray = new DataNode[] { };
  18. private string m_Name;
  19. private Variable m_Data;
  20. private DataNode m_Parent;
  21. private List<DataNode> m_Childs;
  22. public DataNode()
  23. {
  24. m_Name = null;
  25. m_Data = null;
  26. m_Parent = null;
  27. m_Childs = null;
  28. }
  29. /// <summary>
  30. /// 创建数据结点。
  31. /// </summary>
  32. /// <param name="name">数据结点名称。</param>
  33. /// <param name="parent">父数据结点。</param>
  34. /// <returns>创建的数据结点。</returns>
  35. public static DataNode Create(string name, DataNode parent)
  36. {
  37. if (!IsValidName(name))
  38. {
  39. throw new GameFrameworkException("Name of data node is invalid.");
  40. }
  41. DataNode node = ReferencePool.Acquire<DataNode>();
  42. node.m_Name = name;
  43. node.m_Parent = parent;
  44. return node;
  45. }
  46. /// <summary>
  47. /// 获取数据结点的名称。
  48. /// </summary>
  49. public string Name
  50. {
  51. get
  52. {
  53. return m_Name;
  54. }
  55. }
  56. /// <summary>
  57. /// 获取数据结点的完整名称。
  58. /// </summary>
  59. public string FullName
  60. {
  61. get
  62. {
  63. return m_Parent == null ? m_Name : Utility.Text.Format("{0}{1}{2}", m_Parent.FullName, PathSplitSeparator[0], m_Name);
  64. }
  65. }
  66. /// <summary>
  67. /// 获取父数据结点。
  68. /// </summary>
  69. public IDataNode Parent
  70. {
  71. get
  72. {
  73. return m_Parent;
  74. }
  75. }
  76. /// <summary>
  77. /// 获取子数据结点的数量。
  78. /// </summary>
  79. public int ChildCount
  80. {
  81. get
  82. {
  83. return m_Childs != null ? m_Childs.Count : 0;
  84. }
  85. }
  86. /// <summary>
  87. /// 根据类型获取数据结点的数据。
  88. /// </summary>
  89. /// <typeparam name="T">要获取的数据类型。</typeparam>
  90. /// <returns>指定类型的数据。</returns>
  91. public T GetData<T>() where T : Variable
  92. {
  93. return (T)m_Data;
  94. }
  95. /// <summary>
  96. /// 获取数据结点的数据。
  97. /// </summary>
  98. /// <returns>数据结点数据。</returns>
  99. public Variable GetData()
  100. {
  101. return m_Data;
  102. }
  103. /// <summary>
  104. /// 设置数据结点的数据。
  105. /// </summary>
  106. /// <typeparam name="T">要设置的数据类型。</typeparam>
  107. /// <param name="data">要设置的数据。</param>
  108. public void SetData<T>(T data) where T : Variable
  109. {
  110. SetData((Variable)data);
  111. }
  112. /// <summary>
  113. /// 设置数据结点的数据。
  114. /// </summary>
  115. /// <param name="data">要设置的数据。</param>
  116. public void SetData(Variable data)
  117. {
  118. if (m_Data != null)
  119. {
  120. ReferencePool.Release(m_Data);
  121. }
  122. m_Data = data;
  123. }
  124. /// <summary>
  125. /// 根据索引检查是否存在子数据结点。
  126. /// </summary>
  127. /// <param name="index">子数据结点的索引。</param>
  128. /// <returns>是否存在子数据结点。</returns>
  129. public bool HasChild(int index)
  130. {
  131. return index >= 0 && index < ChildCount;
  132. }
  133. /// <summary>
  134. /// 根据名称检查是否存在子数据结点。
  135. /// </summary>
  136. /// <param name="name">子数据结点名称。</param>
  137. /// <returns>是否存在子数据结点。</returns>
  138. public bool HasChild(string name)
  139. {
  140. if (!IsValidName(name))
  141. {
  142. throw new GameFrameworkException("Name is invalid.");
  143. }
  144. if (m_Childs == null)
  145. {
  146. return false;
  147. }
  148. foreach (DataNode child in m_Childs)
  149. {
  150. if (child.Name == name)
  151. {
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. /// <summary>
  158. /// 根据索引获取子数据结点。
  159. /// </summary>
  160. /// <param name="index">子数据结点的索引。</param>
  161. /// <returns>指定索引的子数据结点,如果索引越界,则返回空。</returns>
  162. public IDataNode GetChild(int index)
  163. {
  164. return index >= 0 && index < ChildCount ? m_Childs[index] : null;
  165. }
  166. /// <summary>
  167. /// 根据名称获取子数据结点。
  168. /// </summary>
  169. /// <param name="name">子数据结点名称。</param>
  170. /// <returns>指定名称的子数据结点,如果没有找到,则返回空。</returns>
  171. public IDataNode GetChild(string name)
  172. {
  173. if (!IsValidName(name))
  174. {
  175. throw new GameFrameworkException("Name is invalid.");
  176. }
  177. if (m_Childs == null)
  178. {
  179. return null;
  180. }
  181. foreach (DataNode child in m_Childs)
  182. {
  183. if (child.Name == name)
  184. {
  185. return child;
  186. }
  187. }
  188. return null;
  189. }
  190. /// <summary>
  191. /// 根据名称获取或增加子数据结点。
  192. /// </summary>
  193. /// <param name="name">子数据结点名称。</param>
  194. /// <returns>指定名称的子数据结点,如果对应名称的子数据结点已存在,则返回已存在的子数据结点,否则增加子数据结点。</returns>
  195. public IDataNode GetOrAddChild(string name)
  196. {
  197. DataNode node = (DataNode)GetChild(name);
  198. if (node != null)
  199. {
  200. return node;
  201. }
  202. node = Create(name, this);
  203. if (m_Childs == null)
  204. {
  205. m_Childs = new List<DataNode>();
  206. }
  207. m_Childs.Add(node);
  208. return node;
  209. }
  210. /// <summary>
  211. /// 获取所有子数据结点。
  212. /// </summary>
  213. /// <returns>所有子数据结点。</returns>
  214. public IDataNode[] GetAllChild()
  215. {
  216. if (m_Childs == null)
  217. {
  218. return EmptyDataNodeArray;
  219. }
  220. return m_Childs.ToArray();
  221. }
  222. /// <summary>
  223. /// 获取所有子数据结点。
  224. /// </summary>
  225. /// <param name="results">所有子数据结点。</param>
  226. public void GetAllChild(List<IDataNode> results)
  227. {
  228. if (results == null)
  229. {
  230. throw new GameFrameworkException("Results is invalid.");
  231. }
  232. results.Clear();
  233. if (m_Childs == null)
  234. {
  235. return;
  236. }
  237. foreach (DataNode child in m_Childs)
  238. {
  239. results.Add(child);
  240. }
  241. }
  242. /// <summary>
  243. /// 根据索引移除子数据结点。
  244. /// </summary>
  245. /// <param name="index">子数据结点的索引位置。</param>
  246. public void RemoveChild(int index)
  247. {
  248. DataNode node = (DataNode)GetChild(index);
  249. if (node == null)
  250. {
  251. return;
  252. }
  253. m_Childs.Remove(node);
  254. ReferencePool.Release(node);
  255. }
  256. /// <summary>
  257. /// 根据名称移除子数据结点。
  258. /// </summary>
  259. /// <param name="name">子数据结点名称。</param>
  260. public void RemoveChild(string name)
  261. {
  262. DataNode node = (DataNode)GetChild(name);
  263. if (node == null)
  264. {
  265. return;
  266. }
  267. m_Childs.Remove(node);
  268. ReferencePool.Release(node);
  269. }
  270. public void Clear()
  271. {
  272. if (m_Data != null)
  273. {
  274. ReferencePool.Release(m_Data);
  275. m_Data = null;
  276. }
  277. if (m_Childs != null)
  278. {
  279. foreach (DataNode child in m_Childs)
  280. {
  281. ReferencePool.Release(child);
  282. }
  283. m_Childs.Clear();
  284. }
  285. }
  286. /// <summary>
  287. /// 获取数据结点字符串。
  288. /// </summary>
  289. /// <returns>数据结点字符串。</returns>
  290. public override string ToString()
  291. {
  292. return Utility.Text.Format("{0}: {1}", FullName, ToDataString());
  293. }
  294. /// <summary>
  295. /// 获取数据字符串。
  296. /// </summary>
  297. /// <returns>数据字符串。</returns>
  298. public string ToDataString()
  299. {
  300. if (m_Data == null)
  301. {
  302. return "<Null>";
  303. }
  304. return Utility.Text.Format("[{0}] {1}", m_Data.Type.Name, m_Data);
  305. }
  306. /// <summary>
  307. /// 检测数据结点名称是否合法。
  308. /// </summary>
  309. /// <param name="name">要检测的数据结点名称。</param>
  310. /// <returns>是否是合法的数据结点名称。</returns>
  311. private static bool IsValidName(string name)
  312. {
  313. if (string.IsNullOrEmpty(name))
  314. {
  315. return false;
  316. }
  317. foreach (string pathSplitSeparator in PathSplitSeparator)
  318. {
  319. if (name.Contains(pathSplitSeparator))
  320. {
  321. return false;
  322. }
  323. }
  324. return true;
  325. }
  326. void IReference.Clear()
  327. {
  328. m_Name = null;
  329. m_Parent = null;
  330. Clear();
  331. }
  332. }
  333. }
  334. }