Skip to content

Commit

Permalink
Merge pull request #306 from tanaka141/patch-1
Browse files Browse the repository at this point in the history
Patch 1, NBT support for long array tag needed to for #303
  • Loading branch information
evildeeds authored Aug 17, 2019
2 parents 001b153 + 370f63a commit 9f201f6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 35 deletions.
41 changes: 9 additions & 32 deletions src/mc/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,15 @@ Compound() {
Long(LastUpdate): 7446079
Int(xPos): -1
Int(zPos): -1
List(TileEntities, TAG_Byte, 0): [ ]
List(TileEntities, TAG_Compound, 0): [ ]
Byte(TerrainPopulated): 0x1
IntArray(HeightMap): (256 ints)
List(Sections, TAG_Compound, 5): [
List(Sections, TAG_Compound, 1): [
Compound() {
ByteArray(Data): (2048 bytes)
ByteArray(SkyLight): (2048 bytes)
ByteArray(BlockLight): (2048 bytes)
Byte(Y): 0x0
ByteArray(Blocks): (4096 bytes)
}
Compound() {
ByteArray(Data): (2048 bytes)
ByteArray(SkyLight): (2048 bytes)
ByteArray(BlockLight): (2048 bytes)
Byte(Y): 0x1
ByteArray(Blocks): (4096 bytes)
}
Compound() {
ByteArray(Data): (2048 bytes)
ByteArray(SkyLight): (2048 bytes)
ByteArray(BlockLight): (2048 bytes)
Byte(Y): 0x2
ByteArray(Blocks): (4096 bytes)
}
Compound() {
ByteArray(Data): (2048 bytes)
ByteArray(SkyLight): (2048 bytes)
ByteArray(BlockLight): (2048 bytes)
Byte(Y): 0x3
ByteArray(Blocks): (4096 bytes)
}
Compound() {
ByteArray(Data): (2048 bytes)
ByteArray(SkyLight): (2048 bytes)
ByteArray(BlockLight): (2048 bytes)
Byte(Y): 0x4
ByteArray(Blocks): (4096 bytes)
ByteArray(BlockStates): (4096 bytes)
}
]
}
Expand Down Expand Up @@ -193,6 +164,11 @@ namespace mc {
delete byte_array;
}

void register_long_array(level_context* C, nbt::String name, nbt::LongArray* long_array) {

delete long_array;
}

void error_handler(level_context* C, size_t where, const char *why) {
C->error = true;
C->error_where = where;
Expand Down Expand Up @@ -232,6 +208,7 @@ namespace mc {

parser.register_byte_array = register_byte_array;
parser.register_int_array = register_int_array;
parser.register_long_array = register_long_array;
parser.register_byte = register_byte;
parser.register_string = register_string;
parser.register_int = register_int;
Expand Down
7 changes: 7 additions & 0 deletions src/mc/region_inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ void register_int_array(inspect_context* inspect, nbt::String name, nbt::IntArra
delete value;
}

void register_long_array(inspect_context* inspect, nbt::String name, nbt::LongArray* value) {
cout << setw(inspect->width) << ""
<< "LongArray(" << name << "): " << "(" << dec << int(value->length) << " longs)" << endl;
delete value;
}

void error_handler(inspect_context* ctx, size_t where, const char* why) {
std::cout << where << ":" << why << std::endl;
}
Expand Down Expand Up @@ -115,6 +121,7 @@ int main(int argc, char* argv[]) {
parser.register_byte = register_byte;
parser.register_byte_array = register_byte_array;
parser.register_int_array = register_int_array;
parser.register_long_array = register_long_array;

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

Expand Down
39 changes: 37 additions & 2 deletions src/nbt/nbt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ namespace nbt {
const Byte TAG_List = 0x9;
const Byte TAG_Compound = 0xa;
const Byte TAG_Int_Array = 0xb;
const Byte TAG_Long_Array = 0xc;

const std::string TAG_End_str("TAG_End");
const std::string TAG_Byte_str("TAG_Byte");
Expand All @@ -249,6 +250,7 @@ namespace nbt {
const std::string TAG_List_str("TAG_List");
const std::string TAG_Compound_str("TAG_Compound");
const std::string TAG_Int_Array_str("TAG_Int_Array");
const std::string TAG_Long_Array_str("TAG_Long_Array");

const std::string tag_string_map[] = {
TAG_End_str,
Expand All @@ -262,7 +264,8 @@ namespace nbt {
TAG_String_str,
TAG_List_str,
TAG_Compound_str,
TAG_Int_Array_str
TAG_Int_Array_str,
TAG_Long_Array_str
};

bool is_big_endian();
Expand Down Expand Up @@ -408,7 +411,7 @@ namespace nbt {
inline Byte read_tagType(input_buffer_ptr file) {
Byte type = read_byte(file);

nbt_assert_error(exc_env, file, type >= 0 && type <= TAG_Int_Array,
nbt_assert_error(exc_env, file, type >= 0 && type <= TAG_Long_Array,
"Not a valid tag type");

return type;
Expand Down Expand Up @@ -454,6 +457,27 @@ namespace nbt {
array->length = length;
register_int_array(context, name, array);
}

inline void flush_long_array(input_buffer_ptr file) {
Int length = read_int(file);

nbt_assert_error(exc_env, file, file->flush(length * sizeof(Long)) != -1,
"Buffer to short to flush LongArray");
}

inline void handle_long_array(String name, input_buffer_ptr file) {
Int length = read_int(file);
Long *values = new Long[length];

for (Int i = 0; i < length; i++) {
values[i] = read_long(file);
}

LongArray *array = new LongArray();
array->values = values;
array->length = length;
register_long_array(context, name, array);
}
public:
typedef void (*begin_compound_t)(C*, String name);
typedef void (*end_compound_t)(C*, String name);
Expand All @@ -470,6 +494,7 @@ namespace nbt {
typedef void (*register_byte_t)(C*, String name, Byte b);
typedef void (*register_byte_array_t)(C*, String name, ByteArray* array);
typedef void (*register_int_array_t)(C*, String name, IntArray* array);
typedef void (*register_long_array_t)(C*, String name, LongArray* array);
typedef void (*error_handler_t)(C*, size_t where, const char *why);

register_long_t register_long;
Expand All @@ -481,6 +506,7 @@ namespace nbt {
register_byte_t register_byte;
register_byte_array_t register_byte_array;
register_int_array_t register_int_array;
register_long_array_t register_long_array;

begin_compound_t begin_compound;
end_compound_t end_compound;
Expand All @@ -499,6 +525,7 @@ namespace nbt {
register_byte(NULL),
register_byte_array(NULL),
register_int_array(NULL),
register_long_array(NULL),
begin_compound(&default_begin_compound<C>),
end_compound(&default_end_compound<C>),
begin_list(&default_begin_list<C>),
Expand All @@ -518,6 +545,7 @@ namespace nbt {
register_byte(NULL),
register_byte_array(NULL),
register_int_array(NULL),
register_long_array(NULL),
begin_compound(&default_begin_compound<C>),
end_compound(&default_end_compound<C>),
begin_list(&default_begin_list<C>),
Expand Down Expand Up @@ -684,6 +712,13 @@ namespace nbt {
handle_int_array(name, file);
}
break;
case TAG_Long_Array:
if (register_long_array == NULL) {
flush_long_array(file);
} else {
handle_long_array(name, file);
}
break;
default:
nbt_assert_error(exc_env, file, 0, "Encountered unknown type");
break;
Expand Down
9 changes: 8 additions & 1 deletion src/nbt/nbt_inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ void register_byte_array(inspect_context* inspect, nbt::String name, nbt::ByteAr
delete value;
}

void register_long_array(inspect_context* inspect, nbt::String name, nbt::LongArray* value) {
cout << setw(inspect->width) << ""
<< "LongArray(" << name << "): " << "(binary blob)" << endl;
delete value;
}

int main(int argc, char* argv[]) {
if (argc < 2) {
return 1;
Expand All @@ -98,7 +104,8 @@ int main(int argc, char* argv[]) {
parser.register_int = register_int;
parser.register_byte = register_byte;
parser.register_byte_array = register_byte_array;

parser.register_long_array = register_long_array;

parser.parse_file(argv[1]);
return 0;
}
9 changes: 9 additions & 0 deletions src/nbt/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ namespace nbt {
}
};


struct LongArray {
Int length;
Long *values;
~LongArray() {
delete [] values;
}
};

struct stack_entry {
Byte type;
String name;
Expand Down

0 comments on commit 9f201f6

Please sign in to comment.