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/World/Chunk.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/World/Chunk.hpp | 54 +++++++++++++++++++++++++++ src/World/Region.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/World/Region.hpp | 64 ++++++++++++++++++++++++++++++++ 4 files changed, 320 insertions(+) create mode 100644 src/World/Chunk.cpp create mode 100644 src/World/Chunk.hpp create mode 100644 src/World/Region.cpp create mode 100644 src/World/Region.hpp (limited to 'src/World') 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 + 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 +#include +#include + +#include + + +namespace MinedMap { +namespace World { + +std::tuple, 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(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(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 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> 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 + 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 +#include + + +namespace MinedMap { +namespace World { + +class Chunk { +public: + static const size_t SIZE = 16; + +private: + static std::tuple, size_t> inflateChunk(uint8_t *data, size_t len); + + std::shared_ptr 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 + 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 +#include + + +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 + 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 +#include +#include +#include + + +namespace MinedMap { +namespace World { + +class Region { +public: + static const size_t SIZE = 32; + +private: + typedef std::tuple ChunkDesc; + typedef std::unordered_map ChunkMap; + + std::unique_ptr 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(); + } +}; + +} +} -- cgit v1.2.3