diff --git a/Engine/Source/Common/Include/Common/Math/Box.h b/Engine/Source/Common/Include/Common/Math/Box.h index 7d8654e6..42909de3 100644 --- a/Engine/Source/Common/Include/Common/Math/Box.h +++ b/Engine/Source/Common/Include/Common/Math/Box.h @@ -55,27 +55,20 @@ namespace Common { namespace Common { // NOLINT template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Box") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Box& value) + static size_t Serialize(SerializeStream& stream, const Box& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer>::Serialize(stream, value.min); - Serializer>::Serialize(stream, value.max); + return Serializer>::Serialize(stream, value.min) + + Serializer>::Serialize(stream, value.max); } - static bool Deserialize(DeserializeStream& stream, Box& value) + static size_t Deserialize(DeserializeStream& stream, Box& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer>::Deserialize(stream, value.min); - Serializer>::Deserialize(stream, value.max); - return true; + return Serializer>::Deserialize(stream, value.min) + + Serializer>::Deserialize(stream, value.max); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Color.h b/Engine/Source/Common/Include/Common/Math/Color.h index 220ec084..3dde1132 100644 --- a/Engine/Source/Common/Include/Common/Math/Color.h +++ b/Engine/Source/Common/Include/Common/Math/Color.h @@ -71,57 +71,43 @@ namespace Common { namespace Common { template <> struct Serializer { - static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Color"); + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Color"); - static void Serialize(SerializeStream& stream, const Color& value) + static size_t Serialize(SerializeStream& stream, const Color& value) { - TypeIdSerializer::Serialize(stream); - - Serializer::Serialize(stream, value.r); - Serializer::Serialize(stream, value.g); - Serializer::Serialize(stream, value.b); - Serializer::Serialize(stream, value.a); + return Serializer::Serialize(stream, value.r) + + Serializer::Serialize(stream, value.g) + + Serializer::Serialize(stream, value.b) + + Serializer::Serialize(stream, value.a); } - static bool Deserialize(DeserializeStream& stream, Color& value) + static size_t Deserialize(DeserializeStream& stream, Color& value) { - if (!TypeIdSerializer::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.r); - Serializer::Deserialize(stream, value.g); - Serializer::Deserialize(stream, value.b); - Serializer::Deserialize(stream, value.a); - return true; + return Serializer::Deserialize(stream, value.r) + + Serializer::Deserialize(stream, value.g) + + Serializer::Deserialize(stream, value.b) + + Serializer::Deserialize(stream, value.a); } }; template <> struct Serializer { - static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::LinearColor"); + static constexpr size_t typeId = HashUtils::StrCrc32("Common::LinearColor"); - static void Serialize(SerializeStream& stream, const LinearColor& value) + static size_t Serialize(SerializeStream& stream, const LinearColor& value) { - TypeIdSerializer::Serialize(stream); - - Serializer::Serialize(stream, value.r); - Serializer::Serialize(stream, value.g); - Serializer::Serialize(stream, value.b); - Serializer::Serialize(stream, value.a); + return Serializer::Serialize(stream, value.r) + + Serializer::Serialize(stream, value.g) + + Serializer::Serialize(stream, value.b) + + Serializer::Serialize(stream, value.a); } - static bool Deserialize(DeserializeStream& stream, LinearColor& value) + static size_t Deserialize(DeserializeStream& stream, LinearColor& value) { - if (!TypeIdSerializer::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.r); - Serializer::Deserialize(stream, value.g); - Serializer::Deserialize(stream, value.b); - Serializer::Deserialize(stream, value.a); - return true; + return Serializer::Deserialize(stream, value.r) + + Serializer::Deserialize(stream, value.g) + + Serializer::Deserialize(stream, value.b) + + Serializer::Deserialize(stream, value.a); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Half.h b/Engine/Source/Common/Include/Common/Math/Half.h index 95bf4d44..5b857106 100644 --- a/Engine/Source/Common/Include/Common/Math/Half.h +++ b/Engine/Source/Common/Include/Common/Math/Half.h @@ -84,45 +84,31 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Internal::FullFloat"); + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Internal::FullFloat"); - static void Serialize(SerializeStream& stream, const Internal::FullFloat& value) + static size_t Serialize(SerializeStream& stream, const Internal::FullFloat& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.value); + return Serializer::Serialize(stream, value.value); } - static bool Deserialize(DeserializeStream& stream, Internal::FullFloat& value) + static size_t Deserialize(DeserializeStream& stream, Internal::FullFloat& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.value); - return true; + return Serializer::Deserialize(stream, value.value); } }; template struct Serializer> { - static constexpr uint32_t typeId = HashUtils::StrCrc32("Common::Internal::HalfFloat"); + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Internal::HalfFloat"); - static void Serialize(SerializeStream& stream, const HalfFloat& value) + static size_t Serialize(SerializeStream& stream, const HalfFloat& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.value); + return Serializer::Serialize(stream, value.value); } - static bool Deserialize(DeserializeStream& stream, HalfFloat& value) + static size_t Deserialize(DeserializeStream& stream, HalfFloat& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.value); - return true; + return Serializer::Deserialize(stream, value.value); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Matrix.h b/Engine/Source/Common/Include/Common/Math/Matrix.h index 173e68ad..b09eee6c 100644 --- a/Engine/Source/Common/Include/Common/Math/Matrix.h +++ b/Engine/Source/Common/Include/Common/Math/Matrix.h @@ -304,29 +304,26 @@ namespace Common { namespace Common { // NOLINT template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Matrix") + Serializer::typeId + (R << 8) + C; - static void Serialize(SerializeStream& stream, const Mat& value) + static size_t Serialize(SerializeStream& stream, const Mat& value) { - TypeIdSerializer>::Serialize(stream); - + auto serialized = 0; for (auto i = 0; i < R * C; i++) { - Serializer::Serialize(stream, value.data[i]); + serialized += Serializer::Serialize(stream, value.data[i]); } + return serialized; } - static bool Deserialize(DeserializeStream& stream, Mat& value) + static size_t Deserialize(DeserializeStream& stream, Mat& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - + auto deserialized = 0; for (auto i = 0; i < R * C; i++) { - Serializer::Deserialize(stream, value.data[i]); + deserialized += Serializer::Deserialize(stream, value.data[i]); } - return true; + return deserialized; } }; diff --git a/Engine/Source/Common/Include/Common/Math/Projection.h b/Engine/Source/Common/Include/Common/Math/Projection.h index e315026d..555e08b5 100644 --- a/Engine/Source/Common/Include/Common/Math/Projection.h +++ b/Engine/Source/Common/Include/Common/Math/Projection.h @@ -61,63 +61,49 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::ReversedZOrthogonalProjection") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const ReversedZOrthogonalProjection& value) + static size_t Serialize(SerializeStream& stream, const ReversedZOrthogonalProjection& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.width); - Serializer::Serialize(stream, value.height); - Serializer::Serialize(stream, value.nearPlane); - Serializer>::Serialize(stream, value.farPlane); + return Serializer::Serialize(stream, value.width) + + Serializer::Serialize(stream, value.height) + + Serializer::Serialize(stream, value.nearPlane) + + Serializer>::Serialize(stream, value.farPlane); } - static bool Deserialize(DeserializeStream& stream, ReversedZOrthogonalProjection& value) + static size_t Deserialize(DeserializeStream& stream, ReversedZOrthogonalProjection& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.width); - Serializer::Deserialize(stream, value.height); - Serializer::Deserialize(stream, value.nearPlane); - Serializer>::Deserialize(stream, value.farPlane); - return true; + return Serializer::Deserialize(stream, value.width) + + Serializer::Deserialize(stream, value.height) + + Serializer::Deserialize(stream, value.nearPlane) + + Serializer>::Deserialize(stream, value.farPlane); } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::ReversedZPerspectiveProjection") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const ReversedZPerspectiveProjection& value) + static size_t Serialize(SerializeStream& stream, const ReversedZPerspectiveProjection& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.fov); - Serializer::Serialize(stream, value.width); - Serializer::Serialize(stream, value.height); - Serializer::Serialize(stream, value.nearPlane); - Serializer>::Serialize(stream, value.farPlane); + return Serializer::Serialize(stream, value.fov) + + Serializer::Serialize(stream, value.width) + + Serializer::Serialize(stream, value.height) + + Serializer::Serialize(stream, value.nearPlane) + + Serializer>::Serialize(stream, value.farPlane); } - static bool Deserialize(DeserializeStream& stream, ReversedZPerspectiveProjection& value) + static size_t Deserialize(DeserializeStream& stream, ReversedZPerspectiveProjection& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.fov); - Serializer::Deserialize(stream, value.width); - Serializer::Deserialize(stream, value.height); - Serializer::Deserialize(stream, value.nearPlane); - Serializer>::Deserialize(stream, value.farPlane); - return true; + return Serializer::Deserialize(stream, value.fov) + + Serializer::Deserialize(stream, value.width) + + Serializer::Deserialize(stream, value.height) + + Serializer::Deserialize(stream, value.nearPlane) + + Serializer>::Deserialize(stream, value.farPlane); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Quaternion.h b/Engine/Source/Common/Include/Common/Math/Quaternion.h index 45c2d6fc..c7cb3207 100644 --- a/Engine/Source/Common/Include/Common/Math/Quaternion.h +++ b/Engine/Source/Common/Include/Common/Math/Quaternion.h @@ -123,79 +123,58 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Angle") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Angle& value) + static size_t Serialize(SerializeStream& stream, const Angle& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.value); + return Serializer::Serialize(stream, value.value); } - static bool Deserialize(DeserializeStream& stream, Angle& value) + static size_t Deserialize(DeserializeStream& stream, Angle& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.value); - return true; + return Serializer::Deserialize(stream, value.value); } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Radian") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Radian& value) + static size_t Serialize(SerializeStream& stream, const Radian& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.value); + return Serializer::Serialize(stream, value.value); } - static bool Deserialize(DeserializeStream& stream, Radian& value) + static size_t Deserialize(DeserializeStream& stream, Radian& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.value); - return true; + return Serializer::Deserialize(stream, value.value); } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Quaternion") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Quaternion& value) + static size_t Serialize(SerializeStream& stream, const Quaternion& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.x); - Serializer::Serialize(stream, value.y); - Serializer::Serialize(stream, value.z); - Serializer::Serialize(stream, value.w); + return Serializer::Serialize(stream, value.x) + + Serializer::Serialize(stream, value.y) + + Serializer::Serialize(stream, value.z) + + Serializer::Serialize(stream, value.w); } - static bool Deserialize(DeserializeStream& stream, Quaternion& value) + static size_t Deserialize(DeserializeStream& stream, Quaternion& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.x); - Serializer::Deserialize(stream, value.y); - Serializer::Deserialize(stream, value.z); - Serializer::Deserialize(stream, value.w); - return true; + return Serializer::Deserialize(stream, value.x) + + Serializer::Deserialize(stream, value.y) + + Serializer::Deserialize(stream, value.z) + + Serializer::Deserialize(stream, value.w); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Rect.h b/Engine/Source/Common/Include/Common/Math/Rect.h index 55466f32..86938445 100644 --- a/Engine/Source/Common/Include/Common/Math/Rect.h +++ b/Engine/Source/Common/Include/Common/Math/Rect.h @@ -53,27 +53,20 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Rect") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Rect& value) + static size_t Serialize(SerializeStream& stream, const Rect& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer>::Serialize(stream, value.min); - Serializer>::Serialize(stream, value.max); + return Serializer>::Serialize(stream, value.min) + + Serializer>::Serialize(stream, value.max); } - static bool Deserialize(DeserializeStream& stream, Rect& value) + static size_t Deserialize(DeserializeStream& stream, Rect& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer>::Deserialize(stream, value.min); - Serializer>::Deserialize(stream, value.max); - return true; + return Serializer>::Deserialize(stream, value.min) + + Serializer>::Deserialize(stream, value.max); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Sphere.h b/Engine/Source/Common/Include/Common/Math/Sphere.h index c816c7b3..f5cee357 100644 --- a/Engine/Source/Common/Include/Common/Math/Sphere.h +++ b/Engine/Source/Common/Include/Common/Math/Sphere.h @@ -41,27 +41,20 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Sphere") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Sphere& value) + static size_t Serialize(SerializeStream& stream, const Sphere& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer>::Serialize(stream, value.center); - Serializer::Serialize(stream, value.radius); + return Serializer>::Serialize(stream, value.center) + + Serializer::Serialize(stream, value.radius); } - static bool Deserialize(DeserializeStream& stream, Sphere& value) + static size_t Deserialize(DeserializeStream& stream, Sphere& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer>::Deserialize(stream, value.center); - Serializer::Deserialize(stream, value.radius); - return true; + return Serializer>::Deserialize(stream, value.center) + + Serializer::Deserialize(stream, value.radius); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Transform.h b/Engine/Source/Common/Include/Common/Math/Transform.h index 9425a675..7d5832d0 100644 --- a/Engine/Source/Common/Include/Common/Math/Transform.h +++ b/Engine/Source/Common/Include/Common/Math/Transform.h @@ -69,29 +69,22 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::Transform") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const Transform& value) + static size_t Serialize(SerializeStream& stream, const Transform& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer>::Serialize(stream, value.scale); - Serializer>::Serialize(stream, value.rotation); - Serializer>::Serialize(stream, value.translation); + return Serializer>::Serialize(stream, value.scale) + + Serializer>::Serialize(stream, value.rotation) + + Serializer>::Serialize(stream, value.translation); } - static bool Deserialize(DeserializeStream& stream, Transform& value) + static size_t Deserialize(DeserializeStream& stream, Transform& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer>::Deserialize(stream, value.scale); - Serializer>::Deserialize(stream, value.rotation); - Serializer>::Deserialize(stream, value.translation); - return true; + return Serializer>::Deserialize(stream, value.scale) + + Serializer>::Deserialize(stream, value.rotation) + + Serializer>::Deserialize(stream, value.translation); } }; diff --git a/Engine/Source/Common/Include/Common/Math/Vector.h b/Engine/Source/Common/Include/Common/Math/Vector.h index 834ee14e..aa400b1f 100644 --- a/Engine/Source/Common/Include/Common/Math/Vector.h +++ b/Engine/Source/Common/Include/Common/Math/Vector.h @@ -216,30 +216,27 @@ namespace Common::Internal { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = Common::HashUtils::StrCrc32("Common::Vector") + Serializer::typeId + L; - static void Serialize(SerializeStream& stream, const Vec& value) + static size_t Serialize(SerializeStream& stream, const Vec& value) { - TypeIdSerializer>::Serialize(stream); - + size_t serialized = 0; for (auto i = 0; i < L; i++) { - Serializer::Serialize(stream, value.data[i]); + serialized += Serializer::Serialize(stream, value.data[i]); } + return serialized; } - static bool Deserialize(DeserializeStream& stream, Vec& value) + static size_t Deserialize(DeserializeStream& stream, Vec& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - + size_t deserialized = 0; for (auto i = 0; i < L; i++) { - Serializer::Deserialize(stream, value.data[i]); + deserialized += Serializer::Deserialize(stream, value.data[i]); } - return true; + return deserialized; } }; diff --git a/Engine/Source/Common/Include/Common/Math/View.h b/Engine/Source/Common/Include/Common/Math/View.h index 0933049b..92cf2fa1 100644 --- a/Engine/Source/Common/Include/Common/Math/View.h +++ b/Engine/Source/Common/Include/Common/Math/View.h @@ -32,25 +32,18 @@ namespace Common { namespace Common { template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("Common::ViewTransform") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const ViewTransform& value) + static size_t Serialize(SerializeStream& stream, const ViewTransform& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer>::Serialize(stream, value); + return Serializer>::Serialize(stream, value); } - static bool Deserialize(DeserializeStream& stream, ViewTransform& value) + static size_t Deserialize(DeserializeStream& stream, ViewTransform& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer>::Deserialize(stream, value); - return true; + return Serializer>::Deserialize(stream, value); } }; diff --git a/Engine/Source/Common/Include/Common/Serialization.h b/Engine/Source/Common/Include/Common/Serialization.h index 423384b7..540f4eed 100644 --- a/Engine/Source/Common/Include/Common/Serialization.h +++ b/Engine/Source/Common/Include/Common/Serialization.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -26,6 +27,7 @@ namespace Common { virtual void Write(const void* data, size_t size) = 0; virtual void Seek(int64_t offset) = 0; + virtual size_t Loc() = 0; protected: SerializeStream(); @@ -38,6 +40,7 @@ namespace Common { virtual void Read(void* data, size_t size) = 0; virtual void Seek(int64_t offset) = 0; + virtual size_t Loc() = 0; protected: DeserializeStream(); @@ -50,6 +53,7 @@ namespace Common { ~BinaryFileSerializeStream() override; void Write(const void* data, size_t size) override; void Seek(int64_t offset) override; + size_t Loc() override; void Close(); private: @@ -63,10 +67,12 @@ namespace Common { ~BinaryFileDeserializeStream() override; void Read(void* data, size_t size) override; void Seek(int64_t offset) override; + size_t Loc() override; void Close(); private: std::ifstream file; + size_t fileSize; }; class ByteSerializeStream final : public SerializeStream { @@ -76,6 +82,7 @@ namespace Common { ~ByteSerializeStream() override; void Write(const void* data, size_t size) override; void Seek(int64_t offset) override; + size_t Loc() override; private: size_t pointer; @@ -89,6 +96,7 @@ namespace Common { ~ByteDeserializeStream() override; void Read(void* data, size_t size) override; void Seek(int64_t offset) override; + size_t Loc() override; private: size_t pointer; @@ -99,18 +107,14 @@ namespace Common { template concept Serializable = requires(T inValue, SerializeStream& serializeStream, DeserializeStream& deserializeStream) { { Serializer::typeId } -> std::convertible_to; - Serializer::Serialize(serializeStream, inValue); - Serializer::Deserialize(deserializeStream, inValue); + { Serializer::Serialize(serializeStream, inValue) } -> std::convertible_to; + { Serializer::Deserialize(deserializeStream, inValue) } -> std::convertible_to; }; - template - struct TypeIdSerializer { - static void Serialize(SerializeStream& stream); - static bool Deserialize(DeserializeStream& stream); - }; + template struct FieldSerializer; - template void Serialize(SerializeStream& inStream, const T& inValue); - template bool Deserialize(DeserializeStream& inStream, T& inValue); + template size_t Serialize(SerializeStream& inStream, const T& inValue); + template std::pair Deserialize(DeserializeStream& inStream, T& inValue); template struct JsonSerializer {}; template concept JsonSerializable = requires( @@ -129,58 +133,41 @@ namespace Common { #define IMPL_BASIC_TYPE_SERIALIZER(typeName) \ template <> \ struct Serializer { \ - static constexpr uint32_t typeId = HashUtils::StrCrc32(#typeName); \ + static constexpr size_t typeId = HashUtils::StrCrc32(#typeName); \ \ - static void Serialize(SerializeStream& stream, const typeName& value) \ + static size_t Serialize(SerializeStream& stream, const typeName& value) \ { \ - TypeIdSerializer::Serialize(stream); \ stream.Write(&value, sizeof(typeName)); \ + return sizeof(typeName); \ } \ \ - static bool Deserialize(DeserializeStream& stream, typeName& value) \ + static size_t Deserialize(DeserializeStream& stream, typeName& value) \ { \ - if (!TypeIdSerializer::Deserialize(stream)) { \ - return false;\ - } \ stream.Read(&value, sizeof(typeName)); \ - return true; \ + return sizeof(typeName); \ } \ }; \ namespace Common { - template - void TypeIdSerializer::Serialize(SerializeStream& stream) - { - const uint32_t typeId = Serializer::typeId; - stream.Write(&typeId, sizeof(uint32_t)); - } - - template - bool TypeIdSerializer::Deserialize(DeserializeStream& stream) - { - uint32_t typeId; - stream.Read(&typeId, sizeof(uint32_t)); - return typeId == Serializer::typeId; - } - template - void Serialize(SerializeStream& inStream, const T& inValue) + size_t Serialize(SerializeStream& inStream, const T& inValue) { if constexpr (Serializable) { - Serializer::Serialize(inStream, inValue); + return FieldSerializer::Serialize(inStream, inValue); } else { QuickFailWithReason("your type is not support serialization"); + return 0; } } template - bool Deserialize(DeserializeStream& inStream, T& inValue) + std::pair Deserialize(DeserializeStream& inStream, T& inValue) { if constexpr (Serializable) { - return Serializer::Deserialize(inStream, inValue); + return FieldSerializer::Deserialize(inStream, inValue); } else { QuickFailWithReason("your type is not support serialization"); - return false; + return { false, 0 }; } } @@ -204,6 +191,45 @@ namespace Common { } } + template + struct FieldSerializer { + struct Header { + size_t typeId; + size_t contentSize; + }; + + static size_t Serialize(SerializeStream& stream, const T& value) + { + Header header; + header.typeId = Serializer::typeId; + + stream.Seek(sizeof(Header)); + header.contentSize = Serializer::Serialize(stream, value); + stream.Seek(-static_cast(sizeof(Header)) - static_cast(header.contentSize)); + stream.Write(&header, sizeof(Header)); + stream.Seek(header.contentSize); + return sizeof(Header) + header.contentSize; + } + + static std::pair Deserialize(DeserializeStream& stream, T& value) + { + Header header {}; + stream.Read(&header, sizeof(Header)); + + if (header.typeId != Serializer::typeId) { + stream.Seek(header.contentSize); + return { false, sizeof(Header) }; + } + + size_t deserializedSize = Serializer::Deserialize(stream, value); + if (deserializedSize != header.contentSize) { + stream.Seek(header.contentSize - deserializedSize); + return { false, sizeof(Header) + deserializedSize }; + } + return { true, sizeof(Header) + header.contentSize }; + } + }; + IMPL_BASIC_TYPE_SERIALIZER(bool) IMPL_BASIC_TYPE_SERIALIZER(int8_t) IMPL_BASIC_TYPE_SERIALIZER(uint8_t) @@ -218,212 +244,199 @@ namespace Common { template <> struct Serializer { - static constexpr uint32_t typeId = HashUtils::StrCrc32("string"); + static constexpr size_t typeId = HashUtils::StrCrc32("string"); - static void Serialize(SerializeStream& stream, const std::string& value) + static size_t Serialize(SerializeStream& stream, const std::string& value) { - TypeIdSerializer::Serialize(stream); + size_t serialized = 0; const uint64_t size = value.size(); - Serializer::Serialize(stream, size); + serialized += Serializer::Serialize(stream, size); + stream.Write(value.data(), value.size()); + serialized += size; + return serialized; } - static bool Deserialize(DeserializeStream& stream, std::string& value) + static size_t Deserialize(DeserializeStream& stream, std::string& value) { - if (!TypeIdSerializer::Deserialize(stream)) { - return false; - } + size_t deserialized = 0; uint64_t size; - Serializer::Deserialize(stream, size); + deserialized += Serializer::Deserialize(stream, size); + value.resize(size); stream.Read(value.data(), size); - return true; + deserialized += size; + return deserialized; } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("std::optional") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const std::optional& value) + static size_t Serialize(SerializeStream& stream, const std::optional& value) { - TypeIdSerializer>::Serialize(stream); + size_t serialized = 0; const bool hasValue = value.has_value(); - Serializer::Serialize(stream, hasValue); + serialized += Serializer::Serialize(stream, hasValue); if (hasValue) { - Serializer::Serialize(stream, value.value()); + serialized += Serializer::Serialize(stream, value.value()); } + return serialized; } - static bool Deserialize(DeserializeStream& stream, std::optional& value) + static size_t Deserialize(DeserializeStream& stream, std::optional& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } + size_t deserialized = 0; value.reset(); bool hasValue; - Serializer::Deserialize(stream, hasValue); + deserialized += Serializer::Deserialize(stream, hasValue); if (hasValue) { T temp; - Serializer::Deserialize(stream, temp); + deserialized += Serializer::Deserialize(stream, temp); value.emplace(std::move(temp)); } - return true; + return deserialized; } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("std::pair") + Serializer::typeId + Serializer::typeId; - static void Serialize(SerializeStream& stream, const std::pair& value) + static size_t Serialize(SerializeStream& stream, const std::pair& value) { - TypeIdSerializer>::Serialize(stream); - - Serializer::Serialize(stream, value.first); - Serializer::Serialize(stream, value.second); + return Serializer::Serialize(stream, value.first) + + Serializer::Serialize(stream, value.second); } - static bool Deserialize(DeserializeStream& stream, std::pair& value) + static size_t Deserialize(DeserializeStream& stream, std::pair& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - - Serializer::Deserialize(stream, value.first); - Serializer::Deserialize(stream, value.second); - return true; + return Serializer::Deserialize(stream, value.first) + + Serializer::Deserialize(stream, value.second); } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("std::vector") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const std::vector& value) + static size_t Serialize(SerializeStream& stream, const std::vector& value) { - TypeIdSerializer>::Serialize(stream); + size_t serialized = 0; const uint64_t size = value.size(); - Serializer::Serialize(stream, size); + serialized += Serializer::Serialize(stream, size); for (auto i = 0; i < size; i++) { - Serializer::Serialize(stream, value[i]); + serialized += Serializer::Serialize(stream, value[i]); } + return serialized; } - static bool Deserialize(DeserializeStream& stream, std::vector& value) + static size_t Deserialize(DeserializeStream& stream, std::vector& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } + size_t deserialized = 0; value.clear(); - uint64_t size; - Serializer::Deserialize(stream, size); + deserialized += Serializer::Deserialize(stream, size); value.reserve(size); for (auto i = 0; i < size; i++) { T element; - Serializer::Deserialize(stream, element); + deserialized += Serializer::Deserialize(stream, element); value.emplace_back(std::move(element)); } - return true; + return deserialized; } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("std::unordered_set") + Serializer::typeId; - static void Serialize(SerializeStream& stream, const std::unordered_set& value) + static size_t Serialize(SerializeStream& stream, const std::unordered_set& value) { - TypeIdSerializer::Serialize(stream); + size_t serialized = 0; const uint64_t size = value.size(); - Serializer::Serialize(stream, size); + serialized += Serializer::Serialize(stream, size); for (const auto& element : value) { - Serializer::Serialize(stream, element); + serialized += Serializer::Serialize(stream, element); } + return serialized; } - static bool Deserialize(DeserializeStream& stream, std::unordered_set& value) + static size_t Deserialize(DeserializeStream& stream, std::unordered_set& value) { - if (!TypeIdSerializer::Deserialize(stream)) { - return false; - } + size_t deserialized = 0; value.clear(); - uint64_t size; - Serializer::Deserialize(stream, size); + deserialized += Serializer::Deserialize(stream, size); value.reserve(size); for (auto i = 0; i < size; i++) { T temp; - Serializer::Deserialize(stream, temp); + deserialized += Serializer::Deserialize(stream, temp); value.emplace(std::move(temp)); } - return true; + return deserialized; } }; template struct Serializer> { - static constexpr uint32_t typeId + static constexpr size_t typeId = HashUtils::StrCrc32("std::unordered_map") + Serializer::typeId + Serializer::typeId; - static void Serialize(SerializeStream& stream, const std::unordered_map& value) + static size_t Serialize(SerializeStream& stream, const std::unordered_map& value) { - TypeIdSerializer>::Serialize(stream); + size_t serialized = 0; const uint64_t size = value.size(); - Serializer::Serialize(stream, size); + serialized += Serializer::Serialize(stream, size); for (const auto& pair : value) { - Serializer::Serialize(stream, pair.first); - Serializer::Serialize(stream, pair.second); + serialized += Serializer>::Serialize(stream, pair); } + return serialized; } - static bool Deserialize(DeserializeStream& stream, std::unordered_map& value) + static size_t Deserialize(DeserializeStream& stream, std::unordered_map& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } + size_t deserialized = 0; value.clear(); - uint64_t size; - Serializer::Deserialize(stream, size); + deserialized += Serializer::Deserialize(stream, size); value.reserve(size); for (auto i = 0; i < size; i++) { std::pair pair; - Serializer::Deserialize(stream, pair.first); - Serializer::Deserialize(stream, pair.second); + deserialized += Serializer>::Deserialize(stream, pair); value.emplace(std::move(pair)); } - return true; + return deserialized; } }; diff --git a/Engine/Source/Common/Src/Serialization.cpp b/Engine/Source/Common/Src/Serialization.cpp index 74eed087..9819759e 100644 --- a/Engine/Source/Common/Src/Serialization.cpp +++ b/Engine/Source/Common/Src/Serialization.cpp @@ -39,6 +39,11 @@ namespace Common { file.seekp(offset, std::ios::cur); } + size_t BinaryFileSerializeStream::Loc() + { + return file.tellp(); + } + void BinaryFileSerializeStream::Close() { if (!file.is_open()) { @@ -54,6 +59,9 @@ namespace Common { BinaryFileDeserializeStream::BinaryFileDeserializeStream(const std::string& inFileName) : file(inFileName, std::ios::binary) { + file.seekg(0, std::ios::end); + fileSize = file.tellg(); + file.seekg(0, std::ios::beg); } BinaryFileDeserializeStream::~BinaryFileDeserializeStream() @@ -63,6 +71,7 @@ namespace Common { void BinaryFileDeserializeStream::Read(void* data, const size_t size) { + Assert(static_cast(file.tellg()) + size <= fileSize); file.read(static_cast(data), static_cast(size)); } @@ -71,6 +80,11 @@ namespace Common { file.seekg(offset, std::ios::cur); } + size_t BinaryFileDeserializeStream::Loc() + { + return file.tellg(); + } + void BinaryFileDeserializeStream::Close() { if (!file.is_open()) { @@ -110,6 +124,11 @@ namespace Common { pointer += offset; } + size_t ByteSerializeStream::Loc() + { + return pointer; + } + ByteDeserializeStream::ByteDeserializeStream(const std::vector& inBytes, const size_t pointerBegin) : pointer(pointerBegin) , bytes(inBytes) @@ -131,4 +150,9 @@ namespace Common { { pointer += offset; } + + size_t ByteDeserializeStream::Loc() + { + return pointer; + } } diff --git a/Engine/Source/Core/Include/Core/Uri.h b/Engine/Source/Core/Include/Core/Uri.h index 1e67a3fa..9cee5daf 100644 --- a/Engine/Source/Core/Include/Core/Uri.h +++ b/Engine/Source/Core/Include/Core/Uri.h @@ -65,25 +65,19 @@ namespace std { // NOLINT namespace Common { // NOLINT template <> struct Serializer { - static constexpr uint32_t typeId = HashUtils::StrCrc32("Core::Uri"); + static constexpr size_t typeId = HashUtils::StrCrc32("Core::Uri"); - static void Serialize(SerializeStream& stream, const Core::Uri& value) + static size_t Serialize(SerializeStream& stream, const Core::Uri& value) { - TypeIdSerializer::Serialize(stream); - - Serializer::Serialize(stream, value.Str()); + return Serializer::Serialize(stream, value.Str()); } - static bool Deserialize(DeserializeStream& stream, Core::Uri& value) + static size_t Deserialize(DeserializeStream& stream, Core::Uri& value) { - if (!TypeIdSerializer::Deserialize(stream)) { - return false; - } - std::string str; - Serializer::Deserialize(stream, str); + const auto deserialized = Serializer::Deserialize(stream, str); value = str; - return true; + return deserialized; } }; } diff --git a/Engine/Source/Mirror/Include/Mirror/Mirror.h b/Engine/Source/Mirror/Include/Mirror/Mirror.h index 92b2085b..ce06cea9 100644 --- a/Engine/Source/Mirror/Include/Mirror/Mirror.h +++ b/Engine/Source/Mirror/Include/Mirror/Mirror.h @@ -91,8 +91,8 @@ namespace Mirror { using GetPtrFunc = Any(void*); using GetConstPtrFunc = Any(const void*); using DerefFunc = Any(const void*); - using SerializeFunc = void(const void*, Common::SerializeStream&); - using DeserializeFunc = bool(void*, Common::DeserializeStream&); + using SerializeFunc = size_t(const void*, Common::SerializeStream&); + using DeserializeFunc = std::pair(void*, Common::DeserializeStream&); using JsonSerializeFunc = void(const void*, rapidjson::Value&, rapidjson::Document::AllocatorType&); using JsonDeserializeFunc = void(void*, const rapidjson::Value&); using ToStringFunc = std::string(const void*); @@ -111,8 +111,8 @@ namespace Mirror { template static Any GetPtr(void* inThis); template static Any GetConstPtr(const void* inThis); template static Any Deref(const void* inThis); - template static void Serialize(const void* inThis, Common::SerializeStream& inStream); - template static bool Deserialize(void* inThis, Common::DeserializeStream& inStream); + template static size_t Serialize(const void* inThis, Common::SerializeStream& inStream); + template static std::pair Deserialize(void* inThis, Common::DeserializeStream& inStream); template static void JsonSerialize(const void* inThis, rapidjson::Value& outJsonValue, rapidjson::Document::AllocatorType& inAllocator); template static void JsonDeserialize(void* inThis, const rapidjson::Value& inJsonValue); template static std::string ToString(const void* inThis); @@ -222,9 +222,9 @@ namespace Mirror { Mirror::TypeId TypeId() const; void Reset(); bool Empty() const; - void Serialize(Common::SerializeStream& inStream) const; - bool Deserialize(Common::DeserializeStream& inStream); - bool Deserialize(Common::DeserializeStream& inStream) const; + size_t Serialize(Common::SerializeStream& inStream) const; + std::pair Deserialize(Common::DeserializeStream& inStream); + std::pair Deserialize(Common::DeserializeStream& inStream) const; void JsonSerialize(rapidjson::Value& outJsonValue, rapidjson::Document::AllocatorType& inAllocator) const; void JsonDeserialize(const rapidjson::Value& inJsonValue); void JsonDeserialize(const rapidjson::Value& inJsonValue) const; @@ -768,8 +768,8 @@ namespace Mirror::Internal { { using RawType = std::remove_cvref_t; static_assert( - !std::is_same_v && !std::is_same_v && !std::is_same_v, - "static version reflection method do no support use Any/Any*/const Any* as argument, please check you arguments"); + !std::is_same_v && !std::is_same_v && !std::is_same_v && !std::is_same_v, + "static version reflection method do no support use Any/Any*/const Any*/Argument as argument, please check you arguments"); } template @@ -805,116 +805,125 @@ namespace Mirror::Internal { namespace Common { // NOLINT template struct Serializer { - static constexpr uint32_t typeId = HashUtils::StrCrc32("_MetaObject"); - - // TODO need process serialize failed - static void SerializeDyn(SerializeStream& stream, const Mirror::Class& clazz, const Mirror::Argument& obj) + static constexpr size_t typeId = HashUtils::StrCrc32("_MetaObject"); + + // struct + // std::string className : classNameSize + // size_t baseContentSize : sizeof(size_t) + // void* baseContent : baseContentSize + // size_t memberVariableCount : sizeof(size_t) + // size_t[] memberVariableContentEnds : sizeof(size_t) * memberVariableCount + // void*[] memberVariableContent : memberVariablesContentSize + // |- std::string memberVariableName : memberVariableNameSize + // |- bool sameAsDefaultObject : sizeof(bool) + // |- void* memberVariableContent : memberVariableEnd - memberVariableLastEnd + + static size_t SerializeDyn(SerializeStream& stream, const Mirror::Class& clazz, const Mirror::Argument& obj) { - if (const auto* baseClass = clazz.GetBaseClass(); - baseClass != nullptr) { - SerializeDyn(stream, *baseClass, obj); - } - + const auto& className = clazz.GetName(); + const auto* baseClass = clazz.GetBaseClass(); + const auto& memberVariables = clazz.GetMemberVariables(); const auto defaultObject = clazz.GetDefaultObject(); - AssertWithReason(!defaultObject.Empty(), "do you forget add default constructor to EClass() which you want to serialize ?"); - const auto& name = clazz.GetName(); - const auto& memberVariables = clazz.GetMemberVariables(); + const auto classNameSize = Serializer::Serialize(stream, className); - const auto memberVariablesNum = memberVariables.size(); - Serializer::Serialize(stream, name); - Serializer::Serialize(stream, memberVariablesNum); + uint64_t baseClassContentSize = 0; + stream.Seek(sizeof(uint64_t)); + if (baseClass != nullptr) { + baseClassContentSize = SerializeDyn(stream, *baseClass, obj); + } - for (const auto& [id, memberVariable] : memberVariables) { - if (memberVariable.IsTransient()) { - continue; - } + stream.Seek(-static_cast(baseClassContentSize) - static_cast(sizeof(uint64_t))); + Serializer::Serialize(stream, baseClassContentSize); + stream.Seek(static_cast(baseClassContentSize)); - Serializer::Serialize(stream, id.name); + const uint64_t memberVariableCount = memberVariables.size(); + std::vector memberVariableContentEnds; + memberVariableContentEnds.reserve(memberVariableCount); - const bool sameWithDefaultObject = memberVariable.GetDyn(obj) == memberVariable.GetDyn(defaultObject); - Serializer::Serialize(stream, sameWithDefaultObject); + stream.Seek(static_cast(sizeof(uint64_t) * (memberVariableCount + 1))); + uint64_t memberVariableContentSize = 0; + for (const auto& [id, memberVariable] : memberVariables) { + const bool sameAsDefaultObject = defaultObject.Empty() ? false : memberVariable.GetDyn(obj) == memberVariable.GetDyn(defaultObject); - if (sameWithDefaultObject) { - Serializer::Serialize(stream, 0); - } else { - Serializer::Serialize(stream, memberVariable.SizeOf()); - memberVariable.GetDyn(obj).Serialize(stream); + memberVariableContentSize += Serializer::Serialize(stream, memberVariable.GetName()); + memberVariableContentSize += Serializer::Serialize(stream, sameAsDefaultObject); + if (!sameAsDefaultObject) { + memberVariableContentSize += memberVariable.GetDyn(obj).Serialize(stream); } + memberVariableContentEnds.emplace_back(memberVariableContentSize); } - if (clazz.HasMemberFunction("OnSerialized")) { - (void) clazz.GetMemberFunction("OnSerialized").InvokeDyn(obj, {}); + stream.Seek(-static_cast(memberVariableContentSize) - static_cast(sizeof(uint64_t) * (memberVariableCount + 1))); + Serializer::Serialize(stream, memberVariableCount); + for (const auto& end : memberVariableContentEnds) { + Serializer::Serialize(stream, end); } + stream.Seek(static_cast(memberVariableContentSize)); + return classNameSize + baseClassContentSize + sizeof(uint64_t) * (memberVariableCount + 2) + memberVariableContentSize; // NOLINT } - static bool DeserializeDyn(DeserializeStream& stream, const Mirror::Class& clazz, const Mirror::Argument& obj) + static size_t DeserializeDyn(DeserializeStream& stream, const Mirror::Class& clazz, const Mirror::Argument& obj) { - if (const auto* baseClass = clazz.GetBaseClass(); - baseClass != nullptr) { - DeserializeDyn(stream, *baseClass, obj); - } - + const auto& className = clazz.GetName(); + const auto* baseClass = clazz.GetBaseClass(); + const auto& memberVariables = clazz.GetMemberVariables(); const auto defaultObject = clazz.GetDefaultObject(); - AssertWithReason(!defaultObject.Empty(), "do you forget add default constructor to EClass() which you want to serialize ?"); - - std::string className; - Serializer::Deserialize(stream, className); - uint64_t memberVariableSize; - Serializer::Deserialize(stream, memberVariableSize); + std::string name; + const auto nameSize = Serializer::Deserialize(stream, name); - uint32_t failedCount = 0; - const auto& memberVariables = clazz.GetMemberVariables(); - for (auto i = 0; i < memberVariableSize; i++) { - std::string varName; - Serializer::Deserialize(stream, varName); + uint64_t aspectBaseClassContentSize = 0; + Serializer::Deserialize(stream, aspectBaseClassContentSize); + if (aspectBaseClassContentSize != 0 && baseClass != nullptr) { + const auto actualBaseClassContentSize = DeserializeDyn(stream, *baseClass, obj); + stream.Seek(static_cast(aspectBaseClassContentSize) - static_cast(actualBaseClassContentSize)); + } - auto iter = memberVariables.find(varName); - if (iter == memberVariables.end()) { - failedCount++; - continue; - } + uint64_t memberVariableCount = 0; + Serializer::Deserialize(stream, memberVariableCount); - const auto& memberVariable = iter->second; - if (memberVariable.IsTransient()) { - continue; - } + std::vector memberVariableEnds; + memberVariableEnds.resize(memberVariableCount); + for (auto& offset : memberVariableEnds) { + Serializer::Deserialize(stream, offset); + } - bool restoreAsDefaultObject = true; - Serializer::Deserialize(stream, restoreAsDefaultObject); + uint64_t memberVariableContentCur = 0; + for (const auto& end : memberVariableEnds) { + std::string memberVariableName; + memberVariableContentCur += Serializer::Deserialize(stream, memberVariableName); - uint32_t memorySize = 0; - Serializer::Deserialize(stream, memorySize); - if (memorySize == 0 || memorySize != memberVariable.SizeOf()) { - failedCount++; - restoreAsDefaultObject = true; + if (!clazz.HasMemberVariable(memberVariableName)) { + stream.Seek(static_cast(end) - static_cast(memberVariableContentCur)); + memberVariableContentCur = end; + continue; } - - if (restoreAsDefaultObject) { - memberVariable.SetDyn(obj, memberVariable.GetDyn(defaultObject)); - } else { - memberVariable.GetDyn(obj).Deserialize(stream); + const auto& memberVariable = clazz.GetMemberVariable(memberVariableName); + + bool sameAsDefaultObject = false; + memberVariableContentCur += Serializer::Deserialize(stream, sameAsDefaultObject); + if (sameAsDefaultObject) { + if (!defaultObject.Empty()) { + memberVariable.SetDyn(obj, memberVariable.GetDyn(obj)); + } + continue; } - } - if (clazz.HasMemberFunction("OnDeserialize")) { - (void) clazz.GetMemberFunction("OnDeserialize").InvokeDyn(obj, {}); + memberVariableContentCur += memberVariable.GetDyn(obj).Deserialize(stream).second; + stream.Seek(static_cast(end) - static_cast(memberVariableContentCur)); + memberVariableContentCur = end; } - return failedCount == 0; + return nameSize + aspectBaseClassContentSize + sizeof(uint64_t) * (memberVariableCount + 2) + memberVariableContentCur; } - static void Serialize(SerializeStream& stream, const T& value) + static size_t Serialize(SerializeStream& stream, const T& value) { - TypeIdSerializer::Serialize(stream); - SerializeDyn(stream, Mirror::Class::Get(), Mirror::Internal::ForwardAsArgument(value)); + return SerializeDyn(stream, Mirror::Class::Get(), Mirror::Internal::ForwardAsArgument(value)); } - static bool Deserialize(DeserializeStream& stream, T& value) + static size_t Deserialize(DeserializeStream& stream, T& value) { - if (!TypeIdSerializer::Deserialize(stream)) { - return false; - } return DeserializeDyn(stream, Mirror::Class::Get(), Mirror::Internal::ForwardAsArgument(value)); } }; @@ -1094,13 +1103,13 @@ namespace Mirror { } template - void AnyRtti::Serialize(const void* inThis, Common::SerializeStream& inStream) + size_t AnyRtti::Serialize(const void* inThis, Common::SerializeStream& inStream) { - Common::Serialize(inStream, *static_cast(inThis)); + return Common::Serialize(inStream, *static_cast(inThis)); } template - bool AnyRtti::Deserialize(void* inThis, Common::DeserializeStream& inStream) + std::pair AnyRtti::Deserialize(void* inThis, Common::DeserializeStream& inStream) { return Common::Deserialize(inStream, *static_cast(inThis)); } diff --git a/Engine/Source/Mirror/Src/Mirror.cpp b/Engine/Source/Mirror/Src/Mirror.cpp index 52822768..117d880c 100644 --- a/Engine/Source/Mirror/Src/Mirror.cpp +++ b/Engine/Source/Mirror/Src/Mirror.cpp @@ -394,19 +394,19 @@ namespace Mirror { return rtti == nullptr; } - void Any::Serialize(Common::SerializeStream& inStream) const + size_t Any::Serialize(Common::SerializeStream& inStream) const { Assert(rtti != nullptr); - rtti->serialize(Data(), inStream); + return rtti->serialize(Data(), inStream); } - bool Any::Deserialize(Common::DeserializeStream& inStream) + std::pair Any::Deserialize(Common::DeserializeStream& inStream) { Assert(rtti != nullptr && !IsConstRef()); return rtti->deserialize(Data(), inStream); } - bool Any::Deserialize(Common::DeserializeStream& inStream) const + std::pair Any::Deserialize(Common::DeserializeStream& inStream) const { Assert(rtti != nullptr && IsNonConstRef()); return rtti->deserialize(Data(), inStream); diff --git a/Engine/Source/Mirror/Test/SerializationTest.cpp b/Engine/Source/Mirror/Test/SerializationTest.cpp index e8646694..1c2e24e0 100644 --- a/Engine/Source/Mirror/Test/SerializationTest.cpp +++ b/Engine/Source/Mirror/Test/SerializationTest.cpp @@ -13,7 +13,7 @@ int ga = 0; float gb = 0.0f; std::string gc; -TEST(SerializationTest, VariableFileSerializationTest) +TEST(SerializationTest, VariableFileTest) { static std::filesystem::path fileName = "../Test/Generated/Mirror/SerializationTest.VariableFileSerializationTest.bin"; std::filesystem::create_directories(fileName.parent_path()); @@ -48,7 +48,7 @@ TEST(SerializationTest, VariableFileSerializationTest) } } -TEST(SerializationTest, ClassFileSerializationTest) +TEST(SerializationTest, ClassFileTest) { static std::filesystem::path fileName = "../Test/Generated/Mirror/SerializationTest.ClassFileSerializationTest.bin"; std::filesystem::create_directories(fileName.parent_path()); @@ -75,7 +75,7 @@ TEST(SerializationTest, ClassFileSerializationTest) } } -TEST(SerializationTest, ContainerFileSerializationTest) +TEST(SerializationTest, ContainerFileTest) { static std::filesystem::path fileName = "../Test/Generated/Mirror/SerializationTest.ContainerFileSerializationTest.bin"; std::filesystem::create_directories(fileName.parent_path()); @@ -124,7 +124,22 @@ TEST(SerializationTest, ContainerFileSerializationTest) } } -TEST(SerializationTest, MetaObjectTypeSerializationTest) +TEST(SerializationTest, MetaObjectTypeTest) +{ + // TODO +} + +TEST(SerializationTest, MetaObjectSameAsDefaultObjectTest) +{ + // TODO +} + +TEST(SerializationTest, MetaObjectWithBaseClassTest) +{ + // TODO +} + +TEST(SerializationTest, MetaObjectUpgradeTest) { // TODO } diff --git a/Engine/Source/Runtime/Include/Runtime/Asset.h b/Engine/Source/Runtime/Include/Runtime/Asset.h index fa0114b5..1e33c395 100644 --- a/Engine/Source/Runtime/Include/Runtime/Asset.h +++ b/Engine/Source/Runtime/Include/Runtime/Asset.h @@ -410,47 +410,37 @@ namespace Runtime { namespace Common { template A> struct Serializer> { - static constexpr uint32_t typeId = Common::HashUtils::StrCrc32("Runtime::AssetRef"); + static constexpr size_t typeId = Common::HashUtils::StrCrc32("Runtime::AssetRef"); - static void Serialize(SerializeStream& stream, const Runtime::AssetRef& value) + static size_t Serialize(SerializeStream& stream, const Runtime::AssetRef& value) { - TypeIdSerializer>::Serialize(stream); - Serializer::Serialize(stream, value); + return Serializer::Serialize(stream, value); } - static bool Deserialize(DeserializeStream& stream, Runtime::AssetRef& value) + static size_t Deserialize(DeserializeStream& stream, Runtime::AssetRef& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - Core::Uri uri; - Serializer::Deserialize(stream, uri); + const auto deserialized = Serializer::Deserialize(stream, uri); value = Runtime::AssetManager::Get().SyncLoad(uri, uri); - return true; + return deserialized; } }; template A> struct Serializer> { - static constexpr uint32_t typeId = Common::HashUtils::StrCrc32("Runtime::SoftAssetRef"); + static constexpr size_t typeId = Common::HashUtils::StrCrc32("Runtime::SoftAssetRef"); - static void Serialize(SerializeStream& stream, const Runtime::SoftAssetRef& value) + static size_t Serialize(SerializeStream& stream, const Runtime::SoftAssetRef& value) { - TypeIdSerializer>::Serialize(stream); - Serializer::Serialize(stream, value); + return Serializer::Serialize(stream, value); } - static bool Deserialize(DeserializeStream& stream, Runtime::SoftAssetRef& value) + static size_t Deserialize(DeserializeStream& stream, Runtime::SoftAssetRef& value) { - if (!TypeIdSerializer>::Deserialize(stream)) { - return false; - } - Core::Uri uri; - Serializer::Deserialize(stream, uri); + const auto deserialized = Serializer::Deserialize(stream, uri); value = uri; - return true; + return deserialized; } }; }