Skip to content

Commit

Permalink
Merge pull request #445 from MihailRis/chunks-data-save-load
Browse files Browse the repository at this point in the history
Add world.save_chunk_data
  • Loading branch information
MihailRis authored Jan 19, 2025
2 parents 0e95d9d + d113cfa commit eca3a54
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 24 deletions.
11 changes: 10 additions & 1 deletion doc/en/scripting/builtins/libworld.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ world.is_night() -> bool
world.count_chunks() -> int

-- Returns the compressed chunk data to send.
-- If the chunk is not loaded, returns the saved data.
-- Currently includes:
-- 1. Voxel data (id and state)
-- 2. Voxel metadata (fields)
world.get_chunk_data(x: int, z: int) -> Bytearray
world.get_chunk_data(x: int, z: int) -> Bytearray or nil

-- Modifies the chunk based on the compressed data.
-- Returns true if the chunk exists.
Expand All @@ -61,4 +62,12 @@ world.set_chunk_data(
-- compressed chunk data
data: Bytearray
) -> bool

-- Saves chunk data to region.
-- Changes will be written to file only on world save.
world.save_chunk_data(
x: int, z: int,
-- compressed chunk data
data: Bytearray
)
```
11 changes: 10 additions & 1 deletion doc/ru/scripting/builtins/libworld.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ world.is_night() -> bool
world.count_chunks() -> int

-- Возвращает сжатые данные чанка для отправки.
-- Если чанк не загружен, возвращает сохранённые данные.
-- На данный момент включает:
-- 1. Данные вокселей (id и состояние)
-- 2. Метаданные (поля) вокселей
world.get_chunk_data(x: int, z: int) -> Bytearray
world.get_chunk_data(x: int, z: int) -> Bytearray или nil

-- Изменяет чанк на основе сжатых данных.
-- Возвращает true если чанк существует.
Expand All @@ -60,4 +61,12 @@ world.set_chunk_data(
-- сжатые данные чанка
data: Bytearray
) -> bool

-- Сохраняет данные чанка в регион.
-- Изменения будет записаны в файл только после сохранения мира.
world.save_chunk_data(
x: int, z: int,
-- сжатые данные чанка
data: Bytearray
)
```
38 changes: 35 additions & 3 deletions src/logic/scripting/lua/libs/libworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "assets/AssetsLoader.hpp"
#include "coders/json.hpp"
#include "engine/Engine.hpp"
#include "files/WorldFiles.hpp"
#include "files/engine_paths.hpp"
#include "files/files.hpp"
#include "lighting/Lighting.hpp"
Expand Down Expand Up @@ -125,11 +126,21 @@ static int l_get_chunk_data(lua::State* L) {
int x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2));
const auto& chunk = level->chunks->getChunk(x, z);

std::vector<ubyte> chunkData;
if (chunk == nullptr) {
lua::pushnil(L);
return 0;
auto& regions = level->getWorld()->wfile->getRegions();
auto voxelData = regions.getVoxels(x, z);
if (voxelData == nullptr) {
return 0;
}
static util::Buffer<ubyte> rleBuffer(CHUNK_DATA_LEN * 2);
auto metadata = regions.getBlocksData(x, z);
chunkData =
compressed_chunks::encode(voxelData.get(), metadata, rleBuffer);
} else {
chunkData = compressed_chunks::encode(*chunk);
}
auto chunkData = compressed_chunks::encode(*chunk);
return lua::newuserdata<lua::LuaBytearray>(L, std::move(chunkData));
}

Expand Down Expand Up @@ -158,9 +169,14 @@ static void integrate_chunk_client(Chunk& chunk) {
}

static int l_set_chunk_data(lua::State* L) {
if (level == nullptr) {
throw std::runtime_error("no open world");
}

int x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2));
auto buffer = lua::require_bytearray(L, 3);

auto chunk = level->chunks->getChunk(x, z);
if (chunk == nullptr) {
return lua::pushboolean(L, false);
Expand All @@ -175,6 +191,21 @@ static int l_set_chunk_data(lua::State* L) {
return lua::pushboolean(L, true);
}

static int l_save_chunk_data(lua::State* L) {
if (level == nullptr) {
throw std::runtime_error("no open world");
}

int x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2));
auto buffer = lua::require_bytearray(L, 3);

compressed_chunks::save(
x, z, std::move(buffer), level->getWorld()->wfile->getRegions()
);
return 0;
}

