MetadataDeserialization.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "MetadataDeserialization.h"
  2. // Keep the logic in this file in sync with Unity.IL2CPP/Metadata.Dat/Serialization/*.cs
  3. using namespace il2cpp::utils;
  4. int GetIndexSize(const int numberOfElements)
  5. {
  6. if (numberOfElements <= UINT8_MAX)
  7. {
  8. return 1;
  9. }
  10. else if (numberOfElements <= UINT16_MAX)
  11. {
  12. return 2;
  13. }
  14. else
  15. {
  16. return 4;
  17. }
  18. }
  19. Il2CppEventDefinition DeserializeEventDefinition(const char* ptr, const SerializedIndexSizes& sizes)
  20. {
  21. return Il2CppEventDefinition {
  22. Read<StringIndex>(ptr), // nameIndex
  23. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  24. Read<MethodIndex>(ptr), // add
  25. Read<MethodIndex>(ptr), // remove
  26. Read<MethodIndex>(ptr), // raise
  27. Read<uint32_t>(ptr), // token
  28. };
  29. }
  30. Il2CppMethodDefinition DeserializeMethodDefinition(const Il2CppMetadataMethodDefinitionHandle handle, const SerializedIndexSizes& sizes)
  31. {
  32. const auto* ptr = reinterpret_cast<const char*>(handle);
  33. return Il2CppMethodDefinition {
  34. Read<StringIndex>(ptr), // nameIndex
  35. ReadIndex<TypeDefinitionIndex>(ptr, sizes.typeDefinitionIndex), // declaringType
  36. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // returnType
  37. Read<uint32_t>(ptr), // returnParameterToken
  38. ReadIndex<ParameterIndex>(ptr, sizes.parameterIndex), // parameterStart
  39. ReadIndex<GenericContainerIndex>(ptr, sizes.genericContainerIndex), // genericContainerIndex
  40. Read<uint32_t>(ptr), // token
  41. Read<uint16_t>(ptr), // flags
  42. Read<uint16_t>(ptr), // iflags
  43. Read<uint16_t>(ptr), // slot
  44. Read<uint16_t>(ptr), // parameterCount
  45. };
  46. }
  47. Il2CppParameterDefaultValue DeserializeParameterDefaultValue(const char* ptr, const SerializedIndexSizes& sizes)
  48. {
  49. return Il2CppParameterDefaultValue {
  50. ReadIndex<ParameterIndex>(ptr, sizes.parameterIndex), // parameterIndex
  51. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  52. Read<DefaultValueDataIndex>(ptr), // dataIndex
  53. };
  54. }
  55. Il2CppFieldDefaultValue DeserializeFieldDefaultValue(const char* ptr, const SerializedIndexSizes& sizes)
  56. {
  57. return Il2CppFieldDefaultValue {
  58. Read<FieldIndex>(ptr), // fieldIndex
  59. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  60. Read<DefaultValueDataIndex>(ptr), // dataIndex
  61. };
  62. }
  63. Il2CppFieldMarshaledSize DeserializeFieldMarshaledSize(const char* ptr, const SerializedIndexSizes& sizes)
  64. {
  65. return Il2CppFieldMarshaledSize {
  66. Read<FieldIndex>(ptr), // fieldIndex
  67. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  68. Read<int32_t>(ptr), // size
  69. };
  70. }
  71. Il2CppParameterDefinition DeserializeParameterDefinition(const char* ptr, const SerializedIndexSizes& sizes)
  72. {
  73. return Il2CppParameterDefinition {
  74. Read<StringIndex>(ptr), // nameIndex
  75. Read<uint32_t>(ptr), // token
  76. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  77. };
  78. }
  79. Il2CppFieldDefinition DeserializeFieldDefinition(const char* ptr, const SerializedIndexSizes& sizes)
  80. {
  81. return Il2CppFieldDefinition {
  82. Read<StringIndex>(ptr), // nameIndex
  83. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  84. Read<uint32_t>(ptr), // token
  85. };
  86. }
  87. Il2CppInterfaceOffsetPair DeserializeInterfaceOffsetPair(const char* ptr, const SerializedIndexSizes& sizes)
  88. {
  89. return Il2CppInterfaceOffsetPair {
  90. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // interfaceTypeIndex
  91. Read<int32_t>(ptr), // offset
  92. };
  93. }
  94. Il2CppTypeDefinition DeserializeTypeDefinition(const Il2CppMetadataTypeHandle handle, const SerializedIndexSizes& sizes)
  95. {
  96. const auto* ptr = reinterpret_cast<const char*>(handle);
  97. const auto typeIndexSize = sizes.typeIndex;
  98. return Il2CppTypeDefinition {
  99. Read<StringIndex>(ptr), // nameIndex
  100. Read<StringIndex>(ptr), // namespaceIndex
  101. ReadIndex<TypeIndex>(ptr, typeIndexSize), // byvalTypeIndex
  102. ReadIndex<TypeIndex>(ptr, typeIndexSize), // declaringTypeIndex
  103. ReadIndex<TypeIndex>(ptr, typeIndexSize), // parentIndex
  104. ReadIndex<GenericContainerIndex>(ptr, sizes.genericContainerIndex), // genericContainerIndex
  105. Read<uint32_t>(ptr), // flags
  106. Read<FieldIndex>(ptr), // fieldStart
  107. Read<MethodIndex>(ptr), // methodStart
  108. Read<EventIndex>(ptr), // eventStart
  109. Read<PropertyIndex>(ptr), // propertyStart
  110. Read<NestedTypeIndex>(ptr), // nestedTypesStart
  111. Read<InterfacesIndex>(ptr), // interfacesStart
  112. Read<VTableIndex>(ptr), // vtableStart
  113. Read<InterfacesIndex>(ptr), // interfaceOffsetsStart
  114. Read<uint16_t>(ptr), // method_count
  115. Read<uint16_t>(ptr), // property_count
  116. Read<uint16_t>(ptr), // field_count
  117. Read<uint16_t>(ptr), // event_count
  118. Read<uint16_t>(ptr), // nested_type_count
  119. Read<uint16_t>(ptr), // vtable_count
  120. Read<uint16_t>(ptr), // interfaces_count
  121. Read<uint16_t>(ptr), // interface_offsets_count
  122. Read<uint32_t>(ptr), // bitfield
  123. Read<uint32_t>(ptr), // token
  124. };
  125. }
  126. Il2CppFieldRef DeserializeFieldRef(const char* ptr, const SerializedIndexSizes& sizes)
  127. {
  128. return Il2CppFieldRef {
  129. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  130. Read<FieldIndex>(ptr), // fieldIndex
  131. };
  132. }
  133. Il2CppImageDefinition DeserializeImageDefinition(const char* ptr, const SerializedIndexSizes& sizes)
  134. {
  135. return Il2CppImageDefinition {
  136. Read<StringIndex>(ptr), // nameIndex
  137. Read<AssemblyIndex>(ptr), // assemblyIndex
  138. ReadIndex<TypeDefinitionIndex>(ptr, sizes.typeDefinitionIndex), // typeStart
  139. Read<uint32_t>(ptr), // typeCount
  140. ReadIndex<TypeDefinitionIndex>(ptr, sizes.typeDefinitionIndex), // exportedTypeStart
  141. Read<uint32_t>(ptr), // exportedTypeCount
  142. Read<MethodIndex>(ptr), // entryPointIndex
  143. Read<uint32_t>(ptr), // token
  144. Read<CustomAttributeIndex>(ptr), // customAttributeStart
  145. Read<uint32_t>(ptr), // customAttributeCount
  146. };
  147. }
  148. Il2CppGenericParameter DeserializeGenericParameter(const Il2CppMetadataGenericParameterHandle handle, const SerializedIndexSizes& sizes)
  149. {
  150. const auto* ptr = reinterpret_cast<const char*>(handle);
  151. return Il2CppGenericParameter {
  152. ReadIndex<GenericContainerIndex>(ptr, sizes.genericContainerIndex), // ownerIndex
  153. Read<StringIndex>(ptr), // nameIndex
  154. Read<GenericParameterConstraintIndex>(ptr), // constraintsStart
  155. Read<int16_t>(ptr), // constraintsCount
  156. Read<uint16_t>(ptr), // num
  157. Read<uint16_t>(ptr), // flags
  158. };
  159. }
  160. Il2CppWindowsRuntimeTypeNamePair DeserializeWindowsRuntimeTypeNamePair(const char* ptr, const SerializedIndexSizes& sizes)
  161. {
  162. return Il2CppWindowsRuntimeTypeNamePair {
  163. Read<StringIndex>(ptr), // nameIndex
  164. ReadIndex<TypeIndex>(ptr, sizes.typeIndex), // typeIndex
  165. };
  166. }