From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- .../Mono/MonoBehaviourSerialization_YamlBackup.cpp | 201 +++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 Runtime/Mono/MonoBehaviourSerialization_YamlBackup.cpp (limited to 'Runtime/Mono/MonoBehaviourSerialization_YamlBackup.cpp') diff --git a/Runtime/Mono/MonoBehaviourSerialization_YamlBackup.cpp b/Runtime/Mono/MonoBehaviourSerialization_YamlBackup.cpp new file mode 100644 index 0000000..9ea4681 --- /dev/null +++ b/Runtime/Mono/MonoBehaviourSerialization_YamlBackup.cpp @@ -0,0 +1,201 @@ +#include "UnityPrefix.h" + +#if ENABLE_SCRIPTING +#include "MonoBehaviour.h" +#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h" +#include "Runtime/Serialize/SerializedFile.h" +#include "Runtime/Serialize/IterateTypeTree.h" +#include "MonoScript.h" +#include "MonoTypeSignatures.h" +#include "MonoManager.h" +#include "Runtime/Math/Quaternion.h" +#include "Runtime/Math/Color.h" +#include "Runtime/Math/Vector4.h" +#include "Runtime/Math/Vector2.h" +#include "Runtime/Math/Rect.h" +#include "Runtime/Scripting/ScriptingUtility.h" + +using namespace std; + +#if SUPPORT_TEXT_SERIALIZATION +struct ArrayAsString +{ + char const* name; + bool serializeAsString; +}; + +ArrayAsString g_ArrayAsString[] = +{ +#define YAML_DECLARE_WRITE_ARRAY_AS_STRING(x) \ +{ #x, YAMLSerializeTraits::ShouldSerializeArrayAsCompactString () } + + YAML_DECLARE_WRITE_ARRAY_AS_STRING(int), + YAML_DECLARE_WRITE_ARRAY_AS_STRING(char), + YAML_DECLARE_WRITE_ARRAY_AS_STRING(SInt32), +}; + +static const size_t g_ArrayAsStringCount = sizeof (g_ArrayAsString) / sizeof (g_ArrayAsString[0]); + +bool ConvertArrayAsString (std::string const& typeName) +{ + for (int i=0; i& m_Data; + YAMLWrite& m_Writer; + + template + T* ExtractAndAdvance (int* bytePosition) + { + T* ptr = reinterpret_cast (&m_Data[*bytePosition]); + *bytePosition += sizeof (T); + return ptr; + } + + YAMLConverterContext (dynamic_array& data, YAMLWrite& writer) + : m_Data (data), m_Writer (writer) + {} + + void EmitBasicData (TypeTree const& typeTree, int* bytePosition) + { +#define HANDLE_COMPLEX_TYPE(T) \ +if (typeTree.m_Type == #T) { \ +T* data = ExtractAndAdvance (bytePosition); \ +m_Writer.Transfer (*data, typeTree.m_Name.c_str()); \ +} else + +#define HANDLE_BASIC_TYPE(T) \ +if (typeTree.m_Type == #T) { \ +T data = *reinterpret_cast (&m_Data[*bytePosition]); \ +m_Writer.Transfer (data, typeTree.m_Name.c_str()); \ +} else + + HANDLE_BASIC_TYPE (int) + HANDLE_BASIC_TYPE (SInt32) + HANDLE_BASIC_TYPE (bool) + HANDLE_BASIC_TYPE (float) + HANDLE_BASIC_TYPE (double) + HANDLE_BASIC_TYPE (UInt8) + { + ErrorStringMsg ("Binary to YAML conversion: type %s is unsupported\n", typeTree.m_Type.c_str()); + } + } + + void ConvertDataToYAML (TypeTree const& typeTree) + { + int bytePosition = 0; + + for (TypeTree::TypeTreeList::const_iterator i = typeTree.m_Children.begin (); i != typeTree.m_Children.end ();++i) + { + ConvertDataToYAML (*i, &bytePosition); + } + } + + void ConvertDataToYAML (TypeTree const& typeTree, int* bytePosition) + { + if (typeTree.IsBasicDataType()) + { + // TypeTrees can contain $initialized_XXX$ member for Mono structs. We don't want to appear in YAML + bool skip = !typeTree.m_Name.empty () && typeTree.m_Name[0] == '$' && typeTree.m_Name[typeTree.m_Name.size ()-1] == '$'; + if (!skip) + EmitBasicData (typeTree, bytePosition); + } + else if (IsTypeTreePPtr (typeTree)) + { + m_Writer.PushMetaFlag (kTransferUsingFlowMappingStyle); + + m_Writer.BeginMetaGroup (typeTree.m_Name); + SInt32 instanceID = *reinterpret_cast (&m_Data[*bytePosition]); + m_Writer.Transfer (instanceID, "instanceID"); + *bytePosition += typeTree.m_ByteSize; + + m_Writer.EndMetaGroup (); + m_Writer.PopMetaFlag (); + } + else if (typeTree.m_IsArray) + { + SInt32 arraySize = *ExtractAndAdvance (bytePosition); + Assert (typeTree.m_Children.size () == 2); + TypeTree::const_iterator sizeIt = typeTree.begin (); + TypeTree::const_iterator dataIt = sizeIt; ++dataIt; + TypeTree const& elementTypeTree = *dataIt; + + if (elementTypeTree.IsBasicDataType () && ConvertArrayAsString (elementTypeTree.m_Type)) + { + std::string str; + size_t elementSize = elementTypeTree.m_ByteSize; + size_t numBytes = arraySize * elementSize; + str.resize (numBytes*2); + + for (size_t i=0; iEmitYAMLString(); + printf_console ("CONVERTED BINARY TO YAML:\n%s<< END\n", str.c_str()); + } +#endif + + return node; +} +#endif // SUPPORT_TEXT_SERIALIZATION + +#endif // ENABLE_SCRIPTING -- cgit v1.1-26-g67d0