diff options
Diffstat (limited to 'src/NBT')
-rw-r--r-- | src/NBT/ByteArrayTag.hpp | 18 | ||||
-rw-r--r-- | src/NBT/ByteTag.hpp | 6 | ||||
-rw-r--r-- | src/NBT/CompoundTag.hpp | 14 | ||||
-rw-r--r-- | src/NBT/DoubleTag.hpp | 10 | ||||
-rw-r--r-- | src/NBT/EndTag.hpp | 3 | ||||
-rw-r--r-- | src/NBT/FloatTag.hpp | 10 | ||||
-rw-r--r-- | src/NBT/IntArrayTag.hpp | 18 | ||||
-rw-r--r-- | src/NBT/IntTag.hpp | 6 | ||||
-rw-r--r-- | src/NBT/ListTag.hpp | 14 | ||||
-rw-r--r-- | src/NBT/LongTag.hpp | 6 | ||||
-rw-r--r-- | src/NBT/ShortTag.hpp | 6 | ||||
-rw-r--r-- | src/NBT/StringTag.hpp | 4 | ||||
-rw-r--r-- | src/NBT/Tag.hpp | 7 |
13 files changed, 122 insertions, 0 deletions
diff --git a/src/NBT/ByteArrayTag.hpp b/src/NBT/ByteArrayTag.hpp index 0e56a9a..36d9fe7 100644 --- a/src/NBT/ByteArrayTag.hpp +++ b/src/NBT/ByteArrayTag.hpp @@ -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; } diff --git a/src/NBT/ByteTag.hpp b/src/NBT/ByteTag.hpp index 64bdaa1..dc7e212 100644 --- a/src/NBT/ByteTag.hpp +++ b/src/NBT/ByteTag.hpp @@ -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; } diff --git a/src/NBT/CompoundTag.hpp b/src/NBT/CompoundTag.hpp index 39fa25d..6be0479 100644 --- a/src/NBT/CompoundTag.hpp +++ b/src/NBT/CompoundTag.hpp @@ -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()) diff --git a/src/NBT/DoubleTag.hpp b/src/NBT/DoubleTag.hpp index dace193..035ff94 100644 --- a/src/NBT/DoubleTag.hpp +++ b/src/NBT/DoubleTag.hpp @@ -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; + } }; } diff --git a/src/NBT/EndTag.hpp b/src/NBT/EndTag.hpp index c969e98..4582f5d 100644 --- a/src/NBT/EndTag.hpp +++ b/src/NBT/EndTag.hpp @@ -42,6 +42,9 @@ public: virtual Type getType() const { return Type::End; } + + virtual void print(std::ostream&, const std::string &) const { + } }; } diff --git a/src/NBT/FloatTag.hpp b/src/NBT/FloatTag.hpp index ad9cc64..7bb8df2 100644 --- a/src/NBT/FloatTag.hpp +++ b/src/NBT/FloatTag.hpp @@ -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; + } }; } diff --git a/src/NBT/IntArrayTag.hpp b/src/NBT/IntArrayTag.hpp index 22f84b2..5fdd3fc 100644 --- a/src/NBT/IntArrayTag.hpp +++ b/src/NBT/IntArrayTag.hpp @@ -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 << "]"; + } }; } diff --git a/src/NBT/IntTag.hpp b/src/NBT/IntTag.hpp index b0337c7..eacac52 100644 --- a/src/NBT/IntTag.hpp +++ b/src/NBT/IntTag.hpp @@ -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); } diff --git a/src/NBT/ListTag.hpp b/src/NBT/ListTag.hpp index 757a0aa..833c41e 100644 --- a/src/NBT/ListTag.hpp +++ b/src/NBT/ListTag.hpp @@ -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 << "]"; + } }; } diff --git a/src/NBT/LongTag.hpp b/src/NBT/LongTag.hpp index b07bc1a..4f6bec3 100644 --- a/src/NBT/LongTag.hpp +++ b/src/NBT/LongTag.hpp @@ -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); } diff --git a/src/NBT/ShortTag.hpp b/src/NBT/ShortTag.hpp index 7415317..c08e1fe 100644 --- a/src/NBT/ShortTag.hpp +++ b/src/NBT/ShortTag.hpp @@ -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); } diff --git a/src/NBT/StringTag.hpp b/src/NBT/StringTag.hpp index 42432f2..5f68d63 100644 --- a/src/NBT/StringTag.hpp +++ b/src/NBT/StringTag.hpp @@ -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) << "\""; + } }; } diff --git a/src/NBT/Tag.hpp b/src/NBT/Tag.hpp index a3c0c76..bb71b23 100644 --- a/src/NBT/Tag.hpp +++ b/src/NBT/Tag.hpp @@ -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; +} + } } |