summaryrefslogtreecommitdiffstats
path: root/src/World/Chunk.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/World/Chunk.cpp')
-rw-r--r--src/World/Chunk.cpp63
1 files changed, 3 insertions, 60 deletions
diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp
index bf59068..07e38a4 100644
--- a/src/World/Chunk.cpp
+++ b/src/World/Chunk.cpp
@@ -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.
Redistribution and use in source and binary forms, with or without
@@ -38,66 +38,9 @@
namespace MinedMap {
namespace World {
-Chunk::Chunk(Buffer buffer) {
- size_t size = buffer.get32();
+Chunk::Chunk(const ChunkData *data) {
+ 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");
bool lightPopulated = lightPopulatedTag && lightPopulatedTag->getValue();