Moved StreamPeer stuff to Godot-specific wrapper
This commit is contained in:
parent
3d81a52ccb
commit
f48ebe8557
@ -13,7 +13,7 @@
|
||||
#include <core/io/marshalls.h> // For `encode_variant`
|
||||
#endif
|
||||
|
||||
#include <core/io/stream_peer.h>
|
||||
#include <core/io/file_access.h>
|
||||
#include <limits>
|
||||
|
||||
namespace zylann::voxel {
|
||||
@ -29,6 +29,14 @@ thread_local std::vector<uint8_t> tls_data;
|
||||
thread_local std::vector<uint8_t> tls_compressed_data;
|
||||
thread_local std::vector<uint8_t> tls_metadata_tmp;
|
||||
|
||||
std::vector<uint8_t> &get_tls_data() {
|
||||
return tls_data;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> &get_tls_compressed_data() {
|
||||
return tls_compressed_data;
|
||||
}
|
||||
|
||||
size_t get_metadata_size_in_bytes(const VoxelMetadata &meta) {
|
||||
size_t size = 1; // Type
|
||||
switch (meta.get_type()) {
|
||||
@ -687,40 +695,7 @@ bool decompress_and_deserialize(FileAccess &f, unsigned int size_to_read, VoxelB
|
||||
const unsigned int read_size = f.get_buffer(compressed_data.data(), size_to_read);
|
||||
ERR_FAIL_COND_V(read_size != size_to_read, false);
|
||||
|
||||
return decompress_and_deserialize(to_span_const(compressed_data), out_voxel_buffer);
|
||||
}
|
||||
|
||||
int serialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, bool compress) {
|
||||
if (compress) {
|
||||
SerializeResult res = serialize_and_compress(voxel_buffer);
|
||||
ERR_FAIL_COND_V(!res.success, -1);
|
||||
peer.put_data(res.data.data(), res.data.size());
|
||||
return res.data.size();
|
||||
|
||||
} else {
|
||||
SerializeResult res = serialize(voxel_buffer);
|
||||
ERR_FAIL_COND_V(!res.success, -1);
|
||||
peer.put_data(res.data.data(), res.data.size());
|
||||
return res.data.size();
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress) {
|
||||
if (decompress) {
|
||||
std::vector<uint8_t> &compressed_data = tls_compressed_data;
|
||||
compressed_data.resize(size);
|
||||
const Error err = peer.get_data(compressed_data.data(), compressed_data.size());
|
||||
ERR_FAIL_COND(err != OK);
|
||||
bool success = decompress_and_deserialize(to_span_const(compressed_data), voxel_buffer);
|
||||
ERR_FAIL_COND(!success);
|
||||
|
||||
} else {
|
||||
std::vector<uint8_t> &data = tls_data;
|
||||
data.resize(size);
|
||||
const Error err = peer.get_data(data.data(), data.size());
|
||||
ERR_FAIL_COND(err != OK);
|
||||
deserialize(to_span_const(data), voxel_buffer);
|
||||
}
|
||||
return decompress_and_deserialize(to_span(compressed_data), out_voxel_buffer);
|
||||
}
|
||||
|
||||
} // namespace BlockSerializer
|
||||
|
@ -31,8 +31,9 @@ SerializeResult serialize_and_compress(const VoxelBufferInternal &voxel_buffer);
|
||||
bool decompress_and_deserialize(Span<const uint8_t> p_data, VoxelBufferInternal &out_voxel_buffer);
|
||||
bool decompress_and_deserialize(FileAccess &f, unsigned int size_to_read, VoxelBufferInternal &out_voxel_buffer);
|
||||
|
||||
int serialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, bool compress);
|
||||
void deserialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress);
|
||||
// Temporary thread-local buffers for internal use
|
||||
std::vector<uint8_t> &get_tls_data();
|
||||
std::vector<uint8_t> &get_tls_compressed_data();
|
||||
|
||||
} // namespace BlockSerializer
|
||||
} // namespace zylann::voxel
|
||||
|
69
streams/voxel_block_serializer_gd.cpp
Normal file
69
streams/voxel_block_serializer_gd.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "voxel_block_serializer_gd.h"
|
||||
#include "voxel_block_serializer.h"
|
||||
#include <core/io/stream_peer.h>
|
||||
|
||||
namespace zylann::voxel::gd {
|
||||
|
||||
int VoxelBlockSerializer::serialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, bool compress) {
|
||||
if (compress) {
|
||||
BlockSerializer::SerializeResult res = BlockSerializer::serialize_and_compress(voxel_buffer);
|
||||
ERR_FAIL_COND_V(!res.success, -1);
|
||||
peer.put_data(res.data.data(), res.data.size());
|
||||
return res.data.size();
|
||||
|
||||
} else {
|
||||
BlockSerializer::SerializeResult res = BlockSerializer::serialize(voxel_buffer);
|
||||
ERR_FAIL_COND_V(!res.success, -1);
|
||||
peer.put_data(res.data.data(), res.data.size());
|
||||
return res.data.size();
|
||||
}
|
||||
}
|
||||
|
||||
void VoxelBlockSerializer::deserialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress) {
|
||||
if (decompress) {
|
||||
std::vector<uint8_t> &compressed_data = BlockSerializer::get_tls_compressed_data();
|
||||
compressed_data.resize(size);
|
||||
const Error err = peer.get_data(compressed_data.data(), compressed_data.size());
|
||||
ERR_FAIL_COND(err != OK);
|
||||
bool success = BlockSerializer::decompress_and_deserialize(to_span(compressed_data), voxel_buffer);
|
||||
ERR_FAIL_COND(!success);
|
||||
|
||||
} else {
|
||||
std::vector<uint8_t> &data = BlockSerializer::get_tls_data();
|
||||
data.resize(size);
|
||||
const Error err = peer.get_data(data.data(), data.size());
|
||||
ERR_FAIL_COND(err != OK);
|
||||
BlockSerializer::deserialize(to_span(data), voxel_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
int VoxelBlockSerializer::serialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, bool compress) {
|
||||
ERR_FAIL_COND_V(voxel_buffer.is_null(), 0);
|
||||
ERR_FAIL_COND_V(peer.is_null(), 0);
|
||||
return serialize(**peer, voxel_buffer->get_buffer(), compress);
|
||||
}
|
||||
|
||||
void VoxelBlockSerializer::deserialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, int size, bool decompress) {
|
||||
ERR_FAIL_COND(voxel_buffer.is_null());
|
||||
ERR_FAIL_COND(peer.is_null());
|
||||
ERR_FAIL_COND(size <= 0);
|
||||
deserialize(**peer, voxel_buffer->get_buffer(), size, decompress);
|
||||
}
|
||||
|
||||
int VoxelBlockSerializer::_b_serialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, bool compress) {
|
||||
return serialize(peer, voxel_buffer, compress);
|
||||
}
|
||||
|
||||
void VoxelBlockSerializer::_b_deserialize(
|
||||
Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, int size, bool decompress) {
|
||||
deserialize(peer, voxel_buffer, size, decompress);
|
||||
}
|
||||
|
||||
void VoxelBlockSerializer::_bind_methods() {
|
||||
ClassDB::bind_method(
|
||||
D_METHOD("serialize", "peer", "voxel_buffer", "compress"), &VoxelBlockSerializer::_b_serialize);
|
||||
ClassDB::bind_method(D_METHOD("deserialize", "peer", "voxel_buffer", "size", "decompress"),
|
||||
&VoxelBlockSerializer::_b_deserialize);
|
||||
}
|
||||
|
||||
} // namespace zylann::voxel::gd
|
@ -2,8 +2,8 @@
|
||||
#define VOXEL_BLOCK_SERIALIZER_GD_H
|
||||
|
||||
#include "../storage/voxel_buffer_gd.h"
|
||||
#include "voxel_block_serializer.h"
|
||||
#include <core/io/stream_peer.h>
|
||||
|
||||
class StreamPeer;
|
||||
|
||||
namespace zylann::voxel::gd {
|
||||
|
||||
@ -14,26 +14,17 @@ class VoxelBuffer;
|
||||
class VoxelBlockSerializer : public RefCounted {
|
||||
GDCLASS(VoxelBlockSerializer, RefCounted)
|
||||
public:
|
||||
int serialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, bool compress) {
|
||||
ERR_FAIL_COND_V(voxel_buffer.is_null(), 0);
|
||||
ERR_FAIL_COND_V(peer.is_null(), 0);
|
||||
return BlockSerializer::serialize(**peer, voxel_buffer->get_buffer(), compress);
|
||||
}
|
||||
static int serialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, bool compress);
|
||||
static void deserialize(StreamPeer &peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress);
|
||||
|
||||
void deserialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, int size, bool decompress) {
|
||||
ERR_FAIL_COND(voxel_buffer.is_null());
|
||||
ERR_FAIL_COND(peer.is_null());
|
||||
ERR_FAIL_COND(size <= 0);
|
||||
BlockSerializer::deserialize(**peer, voxel_buffer->get_buffer(), size, decompress);
|
||||
}
|
||||
int serialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, bool compress);
|
||||
void deserialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, int size, bool decompress);
|
||||
|
||||
private:
|
||||
static void _bind_methods() {
|
||||
ClassDB::bind_method(
|
||||
D_METHOD("serialize", "peer", "voxel_buffer", "compress"), &VoxelBlockSerializer::serialize);
|
||||
ClassDB::bind_method(D_METHOD("deserialize", "peer", "voxel_buffer", "size", "decompress"),
|
||||
&VoxelBlockSerializer::deserialize);
|
||||
}
|
||||
int _b_serialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, bool compress);
|
||||
void _b_deserialize(Ref<StreamPeer> peer, Ref<VoxelBuffer> voxel_buffer, int size, bool decompress);
|
||||
|
||||
static void _bind_methods();
|
||||
};
|
||||
|
||||
} // namespace zylann::voxel::gd
|
||||
|
@ -26,6 +26,7 @@
|
||||
#endif
|
||||
|
||||
#include <core/io/dir_access.h>
|
||||
#include <core/io/stream_peer.h>
|
||||
#include <core/string/print_string.h>
|
||||
#include <core/templates/hash_map.h>
|
||||
#include <modules/noise/fastnoise_lite.h>
|
||||
|
Loading…
x
Reference in New Issue
Block a user