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/DumpSerializedDataToText.cpp | 372 +++++++++++++++++++++++++ 1 file changed, 372 insertions(+) create mode 100644 Runtime/Serialize/DumpSerializedDataToText.cpp (limited to 'Runtime/Serialize/DumpSerializedDataToText.cpp') diff --git a/Runtime/Serialize/DumpSerializedDataToText.cpp b/Runtime/Serialize/DumpSerializedDataToText.cpp new file mode 100644 index 0000000..1be6ddf --- /dev/null +++ b/Runtime/Serialize/DumpSerializedDataToText.cpp @@ -0,0 +1,372 @@ +#include "UnityPrefix.h" +#include "DumpSerializedDataToText.h" +#include "TypeTree.h" +#include "CacheWrap.h" +#include "Runtime/Utilities/GUID.h" + +using namespace std; + +#if (UNITY_EDITOR || UNITY_INCLUDE_SERIALIZATION_DUMP || DEBUGMODE) + +void DumpSerializedDataToText (const TypeTree& typeTree, dynamic_array& data) +{ + int offset = 0; + RecursiveOutput(typeTree, data.begin(), &offset, 0, cout, kDumpNormal, 0, false, -1); +} +#define TAB for (int t=0;t +void DoSwap (T& t, bool swapBytes) +{ + if (swapBytes) + SwapEndianBytes(t); +} + + +SInt32 CalculateByteSize(const TypeTree& type) { + if(type.m_ByteSize != -1) + return type.m_ByteSize; + + SInt32 r=0; + for (TypeTree::const_iterator i=type.begin ();i != type.end ();i++) + r+=CalculateByteSize(*i); + return r; +} + +void OutputType (const TypeTree& type, ostream& os) +{ + os << "Name: " << type.m_Name; + os << " Type: " << type.m_Type; + os << " ByteSize: " << type.m_ByteSize; + os << " TypeTreePosition: " << type.m_Index; + os << " IsArray: " << type.m_IsArray; + os << " Version: " << type.m_Version; + os << " MetaFlag: " << type.m_MetaFlag; + os << " IsArray: " << type.m_IsArray; +} + +string ExtractString (const TypeTree& type, const UInt8* data, int* offset, bool doSwap) +{ + string value; + SInt32 size = *reinterpret_cast (data + *offset); + DoSwap(size, doSwap); + value.reserve (size); + for (int i=0;i (data + *offset); + DoSwap(size, doSwap); + value.reserve (size*2); + for (int i=0;i (data + *offset); + DoSwap(v, doSwap); + if (i != 0) + val += ' '; + val += Format("%g", v); + *offset += 4; + } + val += ')'; + + if (type.m_MetaFlag & kAlignBytesFlag) + { + *offset = Align4(*offset); + } + + return val; +} + +string ExtractRectOffset (const TypeTree& type, const UInt8* data, int* offset, bool doSwap) +{ + AssertIf(type.m_Father == NULL); + AssertIf(type.m_Children.size() != 4); + + string val = "("; + TypeTree::TypeTreeList::const_iterator it = type.m_Children.begin(); + for (int i = 0; i < 4; ++i, ++it) + { + int v = *reinterpret_cast (data + *offset); + DoSwap(v, doSwap); + if (i != 0) + val += ' '; + val += Format("%s %i", it->m_Name.c_str(), v); + *offset += 4; + } + val += ')'; + + if (type.m_MetaFlag & kAlignBytesFlag) + { + *offset = Align4(*offset); + } + + return val; +} + + +string ExtractPPtr (const TypeTree& type, const UInt8* data, int* offset, bool doSwap) +{ + SInt32 fileID = *reinterpret_cast(data + *offset); + SInt32 pathID = *reinterpret_cast(data + *offset + 4); + DoSwap(fileID, doSwap); + DoSwap(pathID, doSwap); + + if (type.m_MetaFlag & kAlignBytesFlag) + { + *offset = Align4(*offset); + } + + *offset += 8; + + return Format ("(file %i path %i)", (int)fileID, (int)pathID); +} + +string ExtractGUID (const TypeTree& type, const UInt8* data, int* offset, bool doSwap) +{ + AssertIf(type.m_Father == NULL); + AssertIf(type.m_Children.size() != 4); + AssertIf(CalculateByteSize(type) != 4*4); + + UnityGUID val; + for (int i = 0; i < 4; ++i) { + UInt32 v = *reinterpret_cast (data + *offset); + val.data[i]=v; + *offset += 4; + } + + if (type.m_MetaFlag & kAlignBytesFlag) + { + *offset = Align4(*offset); + } + + return GUIDToString(val); +} + + +void OutputValue (const TypeTree& type, const UInt8* data, int* offset, ostream& os, bool doSwap) +{ +#define OUTPUT(x) \ +else if (type.m_Type == #x) \ +{ \ +x value = *reinterpret_cast (data + *offset); \ +DoSwap(value, doSwap);\ +os << value; \ +} + +#define OUTPUT_INT(x) \ +else if (type.m_Type == #x) \ +{ \ +x value = *reinterpret_cast (data + *offset); \ +DoSwap(value, doSwap);\ +int intValue = value; \ +os << intValue; \ +} + + + if (false) { } + OUTPUT (float) + OUTPUT (double) + OUTPUT (int) + OUTPUT (unsigned int) + OUTPUT (SInt32) + OUTPUT (UInt32) + OUTPUT (SInt16) + OUTPUT (UInt16) + OUTPUT (SInt64) + OUTPUT (UInt64) + OUTPUT_INT (SInt8) + OUTPUT_INT (UInt8) + OUTPUT (char) + OUTPUT (bool) + else + { + AssertString ("Unsupported type! " + type.m_Type); + } + *offset = *offset + type.m_ByteSize; +} + +const int kArrayMemberColumns = 25; + +void RecursiveOutput (const TypeTree& type, const UInt8* data, int* offset, int tab, ostream& os, DumpOutputMode mode, int pathID, bool doSwap, int arrayIndex) +{ + if (type.m_Type == "Vector3f" && type.m_ByteSize != 12) + { + AssertString ("Unsupported type! " + type.m_Type); + } + + if (!type.m_IsArray && arrayIndex == -1 && mode != kDumpClean) + os << Format("% 5d: ", (int)type.m_Index); + + if (type.IsBasicDataType ()) + { + // basic data type + if (arrayIndex == -1) + { + TAB os << type.m_Name << " "; + OutputValue (type, data, offset, os, doSwap); + os << " (" << type.m_Type << ")"; + os << endl; + } + else + { + // array members: multiple members per line + if (arrayIndex % kArrayMemberColumns == 0) + { + if (arrayIndex != 0) + os << endl; + TAB os << type.m_Name << " (" << type.m_Type << ") #" << arrayIndex << ": "; + OutputValue (type, data, offset, os, doSwap); + } + else + { + os << ' '; + OutputValue (type, data, offset, os, doSwap); + } + } + } + else if (type.m_IsArray) + { + // Extract and Print size + int size = *reinterpret_cast (data + *offset); + DoSwap(size, doSwap); + + RecursiveOutput (type.m_Children.front (), data, offset, tab, os, mode, 0, doSwap, -1); + // Print children + for (int i=0;i