/
Tag.cs
296 lines (241 loc) · 6.66 KB
/
Tag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Cyotek.Data.Nbt
{
#pragma warning disable CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
// Disabling 659 this as it's pointless overriding
// GetHashCode when each concrete instance already
// is, making a base version uncallable
public abstract class Tag : IEquatable<Tag>
#pragma warning restore CS0659 // Type overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
#region Fields
private string _name;
private Tag _parent;
#endregion
#region Constructors
/// <summary>
/// Specialised constructor for use only by derived class.
/// </summary>
/// <param name="name">The name of the tag.</param>
protected Tag(string name)
{
_name = name ?? string.Empty;
}
#endregion
#region Properties
[Browsable(false)]
public string FullPath
{
get
{
Tag[] ancestors;
StringBuilder sb;
ancestors = this.GetAncestors();
sb = new StringBuilder();
// ReSharper disable once ForCanBeConvertedToForeach
for (int i = 0; i < ancestors.Length; i++)
{
Tag ancestor;
ICollectionTag container;
ancestor = ancestors[i];
container = ancestor.Parent as ICollectionTag;
if (sb.Length != 0)
{
sb.Append('\\');
}
if (container == null || !container.IsList)
{
sb.Append(ancestor.Name);
}
else
{
sb.Append(container.Values.IndexOf(ancestor));
}
}
if (sb.Length != 0)
{
sb.Append('\\');
}
sb.Append(_name);
return sb.ToString();
}
}
/// <summary>
/// Gets or sets the tag name.
/// </summary>
/// <value>
/// The name of the tag.
/// </value>
[Category("Data")]
[DefaultValue("")]
public string Name
{
get { return _name; }
set
{
if (value == null)
{
value = string.Empty;
}
if (this.Name != value)
{
ICollectionTag collection;
TagDictionary values;
collection = _parent as ICollectionTag;
values = collection?.Values as TagDictionary;
values?.ChangeKey(this, value);
_name = value;
}
}
}
/// <summary>
/// Gets the parent <see cref="Tag"/>.
/// </summary>
/// <value>
/// The parent <see cref="Tag"/>.
/// </value>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Tag Parent
{
get { return _parent; }
internal set { _parent = value; }
}
/// <summary>
/// Gets the tag type.
/// </summary>
/// <value>
/// The tag type.
/// </value>
public abstract TagType Type { get; }
#endregion
#region Methods
#pragma warning disable 659
/// <summary>
/// Tests if this <see cref="Tag"/> is considered equal to another.
/// </summary>
/// <param name="obj">The object to compare to this object.</param>
/// <returns>
/// <c>true</c> if the objects are considered equal, <c>false</c> if they are not.
/// </returns>
/// <seealso cref="M:Cyotek.Data.Nbt.Tag.Equals(object)"/>
public override bool Equals(object obj)
#pragma warning restore 659
{
return !ReferenceEquals(null, obj) && (ReferenceEquals(this, obj) || obj.GetType() == this.GetType() && this.Equals((Tag)obj));
}
public Tag[] Flatten()
{
Tag[] results;
if (!(this is ICollectionTag))
{
// single value
results = new[]
{
this
};
}
else
{
// multiple values;
List<Tag> tags;
tags = new List<Tag>();
this.FlattenTag(this, tags);
results = tags.ToArray();
}
return results;
}
public Tag[] GetAncestors()
{
Tag[] results;
List<Tag> tags;
Tag tag;
int arrayIndex;
tags = new List<Tag>();
tag = _parent;
while (tag != null)
{
tags.Add(tag);
tag = tag.Parent;
}
results = new Tag[tags.Count];
arrayIndex = 0;
for (int i = tags.Count; i > 0; i--)
{
results[arrayIndex++] = tags[i - 1];
}
return results;
}
/// <summary>
/// Gets the value of a tag.
/// </summary>
/// <returns>
/// The tag's value.
/// </returns>
/// <remarks>Where possible, it is recommended the <c>Value</c> property of a tag is used to avoid boxing.</remarks>
public abstract object GetValue();
/// <summary>
/// Sets the value of the tag.
/// </summary>
/// <param name="value">The new value of the tag.</param>
/// <remarks>Where possible, it is recommended the <c>Value</c> property of a tag is used to avoid boxing.</remarks>
public abstract void SetValue(object value);
/// <summary>
/// Convert this object into a string representation.
/// </summary>
/// <returns>
/// A string that represents this object.
/// </returns>
public override string ToString()
{
return string.Concat("[", this.Type, ": ", this.Name, "=", this.ToValueString(), "]");
}
/// <summary>
/// Converts the value of this object to a string.
/// </summary>
/// <returns>
/// The value of this object as a string.
/// </returns>
public abstract string ToValueString();
private void FlattenTag(Tag tag, ICollection<Tag> tags)
{
ICollectionTag collectionTag;
tags.Add(tag);
collectionTag = tag as ICollectionTag;
if (collectionTag != null)
{
foreach (Tag childTag in collectionTag.Values)
{
this.FlattenTag(childTag, tags);
}
}
}
#endregion
#region IEquatable<Tag> Interface
/// <summary>
/// Tests if this <see cref="Tag"/> is considered equal to another.
/// </summary>
/// <param name="other">The tag to compare to this object.</param>
/// <returns>
/// <c>true</c> if the objects are considered equal, <c>false</c> if they are not.
/// </returns>
public bool Equals(Tag other)
{
bool result;
result = !ReferenceEquals(null, other);
if (result && !ReferenceEquals(this, other))
{
result = string.Equals(_name, other.Name);
if (result)
{
result = Equals(this.GetValue(), other.GetValue());
}
}
return result;
}
#endregion
}
}