2015-02-01 00:21:17 +01:00
|
|
|
/*
|
|
|
|
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"
|
2015-02-01 05:18:35 +01:00
|
|
|
#include "../Util.hpp"
|
|
|
|
#include "../NBT/ByteTag.hpp"
|
2015-02-01 05:54:42 +01:00
|
|
|
#include "../NBT/ByteArrayTag.hpp"
|
2015-02-01 00:21:17 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinedMap {
|
|
|
|
namespace World {
|
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
void Chunk::inflateChunk(Buffer buffer) {
|
2015-02-01 00:21:17 +01:00
|
|
|
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");
|
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
stream.avail_in = buffer.getRemaining();
|
|
|
|
stream.next_in = const_cast<uint8_t *>(buffer.get(stream.avail_in));
|
2015-02-01 00:21:17 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-02-01 05:18:35 +01:00
|
|
|
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");
|
|
|
|
|
2015-02-01 05:54:42 +01:00
|
|
|
level = assertValue((*this)->get<NBT::CompoundTag>("Level"));
|
|
|
|
sections = assertValue(level->get<NBT::ListTag<NBT::CompoundTag>>("Sections"));
|
2015-02-01 05:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chunk::analyzeChunk() {
|
|
|
|
maxY = (assertValue(sections->back()->get<NBT::ByteTag>("Y"))->getValue() + 1) * SIZE;
|
|
|
|
|
2015-02-01 05:54:42 +01:00
|
|
|
for (auto §ion : *sections) {
|
|
|
|
if (assertValue(section->get<NBT::ByteArrayTag>("Blocks"))->getLength() != SIZE*SIZE*SIZE
|
|
|
|
|| assertValue(section->get<NBT::ByteArrayTag>("Data"))->getLength() != SIZE*SIZE*SIZE/2)
|
|
|
|
throw std::invalid_argument("corrupt chunk data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Chunk::getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) {
|
|
|
|
return (*section->get<NBT::ByteArrayTag>("Blocks"))[getIndex(x, y, z)];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) {
|
|
|
|
size_t i = getIndex(x, y, z);
|
|
|
|
uint8_t v = (*section->get<NBT::ByteArrayTag>("Data"))[i / 2];
|
|
|
|
|
|
|
|
if (i % 2)
|
|
|
|
return (v >> 4);
|
|
|
|
else
|
|
|
|
return (v & 0xf);
|
2015-02-01 00:21:17 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
Chunk::Chunk(Buffer buffer) {
|
|
|
|
size_t size = buffer.get32();
|
2015-02-01 00:21:17 +01:00
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
Buffer buffer2(buffer.get(size), size);
|
2015-02-01 00:21:17 +01:00
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
uint8_t format = buffer2.get8();
|
2015-02-01 00:21:17 +01:00
|
|
|
if (format != 2)
|
|
|
|
throw std::invalid_argument("unknown chunk format");
|
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
inflateChunk(buffer2);
|
2015-02-01 05:18:35 +01:00
|
|
|
parseChunk();
|
|
|
|
analyzeChunk();
|
2015-02-01 00:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|