Skip to content

Commit 2f3846b

Browse files
committed
- added support for the IntArray NBT from ymod
1 parent ecae845 commit 2f3846b

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

nbt.cpp

+36-23
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ NBT::NBT(const char *file, bool &success)
8484
_blob = NULL;
8585
_filename = NULL;
8686
if (file == NULL || *file == '\0' || !fileExists(file)) {
87+
//fprintf(stderr, "File not found: %s\n", file);
8788
success = false;
8889
return;
8990
}
@@ -97,14 +98,16 @@ NBT::NBT(const char *file, bool &success)
9798
}
9899
success = (fh != 0);
99100
if (!success) {
101+
//fprintf(stderr, "Error opening file %s\n", file);
100102
return;
101103
}
102104
// File is open, read data
103-
int length = 150000; // I checked a few chunk files in their decompressed form, I think they can't really get any bigger than 80~90KiB, but just to be sure...
105+
int length = 150000 * (CHUNKSIZE_Y / 128); // I checked a few chunk files in their decompressed form, I think they can't really get any bigger than 80~90KiB, but just to be sure...
104106
_blob = new uint8_t[length];
105107
int ret = gzread(fh, _blob, length);
106108
gzclose(fh);
107109
if (ret == -1 || _blob[0] != 10) { // Has to start with TAG_Compound
110+
//fprintf(stderr, "Ret: %d/%d - start: %d\n", ret, length, (int)_blob[0]);
108111
success = false;
109112
return;
110113
}
@@ -113,7 +116,7 @@ NBT::NBT(const char *file, bool &success)
113116
_type = tagCompound;
114117
NBT_Tag::parseData(position, end);
115118
if (position == NULL) {
116-
//printf("Error parsing NBT from file\n");
119+
//fprintf(stderr, "Error parsing NBT from file\n");
117120
}
118121
success = (position != NULL);
119122
}
@@ -132,15 +135,15 @@ NBT::NBT(uint8_t * const file, const size_t len, const bool shared, bool &succes
132135
_type = tagCompound;
133136
NBT_Tag::parseData(position, end);
134137
if (position == NULL) {
135-
//printf("Error parsing NBT from stream\n");
138+
//fprintf(stderr, "Error parsing NBT from stream\n");
136139
}
137140
success = (position != NULL);
138141
}
139142

140143
NBT::~NBT()
141144
{
142145
if (_blob != NULL) {
143-
////printf("DELETE BLOB\n");
146+
//fprintf(stderr, "DELETE BLOB\n");
144147
delete[] _blob;
145148
}
146149
if (_filename != NULL) {
@@ -176,7 +179,7 @@ bool NBT::save()
176179
}
177180
//remove(tmpfile);
178181
delete[] tmpfile;
179-
//printf("Saved %s\n", _filename);
182+
//fprintf(stderr, "Saved %s\n", _filename);
180183
return true;
181184
}
182185
*/
@@ -195,7 +198,7 @@ NBT_Tag::NBT_Tag(uint8_t* &position, const uint8_t *end, string &name)
195198
_elems = NULL;
196199
_data = NULL;
197200
_type = tagUnknown;
198-
if (*position < 1 || *position > 10 || end - position < 3) {
201+
if (*position < 1 || *position > 11 || end - position < 3) {
199202
position = NULL;
200203
return;
201204
}
@@ -207,7 +210,7 @@ NBT_Tag::NBT_Tag(uint8_t* &position, const uint8_t *end, string &name)
207210
}
208211
name = string((char *)position+3, len);
209212
position += 3 + len;
210-
//printf("Tag name is %s\n", name.c_str());
213+
//fprintf(stderr, "Tag name is %s\n", name.c_str());
211214
this->parseData(position, end, &name);
212215
}
213216

