forked from ShadowNinja/minetest-mapper-cpp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for map block version 29
- Loading branch information
Showing
7 changed files
with
163 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <zstd.h> | ||
#include "ZstdDecompressor.h" | ||
|
||
ZstdDecompressor::ZstdDecompressor(): | ||
m_data(nullptr), | ||
m_seekPos(0), | ||
m_size(0) | ||
{ | ||
m_stream = ZSTD_createDStream(); | ||
} | ||
|
||
ZstdDecompressor::~ZstdDecompressor() | ||
{ | ||
ZSTD_freeDStream(reinterpret_cast<ZSTD_DStream*>(m_stream)); | ||
} | ||
|
||
void ZstdDecompressor::setData(const u8 *data, size_t size, size_t seekPos) | ||
{ | ||
m_data = data; | ||
m_seekPos = seekPos; | ||
m_size = size; | ||
} | ||
|
||
std::size_t ZstdDecompressor::seekPos() const | ||
{ | ||
return m_seekPos; | ||
} | ||
|
||
ustring ZstdDecompressor::decompress() | ||
{ | ||
ZSTD_DStream *stream = reinterpret_cast<ZSTD_DStream*>(m_stream); | ||
ZSTD_inBuffer inbuf = { m_data, m_size, m_seekPos }; | ||
|
||
ustring buffer; | ||
constexpr size_t BUFSIZE = 32 * 1024; | ||
|
||
buffer.resize(BUFSIZE); | ||
ZSTD_outBuffer outbuf = { &buffer[0], buffer.size(), 0 }; | ||
|
||
ZSTD_initDStream(stream); | ||
|
||
size_t ret; | ||
do { | ||
ret = ZSTD_decompressStream(stream, &outbuf, &inbuf); | ||
if (outbuf.size == outbuf.pos) { | ||
outbuf.size += BUFSIZE; | ||
buffer.resize(outbuf.size); | ||
outbuf.dst = &buffer[0]; | ||
} | ||
} while (ret != 0); | ||
if (ZSTD_isError(ret)) | ||
throw DecompressError(); | ||
|
||
m_seekPos = inbuf.pos; | ||
buffer.resize(outbuf.pos); | ||
|
||
return buffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
mark_as_advanced(ZSTD_LIBRARY ZSTD_INCLUDE_DIR) | ||
|
||
find_path(ZSTD_INCLUDE_DIR NAMES zstd.h) | ||
|
||
find_library(ZSTD_LIBRARY NAMES zstd) | ||
|
||
if(ZSTD_INCLUDE_DIR AND ZSTD_LIBRARY) | ||
# Check that the API we use exists | ||
include(CheckSymbolExists) | ||
unset(HAVE_ZSTD_INITDSTREAM CACHE) | ||
set(CMAKE_REQUIRED_INCLUDES ${ZSTD_INCLUDE_DIR}) | ||
set(CMAKE_REQUIRED_LIBRARIES ${ZSTD_LIBRARY}) | ||
check_symbol_exists(ZSTD_initDStream zstd.h HAVE_ZSTD_INITDSTREAM) | ||
unset(CMAKE_REQUIRED_INCLUDES) | ||
unset(CMAKE_REQUIRED_LIBRARIES) | ||
|
||
if(NOT HAVE_ZSTD_INITDSTREAM) | ||
unset(ZSTD_INCLUDE_DIR CACHE) | ||
unset(ZSTD_LIBRARY CACHE) | ||
endif() | ||
endif() | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(Zstd DEFAULT_MSG ZSTD_LIBRARY ZSTD_INCLUDE_DIR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <cstdlib> | ||
#include <string> | ||
#include "types.h" | ||
|
||
class ZstdDecompressor | ||
{ | ||
public: | ||
class DecompressError {}; | ||
|
||
ZstdDecompressor(); | ||
~ZstdDecompressor(); | ||
void setData(const u8 *data, size_t size, size_t seekPos); | ||
size_t seekPos() const; | ||
ustring decompress(); | ||
|
||
private: | ||
void *m_stream; // ZSTD_DStream | ||
const u8 *m_data; | ||
size_t m_seekPos, m_size; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters