diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-01 00:21:17 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-01 00:27:01 +0100 |
commit | cbe64618821a0ae9f1901ce1a4a3893d939fdc32 (patch) | |
tree | 6a8341bdd90623817c26b2707df98910dd6a9e29 | |
parent | 1e74afda41658ad38cbf0bd65847571861237cb5 (diff) | |
download | MinedMap-cbe64618821a0ae9f1901ce1a4a3893d939fdc32.tar MinedMap-cbe64618821a0ae9f1901ce1a4a3893d939fdc32.zip |
Implement most of the chunk format
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/Buffer.hpp | 63 | ||||
-rw-r--r-- | src/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/MinedMap.cpp | 17 | ||||
-rw-r--r-- | src/NBT/ByteArrayTag.hpp | 62 | ||||
-rw-r--r-- | src/NBT/ByteTag.hpp | 56 | ||||
-rw-r--r-- | src/NBT/CompoundTag.hpp | 61 | ||||
-rw-r--r-- | src/NBT/DoubleTag.hpp | 59 | ||||
-rw-r--r-- | src/NBT/EndTag.hpp | 48 | ||||
-rw-r--r-- | src/NBT/FloatTag.hpp | 55 | ||||
-rw-r--r-- | src/NBT/IntArrayTag.hpp | 68 | ||||
-rw-r--r-- | src/NBT/IntTag.hpp | 59 | ||||
-rw-r--r-- | src/NBT/ListTag.hpp | 65 | ||||
-rw-r--r-- | src/NBT/LongTag.hpp | 63 | ||||
-rw-r--r-- | src/NBT/ShortTag.hpp | 57 | ||||
-rw-r--r-- | src/NBT/StringTag.hpp | 55 | ||||
-rw-r--r-- | src/NBT/Tag.cpp | 105 | ||||
-rw-r--r-- | src/NBT/Tag.hpp | 65 | ||||
-rw-r--r-- | src/UniqueCPtr.hpp | 37 | ||||
-rw-r--r-- | src/World/Chunk.cpp | 101 | ||||
-rw-r--r-- | src/World/Chunk.hpp | 54 | ||||
-rw-r--r-- | src/World/Region.cpp | 101 | ||||
-rw-r--r-- | src/World/Region.hpp | 64 |
23 files changed, 1327 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d94e18..f476f23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,4 +2,8 @@ cmake_minimum_required(VERSION 2.8.3) project(MINEDMAP CXX) +find_package(PkgConfig REQUIRED) +pkg_check_modules(ZLIB REQUIRED zlib) + + add_subdirectory(src) diff --git a/src/Buffer.hpp b/src/Buffer.hpp new file mode 100644 index 0000000..9af622d --- /dev/null +++ b/src/Buffer.hpp @@ -0,0 +1,63 @@ +/* + Copyright (c) 2015, 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. +*/ + + +#pragma once + +#include <cstdint> +#include <stdexcept> +#include <string> + + +namespace MinedMap { + +class Buffer { +private: + const uint8_t *data; + size_t len; + +public: + Buffer(const uint8_t *data0, size_t len0) : data(data0), len(len0) {} + + uint8_t get() { + if (!len) + throw std::runtime_error("Buffer::get(): buffer underrun"); + + data++; + len--; + return data[-1]; + } + + std::string getString(size_t n) { + if (n > len) + throw std::runtime_error("Buffer::get(): buffer underrun"); + + data += n; + len -= n; + return std::string(reinterpret_cast<const char *>(data - n), n); + } +}; + +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4559a19..df50419 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,12 @@ +include_directories(${ZLIB_INCLUDE_DIRS}) +link_directories(${ZLIB_LIBRARY_DIRS}) + add_executable(MinedMap MinedMap.cpp + NBT/Tag.cpp + World/Chunk.cpp + World/Region.cpp ) +set_target_properties(MinedMap PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall ${ZLIB_CFLAGS_OTHER}") +set_target_properties(MinedMap PROPERTIES LINK_FLAGS "${ZLIB_LDFLAGS_OTHER}") +target_link_libraries(MinedMap ${ZLIB_LIBRARIES}) diff --git a/src/MinedMap.cpp b/src/MinedMap.cpp index c28911a..306837d 100644 --- a/src/MinedMap.cpp +++ b/src/MinedMap.cpp @@ -24,6 +24,21 @@ */ -int main() { +#include "World/Region.hpp" + +#include <iostream> +#include <unistd.h> + + +int main(int argc, char *argv[]) { + using namespace MinedMap; + + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " region" << std::endl; + return 1; + } + + World::Region region(argv[1]); + return 0; } 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 <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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include <vector> + + +namespace MinedMap { +namespace NBT { + +class ByteArrayTag : public Tag { +private: + friend class Tag; + + std::vector<uint8_t> 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 <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. +*/ + + +#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 <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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include <string> +#include <unordered_map> + + +namespace MinedMap { +namespace NBT { + +class CompoundTag : public Tag { +private: + friend class Tag; + + std::unordered_map<std::string, std::shared_ptr<Tag>> value; + + CompoundTag(Buffer *buffer) { + while (true) { + std::pair<std::string, std::shared_ptr<Tag>> 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 <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. +*/ + + +#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 <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. +*/ + + +#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 <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. +*/ + + +#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 <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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include <vector> + + +namespace MinedMap { +namespace NBT { + +class IntArrayTag : public Tag { +private: + friend class Tag; + + std::vector<uint32_t> 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 <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. +*/ + + +#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 <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. +*/ + + +#pragma once + +#include "Tag.hpp" + +#include <vector> + + +namespace MinedMap { +namespace NBT { + +class ListTag : public Tag { +private: + friend class Tag; + + std::vector<std::shared_ptr<Tag>> value; + + + ListTag(Buffer *buffer) { + Type type = static_cast<Type>(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 <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. +*/ + + +#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 <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. +*/ + + +#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 <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. +*/ + + +#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 <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 "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 <stdexcept> + + +namespace MinedMap { +namespace NBT { + +std::shared_ptr<Tag> Tag::readTag(Type type, Buffer *buffer) { + switch (type) { + case Type::End: + return std::shared_ptr<EndTag>(new EndTag()); + + case Type::Byte: + return std::shared_ptr<ByteTag>(new ByteTag(buffer)); + + case Type::Short: + return std::shared_ptr<ShortTag>(new ShortTag(buffer)); + + case Type::Int: + return std::shared_ptr<IntTag>(new IntTag(buffer)); + + case Type::Long: + return std::shared_ptr<LongTag>(new LongTag(buffer)); + + case Type::Float: + return std::shared_ptr<FloatTag>(new FloatTag(buffer)); + + case Type::Double: + return std::shared_ptr<DoubleTag>(new DoubleTag(buffer)); + + case Type::ByteArray: + return std::shared_ptr<ByteArrayTag>(new ByteArrayTag(buffer)); + + case Type::String: + return std::shared_ptr<StringTag>(new StringTag(buffer)); + + case Type::List: + return std::shared_ptr<ListTag>(new ListTag(buffer)); + + case Type::Compound: + return std::shared_ptr<CompoundTag>(new CompoundTag(buffer)); + + case Type::IntArray: + return std::shared_ptr<IntArrayTag>(new IntArrayTag(buffer)); + + default: + throw std::runtime_error("Tag::read: unknown tag type"); + } +} + +std::pair<std::string, std::shared_ptr<Tag>> Tag::readNamedTag(Buffer *buffer) { + Type type = static_cast<Type>(buffer->get()); + if (type == Type::End) + return std::make_pair("", std::shared_ptr<EndTag>(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 <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. +*/ + + +#pragma once + +#include <cstdint> +#include <memory> + +#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<Tag> readTag(Type type, Buffer *buffer); + + static std::pair<std::string, std::shared_ptr<Tag>> readNamedTag(Buffer *buffer); + + virtual Type getType() const = 0; + + virtual ~Tag() {} +}; + +} +} diff --git a/src/UniqueCPtr.hpp b/src/UniqueCPtr.hpp new file mode 100644 index 0000000..9e70b89 --- /dev/null +++ b/src/UniqueCPtr.hpp @@ -0,0 +1,37 @@ +/* + Copyright (c) 2015, 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. +*/ + + +#pragma once + +#include <cstdlib> +#include <memory> + + +template<typename T> class UniqueCPtr : public std::unique_ptr<T, void (*)(void *)> { +public: + UniqueCPtr() : std::unique_ptr<T, void (*)(void *)>(nullptr, std::free) {} + template<typename T2> UniqueCPtr(T2 ptr) : std::unique_ptr<T, void (*)(void *)>(ptr, std::free) {} +}; diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp new file mode 100644 index 0000000..ccd0d87 --- /dev/null +++ b/src/World/Chunk.cpp @@ -0,0 +1,101 @@ +/* + Copyright (c) 2015, 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 "Chunk.hpp" + +#include <iostream> +#include <stdexcept> +#include <cstdlib> + +#include <zlib.h> + + +namespace MinedMap { +namespace World { + +std::tuple<UniqueCPtr<uint8_t[]>, size_t> Chunk::inflateChunk(uint8_t *data, size_t len) { + size_t outlen = 0; + uint8_t *output = nullptr; + + z_stream stream = {}; + int ret = inflateInit(&stream); + if (ret != Z_OK) + throw std::runtime_error("inflateInit() failed"); + + stream.next_in = data; + stream.avail_in = len; + + while (stream.avail_in) { + outlen += 65536; + output = static_cast<uint8_t *>(std::realloc(output, outlen)); + + stream.next_out = output + stream.total_out; + stream.avail_out = outlen - stream.total_out; + + ret = inflate(&stream, Z_NO_FLUSH); + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: + inflateEnd(&stream); + throw std::runtime_error("inflate() failed"); + } + } + + inflateEnd(&stream); + + return std::make_tuple(UniqueCPtr<uint8_t[]>(output), stream.total_out); +} + +Chunk::Chunk(uint8_t *buffer, size_t buflen) { + if (buflen < 5) + throw std::invalid_argument("short chunk"); + + size_t size = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; + if (size < 1 || size > (buflen - 4)) + throw std::invalid_argument("invalid chunk size"); + + uint8_t format = buffer[4]; + if (format != 2) + throw std::invalid_argument("unknown chunk format"); + + UniqueCPtr<uint8_t[]> data; + size_t len; + std::tie(data, len) = inflateChunk(buffer+5, size-1); + + std::cerr << "Chunk has size " << size << " (" << len << " inflated)" << std::endl; + + Buffer nbt(data.get(), len); + + std::pair<std::string, std::shared_ptr<NBT::Tag>> tag = NBT::Tag::readNamedTag(&nbt); + if (tag.first != "") + throw std::invalid_argument("non-empty root tag"); + + content = std::move(tag.second); +} + +} +} diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp new file mode 100644 index 0000000..eba4626 --- /dev/null +++ b/src/World/Chunk.hpp @@ -0,0 +1,54 @@ +/* + Copyright (c) 2015, 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. +*/ + + +#pragma once + + +#include "../UniqueCPtr.hpp" +#include "../NBT/Tag.hpp" + +#include <cstdint> +#include <tuple> + + +namespace MinedMap { +namespace World { + +class Chunk { +public: + static const size_t SIZE = 16; + +private: + static std::tuple<UniqueCPtr<uint8_t[]>, size_t> inflateChunk(uint8_t *data, size_t len); + + std::shared_ptr<NBT::Tag> content; + +public: + Chunk(uint8_t *buffer, size_t buflen); +}; + +} +} diff --git a/src/World/Region.cpp b/src/World/Region.cpp new file mode 100644 index 0000000..4c435f7 --- /dev/null +++ b/src/World/Region.cpp @@ -0,0 +1,101 @@ +/* + Copyright (c) 2015, 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 "Region.hpp" + +#include <fstream> +#include <iostream> + + +namespace MinedMap { +namespace World { + +Region::ChunkMap Region::processHeader(const uint8_t header[4096]) { + ChunkMap map; + + for (size_t z = 0; z < 32; z++) { + for (size_t x = 0; x < 32; x++) { + const uint8_t *p = &header[128*z + x*4]; + + size_t offset = (p[0] << 16) | (p[1] << 8) | p[2]; + + if (!offset) + continue; + + size_t len = p[3]; + + map.emplace(offset, ChunkDesc(x, z, len)); + } + } + + return map; +} + +Region::Region(const char *filename) { + std::ifstream file; + file.exceptions(std::ios::failbit | std::ios::badbit); + file.open(filename, std::ios::in | std::ios::binary); + + ChunkMap chunkMap; + + { + uint8_t header[4096]; + file.read((char *)header, sizeof(header)); + + chunkMap = processHeader(header); + } + + size_t i = 1, c = 0; + while (!file.eof()) { + auto it = chunkMap.find(i); + if (it == chunkMap.end()) { + file.ignore(4096); + i++; + continue; + } + + size_t x, z, len; + std::tie(x, z, len) = it->second; + + if (chunks[x][z]) + throw std::invalid_argument("duplicate chunk"); + + uint8_t buffer[len * 4096]; + file.read((char *)buffer, len * 4096); + + chunks[x][z].reset(new Chunk(buffer, len * 4096)); + + std::cerr << "Read chunk (" << x << "," << z << ") of length " << len << std::endl; + + i += len; + c++; + } + + std::cerr << "Read " << c <<" of " << chunkMap.size() << " chunks" << std::endl; +} + +} +} diff --git a/src/World/Region.hpp b/src/World/Region.hpp new file mode 100644 index 0000000..0843724 --- /dev/null +++ b/src/World/Region.hpp @@ -0,0 +1,64 @@ +/* + Copyright (c) 2015, 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. +*/ + + +#pragma once + +#include "Chunk.hpp" + +#include <memory> +#include <stdexcept> +#include <tuple> +#include <unordered_map> + + +namespace MinedMap { +namespace World { + +class Region { +public: + static const size_t SIZE = 32; + +private: + typedef std::tuple<size_t, size_t, size_t> ChunkDesc; + typedef std::unordered_map<size_t, ChunkDesc> ChunkMap; + + std::unique_ptr<Chunk> chunks[SIZE][SIZE]; + + static ChunkMap processHeader(const uint8_t header[4096]); + +public: + Region(const char *filename); + + const Chunk * operator()(size_t x, size_t z) { + if (x >= SIZE || z >= SIZE) + throw std::range_error("Region(): bad coordinates"); + + return chunks[x][z].get(); + } +}; + +} +} |