From cbe64618821a0ae9f1901ce1a4a3893d939fdc32 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Feb 2015 00:21:17 +0100 Subject: Implement most of the chunk format --- src/NBT/ByteArrayTag.hpp | 62 ++++++++++++++++++++++++++++ src/NBT/ByteTag.hpp | 56 +++++++++++++++++++++++++ src/NBT/CompoundTag.hpp | 61 +++++++++++++++++++++++++++ src/NBT/DoubleTag.hpp | 59 ++++++++++++++++++++++++++ src/NBT/EndTag.hpp | 48 ++++++++++++++++++++++ src/NBT/FloatTag.hpp | 55 +++++++++++++++++++++++++ src/NBT/IntArrayTag.hpp | 68 ++++++++++++++++++++++++++++++ src/NBT/IntTag.hpp | 59 ++++++++++++++++++++++++++ src/NBT/ListTag.hpp | 65 +++++++++++++++++++++++++++++ src/NBT/LongTag.hpp | 63 ++++++++++++++++++++++++++++ src/NBT/ShortTag.hpp | 57 +++++++++++++++++++++++++ src/NBT/StringTag.hpp | 55 +++++++++++++++++++++++++ src/NBT/Tag.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++ src/NBT/Tag.hpp | 65 +++++++++++++++++++++++++++++ 14 files changed, 878 insertions(+) create mode 100644 src/NBT/ByteArrayTag.hpp create mode 100644 src/NBT/ByteTag.hpp create mode 100644 src/NBT/CompoundTag.hpp create mode 100644 src/NBT/DoubleTag.hpp create mode 100644 src/NBT/EndTag.hpp create mode 100644 src/NBT/FloatTag.hpp create mode 100644 src/NBT/IntArrayTag.hpp create mode 100644 src/NBT/IntTag.hpp create mode 100644 src/NBT/ListTag.hpp create mode 100644 src/NBT/LongTag.hpp create mode 100644 src/NBT/ShortTag.hpp create mode 100644 src/NBT/StringTag.hpp create mode 100644 src/NBT/Tag.cpp create mode 100644 src/NBT/Tag.hpp (limited to 'src/NBT') diff --git a/src/NBT/ByteArrayTag.hpp b/src/NBT/ByteArrayTag.hpp new file mode 100644 index 0000000..d5a6333 --- /dev/null +++ b/src/NBT/ByteArrayTag.hpp @@ -0,0 +1,62 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include + + +namespace MinedMap { +namespace NBT { + +class ByteArrayTag : public Tag { +private: + friend class Tag; + + std::vector value; + + ByteArrayTag(Buffer *buffer) { + uint32_t len = uint32_t(buffer->get()) << 24; + len |= uint32_t(buffer->get()) << 16; + len |= uint32_t(buffer->get()) << 8; + len |= uint32_t(buffer->get()); + + value.resize(len); + + for (uint32_t i = 0; i < len; i++) + value[i] = buffer->get(); + } + +public: + virtual Type getType() const { + return Type::ByteArray; + } +}; + +} +} diff --git a/src/NBT/ByteTag.hpp b/src/NBT/ByteTag.hpp new file mode 100644 index 0000000..cc4aba9 --- /dev/null +++ b/src/NBT/ByteTag.hpp @@ -0,0 +1,56 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class ByteTag : public Tag { +private: + friend class Tag; + + uint8_t value; + + ByteTag(Buffer *buffer) { + value = buffer->get(); + } + +public: + virtual Type getType() const { + return Type::Byte; + } + + uint8_t getValue() const { + return value; + } +}; + +} +} diff --git a/src/NBT/CompoundTag.hpp b/src/NBT/CompoundTag.hpp new file mode 100644 index 0000000..99bbd94 --- /dev/null +++ b/src/NBT/CompoundTag.hpp @@ -0,0 +1,61 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include +#include + + +namespace MinedMap { +namespace NBT { + +class CompoundTag : public Tag { +private: + friend class Tag; + + std::unordered_map> value; + + CompoundTag(Buffer *buffer) { + while (true) { + std::pair> v = Tag::readNamedTag(buffer); + if (v.second->getType() == Type::End) + break; + + value.insert(std::move(v)); + } + } + +public: + virtual Type getType() const { + return Type::Compound; + } +}; + +} +} diff --git a/src/NBT/DoubleTag.hpp b/src/NBT/DoubleTag.hpp new file mode 100644 index 0000000..327dcf5 --- /dev/null +++ b/src/NBT/DoubleTag.hpp @@ -0,0 +1,59 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class DoubleTag : public Tag { +private: + friend class Tag; + + DoubleTag(Buffer *buffer) { + uint64_t value; + + value = uint64_t(buffer->get()) << 56; + value |= uint64_t(buffer->get()) << 48; + value |= uint64_t(buffer->get()) << 40; + value |= uint64_t(buffer->get()) << 32; + value |= uint64_t(buffer->get()) << 24; + value |= uint64_t(buffer->get()) << 16; + value |= uint64_t(buffer->get()) << 8; + value |= uint64_t(buffer->get()); + } + +public: + virtual Type getType() const { + return Type::Double; + } +}; + +} +} diff --git a/src/NBT/EndTag.hpp b/src/NBT/EndTag.hpp new file mode 100644 index 0000000..c969e98 --- /dev/null +++ b/src/NBT/EndTag.hpp @@ -0,0 +1,48 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class EndTag : public Tag { +private: + friend class Tag; + + EndTag() {} + +public: + virtual Type getType() const { + return Type::End; + } +}; + +} +} diff --git a/src/NBT/FloatTag.hpp b/src/NBT/FloatTag.hpp new file mode 100644 index 0000000..8859f94 --- /dev/null +++ b/src/NBT/FloatTag.hpp @@ -0,0 +1,55 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class FloatTag : public Tag { +private: + friend class Tag; + + FloatTag(Buffer *buffer) { + uint32_t value; + + value = uint32_t(buffer->get()) << 24; + value |= uint32_t(buffer->get()) << 16; + value |= uint32_t(buffer->get()) << 8; + value |= uint32_t(buffer->get()); + } + +public: + virtual Type getType() const { + return Type::Float; + } +}; + +} +} diff --git a/src/NBT/IntArrayTag.hpp b/src/NBT/IntArrayTag.hpp new file mode 100644 index 0000000..e9b0d3d --- /dev/null +++ b/src/NBT/IntArrayTag.hpp @@ -0,0 +1,68 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include + + +namespace MinedMap { +namespace NBT { + +class IntArrayTag : public Tag { +private: + friend class Tag; + + std::vector value; + + IntArrayTag(Buffer *buffer) { + uint32_t len = uint32_t(buffer->get()) << 24; + len |= uint32_t(buffer->get()) << 16; + len |= uint32_t(buffer->get()) << 8; + len |= uint32_t(buffer->get()); + + value.resize(len); + + for (uint32_t i = 0; i < len; i++) { + uint32_t v = uint32_t(buffer->get()) << 24; + v |= uint32_t(buffer->get()) << 16; + v |= uint32_t(buffer->get()) << 8; + v |= uint32_t(buffer->get()); + + value[i] = v; + } + } + +public: + virtual Type getType() const { + return Type::IntArray; + } +}; + +} +} diff --git a/src/NBT/IntTag.hpp b/src/NBT/IntTag.hpp new file mode 100644 index 0000000..9e7b3db --- /dev/null +++ b/src/NBT/IntTag.hpp @@ -0,0 +1,59 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class IntTag : public Tag { +private: + friend class Tag; + + uint32_t value; + + IntTag(Buffer *buffer) { + value = uint32_t(buffer->get()) << 24; + value |= uint32_t(buffer->get()) << 16; + value |= uint32_t(buffer->get()) << 8; + value |= uint32_t(buffer->get()); + } + +public: + virtual Type getType() const { + return Type::Int; + } + + uint32_t getValue() const { + return value; + } +}; + +} +} diff --git a/src/NBT/ListTag.hpp b/src/NBT/ListTag.hpp new file mode 100644 index 0000000..6404065 --- /dev/null +++ b/src/NBT/ListTag.hpp @@ -0,0 +1,65 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include + + +namespace MinedMap { +namespace NBT { + +class ListTag : public Tag { +private: + friend class Tag; + + std::vector> value; + + + ListTag(Buffer *buffer) { + Type type = static_cast(buffer->get()); + + uint32_t len = uint32_t(buffer->get()) << 24; + len |= uint32_t(buffer->get()) << 16; + len |= uint32_t(buffer->get()) << 8; + len |= uint32_t(buffer->get()); + + value.resize(len); + + for (uint32_t i = 0; i < len; i++) + value[i] = Tag::readTag(type, buffer); + } + +public: + virtual Type getType() const { + return Type::List; + } +}; + +} +} diff --git a/src/NBT/LongTag.hpp b/src/NBT/LongTag.hpp new file mode 100644 index 0000000..6cd5f8f --- /dev/null +++ b/src/NBT/LongTag.hpp @@ -0,0 +1,63 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class LongTag : public Tag { +private: + friend class Tag; + + uint64_t value; + + LongTag(Buffer *buffer) { + value = uint64_t(buffer->get()) << 56; + value |= uint64_t(buffer->get()) << 48; + value |= uint64_t(buffer->get()) << 40; + value |= uint64_t(buffer->get()) << 32; + value |= uint64_t(buffer->get()) << 24; + value |= uint64_t(buffer->get()) << 16; + value |= uint64_t(buffer->get()) << 8; + value |= uint64_t(buffer->get()); + } + +public: + virtual Type getType() const { + return Type::Long; + } + + uint64_t getValue() const { + return value; + } +}; + +} +} diff --git a/src/NBT/ShortTag.hpp b/src/NBT/ShortTag.hpp new file mode 100644 index 0000000..a44ca9b --- /dev/null +++ b/src/NBT/ShortTag.hpp @@ -0,0 +1,57 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class ShortTag : public Tag { +private: + friend class Tag; + + uint16_t value; + + ShortTag(Buffer *buffer) { + value = buffer->get() << 8; + value |= buffer->get(); + } + +public: + virtual Type getType() const { + return Type::Short; + } + + uint16_t getValue() const { + return value; + } +}; + +} +} diff --git a/src/NBT/StringTag.hpp b/src/NBT/StringTag.hpp new file mode 100644 index 0000000..a120183 --- /dev/null +++ b/src/NBT/StringTag.hpp @@ -0,0 +1,55 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include "Tag.hpp" + + +namespace MinedMap { +namespace NBT { + +class StringTag : public Tag { +private: + friend class Tag; + + std::string value; + + StringTag(Buffer *buffer) { + uint16_t len = buffer->get() << 8; + len |= buffer->get(); + + value = buffer->getString(len); + } + +public: + virtual Type getType() const { + return Type::String; + } +}; + +} +} diff --git a/src/NBT/Tag.cpp b/src/NBT/Tag.cpp new file mode 100644 index 0000000..fddfc4e --- /dev/null +++ b/src/NBT/Tag.cpp @@ -0,0 +1,105 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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 "Tag.hpp" + +#include "EndTag.hpp" +#include "ByteTag.hpp" +#include "ShortTag.hpp" +#include "IntTag.hpp" +#include "LongTag.hpp" +#include "FloatTag.hpp" +#include "DoubleTag.hpp" +#include "ByteArrayTag.hpp" +#include "StringTag.hpp" +#include "ListTag.hpp" +#include "CompoundTag.hpp" +#include "IntArrayTag.hpp" + + +#include + + +namespace MinedMap { +namespace NBT { + +std::shared_ptr Tag::readTag(Type type, Buffer *buffer) { + switch (type) { + case Type::End: + return std::shared_ptr(new EndTag()); + + case Type::Byte: + return std::shared_ptr(new ByteTag(buffer)); + + case Type::Short: + return std::shared_ptr(new ShortTag(buffer)); + + case Type::Int: + return std::shared_ptr(new IntTag(buffer)); + + case Type::Long: + return std::shared_ptr(new LongTag(buffer)); + + case Type::Float: + return std::shared_ptr(new FloatTag(buffer)); + + case Type::Double: + return std::shared_ptr(new DoubleTag(buffer)); + + case Type::ByteArray: + return std::shared_ptr(new ByteArrayTag(buffer)); + + case Type::String: + return std::shared_ptr(new StringTag(buffer)); + + case Type::List: + return std::shared_ptr(new ListTag(buffer)); + + case Type::Compound: + return std::shared_ptr(new CompoundTag(buffer)); + + case Type::IntArray: + return std::shared_ptr(new IntArrayTag(buffer)); + + default: + throw std::runtime_error("Tag::read: unknown tag type"); + } +} + +std::pair> Tag::readNamedTag(Buffer *buffer) { + Type type = static_cast(buffer->get()); + if (type == Type::End) + return std::make_pair("", std::shared_ptr(new EndTag())); + + uint16_t len = buffer->get() << 8; + len |= buffer->get(); + std::string name = buffer->getString(len); + + return std::make_pair(name, readTag(type, buffer)); +} + +} +} diff --git a/src/NBT/Tag.hpp b/src/NBT/Tag.hpp new file mode 100644 index 0000000..0d25299 --- /dev/null +++ b/src/NBT/Tag.hpp @@ -0,0 +1,65 @@ +/* + Copyright (c) 2015, Matthias Schiffer + 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. +*/ + + +#pragma once + +#include +#include + +#include "../Buffer.hpp" + + +namespace MinedMap { +namespace NBT { + +class Tag { +public: + enum class Type { + End = 0, + Byte = 1, + Short = 2, + Int = 3, + Long = 4, + Float = 5, + Double = 6, + ByteArray = 7, + String = 8, + List = 9, + Compound = 10, + IntArray = 11, + }; + + static std::shared_ptr readTag(Type type, Buffer *buffer); + + static std::pair> readNamedTag(Buffer *buffer); + + virtual Type getType() const = 0; + + virtual ~Tag() {} +}; + +} +} -- cgit v1.2.3