Skip to content

Commit 9f201f6

Browse files
authored
Merge pull request #306 from tanaka141/patch-1
Patch 1, NBT support for long array tag needed to for #303
2 parents 001b153 + 370f63a commit 9f201f6

File tree

5 files changed

+70
-35
lines changed

5 files changed

+70
-35
lines changed

src/mc/level.cpp

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,15 @@ Compound() {
1717
Long(LastUpdate): 7446079
1818
Int(xPos): -1
1919
Int(zPos): -1
20-
List(TileEntities, TAG_Byte, 0): [ ]
20+
List(TileEntities, TAG_Compound, 0): [ ]
2121
Byte(TerrainPopulated): 0x1
2222
IntArray(HeightMap): (256 ints)
23-
List(Sections, TAG_Compound, 5): [
23+
List(Sections, TAG_Compound, 1): [
2424
Compound() {
25-
ByteArray(Data): (2048 bytes)
2625
ByteArray(SkyLight): (2048 bytes)
2726
ByteArray(BlockLight): (2048 bytes)
2827
Byte(Y): 0x0
29-
ByteArray(Blocks): (4096 bytes)
30-
}
31-
Compound() {
32-
ByteArray(Data): (2048 bytes)
33-
ByteArray(SkyLight): (2048 bytes)
34-
ByteArray(BlockLight): (2048 bytes)
35-
Byte(Y): 0x1
36-
ByteArray(Blocks): (4096 bytes)
37-
}
38-
Compound() {
39-
ByteArray(Data): (2048 bytes)
40-
ByteArray(SkyLight): (2048 bytes)
41-
ByteArray(BlockLight): (2048 bytes)
42-
Byte(Y): 0x2
43-
ByteArray(Blocks): (4096 bytes)
44-
}
45-
Compound() {
46-
ByteArray(Data): (2048 bytes)
47-
ByteArray(SkyLight): (2048 bytes)
48-
ByteArray(BlockLight): (2048 bytes)
49-
Byte(Y): 0x3
50-
ByteArray(Blocks): (4096 bytes)
51-
}
52-
Compound() {
53-
ByteArray(Data): (2048 bytes)
54-
ByteArray(SkyLight): (2048 bytes)
55-
ByteArray(BlockLight): (2048 bytes)
56-
Byte(Y): 0x4
57-
ByteArray(Blocks): (4096 bytes)
28+
ByteArray(BlockStates): (4096 bytes)
5829
}
5930
]
6031
}
@@ -193,6 +164,11 @@ namespace mc {
193164
delete byte_array;
194165
}
195166

167+
void register_long_array(level_context* C, nbt::String name, nbt::LongArray* long_array) {
168+
169+
delete long_array;
170+
}
171+
196172
void error_handler(level_context* C, size_t where, const char *why) {
197173
C->error = true;
198174
C->error_where = where;
@@ -232,6 +208,7 @@ namespace mc {
232208

233209
parser.register_byte_array = register_byte_array;
234210
parser.register_int_array = register_int_array;
211+
parser.register_long_array = register_long_array;
235212
parser.register_byte = register_byte;
236213
parser.register_string = register_string;
237214
parser.register_int = register_int;

src/mc/region_inspect.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ void register_int_array(inspect_context* inspect, nbt::String name, nbt::IntArra
8686
delete value;
8787
}
8888

89+
void register_long_array(inspect_context* inspect, nbt::String name, nbt::LongArray* value) {
90+
cout << setw(inspect->width) << ""
91+
<< "LongArray(" << name << "): " << "(" << dec << int(value->length) << " longs)" << endl;
92+
delete value;
93+
}
94+
8995
void error_handler(inspect_context* ctx, size_t where, const char* why) {
9096
std::cout << where << ":" << why << std::endl;
9197
}
@@ -115,6 +121,7 @@ int main(int argc, char* argv[]) {
115121
parser.register_byte = register_byte;
116122
parser.register_byte_array = register_byte_array;
117123
parser.register_int_array = register_int_array;
124+
parser.register_long_array = register_long_array;
118125

119126
mc::region region(argv[1]);
120127

src/nbt/nbt.hpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ namespace nbt {
236236
const Byte TAG_List = 0x9;
237237
const Byte TAG_Compound = 0xa;
238238
const Byte TAG_Int_Array = 0xb;
239+
const Byte TAG_Long_Array = 0xc;
239240

240241
const std::string TAG_End_str("TAG_End");
241242
const std::string TAG_Byte_str("TAG_Byte");
@@ -249,6 +250,7 @@ namespace nbt {
249250
const std::string TAG_List_str("TAG_List");
250251
const std::string TAG_Compound_str("TAG_Compound");
251252
const std::string TAG_Int_Array_str("TAG_Int_Array");
253+
const std::string TAG_Long_Array_str("TAG_Long_Array");
252254

253255
const std::string tag_string_map[] = {
254256
TAG_End_str,
@@ -262,7 +264,8 @@ namespace nbt {
262264
TAG_String_str,
263265
TAG_List_str,
264266
TAG_Compound_str,
265-
TAG_Int_Array_str
267+
TAG_Int_Array_str,
268+
TAG_Long_Array_str
266269
};
267270

268271
bool is_big_endian();
@@ -408,7 +411,7 @@ namespace nbt {
408411
inline Byte read_tagType(input_buffer_ptr file) {
409412
Byte type = read_byte(file);
410413

411-
nbt_assert_error(exc_env, file, type >= 0 && type <= TAG_Int_Array,
414+
nbt_assert_error(exc_env, file, type >= 0 && type <= TAG_Long_Array,
412415
"Not a valid tag type");
413416

414417
return type;
@@ -454,6 +457,27 @@ namespace nbt {
454457
array->length = length;
455458
register_int_array(context, name, array);
456459
}
460+
461+
inline void flush_long_array(input_buffer_ptr file) {
462+
Int length = read_int(file);
463+
464+
nbt_assert_error(exc_env, file, file->flush(length * sizeof(Long)) != -1,
465+
"Buffer to short to flush LongArray");
466+
}
467+
468+
inline void handle_long_array(String name, input_buffer_ptr file) {
469+
Int length = read_int(file);
470+
Long *values = new Long[length];
471+
472+
for (Int i = 0; i < length; i++) {
473+
values[i] = read_long(file);
474+
}
475+
476+
LongArray *array = new LongArray();
477+
array->values = values;
478+
array->length = length;
479+
register_long_array(context, name, array);
480+
}
457481
public:
458482
typedef void (*begin_compound_t)(C*, String name);
459483
typedef void (*end_compound_t)(C*, String name);
@@ -470,6 +494,7 @@ namespace nbt {
470494
typedef void (*register_byte_t)(C*, String name, Byte b);
471495
typedef void (*register_byte_array_t)(C*, String name, ByteArray* array);
472496
typedef void (*register_int_array_t)(C*, String name, IntArray* array);
497+
typedef void (*register_long_array_t)(C*, String name, LongArray* array);
473498
typedef void (*error_handler_t)(C*, size_t where, const char *why);
474499

475500
register_long_t register_long;
@@ -481,6 +506,7 @@ namespace nbt {
481506
register_byte_t register_byte;
482507
register_byte_array_t register_byte_array;
483508
register_int_array_t register_int_array;
509+
register_long_array_t register_long_array;
484510

485511
begin_compound_t begin_compound;
486512
end_compound_t end_compound;
@@ -499,6 +525,7 @@ namespace nbt {
499525
register_byte(NULL),
500526
register_byte_array(NULL),
501527
register_int_array(NULL),
528+
register_long_array(NULL),
502529
begin_compound(&default_begin_compound<C>),
503530
end_compound(&default_end_compound<C>),
504531
begin_list(&default_begin_list<C>),
@@ -518,6 +545,7 @@ namespace nbt {
518545
register_byte(NULL),
519546
register_byte_array(NULL),
520547
register_int_array(NULL),
548+
register_long_array(NULL),
521549
begin_compound(&default_begin_compound<C>),
522550
end_compound(&default_end_compound<C>),
523551
begin_list(&default_begin_list<C>),
@@ -684,6 +712,13 @@ namespace nbt {
684712
handle_int_array(name, file);
685713
}
686714
break;
715+
case TAG_Long_Array:
716+
if (register_long_array == NULL) {
717+
flush_long_array(file);
718+
} else {
719+
handle_long_array(name, file);
720+
}
721+
break;
687722
default:
688723
nbt_assert_error(exc_env, file, 0, "Encountered unknown type");
689724
break;

src/nbt/nbt_inspect.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ void register_byte_array(inspect_context* inspect, nbt::String name, nbt::ByteAr
7676
delete value;
7777
}
7878

79+
void register_long_array(inspect_context* inspect, nbt::String name, nbt::LongArray* value) {
80+
cout << setw(inspect->width) << ""
81+
<< "LongArray(" << name << "): " << "(binary blob)" << endl;
82+
delete value;
83+
}
84+
7985
int main(int argc, char* argv[]) {
8086
if (argc < 2) {
8187
return 1;
@@ -98,7 +104,8 @@ int main(int argc, char* argv[]) {
98104
parser.register_int = register_int;
99105
parser.register_byte = register_byte;
100106
parser.register_byte_array = register_byte_array;
101-
107+
parser.register_long_array = register_long_array;
108+
102109
parser.parse_file(argv[1]);
103110
return 0;
104111
}

src/nbt/types.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ namespace nbt {
2929
}
3030
};
3131

32+
33+
struct LongArray {
34+
Int length;
35+
Long *values;
36+
~LongArray() {
37+
delete [] values;
38+
}
39+
};
40+
3241
struct stack_entry {
3342
Byte type;
3443
String name;

0 commit comments

Comments
 (0)