Merge pull request #445 from MihailRis/chunks-data-save-load

Add world.save_chunk_data
This commit is contained in:
MihailRis
2025-01-20 01:51:07 +03:00
committed by GitHub
5 changed files with 118 additions and 24 deletions
+10 -1
View File
@@ -49,10 +49,11 @@ world.is_night() -> bool
world.count_chunks() -> int world.count_chunks() -> int
-- Returns the compressed chunk data to send. -- Returns the compressed chunk data to send.
-- If the chunk is not loaded, returns the saved data.
-- Currently includes: -- Currently includes:
-- 1. Voxel data (id and state) -- 1. Voxel data (id and state)
-- 2. Voxel metadata (fields) -- 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. -- Modifies the chunk based on the compressed data.
-- Returns true if the chunk exists. -- Returns true if the chunk exists.
@@ -61,4 +62,12 @@ world.set_chunk_data(
-- compressed chunk data -- compressed chunk data
data: Bytearray data: Bytearray
) -> bool ) -> 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
)
``` ```
+10 -1
View File
@@ -48,10 +48,11 @@ world.is_night() -> bool
world.count_chunks() -> int world.count_chunks() -> int
-- Возвращает сжатые данные чанка для отправки. -- Возвращает сжатые данные чанка для отправки.
-- Если чанк не загружен, возвращает сохранённые данные.
-- На данный момент включает: -- На данный момент включает:
-- 1. Данные вокселей (id и состояние) -- 1. Данные вокселей (id и состояние)
-- 2. Метаданные (поля) вокселей -- 2. Метаданные (поля) вокселей
world.get_chunk_data(x: int, z: int) -> Bytearray world.get_chunk_data(x: int, z: int) -> Bytearray или nil
-- Изменяет чанк на основе сжатых данных. -- Изменяет чанк на основе сжатых данных.
-- Возвращает true если чанк существует. -- Возвращает true если чанк существует.
@@ -60,4 +61,12 @@ world.set_chunk_data(
-- сжатые данные чанка -- сжатые данные чанка
data: Bytearray data: Bytearray
) -> bool ) -> bool
-- Сохраняет данные чанка в регион.
-- Изменения будет записаны в файл только после сохранения мира.
world.save_chunk_data(
x: int, z: int,
-- сжатые данные чанка
data: Bytearray
)
``` ```
+35 -3
View File
@@ -6,6 +6,7 @@
#include "assets/AssetsLoader.hpp" #include "assets/AssetsLoader.hpp"
#include "coders/json.hpp" #include "coders/json.hpp"
#include "engine/Engine.hpp" #include "engine/Engine.hpp"
#include "files/WorldFiles.hpp"
#include "files/engine_paths.hpp" #include "files/engine_paths.hpp"
#include "files/files.hpp" #include "files/files.hpp"
#include "lighting/Lighting.hpp" #include "lighting/Lighting.hpp"
@@ -125,11 +126,21 @@ static int l_get_chunk_data(lua::State* L) {
int x = static_cast<int>(lua::tointeger(L, 1)); int x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2)); int z = static_cast<int>(lua::tointeger(L, 2));
const auto& chunk = level->chunks->getChunk(x, z); const auto& chunk = level->chunks->getChunk(x, z);
std::vector<ubyte> chunkData;
if (chunk == nullptr) { if (chunk == nullptr) {
lua::pushnil(L); auto& regions = level->getWorld()->wfile->getRegions();
return 0; 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)); return lua::newuserdata<lua::LuaBytearray>(L, std::move(chunkData));
} }
@@ -158,9 +169,14 @@ static void integrate_chunk_client(Chunk& chunk) {
} }
static int l_set_chunk_data(lua::State* L) { 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 x = static_cast<int>(lua::tointeger(L, 1));
int z = static_cast<int>(lua::tointeger(L, 2)); int z = static_cast<int>(lua::tointeger(L, 2));
auto buffer = lua::require_bytearray(L, 3); auto buffer = lua::require_bytearray(L, 3);
auto chunk = level->chunks->getChunk(x, z); auto chunk = level->chunks->getChunk(x, z);
if (chunk == nullptr) { if (chunk == nullptr) {
return lua::pushboolean(L, false); return lua::pushboolean(L, false);
@@ -175,6 +191,21 @@ static int l_set_chunk_data(lua::State* L) {
return lua::pushboolean(L, true); 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) { static int l_count_chunks(lua::State* L) {
if (level == nullptr) { if (level == nullptr) {
return 0; return 0;
@@ -197,6 +228,7 @@ const luaL_Reg worldlib[] = {
{"exists", lua::wrap<l_exists>}, {"exists", lua::wrap<l_exists>},
{"get_chunk_data", lua::wrap<l_get_chunk_data>}, {"get_chunk_data", lua::wrap<l_get_chunk_data>},
{"set_chunk_data", lua::wrap<l_set_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>}, {"count_chunks", lua::wrap<l_count_chunks>},
{NULL, NULL} {NULL, NULL}
}; };
+54 -18
View File
@@ -2,27 +2,24 @@
#include "coders/rle.hpp" #include "coders/rle.hpp"
#include "coders/gzip.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_VOXELS = 0x1;
inline constexpr int HAS_METADATA = 0x2; inline constexpr int HAS_METADATA = 0x2;
std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) { std::vector<ubyte> compressed_chunks::encode(
auto data = chunk.encode(); const ubyte* data,
const BlocksMetadata& metadata,
/// world.get_chunk_data is only available in the main Lua state util::Buffer<ubyte>& rleBuffer
static util::Buffer<ubyte> rleBuffer; ) {
if (rleBuffer.size() < CHUNK_DATA_LEN * 2) {
rleBuffer = util::Buffer<ubyte>(CHUNK_DATA_LEN * 2);
}
size_t rleCompressedSize = 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( const auto gzipCompressedData = gzip::compress(
rleBuffer.data(), rleCompressedSize rleBuffer.data(), rleCompressedSize
); );
auto metadataBytes = chunk.blocksMetadata.serialize(); auto metadataBytes = metadata.serialize();
ByteBuilder builder(2 + 8 + gzipCompressedData.size() + metadataBytes.size()); ByteBuilder builder(2 + 8 + gzipCompressedData.size() + metadataBytes.size());
builder.put(HAS_VOXELS | HAS_METADATA); // flags builder.put(HAS_VOXELS | HAS_METADATA); // flags
@@ -34,6 +31,23 @@ std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
return builder.build(); 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) { void compressed_chunks::decode(Chunk& chunk, const ubyte* src, size_t size) {
ByteReader reader(src, size); ByteReader reader(src, size);
@@ -41,14 +55,9 @@ void compressed_chunks::decode(Chunk& chunk, const ubyte* src, size_t size) {
reader.skip(1); // reserved byte reader.skip(1); // reserved byte
if (flags & HAS_VOXELS) { 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 /// world.get_chunk_data is only available in the main Lua state
static util::Buffer<ubyte> voxelData (CHUNK_DATA_LEN); 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.decode(voxelData.data());
chunk.updateHeights(); chunk.updateHeights();
} }
@@ -59,3 +68,30 @@ void compressed_chunks::decode(Chunk& chunk, const ubyte* src, size_t size) {
} }
chunk.setModifiedAndUnsaved(); 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);
}
}
+9 -1
View File
@@ -1,12 +1,20 @@
#pragma once #pragma once
#include "typedefs.hpp" #include "typedefs.hpp"
#include "Chunk.hpp"
#include "coders/byte_utils.hpp"
#include <vector> #include <vector>
class Chunk; class WorldRegions;
namespace compressed_chunks { namespace compressed_chunks {
std::vector<ubyte> encode(
const ubyte* voxelData,
const BlocksMetadata& metadata,
util::Buffer<ubyte>& rleBuffer
);
std::vector<ubyte> encode(const Chunk& chunk); std::vector<ubyte> encode(const Chunk& chunk);
void decode(Chunk& chunk, const ubyte* src, size_t size); void decode(Chunk& chunk, const ubyte* src, size_t size);
void save(int x, int z, std::vector<ubyte> bytes, WorldRegions& regions);
} }