mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-04-20 03:25:09 +02:00
Start analyzing the chunks
This commit is contained in:
parent
8a8a41a800
commit
8403a4e71c
4 changed files with 80 additions and 24 deletions
42
src/Util.hpp
Normal file
42
src/Util.hpp
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
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 <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
namespace MinedMap {
|
||||||
|
|
||||||
|
template <typename T> static inline T assertValue(T&& val) {
|
||||||
|
if (!val)
|
||||||
|
throw std::invalid_argument("assertValue failed");
|
||||||
|
|
||||||
|
return std::forward<T>(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -25,6 +25,8 @@
|
||||||
|
|
||||||
|
|
||||||
#include "Chunk.hpp"
|
#include "Chunk.hpp"
|
||||||
|
#include "../Util.hpp"
|
||||||
|
#include "../NBT/ByteTag.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -36,7 +38,7 @@
|
||||||
namespace MinedMap {
|
namespace MinedMap {
|
||||||
namespace World {
|
namespace World {
|
||||||
|
|
||||||
std::pair<UniqueCPtr<uint8_t[]>, size_t> Chunk::inflateChunk(uint8_t *data, size_t len) {
|
void Chunk::inflateChunk(uint8_t *buffer, size_t buflen) {
|
||||||
size_t outlen = 0;
|
size_t outlen = 0;
|
||||||
uint8_t *output = nullptr;
|
uint8_t *output = nullptr;
|
||||||
|
|
||||||
|
@ -45,8 +47,8 @@ std::pair<UniqueCPtr<uint8_t[]>, size_t> Chunk::inflateChunk(uint8_t *data, size
|
||||||
if (ret != Z_OK)
|
if (ret != Z_OK)
|
||||||
throw std::runtime_error("inflateInit() failed");
|
throw std::runtime_error("inflateInit() failed");
|
||||||
|
|
||||||
stream.next_in = data;
|
stream.next_in = buffer;
|
||||||
stream.avail_in = len;
|
stream.avail_in = buflen;
|
||||||
|
|
||||||
while (stream.avail_in) {
|
while (stream.avail_in) {
|
||||||
outlen += 65536;
|
outlen += 65536;
|
||||||
|
@ -67,7 +69,28 @@ std::pair<UniqueCPtr<uint8_t[]>, size_t> Chunk::inflateChunk(uint8_t *data, size
|
||||||
|
|
||||||
inflateEnd(&stream);
|
inflateEnd(&stream);
|
||||||
|
|
||||||
return std::make_pair(UniqueCPtr<uint8_t[]>(output), stream.total_out);
|
len = stream.total_out;
|
||||||
|
data = UniqueCPtr<uint8_t[]>(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Chunk::parseChunk() {
|
||||||
|
Buffer nbt(data.get(), len);
|
||||||
|
std::pair<std::string, std::shared_ptr<const NBT::Tag>> tag = NBT::Tag::readNamedTag(&nbt);
|
||||||
|
|
||||||
|
std::shared_ptr<const NBT::CompoundTag>::operator=(std::dynamic_pointer_cast<const NBT::CompoundTag>(tag.second));
|
||||||
|
|
||||||
|
if (!(*this) || tag.first != "")
|
||||||
|
throw std::invalid_argument("invalid root tag");
|
||||||
|
|
||||||
|
sections = (*this)->get<const NBT::ListTag<NBT::CompoundTag>>("Level", "Sections");
|
||||||
|
if (!sections)
|
||||||
|
throw std::invalid_argument("no sections found");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Chunk::analyzeChunk() {
|
||||||
|
maxY = (assertValue(sections->back()->get<NBT::ByteTag>("Y"))->getValue() + 1) * SIZE;
|
||||||
|
|
||||||
|
std::cerr << "maxY: " << maxY << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::Chunk(uint8_t *buffer, size_t buflen) {
|
Chunk::Chunk(uint8_t *buffer, size_t buflen) {
|
||||||
|
@ -82,22 +105,9 @@ Chunk::Chunk(uint8_t *buffer, size_t buflen) {
|
||||||
if (format != 2)
|
if (format != 2)
|
||||||
throw std::invalid_argument("unknown chunk format");
|
throw std::invalid_argument("unknown chunk format");
|
||||||
|
|
||||||
size_t len;
|
inflateChunk(buffer+5, size-1);
|
||||||
std::tie(data, len) = inflateChunk(buffer+5, size-1);
|
parseChunk();
|
||||||
|
analyzeChunk();
|
||||||
std::cerr << "Chunk has size " << size << " (" << len << " inflated)" << std::endl;
|
|
||||||
|
|
||||||
Buffer nbt(data.get(), len);
|
|
||||||
std::pair<std::string, std::shared_ptr<const NBT::Tag>> tag = NBT::Tag::readNamedTag(&nbt);
|
|
||||||
|
|
||||||
std::shared_ptr<const NBT::CompoundTag>::operator=(std::dynamic_pointer_cast<const NBT::CompoundTag>(tag.second));
|
|
||||||
|
|
||||||
if (!(*this) || tag.first != "")
|
|
||||||
throw std::invalid_argument("invalid root tag");
|
|
||||||
|
|
||||||
sections = (*this)->get<const NBT::ListTag<NBT::CompoundTag>>("Level", "Sections");
|
|
||||||
if (!sections)
|
|
||||||
throw std::invalid_argument("no sections found");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,9 +43,14 @@ public:
|
||||||
static const size_t SIZE = 16;
|
static const size_t SIZE = 16;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
size_t len;
|
||||||
UniqueCPtr<uint8_t[]> data;
|
UniqueCPtr<uint8_t[]> data;
|
||||||
|
|
||||||
static std::pair<UniqueCPtr<uint8_t[]>, 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<const NBT::ListTag<NBT::CompoundTag>> sections;
|
std::shared_ptr<const NBT::ListTag<NBT::CompoundTag>> sections;
|
||||||
|
|
||||||
|
|
|
@ -88,13 +88,12 @@ Region::Region(const char *filename) {
|
||||||
|
|
||||||
chunks[x][z].reset(new Chunk(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;
|
i += len;
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Read " << c <<" of " << chunkMap.size() << " chunks" << std::endl;
|
if (c != chunkMap.size())
|
||||||
|
throw std::invalid_argument("region incomplete");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue