#pragma once #include "MinimalEngine.h" #include "Containers/Array.h" #include "Concepts.h" #include 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); enum class SeekOp { CURRENT, BEGIN, END, }; void seek(int64 s, SeekOp op); bool eof() const; size_t size() const; void rewind(); Gfx::PGraphics& getGraphics(); private: Gfx::PGraphics graphics; uint64 version = 0; uint64 position = 0; Array memory; }; namespace Serialization { template static void save(ArchiveBuffer& buffer, const T& type) { buffer.writeBytes(&type, sizeof(type)); } template static void load(ArchiveBuffer& buffer, T& type) { buffer.readBytes(&type, sizeof(type)); } template static void save(ArchiveBuffer& buffer, const T& type) { buffer.writeBytes(&type, sizeof(type)); } template static void load(ArchiveBuffer& buffer, T& type) { buffer.readBytes(&type, sizeof(type)); } template static void save(ArchiveBuffer& buffer, const T& type) { buffer.writeBytes(&type, sizeof(type)); } template static void load(ArchiveBuffer& buffer, T& type) { buffer.readBytes(&type, sizeof(type)); } template static void save2(ArchiveBuffer& buffer, const T& type) requires(serializable) { type.save(buffer); } template static void load2(ArchiveBuffer& buffer, T& type) requires(serializable) { type.load(buffer); } //template //static void save(ArchiveBuffer& buffer, const T& type) //{ // type->save(buffer); //} //template //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 //static void save(ArchiveBuffer& buffer, const RefPtr& ptr) //{ // Serialization::save(buffer, *ptr.getHandle()); //} //template //static void load(ArchiveBuffer& buffer, RefPtr& ptr) //{ // Serialization::load(buffer, *ptr.getHandle()); //} template static void save(ArchiveBuffer& buffer, const Array& type) { uint64 length = type.size(); buffer.writeBytes(&length, sizeof(uint64)); buffer.writeBytes(type.data(), sizeof(T) * type.size()); } template static void load(ArchiveBuffer& buffer, Array& type) { uint64 length = 0; buffer.readBytes(&length, sizeof(uint64)); type.resize(length); buffer.readBytes(type.data(), sizeof(T) * type.size()); } template static void save(ArchiveBuffer& buffer, const Array& type) { uint64 length = type.size(); buffer.writeBytes(&length, sizeof(uint64)); buffer.writeBytes(type.data(), sizeof(T) * type.size()); } template static void load(ArchiveBuffer& buffer, Array& type) { uint64 length = 0; buffer.readBytes(&length, sizeof(uint64)); type.resize(length); buffer.readBytes(type.data(), sizeof(T) * type.size()); } template static void save(ArchiveBuffer& buffer, const StaticArray& type) { buffer.writeBytes(type.data(), sizeof(T) * N); } template static void load(ArchiveBuffer& buffer, StaticArray& type) { buffer.readBytes(type.data(), sizeof(T) * N); } template static void save(ArchiveBuffer& buffer, const StaticArray& type) { buffer.writeBytes(type.data(), sizeof(T) * N); } template static void load(ArchiveBuffer& buffer, StaticArray& type) { buffer.readBytes(type.data(), sizeof(T) * N); } template static void save(ArchiveBuffer& buffer, const Array& type) requires (!std::is_integral_v && !std::is_floating_point_v) { uint64 length = type.size(); buffer.writeBytes(&length, sizeof(uint64)); for (const auto& x : type) { save(buffer, x); } } template static void load(ArchiveBuffer& buffer, Array& type) { uint64 length = 0; buffer.readBytes(&length, sizeof(uint64)); type.resize(length); for (auto& x : type) { load(buffer, x); } } } // namespace Serialization } // namespace Seele