summaryrefslogtreecommitdiffstats
path: root/src/World
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-02-01 00:21:17 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-02-01 00:27:01 +0100
commitcbe64618821a0ae9f1901ce1a4a3893d939fdc32 (patch)
tree6a8341bdd90623817c26b2707df98910dd6a9e29 /src/World
parent1e74afda41658ad38cbf0bd65847571861237cb5 (diff)
downloadMinedMap-cbe64618821a0ae9f1901ce1a4a3893d939fdc32.tar
MinedMap-cbe64618821a0ae9f1901ce1a4a3893d939fdc32.zip
Implement most of the chunk format
Diffstat (limited to 'src/World')
-rw-r--r--src/World/Chunk.cpp101
-rw-r--r--src/World/Chunk.hpp54
-rw-r--r--src/World/Region.cpp101
-rw-r--r--src/World/Region.hpp64
4 files changed, 320 insertions, 0 deletions
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();
+ }
+};
+
+}
+}