From 8403a4e71c1c3557646c9f0d019bc53e014134a0 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 Feb 2015 05:18:35 +0100 Subject: Start analyzing the chunks --- src/World/Chunk.cpp | 50 ++++++++++++++++++++++++++++++-------------------- src/World/Chunk.hpp | 7 ++++++- src/World/Region.cpp | 5 ++--- 3 files changed, 38 insertions(+), 24 deletions(-) (limited to 'src/World') diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index db286ee..90ad21f 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -25,6 +25,8 @@ #include "Chunk.hpp" +#include "../Util.hpp" +#include "../NBT/ByteTag.hpp" #include #include @@ -36,7 +38,7 @@ namespace MinedMap { namespace World { -std::pair, size_t> Chunk::inflateChunk(uint8_t *data, size_t len) { +void Chunk::inflateChunk(uint8_t *buffer, size_t buflen) { size_t outlen = 0; uint8_t *output = nullptr; @@ -45,8 +47,8 @@ std::pair, size_t> Chunk::inflateChunk(uint8_t *data, size if (ret != Z_OK) throw std::runtime_error("inflateInit() failed"); - stream.next_in = data; - stream.avail_in = len; + stream.next_in = buffer; + stream.avail_in = buflen; while (stream.avail_in) { outlen += 65536; @@ -67,7 +69,28 @@ std::pair, size_t> Chunk::inflateChunk(uint8_t *data, size inflateEnd(&stream); - return std::make_pair(UniqueCPtr(output), stream.total_out); + len = stream.total_out; + data = UniqueCPtr(output); +} + +void Chunk::parseChunk() { + Buffer nbt(data.get(), len); + std::pair> tag = NBT::Tag::readNamedTag(&nbt); + + std::shared_ptr::operator=(std::dynamic_pointer_cast(tag.second)); + + if (!(*this) || tag.first != "") + throw std::invalid_argument("invalid root tag"); + + sections = (*this)->get>("Level", "Sections"); + if (!sections) + throw std::invalid_argument("no sections found"); +} + +void Chunk::analyzeChunk() { + maxY = (assertValue(sections->back()->get("Y"))->getValue() + 1) * SIZE; + + std::cerr << "maxY: " << maxY << std::endl; } Chunk::Chunk(uint8_t *buffer, size_t buflen) { @@ -82,22 +105,9 @@ Chunk::Chunk(uint8_t *buffer, size_t buflen) { if (format != 2) throw std::invalid_argument("unknown chunk format"); - 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); - - std::shared_ptr::operator=(std::dynamic_pointer_cast(tag.second)); - - if (!(*this) || tag.first != "") - throw std::invalid_argument("invalid root tag"); - - sections = (*this)->get>("Level", "Sections"); - if (!sections) - throw std::invalid_argument("no sections found"); + inflateChunk(buffer+5, size-1); + parseChunk(); + analyzeChunk(); } } diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index 5b0ecdb..639d1a7 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -43,9 +43,14 @@ public: static const size_t SIZE = 16; private: + size_t len; UniqueCPtr data; - static std::pair, size_t> inflateChunk(uint8_t *data, size_t len); + unsigned maxY; + + void inflateChunk(uint8_t *data, size_t len); + void parseChunk(); + void analyzeChunk(); std::shared_ptr> sections; diff --git a/src/World/Region.cpp b/src/World/Region.cpp index 4c435f7..4f69059 100644 --- a/src/World/Region.cpp +++ b/src/World/Region.cpp @@ -88,13 +88,12 @@ Region::Region(const char *filename) { 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; + if (c != chunkMap.size()) + throw std::invalid_argument("region incomplete"); } } -- cgit v1.2.3