add Chunk.encodeV2, decodeV2

This commit is contained in:
MihailRis 2024-09-05 11:29:07 +03:00
parent cbb0ecd1f6
commit c18eddb63c
5 changed files with 45 additions and 3 deletions

View File

@ -161,7 +161,7 @@ void WorldConverter::upgradeRegion(
) const { ) const {
auto path = wfile->getRegions().getRegionFilePath(layer, x, z); auto path = wfile->getRegions().getRegionFilePath(layer, x, z);
auto bytes = files::read_bytes_buffer(path); 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()); files::write_bytes(path, buffer.data(), buffer.size());
} }

View File

@ -39,7 +39,7 @@ static util::Buffer<ubyte> convert_voxels_1to2(const ubyte* buffer, uint32_t siz
return util::Buffer<ubyte>(std::move(compressed), outLen); return util::Buffer<ubyte>(std::move(compressed), outLen);
} }
util::Buffer<ubyte> compatibility::convertRegion2to3( util::Buffer<ubyte> compatibility::convert_region_2to3(
const util::Buffer<ubyte>& src, RegionLayerIndex layer const util::Buffer<ubyte>& src, RegionLayerIndex layer
) { ) {
const size_t REGION_CHUNKS = 1024; const size_t REGION_CHUNKS = 1024;

View File

@ -9,6 +9,6 @@ namespace compatibility {
/// @see /doc/specs/region_file_spec.md /// @see /doc/specs/region_file_spec.md
/// @param src region file source content /// @param src region file source content
/// @return new region file content /// @return new region file content
util::Buffer<ubyte> convertRegion2to3( util::Buffer<ubyte> convert_region_2to3(
const util::Buffer<ubyte>& src, RegionLayerIndex layer); const util::Buffer<ubyte>& src, RegionLayerIndex layer);
} }

View File

@ -5,6 +5,7 @@
#include "content/ContentReport.hpp" #include "content/ContentReport.hpp"
#include "items/Inventory.hpp" #include "items/Inventory.hpp"
#include "lighting/Lightmap.hpp" #include "lighting/Lightmap.hpp"
#include "util/data_io.hpp"
#include "voxel.hpp" #include "voxel.hpp"
Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos) { Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos) {
@ -103,6 +104,27 @@ std::unique_ptr<ubyte[]> Chunk::encode() const {
return buffer; 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<ubyte[]> Chunk::encodeV2() const {
auto buffer = std::make_unique<ubyte[]>(CHUNK_DATA_LEN);
auto dst = reinterpret_cast<uint16_t*>(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) { bool Chunk::decode(const ubyte* data) {
for (uint i = 0; i < CHUNK_VOL; i++) { for (uint i = 0; i < CHUNK_VOL; i++) {
voxel& vox = voxels[i]; voxel& vox = voxels[i];
@ -123,6 +145,17 @@ bool Chunk::decode(const ubyte* data) {
return true; return true;
} }
bool Chunk::decodeV2(const ubyte* data) {
auto src = reinterpret_cast<const uint16_t*>(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) { void Chunk::convert(ubyte* data, const ContentReport* report) {
for (uint i = 0; i < CHUNK_VOL; i++) { for (uint i = 0; i < CHUNK_VOL; i++) {
// see encode method to understand what the hell is going on here // see encode method to understand what the hell is going on here

View File

@ -66,10 +66,19 @@ public:
flags.unsaved = true; 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<ubyte[]> encode() const; std::unique_ptr<ubyte[]> encode() const;
/// @brief Encode chunk to bytes array of size CHUNK_DATA_LEN
/// @see /doc/specs/region_voxels_chunk_spec.md
std::unique_ptr<ubyte[]> encodeV2() const;
/// @return true if all is fine /// @return true if all is fine
bool decode(const ubyte* data); bool decode(const ubyte* data);
/// @return true if all is fine
bool decodeV2(const ubyte* data);
static void convert(ubyte* data, const ContentReport* report); static void convert(ubyte* data, const ContentReport* report);
}; };