-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tag.cs
64 lines (53 loc) · 1.71 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
using System.Linq;
using NbtEditor.Values;
namespace NbtEditor.Tags
{
public sealed class Tag
{
public enum TagType
{
#pragma warning disable CA1720 // Identifier contains type name
End,
Byte,
Short,
Int,
Long,
Float,
Double,
ByteArray,
String,
List,
Compound,
IntArray,
LongArray
#pragma warning restore CA1720 // Identifier contains type name
}
public static Tag EndTag => new Tag(TagType.End, StringValue.Null, new EndTagValue());
public TagType Type { get; }
public StringValue Name { get; }
public TagValue Value { get; }
public Tag(TagType type, StringValue name, TagValue value)
{
Type = type;
Name = name;
Value = value;
}
public Tag(TagType type, string name, TagValue value) : this(type, new StringValue(name), value) { }
public byte[] ToByteArray() =>
new byte[] { (byte)Type }.Concat(Name.ToByteArray()).Concat(Value.ToByteArray()).ToArray();
public static Tag Parse(byte[] data, int index, out int length)
{
TagType id = (TagType)data[index];
length = sizeof(byte);
if (id == TagType.End)
{
return EndTag;
}
StringValue name = StringValue.Parse(data, index + sizeof(byte), out int nameLength);
length += nameLength;
TagValue value = TagValue.Parse(id, data, index + length, out int valueLength);
length += valueLength;
return new Tag(id, name, value);
}
}
}