godot_voxel/streams/voxel_block_serializer.h

55 lines
1.8 KiB
C
Raw Normal View History

#ifndef VOXEL_BLOCK_SERIALIZER_H
#define VOXEL_BLOCK_SERIALIZER_H
#include <core/io/file_access_memory.h>
2020-07-25 13:56:23 -07:00
#include <core/reference.h>
#include <vector>
2020-07-25 13:56:23 -07:00
class StreamPeer;
class VoxelBufferInternal;
2020-07-25 13:56:23 -07:00
class VoxelBlockSerializerInternal {
// Had to be named differently to not conflict with the wrapper for Godot script API
public:
struct SerializeResult {
const std::vector<uint8_t> &data;
bool success;
inline SerializeResult(const std::vector<uint8_t> &p_data, bool p_success) :
data(p_data), success(p_success) {}
};
SerializeResult serialize(const VoxelBufferInternal &voxel_buffer);
bool deserialize(const std::vector<uint8_t> &p_data, VoxelBufferInternal &out_voxel_buffer);
SerializeResult serialize_and_compress(const VoxelBufferInternal &voxel_buffer);
bool decompress_and_deserialize(const std::vector<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(Ref<StreamPeer> peer, VoxelBufferInternal &voxel_buffer, bool compress);
void deserialize(Ref<StreamPeer> peer, VoxelBufferInternal &voxel_buffer, int size, bool decompress);
2020-07-25 13:56:23 -07:00
private:
// Make thread-locals?
std::vector<uint8_t> _data;
std::vector<uint8_t> _compressed_data;
std::vector<uint8_t> _metadata_tmp;
FileAccessMemory _file_access_memory;
};
class VoxelBuffer;
2020-07-25 13:56:23 -07:00
class VoxelBlockSerializer : public Reference {
GDCLASS(VoxelBlockSerializer, Reference)
public:
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();
VoxelBlockSerializerInternal _serializer;
};
#endif // VOXEL_BLOCK_SERIALIZER_H