/SubstrateCS/Source/Nbt/TagNodeList.cs

https://github.com/StevilKnevil/Substrate · C# · 264 lines · 128 code · 33 blank · 103 comment · 6 complexity · d188715a5901910d8f3985c810deca11 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Substrate.Nbt
  4. {
  5. /// <summary>
  6. /// An NBT node representing a list tag type containing other nodes.
  7. /// </summary>
  8. /// <remarks>
  9. /// A list node contains 0 or more nodes of the same type. The nodes are unnamed
  10. /// but can be accessed by sequential index.
  11. /// </remarks>
  12. public sealed class TagNodeList : TagNode, IList<TagNode>
  13. {
  14. private TagType _type = TagType.TAG_END;
  15. private List<TagNode> _items = null;
  16. /// <summary>
  17. /// Converts the node to itself.
  18. /// </summary>
  19. /// <returns>A reference to itself.</returns>
  20. public override TagNodeList ToTagList ()
  21. {
  22. return this;
  23. }
  24. /// <summary>
  25. /// Gets the tag type of the node.
  26. /// </summary>
  27. /// <returns>The TAG_STRING tag type.</returns>
  28. public override TagType GetTagType ()
  29. {
  30. return TagType.TAG_LIST;
  31. }
  32. /// <summary>
  33. /// Gets the number of subnodes contained in the list.
  34. /// </summary>
  35. public int Count
  36. {
  37. get { return _items.Count; }
  38. }
  39. /// <summary>
  40. /// Gets the tag type of the subnodes contained in the list.
  41. /// </summary>
  42. public TagType ValueType
  43. {
  44. get { return _type; }
  45. }
  46. /// <summary>
  47. /// Constructs a new empty list node.
  48. /// </summary>
  49. /// <param name="type">The tag type of the list's subnodes.</param>
  50. public TagNodeList (TagType type)
  51. {
  52. _type = type;
  53. _items = new List<TagNode>();
  54. }
  55. /// <summary>
  56. /// Constructs a new list node from a list of nodes.
  57. /// </summary>
  58. /// <param name="type">The tag type of the list's subnodes.</param>
  59. /// <param name="items">A list containing node objects matching the type parameter.</param>
  60. public TagNodeList (TagType type, List<TagNode> items)
  61. {
  62. _type = type;
  63. _items = items;
  64. }
  65. /// <summary>
  66. /// Makes a deep copy of the node.
  67. /// </summary>
  68. /// <returns>A new list node containing new subnodes representing the same data.</returns>
  69. public override TagNode Copy ()
  70. {
  71. TagNodeList list = new TagNodeList(_type);
  72. foreach (TagNode item in _items) {
  73. list.Add(item.Copy());
  74. }
  75. return list;
  76. }
  77. /// <summary>
  78. /// Retrieves all the subnodes that match the conditions defined by the specified predicate.
  79. /// </summary>
  80. /// <param name="match">The <see cref="Predicate{TagNode}"/> delegate that defines the conditions of the subnode to search for.</param>
  81. /// <returns>A list of all subnodes matching the predicate.</returns>
  82. public List<TagNode> FindAll (Predicate<TagNode> match)
  83. {
  84. return _items.FindAll(match);
  85. }
  86. /// <summary>
  87. /// Removes all subnodes that match the conditions defined by the specified predicate.
  88. /// </summary>
  89. /// <param name="match">The <see cref="Predicate{TagNode}"/> delegate that defines the conditions of the subnode to search for.</param>
  90. /// <returns>The number of subnodes removed from the node.</returns>
  91. public int RemoveAll (Predicate<TagNode> match)
  92. {
  93. return _items.RemoveAll(match);
  94. }
  95. /// <summary>
  96. /// Gets a string representation of the node's data.
  97. /// </summary>
  98. /// <returns>String representation of the node's data.</returns>
  99. public override string ToString ()
  100. {
  101. return _items.ToString();
  102. }
  103. #region IList<NBT_Value> Members
  104. /// <summary>
  105. /// Searches for the specified subnode and returns the zero-based index of the first occurrence within the entire node's list.
  106. /// </summary>
  107. /// <param name="item">The subnode to locate.</param>
  108. /// <returns>The zero-based index of the subnode within the node's list if found, or -1 otherwise.</returns>
  109. public int IndexOf (TagNode item)
  110. {
  111. return _items.IndexOf(item);
  112. }
  113. /// <summary>
  114. /// Inserts a subnode into the node's list at the specified index.
  115. /// </summary>
  116. /// <param name="index">The zero-based index at which the subnode should be inserted.</param>
  117. /// <param name="item">The subnode to insert.</param>
  118. /// <exception cref="ArgumentException">Thrown when a subnode being inserted has the wrong tag type.</exception>
  119. public void Insert (int index, TagNode item)
  120. {
  121. if (item.GetTagType() != _type) {
  122. throw new ArgumentException("The tag type of item is invalid for this node");
  123. }
  124. _items.Insert(index, item);
  125. }
  126. /// <summary>
  127. /// Removes the subnode from the node's list at the specified index.
  128. /// </summary>
  129. /// <param name="index">The zero-based index to remove a subnode at.</param>
  130. public void RemoveAt (int index)
  131. {
  132. _items.RemoveAt(index);
  133. }
  134. /// <summary>
  135. /// Gets or sets the subnode in the node's list at the specified index.
  136. /// </summary>
  137. /// <param name="index">The zero-based index to get or set from.</param>
  138. /// <returns>The subnode at the specified index.</returns>
  139. /// <exception cref="ArgumentException">Thrown when a subnode being assigned has the wrong tag type.</exception>
  140. public TagNode this[int index]
  141. {
  142. get
  143. {
  144. return _items[index];
  145. }
  146. set
  147. {
  148. if (value.GetTagType() != _type) {
  149. throw new ArgumentException("The tag type of the assigned subnode is invalid for this node");
  150. }
  151. _items[index] = value;
  152. }
  153. }
  154. #endregion
  155. #region ICollection<NBT_Value> Members
  156. /// <summary>
  157. /// Adds a subnode to the end of the node's list.
  158. /// </summary>
  159. /// <param name="item">The subnode to add.</param>
  160. /// <exception cref="ArgumentException">Thrown when a subnode being added has the wrong tag type.</exception>
  161. public void Add (TagNode item)
  162. {
  163. if (item.GetTagType() != _type) {
  164. throw new ArgumentException("The tag type of item is invalid for this node");
  165. }
  166. _items.Add(item);
  167. }
  168. /// <summary>
  169. /// Removes all subnode's from the node's list.
  170. /// </summary>
  171. public void Clear ()
  172. {
  173. _items.Clear();
  174. }
  175. /// <summary>
  176. /// Checks if a subnode is contained within the node's list.
  177. /// </summary>
  178. /// <param name="item">The subnode to check for existance.</param>
  179. /// <returns>Status indicating if the subnode exists in the node's list.</returns>
  180. public bool Contains (TagNode item)
  181. {
  182. return _items.Contains(item);
  183. }
  184. /// <summary>
  185. /// Copies the entire node's list to a compatible one-dimensional array, starting at the specified index of the target array.
  186. /// </summary>
  187. /// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the subnodes copied. The Array must have zero-based indexing.</param>
  188. /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
  189. public void CopyTo (TagNode[] array, int arrayIndex)
  190. {
  191. _items.CopyTo(array, arrayIndex);
  192. }
  193. /// <summary>
  194. /// Gets a value indicating whether the node is readonly.
  195. /// </summary>
  196. public bool IsReadOnly
  197. {
  198. get { return false; }
  199. }
  200. /// <summary>
  201. /// Removes the first occurance of a subnode from the node's list.
  202. /// </summary>
  203. /// <param name="item">The subnode to remove.</param>
  204. /// <returns>Status indicating whether a subnode was removed.</returns>
  205. public bool Remove (TagNode item)
  206. {
  207. return _items.Remove(item);
  208. }
  209. #endregion
  210. #region IEnumerable<NBT_Value> Members
  211. /// <summary>
  212. /// Returns an enumerator that iterates through all of the subnodes in the node's list.
  213. /// </summary>
  214. /// <returns>An enumerator for this node.</returns>
  215. public IEnumerator<TagNode> GetEnumerator ()
  216. {
  217. return _items.GetEnumerator();
  218. }
  219. #endregion
  220. #region IEnumerable Members
  221. /// <summary>
  222. /// Returns an enumerator that iterates through all of the subnodes in the node's list.
  223. /// </summary>
  224. /// <returns>An enumerator for this node.</returns>
  225. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
  226. {
  227. return _items.GetEnumerator();
  228. }
  229. #endregion
  230. }
  231. }