mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-05 01:24:53 +01:00
Use buffer API for chunk decompression
This commit is contained in:
parent
18ed3d4c5d
commit
2d2671a686
4 changed files with 16 additions and 14 deletions
|
@ -56,6 +56,10 @@ public:
|
|||
|
||||
Buffer(const uint8_t *data0, size_t len0) : data(data0), len(len0) {}
|
||||
|
||||
size_t getRemaining() const {
|
||||
return len;
|
||||
}
|
||||
|
||||
const uint8_t * get(size_t n) {
|
||||
if (n > len)
|
||||
throw std::runtime_error("Buffer::get(): buffer underrun");
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
namespace MinedMap {
|
||||
namespace World {
|
||||
|
||||
void Chunk::inflateChunk(uint8_t *buffer, size_t buflen) {
|
||||
void Chunk::inflateChunk(Buffer buffer) {
|
||||
size_t outlen = 0;
|
||||
uint8_t *output = nullptr;
|
||||
|
||||
|
@ -48,8 +48,8 @@ void Chunk::inflateChunk(uint8_t *buffer, size_t buflen) {
|
|||
if (ret != Z_OK)
|
||||
throw std::runtime_error("inflateInit() failed");
|
||||
|
||||
stream.next_in = buffer;
|
||||
stream.avail_in = buflen;
|
||||
stream.avail_in = buffer.getRemaining();
|
||||
stream.next_in = const_cast<uint8_t *>(buffer.get(stream.avail_in));
|
||||
|
||||
while (stream.avail_in) {
|
||||
outlen += 65536;
|
||||
|
@ -111,19 +111,16 @@ uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion,
|
|||
return (v & 0xf);
|
||||
}
|
||||
|
||||
Chunk::Chunk(uint8_t *buffer, size_t buflen) {
|
||||
if (buflen < 5)
|
||||
throw std::invalid_argument("short chunk");
|
||||
Chunk::Chunk(Buffer buffer) {
|
||||
size_t size = buffer.get32();
|
||||
|
||||
size_t size = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
|
||||
if (size < 1 || size > (buflen - 4))
|
||||
throw std::invalid_argument("invalid chunk size");
|
||||
Buffer buffer2(buffer.get(size), size);
|
||||
|
||||
uint8_t format = buffer[4];
|
||||
uint8_t format = buffer2.get8();
|
||||
if (format != 2)
|
||||
throw std::invalid_argument("unknown chunk format");
|
||||
|
||||
inflateChunk(buffer+5, size-1);
|
||||
inflateChunk(buffer2);
|
||||
parseChunk();
|
||||
analyzeChunk();
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
#include "../Buffer.hpp"
|
||||
#include "../UniqueCPtr.hpp"
|
||||
#include "../NBT/CompoundTag.hpp"
|
||||
#include "../NBT/ListTag.hpp"
|
||||
|
@ -51,7 +52,7 @@ private:
|
|||
|
||||
unsigned maxY;
|
||||
|
||||
void inflateChunk(uint8_t *data, size_t len);
|
||||
void inflateChunk(Buffer buffer);
|
||||
void parseChunk();
|
||||
void analyzeChunk();
|
||||
|
||||
|
@ -66,7 +67,7 @@ private:
|
|||
uint8_t getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z);
|
||||
|
||||
public:
|
||||
Chunk(uint8_t *buffer, size_t buflen);
|
||||
Chunk(Buffer buffer);
|
||||
|
||||
const NBT::ListTag<NBT::CompoundTag> & getSections() const {
|
||||
return *sections;
|
||||
|
|
|
@ -86,7 +86,7 @@ Region::Region(const char *filename) {
|
|||
uint8_t buffer[len * 4096];
|
||||
file.read((char *)buffer, len * 4096);
|
||||
|
||||
chunks[x][z].reset(new Chunk(buffer, len * 4096));
|
||||
chunks[x][z].reset(new Chunk(Buffer(buffer, len * 4096)));
|
||||
|
||||
i += len;
|
||||
c++;
|
||||
|
|
Loading…
Add table
Reference in a new issue