From c18eddb63c832b8691983a41e9ad9b1b07f8b741 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 5 Sep 2024 11:29:07 +0300 Subject: [PATCH] add Chunk.encodeV2, decodeV2 --- src/files/WorldConverter.cpp | 2 +- src/files/compatibility.cpp | 2 +- src/files/compatibility.hpp | 2 +- src/voxels/Chunk.cpp | 33 +++++++++++++++++++++++++++++++++ src/voxels/Chunk.hpp | 9 +++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/files/WorldConverter.cpp b/src/files/WorldConverter.cpp index 4748172d..5d24889d 100644 --- a/src/files/WorldConverter.cpp +++ b/src/files/WorldConverter.cpp @@ -161,7 +161,7 @@ void WorldConverter::upgradeRegion( ) const { auto path = wfile->getRegions().getRegionFilePath(layer, x, z); auto bytes = files::read_bytes_buffer(path); - auto buffer = compatibility::convertRegion2to3(bytes, layer); + auto buffer = compatibility::convert_region_2to3(bytes, layer); files::write_bytes(path, buffer.data(), buffer.size()); } diff --git a/src/files/compatibility.cpp b/src/files/compatibility.cpp index 0d9d6c08..161527b1 100644 --- a/src/files/compatibility.cpp +++ b/src/files/compatibility.cpp @@ -39,7 +39,7 @@ static util::Buffer convert_voxels_1to2(const ubyte* buffer, uint32_t siz return util::Buffer(std::move(compressed), outLen); } -util::Buffer compatibility::convertRegion2to3( +util::Buffer compatibility::convert_region_2to3( const util::Buffer& src, RegionLayerIndex layer ) { const size_t REGION_CHUNKS = 1024; diff --git a/src/files/compatibility.hpp b/src/files/compatibility.hpp index eef5b218..cf78b5bc 100644 --- a/src/files/compatibility.hpp +++ b/src/files/compatibility.hpp @@ -9,6 +9,6 @@ namespace compatibility { /// @see /doc/specs/region_file_spec.md /// @param src region file source content /// @return new region file content - util::Buffer convertRegion2to3( + util::Buffer convert_region_2to3( const util::Buffer& src, RegionLayerIndex layer); } diff --git a/src/voxels/Chunk.cpp b/src/voxels/Chunk.cpp index c77532da..5dfb9fe3 100644 --- a/src/voxels/Chunk.cpp +++ b/src/voxels/Chunk.cpp @@ -5,6 +5,7 @@ #include "content/ContentReport.hpp" #include "items/Inventory.hpp" #include "lighting/Lightmap.hpp" +#include "util/data_io.hpp" #include "voxel.hpp" Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos) { @@ -103,6 +104,27 @@ std::unique_ptr Chunk::encode() const { return buffer; } +/** + Current chunk format: + - byte-order: little-endian + + ```cpp + uint16_t voxel_id[CHUNK_VOL]; + uint16_t voxel_states[CHUNK_VOL]; + ``` + + Total size: (CHUNK_VOL * 4) bytes +*/ +std::unique_ptr Chunk::encodeV2() const { + auto buffer = std::make_unique(CHUNK_DATA_LEN); + auto dst = reinterpret_cast(buffer.get()); + for (uint i = 0; i < CHUNK_VOL; i++) { + dst[i] = dataio::h2le(voxels[i].id); + dst[CHUNK_VOL + i] = dataio::h2le(blockstate2int(voxels[i].state)); + } + return buffer; +} + bool Chunk::decode(const ubyte* data) { for (uint i = 0; i < CHUNK_VOL; i++) { voxel& vox = voxels[i]; @@ -123,6 +145,17 @@ bool Chunk::decode(const ubyte* data) { return true; } +bool Chunk::decodeV2(const ubyte* data) { + auto src = reinterpret_cast(data); + for (uint i = 0; i < CHUNK_VOL; i++) { + voxel& vox = voxels[i]; + + vox.id = dataio::le2h(src[i]); + vox.state = int2blockstate(dataio::le2h(src[CHUNK_VOL + i])); + } + return true; +} + void Chunk::convert(ubyte* data, const ContentReport* report) { for (uint i = 0; i < CHUNK_VOL; i++) { // see encode method to understand what the hell is going on here diff --git a/src/voxels/Chunk.hpp b/src/voxels/Chunk.hpp index d9a25186..bd8bf965 100644 --- a/src/voxels/Chunk.hpp +++ b/src/voxels/Chunk.hpp @@ -66,10 +66,19 @@ public: flags.unsaved = true; } + /// @brief Encode chunk to bytes array of size CHUNK_DATA_LEN + /// @see /doc/specs/outdated/region_voxels_chunk_spec_v1.md std::unique_ptr encode() const; + /// @brief Encode chunk to bytes array of size CHUNK_DATA_LEN + /// @see /doc/specs/region_voxels_chunk_spec.md + std::unique_ptr encodeV2() const; + /// @return true if all is fine bool decode(const ubyte* data); + /// @return true if all is fine + bool decodeV2(const ubyte* data); + static void convert(ubyte* data, const ContentReport* report); };