Skip to content

Commit

Permalink
add chunk data validity check to world.set_chunk_data
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Jan 31, 2025
1 parent 1e851b0 commit b00160e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/logic/scripting/lua/libs/libworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "api_lua.hpp"
#include "assets/AssetsLoader.hpp"
#include "coders/json.hpp"
#include "content/Content.hpp"
#include "engine/Engine.hpp"
#include "files/WorldFiles.hpp"
#include "files/engine_paths.hpp"
Expand Down Expand Up @@ -181,7 +182,7 @@ static int l_set_chunk_data(lua::State* L) {
return lua::pushboolean(L, false);
}
compressed_chunks::decode(
*chunk, buffer.data(), buffer.size()
*chunk, buffer.data(), buffer.size(), *content->getIndices()
);
if (controller->getChunksController()->lighting == nullptr) {
return lua::pushboolean(L, true);
Expand Down
18 changes: 16 additions & 2 deletions src/voxels/compressed_chunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "coders/rle.hpp"
#include "coders/gzip.hpp"

#include "content/Content.hpp"
#include "files/WorldFiles.hpp"

inline constexpr int HAS_VOXELS = 0x1;
Expand Down Expand Up @@ -48,7 +48,9 @@ static void read_voxel_data(ByteReader& reader, util::Buffer<ubyte>& dst) {
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, const ContentIndices& indices
) {
ByteReader reader(src, size);

ubyte flags = reader.get();
Expand All @@ -58,6 +60,18 @@ void compressed_chunks::decode(Chunk& chunk, const ubyte* src, size_t size) {
/// world.get_chunk_data is only available in the main Lua state
static util::Buffer<ubyte> voxelData (CHUNK_DATA_LEN);
read_voxel_data(reader, voxelData);
// TODO: move somewhere in Chunk
auto src = reinterpret_cast<const uint16_t*>(voxelData.data());
for (size_t i = 0; i < CHUNK_VOL; i++) {
blockid_t id = dataio::le2h(src[i]);;
if (indices.blocks.get(id) == nullptr) {
throw std::runtime_error(
"block data corruption (chunk: " + std::to_string(chunk.x) +
", " + std::to_string(chunk.z) + ") at " +
std::to_string(i) + " id: " + std::to_string(id)
);
}
}
chunk.decode(voxelData.data());
chunk.updateHeights();
}
Expand Down
8 changes: 7 additions & 1 deletion src/voxels/compressed_chunks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <vector>

class ContentIndices;
class WorldRegions;

namespace compressed_chunks {
Expand All @@ -15,6 +16,11 @@ namespace compressed_chunks {
util::Buffer<ubyte>& rleBuffer
);
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,
const ContentIndices& indices
);
void save(int x, int z, std::vector<ubyte> bytes, WorldRegions& regions);
}

0 comments on commit b00160e

Please sign in to comment.