forked from jbendig/InvGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNBTTag.cpp
More file actions
272 lines (241 loc) · 6.19 KB
/
NBTTag.cpp
File metadata and controls
272 lines (241 loc) · 6.19 KB
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
#include "NBTTag.h"
#include <iostream>
#include <exception>
#include <cassert>
#include "Types.h"
using namespace NBT;
//Private functions that do the actual parsing and formatting.
namespace
{
class DataException : public std::exception
{
};
//Deserialize functions.
void DeserializeListTag(unsigned int& index,const vector<unsigned char>& buffer,Tag& tag);
void DeserializeCompoundTag(unsigned int& index,const vector<unsigned char>& buffer,Tag& tag);
void DeserializeTagData(unsigned int& index,const vector<unsigned char>& buffer,Tag& tag)
{
switch(tag.type)
{
case TAG_BYTE:
tag.byteValue = ReadByte(index,buffer);
break;
case TAG_SHORT:
tag.shortValue = ReadShort(index,buffer);
break;
case TAG_INT:
tag.intValue = ReadInt(index,buffer);
break;
case TAG_LONG:
tag.longValue = ReadLong(index,buffer);
break;
case TAG_FLOAT:
tag.floatValue = ReadFloat(index,buffer);
break;
case TAG_DOUBLE:
tag.doubleValue = ReadDouble(index,buffer);
break;
case TAG_BYTE_ARRAY:
std::cerr << "Byte arrays are not supported.\n";
throw DataException();
break;
case TAG_STRING:
tag.stringValue = ReadString(index,buffer);
break;
case TAG_LIST:
DeserializeListTag(index,buffer,tag);
break;
case TAG_COMPOUND:
DeserializeCompoundTag(index,buffer,tag);
break;
default:
std::cerr << "Encountered unknown tag type " << (int)tag.type << std::endl;
throw DataException();
};
}
void DeserializeListTag(unsigned int& index,const vector<unsigned char>& buffer,Tag& tag)
{
tag.listType = ReadByte(index,buffer);
const int tagCount = ReadInt(index,buffer);
for(int x = 0;x < tagCount;++x)
{
Tag childTag;
childTag.type = tag.listType;
DeserializeTagData(index,buffer,childTag);
tag.childTags.push_back(childTag);
}
}
void DeserializeCompoundTag(unsigned int& index,const vector<unsigned char>& buffer,Tag& tag)
{
const unsigned int indexMax = buffer.size();
while(index < indexMax)
{
Tag childTag;
childTag.type = ReadByte(index,buffer);
if(childTag.type == TAG_END)
return;
childTag.name = ReadString(index,buffer);
DeserializeTagData(index,buffer,childTag);
tag.childTags.push_back(childTag);
}
}
//Serialize functions.
void SerializeListTag(vector<unsigned char>& buffer,Tag& tag);
void SerializeCompoundTag(vector<unsigned char>& buffer,Tag& tag);
void SerializeTagData(vector<unsigned char>& buffer,Tag& tag)
{
switch(tag.type)
{
case TAG_BYTE:
WriteByte(buffer,tag.byteValue);
break;
case TAG_SHORT:
WriteShort(buffer,tag.shortValue);
break;
case TAG_INT:
WriteInt(buffer,tag.intValue);
break;
case TAG_LONG:
WriteLong(buffer,tag.longValue);
break;
case TAG_FLOAT:
WriteFloat(buffer,tag.floatValue);
break;
case TAG_DOUBLE:
WriteDouble(buffer,tag.doubleValue);
break;
case TAG_BYTE_ARRAY:
break;
case TAG_STRING:
WriteString(buffer,tag.stringValue);
break;
case TAG_LIST:
SerializeListTag(buffer,tag);
break;
case TAG_COMPOUND:
SerializeCompoundTag(buffer,tag);
break;
default:
std::cerr << "Unable to serialize unknown child tag of type " << (int)tag.type << std::endl;
throw DataException();
}
}
void SerializeListTag(vector<unsigned char>& buffer,Tag& tag)
{
WriteByte(buffer,tag.listType);
WriteInt(buffer,tag.childTags.size());
for(unsigned int x = 0;x < tag.childTags.size();++x)
{
Tag childTag = tag.childTags[x];
SerializeTagData(buffer,childTag);
}
}
void SerializeCompoundTag(vector<unsigned char>& buffer,Tag& tag)
{
for(unsigned int x = 0;x < tag.childTags.size();++x)
{
Tag childTag = tag.childTags[x];
WriteByte(buffer,childTag.type);
WriteString(buffer,childTag.name);
SerializeTagData(buffer,childTag);
}
WriteByte(buffer,TAG_END);
}
//Printing functions
void PrintTagRecursive(Tag& tag,int& depth)
{
for(int x = 0;x < depth;++x)
{
std::cout << "\t";
}
switch(tag.type)
{
case TAG_BYTE:
std::cout << "TAG_BYTE " << tag.name << " " << (int)tag.byteValue << std::endl;
break;
case TAG_SHORT:
std::cout << "TAG_SHORT " << tag.name << " " << tag.shortValue << std::endl;
break;
case TAG_INT:
std::cout << "TAG_INT " << tag.name << " " << tag.intValue << std::endl;
break;
case TAG_LONG:
std::cout << "TAG_LONG " << tag.name << " " << tag.longValue << std::endl;
break;
case TAG_FLOAT:
std::cout << "TAG_FLOAT " << tag.name << " " << tag.floatValue << std::endl;
break;
case TAG_DOUBLE:
std::cout << "TAG_DOUBLE " << tag.name << " " << tag.doubleValue << std::endl;
break;
case TAG_BYTE_ARRAY:
std::cout << "TAG_BYTE_ARRAY " << tag.name << std::endl;
break;
case TAG_STRING:
std::cout << "TAG_STRING " << tag.name << " " << tag.stringValue << std::endl;
break;
case TAG_LIST:
std::cout << "TAG_LIST " << tag.name << " with " << (int)tag.listType << " type tags\n";
break;
case TAG_COMPOUND:
std::cout << "TAG_COMPOUND " << tag.name << std::endl;
break;
default:
std::cout << "TAG_UNKNOWN\n";
}
depth++;
for(unsigned int x = 0;x < tag.childTags.size();++x)
{
PrintTagRecursive(tag.childTags[x],depth);
}
depth--;
}
};
bool NBT::DeserializeTag(vector<unsigned char>& buffer,Tag& tag)
{
//First tag must be a TAG_COMPOUND or else the format is unsupported.
assert(buffer[0] == TAG_COMPOUND);
unsigned int index = 0;
tag.type = ReadByte(index,buffer);
tag.name = ReadString(index,buffer);
try
{
DeserializeCompoundTag(index,buffer,tag);
}
catch(DataException&)
{
return false;
}
return true;
}
bool NBT::SerializeTag(vector<unsigned char>& buffer,Tag& tag)
{
//Top tag must be a compound tag or else the format is unsupported.
assert(tag.type == TAG_COMPOUND);
//Write header of first compound tag. Child compound tags will have their headers written by parent compound tag.
WriteByte(buffer,TAG_COMPOUND);
WriteString(buffer,tag.name);
try
{
SerializeCompoundTag(buffer,tag);
}
catch(DataException&)
{
return false;
}
return true;
}
void NBT::PrintTag(Tag& tag)
{
int depth = 0;
PrintTagRecursive(tag,depth);
}
NBT::Tag* NBT::GetChildNamedTag(NBT::Tag* parentTag,const string name)
{
for(unsigned int x = 0;x < parentTag->childTags.size();++x)
{
if(parentTag->childTags[x].name == name)
return &parentTag->childTags[x];
}
return NULL;
}