static int l_count_chunks(lua::State* L) {
if (level == nullptr) {
return 0;
Expand All @@ -197,6 +228,7 @@ const luaL_Reg worldlib[] = {
{"exists", lua::wrap<l_exists>},
{"get_chunk_data", lua::wrap<l_get_chunk_data>},
{"set_chunk_data", lua::wrap<l_set_chunk_data>},
{"save_chunk_data", lua::wrap<l_save_chunk_data>},
{"count_chunks", lua::wrap<l_count_chunks>},
{NULL, NULL}
};
72 changes: 54 additions & 18 deletions src/voxels/compressed_chunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@

#include "coders/rle.hpp"
#include "coders/gzip.hpp"
#include "coders/byte_utils.hpp"
#include "voxels/Chunk.hpp"

#include "files/WorldFiles.hpp"

inline constexpr int HAS_VOXELS = 0x1;
inline constexpr int HAS_METADATA = 0x2;

std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
auto data = chunk.encode();

/// world.get_chunk_data is only available in the main Lua state
static util::Buffer<ubyte> rleBuffer;
if (rleBuffer.size() < CHUNK_DATA_LEN * 2) {
rleBuffer = util::Buffer<ubyte>(CHUNK_DATA_LEN * 2);
}
std::vector<ubyte> compressed_chunks::encode(
const ubyte* data,
const BlocksMetadata& metadata,
util::Buffer<ubyte>& rleBuffer
) {
size_t rleCompressedSize =
extrle::encode16(data.get(), CHUNK_DATA_LEN, rleBuffer.data());
extrle::encode16(data, CHUNK_DATA_LEN, rleBuffer.data());

const auto gzipCompressedData = gzip::compress(
rleBuffer.data(), rleCompressedSize
);
auto metadataBytes = chunk.blocksMetadata.serialize();
auto metadataBytes = metadata.serialize();

ByteBuilder builder(2 + 8 + gzipCompressedData.size() + metadataBytes.size());
builder.put(HAS_VOXELS | HAS_METADATA); // flags
Expand All @@ -34,21 +31,33 @@ std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
return builder.build();
}

std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
auto data = chunk.encode();

/// world.get_chunk_data is only available in the main Lua state
static util::Buffer<ubyte> rleBuffer(CHUNK_DATA_LEN * 2);
return encode(data.get(), chunk.blocksMetadata, rleBuffer);
}

static void read_voxel_data(ByteReader& reader, util::Buffer<ubyte>& dst) {
size_t gzipCompressedSize = reader.getInt32();

auto rleData = gzip::decompress(reader.pointer(), gzipCompressedSize);
reader.skip(gzipCompressedSize);

extrle::decode16(rleData.data(), rleData.size(), dst.data());
}

void compressed_chunks::decode(Chunk& chunk, const ubyte* src, size_t size) {
ByteReader reader(src, size);

ubyte flags = reader.get();
reader.skip(1); // reserved byte

if (flags & HAS_VOXELS) {
size_t gzipCompressedSize = reader.getInt32();

auto rleData = gzip::decompress(reader.pointer(), gzipCompressedSize);
reader.skip(gzipCompressedSize);

/// world.get_chunk_data is only available in the main Lua state
static util::Buffer<ubyte> voxelData (CHUNK_DATA_LEN);
extrle::decode16(rleData.data(), rleData.size(), voxelData.data());
read_voxel_data(reader, voxelData);
chunk.decode(voxelData.data());
chunk.updateHeights();
}
Expand All @@ -59,3 +68,30 @@ void compressed_chunks::decode(Chunk& chunk, const ubyte* src, size_t size) {
}
chunk.setModifiedAndUnsaved();
}

void compressed_chunks::save(
int x, int z, std::vector<ubyte> bytes, WorldRegions& regions
) {
ByteReader reader(bytes.data(), bytes.size());

ubyte flags = reader.get();
reader.skip(1); // reserved byte
if (flags & HAS_VOXELS) {
util::Buffer<ubyte> voxelData (CHUNK_DATA_LEN);
read_voxel_data(reader, voxelData);
regions.put(
x, z, REGION_LAYER_VOXELS, voxelData.release(), CHUNK_DATA_LEN
);
}
if (flags & HAS_METADATA) {
size_t metadataSize = reader.getInt32();
regions.put(
x,
z,
REGION_LAYER_BLOCKS_DATA,
util::Buffer<ubyte>(reader.pointer(), metadataSize).release(),
metadataSize
);
reader.skip(metadataSize);
}
}
10 changes: 9 additions & 1 deletion src/voxels/compressed_chunks.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#pragma once

#include "typedefs.hpp"
#include "Chunk.hpp"
#include "coders/byte_utils.hpp"

#include <vector>

class Chunk;
class WorldRegions;

namespace compressed_chunks {
std::vector<ubyte> encode(
const ubyte* voxelData,
const BlocksMetadata& metadata,
util::Buffer<ubyte>& rleBuffer
);
std::vector<ubyte> encode(const Chunk& chunk);
void decode(Chunk& chunk, const ubyte* src, size_t size);
void save(int x, int z, std::vector<ubyte> bytes, WorldRegions& regions);
}

0 comments on commit eca3a54

Please sign in to comment.