@@ -224,41 +227,41 @@ NBT_Tag::NBT_Tag(uint8_t* &position, const uint8_t *end, TagType type)
224227
void NBT_Tag::parseData(uint8_t* &position, const uint8_t *end, string *name)
225228
{
226229
// position should now point to start of data (for named tags, right after the name)
227-
//printf("Have Tag of type %d\n", (int)_type);
230+
//fprintf(stderr, "Have Tag of type %d\n", (int)_type);
228231
switch (_type) {
229232
case tagCompound:
230233
_elems = new tagmap;
231234
while (*position != 0 && position < end) { // No end tag, go on...
232-
//printf("Having a child....*plopp*..\n");
235+
//fprintf(stderr, "Having a child....*plopp*..\n");
233236
string thisname;
234237
NBT_Tag *tmp = new NBT_Tag(position, end, thisname);
235238
if (position == NULL) {
236-
//printf("DELETE tmp in compound because of invalid\n");
239+
//fprintf(stderr, "DELETE tmp in compound because of invalid\n");
237240
delete tmp;
238241
return;
239242
}
240243
if (name != NULL) {
241-
//printf(" ** Adding %s to %s\n", thisname.c_str(), name->c_str());
244+
//fprintf(stderr, " ** Adding %s to %s\n", thisname.c_str(), name->c_str());
242245
}
243246
(*_elems)[thisname] = tmp;
244247
}
245248
++position;
246249
break;
247250
case tagList: {
248-
if (*position < 1 || *position > 10) {
249-
//printf("Invalid list type!\n");
251+
if (*position < 1 || *position > 11) {
252+
//fprintf(stderr, "Invalid list type!\n");
250253
position = NULL;
251254
return;
252255
}
253256
TagType type = (TagType)*position;
254257
uint32_t count = _ntohl(position+1);
255258
position += 5;
256259
_list = new list<NBT_Tag *>;
257-
//printf("List contains %d elements of type %d\n", (int)count, (int)type);
260+
//fprintf(stderr, "List contains %d elements of type %d\n", (int)count, (int)type);
258261
while (count-- && position < end) { // No end tag, go on...
259262
NBT_Tag *tmp = new NBT_Tag(position, end, type);
260263
if (position == NULL) {
261-
//printf("DELETE tmp in list because of invalid\n");
264+
//fprintf(stderr, "DELETE tmp in list because of invalid\n");
262265
delete tmp;
263266
return;
264267
}
@@ -292,9 +295,9 @@ void NBT_Tag::parseData(uint8_t* &position, const uint8_t *end, string *name)
292295
break;
293296
case tagByteArray:
294297
_len = _ntohl(position);
295-
//printf("Array size is %d\n", (int)_len);
298+
//fprintf(stderr, "Array size is %d\n", (int)_len);
296299
if (position + _len + 4 >= end) {
297-
//printf("Too long b< %d bytes!\n", int((position + _len + 4) - end));
300+
printf("ByteArray too long by %d bytes!\n", int((position + _len + 4) - end));
298301
position = NULL;
299302
return;
300303
}
@@ -303,36 +306,46 @@ void NBT_Tag::parseData(uint8_t* &position, const uint8_t *end, string *name)
303306
break;
304307
case tagString:
305308
_len = _ntohs(position);
306-
//printf("Stringlen is %d\n", (int)_len);
309+
//fprintf(stderr, "Stringlen is %d\n", (int)_len);
307310
if (position + _len + 2 >= end) {
308-
//printf("Too long!\n");
311+
//fprintf(stderr, "Too long!\n");
309312
position = NULL;
310313
return;
311314
}
312315
_data = position;
313316
position += 2 + _len;
314317
break;
318+
case tagIntArray:
319+
_len = _ntohl(position) * 4;
320+
//fprintf(stderr, "Array size is %d\n", (int)_len);
321+
if (position + _len + 4 >= end) {
322+
printf("IntArray too long by %d bytes!\n", int((position + _len + 4) - end));
323+
position = NULL;
324+
return;
325+
}
326+
_data = position + 4;
327+
position += 4 + _len;
328+
break;
315329
case tagUnknown:
316330
default:
317-
//printf("UNKNOWN TAG!\n");
331+
printf("UNKNOWN TAG_ %d!\n", (int)_type);
318332
position = NULL;
319333
break;
320334
}
321335
}
322336

323337
NBT_Tag::~NBT_Tag()
324338
{
325-
////printf("Tag Destructor for %p\n", this);
326339
if (_elems) {
327340
for (tagmap::iterator it = _elems->begin(); it != _elems->end(); it++) {
328-
////printf("_elems Deleting %p\n", (it->second));
341+
////fprintf(stderr, "_elems Deleting %p\n", (it->second));
329342
delete (it->second);
330343
}
331344
delete _elems;
332345
}
333346
if (_list) {
334347
for (list<NBT_Tag *>::iterator it = _list->begin(); it != _list->end(); it++) {
335-
////printf("_list Deleting %p\n", *it);
348+
////fprintf(stderr, "_list Deleting %p\n", *it);
336349
delete *it;
337350
}
338351
delete _list;

nbt.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ enum TagType {
2424
tagByteArray = 7,
2525
tagString = 8,
2626
tagList = 9,
27-
tagCompound = 10
27+
tagCompound = 10,
28+
tagIntArray = 11
2829
};
2930

3031
class NBT_Tag

worldloader.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ static bool loadChunk(const char *streamOrFile, const size_t streamLen)
310310
chunk = new NBT((uint8_t*)streamOrFile, streamLen, true, ok);
311311
}
312312
if (!ok) {
313+
//printf("Error loading chunk.\n");
313314
delete chunk;
314315
return false; // chunk does not exist
315316
}

0 commit comments

Comments
 (0)