Implementing basic asset serialization

This commit is contained in:
Dynamitos
2023-02-13 14:56:13 +01:00
parent 9e1e4076f0
commit 48fa098546
82 changed files with 1834 additions and 423 deletions
@@ -0,0 +1,60 @@
#include "ArchiveBuffer.h"
#include "Graphics/Graphics.h"
using namespace Seele;
ArchiveBuffer::ArchiveBuffer()
{}
ArchiveBuffer::ArchiveBuffer(Gfx::PGraphics graphics)
: graphics(graphics)
{}
void ArchiveBuffer::writeBytes(const void* data, uint64 size)
{
if (size + position >= memory.size())
{
memory.resize(size + position);
}
std::memcpy(memory.data() + position, data, size);
position += size;
}
void ArchiveBuffer::readBytes(void* dest, uint64 size)
{
assert(position + size <= memory.size());
std::memcpy(dest, memory.data() + position, size);
position += size;
}
void ArchiveBuffer::writeToStream(std::ostream& stream)
{
uint64 bufferLength = memory.size();
stream.write((char*)&version, sizeof(uint64));
stream.write((char*)&bufferLength, sizeof(uint64));
stream.write((char*)memory.data(), memory.size());
}
void ArchiveBuffer::readFromStream(std::istream& stream)
{
stream.read((char*)&version, sizeof(uint64));
uint64 bufferLength = 0;
stream.read((char*)&bufferLength, sizeof(uint64));
memory.resize(bufferLength);
stream.read((char*)memory.data(), bufferLength);
}
bool ArchiveBuffer::eof() const
{
return position == memory.size();
}
void ArchiveBuffer::rewind()
{
position = 0;
}
Gfx::PGraphics& ArchiveBuffer::getGraphics()
{
return graphics;
}
+135 -2
View File
@@ -1,13 +1,146 @@
#pragma once
#include "Array.h"
#include "MinimalEngine.h"
#include "Containers/Array.h"
#include "Concepts.h"
#include <string>
namespace Seele
{
DECLARE_NAME_REF(Gfx, Graphics)
class ArchiveBuffer
{
public:
ArchiveBuffer();
ArchiveBuffer(Gfx::PGraphics graphics);
void writeBytes(const void* data, uint64 size);
void readBytes(void* dest, uint64 size);
void writeToStream(std::ostream& stream);
void readFromStream(std::istream& stream);
bool eof() const;
void rewind();
Gfx::PGraphics& getGraphics();
private:
Gfx::PGraphics graphics;
uint64 version = 0;
uint64 position = 0;
Array<uint8> memory;
};
namespace Serialization
{
template<std::integral T>
static void save(ArchiveBuffer& buffer, const T& type)
{
buffer.writeBytes(&type, sizeof(type));
}
template<std::integral T>
static void load(ArchiveBuffer& buffer, T& type)
{
buffer.readBytes(&type, sizeof(type));
}
template<std::floating_point T>
static void save(ArchiveBuffer& buffer, const T& type)
{
buffer.writeBytes(&type, sizeof(type));
}
template<std::floating_point T>
static void load(ArchiveBuffer& buffer, T& type)
{
buffer.readBytes(&type, sizeof(type));
}
template<enumeration T>
static void save(ArchiveBuffer& buffer, const T& type)
{
buffer.writeBytes(&type, sizeof(type));
}
template<enumeration T>
static void load(ArchiveBuffer& buffer, T& type)
{
buffer.readBytes(&type, sizeof(type));
}
//template<class T>
//static void save(ArchiveBuffer& buffer, const T& type)
//{
// type->save(buffer);
//}
//template<class T>
//static void load(ArchiveBuffer& buffer, T& type)
//{
// type->load(buffer);
//}
static void save(ArchiveBuffer& buffer, const std::string& type)
{
uint64 length = type.size();
buffer.writeBytes(&length, sizeof(uint64));
buffer.writeBytes(type.data(), type.size() * sizeof(char));
}
static void load(ArchiveBuffer& buffer, std::string& type)
{
uint64 length = 0;
buffer.readBytes(&length, sizeof(uint64));
type.resize(length);
buffer.readBytes(type.data(), length * sizeof(char));
}
//template<typename T>
//static void save(ArchiveBuffer& buffer, const RefPtr<T>& ptr)
//{
// Serialization::save(buffer, *ptr.getHandle());
//}
//template<typename T>
//static void load(ArchiveBuffer& buffer, RefPtr<T>& ptr)
//{
// Serialization::load(buffer, *ptr.getHandle());
//}
template<std::integral T>
static void save(ArchiveBuffer& buffer, const Array<T>& type)
{
uint64 length = type.size();
buffer.writeBytes(&length, sizeof(uint64));
buffer.writeBytes(type.data(), sizeof(T) * type.size());
}
template<std::integral T>
static void load(ArchiveBuffer& buffer, Array<T>& type)
{
uint64 length = 0;
buffer.readBytes(&length, sizeof(uint64));
type.resize(length);
buffer.readBytes(type.data(), sizeof(T) * type.size());
}
template<std::floating_point T>
static void save(ArchiveBuffer& buffer, const Array<T>& type)
{
uint64 length = type.size();
buffer.writeBytes(&length, sizeof(uint64));
buffer.writeBytes(type.data(), sizeof(T) * type.size());
}
template<std::floating_point T>
static void load(ArchiveBuffer& buffer, Array<T>& type)
{
uint64 length = 0;
buffer.readBytes(&length, sizeof(uint64));
type.resize(length);
buffer.readBytes(type.data(), sizeof(T) * type.size());
}
template<typename T>
static void save(ArchiveBuffer& buffer, const Array<T>& type) requires (!std::is_integral_v<T> && !std::is_floating_point_v<T>)
{
uint64 length = type.size();
buffer.writeBytes(&length, sizeof(uint64));
for (const auto& x : type)
{
save(buffer, x);
}
}
template<typename T>
static void load(ArchiveBuffer& buffer, Array<T>& type)
{
uint64 length = 0;
buffer.readBytes(&length, sizeof(uint64));
type.resize(length);
for (auto& x : type)
{
load(buffer, x);
}
}
} // namespace Serialization
} // namespace Seele
+2 -1
View File
@@ -1,6 +1,7 @@
target_sources(Engine
PRIVATE
ArchiveBuffer.h)
ArchiveBuffer.h
ArchiveBuffer.cpp)
target_sources(Engine