Skip to content
Browse files

Added support for IntArray in nbt parser

* First step on #249
  • Loading branch information...
1 parent 7930194 commit 98b1ba37e6f493b59136af7f517a13954b4fb233 @udoprog committed
Showing with 71 additions and 13 deletions.
  1. +4 −1 README.md
  2. +11 −2 src/mc/region_inspect.cpp
  3. +1 −1 src/mc/region_iterator.cpp
  4. +2 −2 src/mc/utils.cpp
  5. +45 −7 src/nbt/nbt.hpp
  6. +8 −0 src/nbt/types.hpp
View
5 README.md
@@ -103,6 +103,10 @@ Issues
* ALWAYS include the following information: Platform (e.g. Windows, Linux, Mac) and Architecture (x86, x86_64)
* IF POSSIBLE include information which can help us reproduce the problem.
+Contributors
+------------
+ UniversE - for epic center calculation, and saving the api!
+
Contributions
-------------
ZomBuster and Firemark - for their original work
@@ -119,4 +123,3 @@ Contributions
Athemis - for fixing beta 1.2 compatibility.
sn4kebite - for implementing Wool colors.
ekryyn - for implementing the altitude graph!
- UniversE - for epic center calculation, and saving the api!
View
13 src/mc/region_inspect.cpp
@@ -76,7 +76,13 @@ void register_byte(inspect_context* inspect, nbt::String name, nbt::Byte value)
void register_byte_array(inspect_context* inspect, nbt::String name, nbt::ByteArray* value) {
cout << setw(inspect->width) << ""
- << "ByteArray(" << name << "): " << "(" << int(value->length) << "B binary)" << endl;
+ << "ByteArray(" << name << "): " << "(" << int(value->length) << " bytes)" << endl;
+ delete value;
+}
+
+void register_int_array(inspect_context* inspect, nbt::String name, nbt::IntArray* value) {
+ cout << setw(inspect->width) << ""
+ << "IntArray(" << name << "): " << "(" << int(value->length) << " ints)" << endl;
delete value;
}
@@ -108,12 +114,13 @@ 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_int_array = register_int_array;
mc::region region(argv[1]);
list<level_coord> coords;
- region.read_coords(coords);
+ std::cout << "Reading Header" << std::endl;
try {
region.read_header();
@@ -122,6 +129,8 @@ int main(int argc, char* argv[]) {
return 1;
}
+ region.read_coords(coords);
+
mc::dynamic_buffer buffer(mc::region::CHUNK_MAX);
BOOST_FOREACH(level_coord c, coords) {
View
2 src/mc/region_iterator.cpp
@@ -11,7 +11,7 @@ bool dir_filter(const std::string& name)
bool file_filter(const std::string& name) {
if (name.length() < 8) return false;
- if (name.compare(name.length() - 4, 4, ".mcr") != 0) return false;
+ if (name.compare(name.length() - 4, 4, ".mca") != 0) return false;
if (name.compare(0, 2, "r.") != 0) return false;
return true;
}
View
4 src/mc/utils.cpp
@@ -109,8 +109,8 @@ namespace mc {
std::vector<string> parts;
split(parts, fs::basename(path), '.');
- if (parts.size() != 3 || extension.compare(".mcr") != 0) {
- throw invalid_argument("level data file name does not match <x>.<z>.mcr");
+ if (parts.size() != 3 || extension.compare(".mca") != 0) {
+ throw invalid_argument("level data file name does not match <x>.<z>.mca");
}
try {
View
52 src/nbt/nbt.hpp
@@ -196,6 +196,7 @@ namespace nbt {
const Byte TAG_String = 0x8;
const Byte TAG_List = 0x9;
const Byte TAG_Compound = 0xa;
+ const Byte TAG_Int_Array = 0xb;
const std::string TAG_End_str("TAG_End");
const std::string TAG_Byte_str("TAG_Byte");
@@ -208,6 +209,7 @@ namespace nbt {
const std::string TAG_String_str("TAG_String");
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_string_map[] = {
TAG_End_str,
@@ -220,29 +222,26 @@ namespace nbt {
TAG_Byte_Array_str,
TAG_String_str,
TAG_List_str,
- TAG_Compound_str
+ TAG_Compound_str,
+ TAG_Int_Array_str
};
bool is_big_endian();
template <class C>
void default_begin_compound(C* context, nbt::String name) {
- //std::cout << "TAG_Compound('" << name << "') BEGIN" << std::endl;
}
template <class C>
void default_end_compound(C* context, String name) {
- //std::cout << "TAG_Compound END" << std::endl;
}
template <class C>
void default_begin_list(C* context, nbt::String name, nbt::Byte type, nbt::Int length) {
- //std::cout << "TAG_List('" << name << "'): " << length << " items" << std::endl;
}
template <class C>
void default_end_list(C* context, nbt::String name) {
- //std::cout << "TAG_List END" << std::endl;
}
template <class C>
@@ -369,7 +368,10 @@ 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_Compound, "Not a valid tag type");
+
+ nbt_assert_error(exc_env, file, type >= 0 && type <= TAG_Int_Array,
+ "Not a valid tag type");
+
return type;
}
@@ -377,17 +379,42 @@ namespace nbt {
Int length = read_int(file);
nbt_assert_error(exc_env, file, file->flush(length) != -1,
"Buffer to short to flush ByteArray");
+
}
inline void handle_byte_array(String name, input_buffer_ptr file) {
Int length = read_int(file);
Byte *values = new Byte[length];
- nbt_assert_error(exc_env, file, file->read(values, length) == length, "Buffer to short to read ByteArray");
+
+ nbt_assert_error(exc_env, file, file->read(values, length) == length,
+ "Buffer to short to read ByteArray");
+
ByteArray *array = new ByteArray();
array->values = values;
array->length = length;
register_byte_array(context, name, array);
}
+
+ inline void flush_int_array(input_buffer_ptr file) {
+ Int length = read_int(file);
+
+ nbt_assert_error(exc_env, file, file->flush(length * sizeof(Int)) != -1,
+ "Buffer to short to flush IntArray");
+ }
+
+ inline void handle_int_array(String name, input_buffer_ptr file) {
+ Int length = read_int(file);
+ Int *values = new Int[length];
+
+ for (Int i = 0; i < length; i++) {
+ values[i] = read_int(file);
+ }
+
+ IntArray *array = new IntArray();
+ array->values = values;
+ array->length = length;
+ register_int_array(context, name, array);
+ }
public:
typedef void (*begin_compound_t)(C*, String name);
typedef void (*end_compound_t)(C*, String name);
@@ -403,6 +430,7 @@ namespace nbt {
typedef void (*register_int_t)(C*, String name, Int l);
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 (*error_handler_t)(C*, size_t where, const char *why);
register_long_t register_long;
@@ -413,6 +441,7 @@ namespace nbt {
register_int_t register_int;
register_byte_t register_byte;
register_byte_array_t register_byte_array;
+ register_int_array_t register_int_array;
begin_compound_t begin_compound;
end_compound_t end_compound;
@@ -430,6 +459,7 @@ namespace nbt {
register_int(NULL),
register_byte(NULL),
register_byte_array(NULL),
+ register_int_array(NULL),
begin_compound(&default_begin_compound<C>),
end_compound(&default_end_compound<C>),
begin_list(&default_begin_list<C>),
@@ -448,6 +478,7 @@ namespace nbt {
register_int(NULL),
register_byte(NULL),
register_byte_array(NULL),
+ register_int_array(NULL),
begin_compound(&default_begin_compound<C>),
end_compound(&default_end_compound<C>),
begin_list(&default_begin_list<C>),
@@ -607,6 +638,13 @@ namespace nbt {
handle_byte_array(name, file);
}
break;
+ case TAG_Int_Array:
+ if (register_int_array == NULL) {
+ flush_int_array(file);
+ } else {
+ handle_int_array(name, file);
+ }
+ break;
default:
nbt_assert_error(exc_env, file, 0, "Encountered unknown type");
break;
View
8 src/nbt/types.hpp
@@ -21,6 +21,14 @@ namespace nbt {
}
};
+ struct IntArray {
+ Int length;
+ Int *values;
+ ~IntArray() {
+ delete [] values;
+ }
+ };
+
struct stack_entry {
Byte type;
String name;

0 comments on commit 98b1ba3

Please sign in to comment.
Something went wrong with that request. Please try again.