mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-05 17:44:52 +01:00
Separate splitting of regions into chunks and actual parsing of chunk structure
This commit is contained in:
parent
4b9bb2ab48
commit
c082c8800c
8 changed files with 170 additions and 79 deletions
|
@ -12,6 +12,7 @@ add_executable(MinedMap
|
||||||
Resource/BlockType.cpp
|
Resource/BlockType.cpp
|
||||||
World/Block.cpp
|
World/Block.cpp
|
||||||
World/Chunk.cpp
|
World/Chunk.cpp
|
||||||
|
World/ChunkData.cpp
|
||||||
World/Level.cpp
|
World/Level.cpp
|
||||||
World/Region.cpp
|
World/Region.cpp
|
||||||
)
|
)
|
||||||
|
|
|
@ -52,8 +52,9 @@ namespace MinedMap {
|
||||||
static const size_t DIM = World::Region::SIZE*World::Chunk::SIZE;
|
static const size_t DIM = World::Region::SIZE*World::Chunk::SIZE;
|
||||||
|
|
||||||
|
|
||||||
static void addChunk(uint32_t image[DIM*DIM], uint8_t lightmap[2*DIM*DIM], size_t X, size_t Z, const World::Chunk *chunk) {
|
static void addChunk(uint32_t image[DIM*DIM], uint8_t lightmap[2*DIM*DIM], size_t X, size_t Z, const World::ChunkData *data) {
|
||||||
World::Chunk::Blocks layer = chunk->getTopLayer();
|
World::Chunk chunk(data);
|
||||||
|
World::Chunk::Blocks layer = chunk.getTopLayer();
|
||||||
|
|
||||||
for (size_t x = 0; x < World::Chunk::SIZE; x++) {
|
for (size_t x = 0; x < World::Chunk::SIZE; x++) {
|
||||||
for (size_t z = 0; z < World::Chunk::SIZE; z++) {
|
for (size_t z = 0; z < World::Chunk::SIZE; z++) {
|
||||||
|
@ -139,7 +140,7 @@ static void doRegion(const std::string &input, const std::string &output, const
|
||||||
std::unique_ptr<uint8_t[]> lightmap(new uint8_t[2*DIM*DIM]);
|
std::unique_ptr<uint8_t[]> lightmap(new uint8_t[2*DIM*DIM]);
|
||||||
std::memset(lightmap.get(), 0, 2*DIM*DIM);
|
std::memset(lightmap.get(), 0, 2*DIM*DIM);
|
||||||
|
|
||||||
World::Region::visitChunks(input.c_str(), [&] (size_t X, size_t Z, const World::Chunk *chunk) { addChunk(image.get(), lightmap.get(), X, Z, chunk); });
|
World::Region::visitChunks(input.c_str(), [&] (size_t X, size_t Z, const World::ChunkData *chunk) { addChunk(image.get(), lightmap.get(), X, Z, chunk); });
|
||||||
|
|
||||||
writeImage(output, reinterpret_cast<const uint8_t*>(image.get()), true, intime);
|
writeImage(output, reinterpret_cast<const uint8_t*>(image.get()), true, intime);
|
||||||
writeImage(output_light, lightmap.get(), false, intime);
|
writeImage(output_light, lightmap.get(), false, intime);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
Copyright (c) 2015-2018, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -38,66 +38,9 @@
|
||||||
namespace MinedMap {
|
namespace MinedMap {
|
||||||
namespace World {
|
namespace World {
|
||||||
|
|
||||||
Chunk::Chunk(Buffer buffer) {
|
Chunk::Chunk(const ChunkData *data) {
|
||||||
size_t size = buffer.get32();
|
level = assertValue(data->getRoot().get<NBT::CompoundTag>("Level"));
|
||||||
|
|
||||||
Buffer buffer2(buffer.get(size), size);
|
|
||||||
|
|
||||||
uint8_t format = buffer2.get8();
|
|
||||||
if (format != 2)
|
|
||||||
throw std::invalid_argument("unknown chunk format");
|
|
||||||
|
|
||||||
inflateChunk(buffer2);
|
|
||||||
parseChunk();
|
|
||||||
analyzeChunk();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Chunk::inflateChunk(Buffer buffer) {
|
|
||||||
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.avail_in = buffer.getRemaining();
|
|
||||||
stream.next_in = const_cast<uint8_t *>(buffer.get(stream.avail_in));
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
if (tag.first != "")
|
|
||||||
throw std::invalid_argument("invalid root tag");
|
|
||||||
|
|
||||||
root = assertValue(std::dynamic_pointer_cast<const NBT::CompoundTag>(tag.second));
|
|
||||||
level = assertValue(root->get<NBT::CompoundTag>("Level"));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Chunk::analyzeChunk() {
|
|
||||||
std::shared_ptr<const NBT::ByteTag> lightPopulatedTag = level->get<NBT::ByteTag>("LightPopulated");
|
std::shared_ptr<const NBT::ByteTag> lightPopulatedTag = level->get<NBT::ByteTag>("LightPopulated");
|
||||||
bool lightPopulated = lightPopulatedTag && lightPopulatedTag->getValue();
|
bool lightPopulated = lightPopulatedTag && lightPopulatedTag->getValue();
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
Copyright (c) 2015-2018, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -28,10 +28,8 @@
|
||||||
|
|
||||||
|
|
||||||
#include "Block.hpp"
|
#include "Block.hpp"
|
||||||
#include "../Buffer.hpp"
|
#include "ChunkData.hpp"
|
||||||
#include "../UniqueCPtr.hpp"
|
|
||||||
#include "../Util.hpp"
|
#include "../Util.hpp"
|
||||||
#include "../NBT/CompoundTag.hpp"
|
|
||||||
#include "../NBT/ListTag.hpp"
|
#include "../NBT/ListTag.hpp"
|
||||||
#include "../NBT/ByteTag.hpp"
|
#include "../NBT/ByteTag.hpp"
|
||||||
#include "../NBT/ByteArrayTag.hpp"
|
#include "../NBT/ByteArrayTag.hpp"
|
||||||
|
@ -53,10 +51,6 @@ public:
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t len;
|
|
||||||
UniqueCPtr<uint8_t[]> data;
|
|
||||||
|
|
||||||
std::shared_ptr<const NBT::CompoundTag> root;
|
|
||||||
std::shared_ptr<const NBT::CompoundTag> level;
|
std::shared_ptr<const NBT::CompoundTag> level;
|
||||||
std::shared_ptr<const NBT::ListTag<NBT::CompoundTag>> sections;
|
std::shared_ptr<const NBT::ListTag<NBT::CompoundTag>> sections;
|
||||||
|
|
||||||
|
@ -106,13 +100,8 @@ private:
|
||||||
return biomes[z*SIZE + x];
|
return biomes[z*SIZE + x];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void inflateChunk(Buffer buffer);
|
|
||||||
void parseChunk();
|
|
||||||
void analyzeChunk();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Chunk(Buffer buffer);
|
Chunk(const ChunkData *data);
|
||||||
|
|
||||||
const NBT::CompoundTag & getLevel() const {
|
const NBT::CompoundTag & getLevel() const {
|
||||||
return *level;
|
return *level;
|
||||||
|
|
97
src/World/ChunkData.cpp
Normal file
97
src/World/ChunkData.cpp
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2015-2018, 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 "ChunkData.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace MinedMap {
|
||||||
|
namespace World {
|
||||||
|
|
||||||
|
ChunkData::ChunkData(Buffer buffer) {
|
||||||
|
size_t size = buffer.get32();
|
||||||
|
|
||||||
|
Buffer buffer2(buffer.get(size), size);
|
||||||
|
|
||||||
|
uint8_t format = buffer2.get8();
|
||||||
|
if (format != 2)
|
||||||
|
throw std::invalid_argument("unknown chunk format");
|
||||||
|
|
||||||
|
inflateChunk(buffer2);
|
||||||
|
parseChunk();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChunkData::inflateChunk(Buffer buffer) {
|
||||||
|
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.avail_in = buffer.getRemaining();
|
||||||
|
stream.next_in = const_cast<uint8_t *>(buffer.get(stream.avail_in));
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
len = stream.total_out;
|
||||||
|
data = UniqueCPtr<uint8_t[]>(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChunkData::parseChunk() {
|
||||||
|
Buffer nbt(data.get(), len);
|
||||||
|
std::pair<std::string, std::shared_ptr<const NBT::Tag>> tag = NBT::Tag::readNamedTag(&nbt);
|
||||||
|
if (tag.first != "")
|
||||||
|
throw std::invalid_argument("invalid root tag");
|
||||||
|
|
||||||
|
root = assertValue(std::dynamic_pointer_cast<const NBT::CompoundTag>(tag.second));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
60
src/World/ChunkData.hpp
Normal file
60
src/World/ChunkData.hpp
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2015-2018, 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 "../Buffer.hpp"
|
||||||
|
#include "../UniqueCPtr.hpp"
|
||||||
|
#include "../Util.hpp"
|
||||||
|
#include "../NBT/CompoundTag.hpp"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
namespace MinedMap {
|
||||||
|
namespace World {
|
||||||
|
|
||||||
|
class ChunkData {
|
||||||
|
private:
|
||||||
|
size_t len;
|
||||||
|
UniqueCPtr<uint8_t[]> data;
|
||||||
|
|
||||||
|
std::shared_ptr<const NBT::CompoundTag> root;
|
||||||
|
|
||||||
|
void inflateChunk(Buffer buffer);
|
||||||
|
void parseChunk();
|
||||||
|
|
||||||
|
public:
|
||||||
|
ChunkData(Buffer buffer);
|
||||||
|
|
||||||
|
const NBT::CompoundTag & getRoot() const {
|
||||||
|
return *root;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -91,7 +91,7 @@ void Region::visitChunks(const char *filename, const ChunkVisitor &visitor) {
|
||||||
uint8_t buffer[len * 4096];
|
uint8_t buffer[len * 4096];
|
||||||
file.read((char *)buffer, len * 4096);
|
file.read((char *)buffer, len * 4096);
|
||||||
|
|
||||||
Chunk chunk(Buffer(buffer, len * 4096));
|
ChunkData chunk(Buffer(buffer, len * 4096));
|
||||||
visitor(x, z, &chunk);
|
visitor(x, z, &chunk);
|
||||||
|
|
||||||
i += len;
|
i += len;
|
||||||
|
|
|
@ -42,7 +42,7 @@ class Region {
|
||||||
public:
|
public:
|
||||||
static const size_t SIZE = 32;
|
static const size_t SIZE = 32;
|
||||||
|
|
||||||
typedef std::function<void (size_t, size_t, const Chunk *)> ChunkVisitor;
|
typedef std::function<void (size_t, size_t, const ChunkData *)> ChunkVisitor;
|
||||||
|
|
||||||
Region() = delete;
|
Region() = delete;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue