From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Serialize/SerializeTraits.h | 533 ++++++++++++++++++++++++++++++++++++ 1 file changed, 533 insertions(+) create mode 100644 Runtime/Serialize/SerializeTraits.h (limited to 'Runtime/Serialize/SerializeTraits.h') diff --git a/Runtime/Serialize/SerializeTraits.h b/Runtime/Serialize/SerializeTraits.h new file mode 100644 index 0000000..9d5b946 --- /dev/null +++ b/Runtime/Serialize/SerializeTraits.h @@ -0,0 +1,533 @@ +#ifndef SERIALIZETRAITS_H +#define SERIALIZETRAITS_H + +#include "TypeTree.h" +#include "Runtime/Utilities/LogAssert.h" +#include "SerializeUtility.h" +#include "SerializationMetaFlags.h" +#include "Runtime/Utilities/vector_utility.h" +#include "Runtime/Utilities/vector_map.h" +#include "Runtime/Utilities/vector_set.h" +#include "Runtime/Utilities/dense_hash_map.h" +#include "Runtime/Utilities/dynamic_array.h" +#include +#include +#include +#include +#include "SerializeTraitsBase.h" + +class SerializedFile; +class SafeBinaryRead; + +typedef void TransferTypelessCallback (UInt8* data, int byteSize, int instanceID, int userdata); + +/* + + You can use SerializeTraits to setup transfer functions for classes where you can't change to code, eg. the STL. + You might also want to use it when writing custom converters. + + template<> + class SerializeTraits : public SerializeTraitsBase + { + public: + + typedef Vector4f value_type; + + inline static const char* GetTypeString () { return value_type::GetTypeString (); } + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + data.Transfer (transfer); + } + + template + static void Convert (value_type& data, TransferFunction& transfer) + { + const TypeTree& oldTypeTree = transfer.GetActiveOldTypeTree (); + const std::string& oldType = transfer.GetActiveOldTypeTree ().m_Type; + if (oldType == "Vector3f") + { + Vector3f temp = data; + temp.Transfer (transfer); + data = temp; + return true; + } + else + return false; + } + + /// Returns whether or not a this type is to be treated as a seperate channel in the animation system + static bool IsAnimationChannel () { return T::IsAnimationChannel (); } + }; + +*/ + + +#define DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS(x) \ + inline static const char* GetTypeString (void* p = 0) { return #x; } \ + inline static bool IsAnimationChannel () { return true; } \ + inline static bool MightContainPPtr () { return false; } \ + inline static bool AllowTransferOptimization () { return true; } + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (float) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (double) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + // We use "int" rather than "SInt32" here for backwards-compatibility reasons. + // "SInt32" and "int" used to be two different types (as were "UInt32" and "unsigned int") + // that we now serialize through same path. We use "int" instead of "SInt32" as the common + // identifier as it was more common. + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (int) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (unsigned int) // See definition of "int" above. +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (SInt64) +}; +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (UInt64) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (SInt16) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (UInt16) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (SInt8) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (UInt8) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBaseForBasicType +{ + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (char) +}; + +template<> +struct SerializeTraits : public SerializeTraitsBase +{ + typedef bool value_type; + DEFINE_GET_TYPESTRING_IS_ANIMATION_CHANNEL_TRAITS (bool) + + static int GetByteSize () { return 1; } + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + #if (defined __ppc__) && !UNITY_WII + AssertIf (sizeof(bool) != 4); + UInt8& temp = *(reinterpret_cast(&data) + 3); + + transfer.TransferBasicData (temp); + + // When running in debug mode in OS X (-O0 in gcc), + // bool values which are not exactly 0x01 are treated as false. + // We don't want this. Cast UInt8 to bool to fix this. + if (transfer.IsReading()) + data = temp; + #if DEBUGMODE + AssertIf((transfer.IsReading() || transfer.IsWriting()) && (reinterpret_cast (data) != 0 && reinterpret_cast (data) != 1)); + #endif + #else + AssertIf (sizeof(bool) != 1); + UInt8& temp = reinterpret_cast(data); + transfer.TransferBasicData (temp); + + // When running in debug mode in OS X (-O0 in gcc), + // bool values which are not exactly 0x01 are treated as false. + // We don't want this. Cast UInt8 to bool to fix this. + #if DEBUGMODE + if (transfer.IsReading()) + data = temp; + // You constructor or Reset function is not setting the bool value to a defined value! + AssertIf((transfer.IsReading() || transfer.IsWriting()) && (temp != 0 && temp != 1)); + #endif + #endif + } +}; + + + +#define DEFINE_GET_TYPESTRING_MAP_CONTAINER(x) \ +inline static const char* GetTypeString (void*) { return #x; } \ +inline static bool IsAnimationChannel () { return false; } \ +inline static bool MightContainPPtr () { return SerializeTraits::MightContainPPtr() || SerializeTraits::MightContainPPtr(); } \ +inline static bool AllowTransferOptimization () { return false; } + +template<> +class SerializeTraits : public SerializeTraitsBase +{ +public: + + typedef UnityStr value_type; + inline static const char* GetTypeString (value_type* x = NULL) { return "string"; } + inline static bool IsAnimationChannel () { return false; } + inline static bool MightContainPPtr () { return false; } + inline static bool AllowTransferOptimization () { return false; } + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data, kHideInEditorMask); + transfer.Align(); + } + + static bool IsContinousMemoryArray () { return true; } + + static void ResizeSTLStyleArray (value_type& data, int rs) + { + data.resize (rs, 1); + } + +}; + +// Do not add this serialization function. All serialized strings should use UnityStr instead of std::string +//template +//class SerializeTraits > : public SerializeTraitsBase > + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::vector value_type; + DEFINE_GET_TYPESTRING_CONTAINER (vector) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { resize_trimmed (data, rs); } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::vector value_type; + + inline static const char* GetTypeString (void* x = NULL) { return "vector"; } + inline static bool IsAnimationChannel () { return false; } + inline static bool MightContainPPtr () { return false; } + inline static bool AllowTransferOptimization () { return false; } + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + transfer.Align(); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { resize_trimmed (data, rs); } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::list value_type; + DEFINE_GET_TYPESTRING_CONTAINER (vector) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return false; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.resize (rs); } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::pair value_type; + inline static const char* GetTypeString (void* x = NULL) { return "pair"; } + inline static bool IsAnimationChannel () { return false; } + inline static bool MightContainPPtr () { return SerializeTraits::MightContainPPtr() || SerializeTraits::MightContainPPtr(); } +// inline static bool AllowTransferOptimization () { return SerializeTraits::AllowTransferOptimization() || SerializeTraits::AllowTransferOptimization(); } + inline static bool AllowTransferOptimization () { return false; } + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.Transfer (data.first, "first"); + transfer.Transfer (data.second, "second"); + } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::map value_type; + DEFINE_GET_TYPESTRING_MAP_CONTAINER(map) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + AssertIf(transfer.IsRemapPPtrTransfer() && SerializeTraits::MightContainPPtr() && transfer.IsReadingPPtr()); + transfer.TransferSTLStyleMap (data); + } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef dense_hash_map value_type; + DEFINE_GET_TYPESTRING_MAP_CONTAINER(map) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + AssertIf(transfer.IsRemapPPtrTransfer() && SerializeTraits::MightContainPPtr() && transfer.IsReadingPPtr()); + transfer.TransferSTLStyleMap (data); + } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::multimap value_type; + DEFINE_GET_TYPESTRING_MAP_CONTAINER(map) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + AssertIf(transfer.IsRemapPPtrTransfer() && SerializeTraits::MightContainPPtr() && transfer.IsReadingPPtr()); + transfer.TransferSTLStyleMap (data); + } +}; + + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef std::set value_type; + DEFINE_GET_TYPESTRING_CONTAINER (set) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + AssertIf(transfer.IsRemapPPtrTransfer() && transfer.IsReadingPPtr()); + transfer.TransferSTLStyleMap (data); + } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef vector_map value_type; + DEFINE_GET_TYPESTRING_MAP_CONTAINER (map) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + AssertIf(transfer.IsRemapPPtrTransfer() && transfer.IsReadingPPtr()); + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.get_vector ().resize (rs); } +}; + + + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef vector_map value_type; + DEFINE_GET_TYPESTRING_MAP_CONTAINER (map) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.get_vector ().resize (rs); } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef vector_set value_type; + DEFINE_GET_TYPESTRING_CONTAINER (set) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + AssertIf(transfer.IsRemapPPtrTransfer() && transfer.IsReadingPPtr()); + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.get_vector ().resize (rs); } +}; + +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + + typedef vector_set value_type; + DEFINE_GET_TYPESTRING_CONTAINER (set) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.get_vector ().resize (rs); } +}; + + +// Vector serialization is not allowed +template +class SerializeTraits > : public SerializeTraitsBase > +{ + public: + // disallow vector serialization +}; + + +template +class SerializeTraits > : public SerializeTraitsBase > +{ +public: + + typedef dynamic_array value_type; + DEFINE_GET_TYPESTRING_CONTAINER (vector) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.resize_initialized(rs); } + + static void resource_image_assign_external (value_type& data, void* begin, void* end) + { + data.assign_external(reinterpret_cast (begin), reinterpret_cast (end)); + } +}; + +template<> +class SerializeTraits > : public SerializeTraitsBase > +{ +public: + + typedef dynamic_array value_type; + typedef UInt8 T; + DEFINE_GET_TYPESTRING_CONTAINER (vector) + + template inline + static void Transfer (value_type& data, TransferFunction& transfer) + { + transfer.TransferSTLStyleArray (data); + transfer.Align(); + } + + static bool IsContinousMemoryArray () { return true; } + static void ResizeSTLStyleArray (value_type& data, int rs) { data.resize_initialized(rs); } + + static void resource_image_assign_external (value_type& data, void* begin, void* end) + { + data.assign_external(reinterpret_cast (begin), reinterpret_cast (end)); + } +}; + + +template +struct NonConstContainerValueType +{ + typedef typename T::value_type value_type; +}; + +template +struct NonConstContainerValueType > +{ + typedef T value_type; +}; + +template +struct NonConstContainerValueType > +{ + typedef std::pair value_type; +}; + +template +struct NonConstContainerValueType > +{ + typedef std::pair value_type; +}; + +template +struct NonConstContainerValueType > +{ + typedef std::pair value_type; +}; + +#endif -- cgit v1.1-26-g67d0