Add Tag print functions and nbtdump tool

This commit is contained in:
Matthias Schiffer 2018-07-20 23:09:33 +02:00
parent 64f81ff50c
commit 4b9bb2ab48
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
16 changed files with 186 additions and 1 deletions

View file

@ -1,4 +1,4 @@
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
Copyright (c) 2015-2018, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without

View file

@ -17,3 +17,11 @@ add_executable(MinedMap
)
set_target_properties(MinedMap PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall")
target_link_libraries(MinedMap ${ZLIB_LIBRARIES} ${PNG_LIBRARIES})
add_executable(nbtdump
nbtdump.cpp
GZip.cpp
NBT/Tag.cpp
)
set_target_properties(nbtdump PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall")
target_link_libraries(nbtdump ${ZLIB_LIBRARIES})

View file

@ -51,6 +51,24 @@ public:
return Type::ByteArray;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << "(" << len << ") [" << std::endl;
std::string inner = indent + " ";
for (size_t i = 0; i < len; i++) {
uint8_t v = value[i];
os << inner
<< (unsigned)v << " / "
<< (int)(int8_t)v << " / "
<< std::hex << "0x" << (unsigned)v << std::dec
<< std::endl;
}
os << indent << "]";
}
uint32_t getLength() const {
return len;
}

View file

@ -47,6 +47,12 @@ public:
return Type::Byte;
}
virtual void print(std::ostream& os, const std::string &) const {
os << (unsigned)getValue() << " / "
<< (int)(int8_t)getValue() << " / "
<< std::hex << "0x" << (unsigned)getValue() << std::dec;
}
uint8_t getValue() const {
return value;
}

View file

@ -54,6 +54,20 @@ public:
return Type::Compound;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << "{" << std::endl;
std::string inner = indent + " ";
for (const auto &item : *this) {
os << inner << item.first << ": " << item.second->getType() << " ";
item.second->print(os, inner);
os << std::endl;
}
os << indent << "}";
}
template<typename T> std::shared_ptr<const T> get(const std::string &key) const {
auto it = find(key);
if (it == end())

View file

@ -46,6 +46,16 @@ public:
virtual Type getType() const {
return Type::Double;
}
virtual void print(std::ostream& os, const std::string &) const {
union {
uint64_t i;
double d;
};
i = Buffer::parse64(ptr);
os << d;
}
};
}

View file

@ -42,6 +42,9 @@ public:
virtual Type getType() const {
return Type::End;
}
virtual void print(std::ostream&, const std::string &) const {
}
};
}

View file

@ -46,6 +46,16 @@ public:
virtual Type getType() const {
return Type::Float;
}
virtual void print(std::ostream& os, const std::string &) const {
union {
uint32_t i;
float f;
};
i = Buffer::parse32(ptr);
os << f;
}
};
}

View file

@ -50,6 +50,24 @@ public:
virtual Type getType() const {
return Type::IntArray;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << "(" << len << ") [" << std::endl;
std::string inner = indent + " ";
for (size_t i = 0; i < len; i++) {
uint32_t v = Buffer::parse32(&ptr[4*i]);
os << inner
<< v << " / "
<< (int32_t)v << " / "
<< std::hex << "0x" << v << std::dec
<< std::endl;
}
os << indent << "]";
}
};
}

View file

@ -47,6 +47,12 @@ public:
return Type::Int;
}
virtual void print(std::ostream& os, const std::string &) const {
os << getValue() << " / "
<< (int32_t)getValue() << " / "
<< std::hex << "0x" << getValue() << std::dec;
}
uint32_t getValue() const {
return Buffer::parse32(ptr);
}

View file

@ -65,6 +65,20 @@ public:
virtual Type getSubtype() const {
return type;
}
virtual void print(std::ostream& os, const std::string &indent) const {
os << getSubtype() << " [" << std::endl;
std::string inner = indent + " ";
for (const auto &item : *this) {
os << inner;
item->print(os, inner);
os << std::endl;
}
os << indent << "]";
}
};
}

View file

@ -47,6 +47,12 @@ public:
return Type::Long;
}
virtual void print(std::ostream& os, const std::string &) const {
os << getValue() << " / "
<< (int64_t)getValue() << " / "
<< std::hex << "0x" << getValue() << std::dec;
}
uint64_t getValue() const {
return Buffer::parse64(ptr);
}

View file

@ -47,6 +47,12 @@ public:
return Type::Short;
}
virtual void print(std::ostream& os, const std::string &) const {
os << getValue() << " / "
<< (int16_t)getValue() << " / "
<< std::hex << "0x" << getValue() << std::dec;
}
uint16_t getValue() const {
return Buffer::parse16(ptr);
}

View file

@ -48,6 +48,10 @@ public:
virtual Type getType() const {
return Type::String;
}
virtual void print(std::ostream& os, const std::string &) const {
os << "\"" << std::string(reinterpret_cast<const char *>(ptr), len) << "\"";
}
};
}

View file

@ -60,11 +60,18 @@ public:
static std::pair<std::string, std::shared_ptr<const Tag>> readNamedTag(Buffer *buffer);
virtual Type getType() const = 0;
virtual void print(std::ostream& os, const std::string &indent) const = 0;
virtual ~Tag() {}
};
std::ostream& operator<<(std::ostream& os, Tag::Type type);
static inline std::ostream& operator<<(std::ostream& os, const Tag &tag) {
os << tag.getType() << " ";
tag.print(os, "");
return os;
}
}
}

55
src/nbtdump.cpp Normal file
View file

@ -0,0 +1,55 @@
/*
Copyright (c) 2018, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Buffer.hpp"
#include "GZip.hpp"
#include "Util.hpp"
#include "NBT/Tag.hpp"
#include <cstdio>
#include <iostream>
#include <stdexcept>
int main(int argc, char *argv[]) {
using namespace MinedMap;
if (argc != 2) {
std::fprintf(stderr, "Usage: %s <nbtfile>\n", argv[0]);
return 1;
}
std::vector<uint8_t> buffer = readGZip(argv[1]);
Buffer nbt(buffer.data(), buffer.size());
std::pair<std::string, std::shared_ptr<const NBT::Tag>> tag = NBT::Tag::readNamedTag(&nbt);
if (tag.first != "")
throw std::invalid_argument("invalid root tag");
std::cout << *tag.second << std::endl;
return 0;
}