MetadataModule.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #include "InterpreterImage.h"
  3. #include "AOTHomologousImage.h"
  4. #include "Assembly.h"
  5. namespace hybridclr
  6. {
  7. namespace metadata
  8. {
  9. class MetadataModule
  10. {
  11. public:
  12. static void Initialize();
  13. static InterpreterImage* GetImage(uint32_t imageIndex)
  14. {
  15. return InterpreterImage::GetImage(imageIndex);
  16. }
  17. static InterpreterImage* GetImage(const Il2CppImage* image)
  18. {
  19. return GetImage(DecodeImageIndex(image->token));
  20. }
  21. static InterpreterImage* GetImage(const Il2CppClass* klass)
  22. {
  23. return GetImage(klass->image);
  24. }
  25. static InterpreterImage* GetImage(const Il2CppTypeDefinition* typeDef)
  26. {
  27. return GetImage(DecodeImageIndex(typeDef->byvalTypeIndex));
  28. }
  29. static InterpreterImage* GetImage(const Il2CppMethodDefinition* method)
  30. {
  31. return GetImage(DecodeImageIndex(method->nameIndex));
  32. }
  33. static InterpreterImage* GetImage(const MethodInfo* method)
  34. {
  35. return GetImage(method->klass->image);
  36. }
  37. static InterpreterImage* GetImageByEncodedIndex(uint32_t encodedIndex)
  38. {
  39. return GetImage(DecodeImageIndex(encodedIndex));
  40. }
  41. static Image* GetUnderlyingInterpreterImage(const MethodInfo* method);
  42. static const char* GetStringFromEncodeIndex(StringIndex index)
  43. {
  44. uint32_t imageIndex = DecodeImageIndex(index);
  45. return GetImage(imageIndex)->GetStringFromRawIndex(DecodeMetadataIndex(index));
  46. }
  47. static uint32_t GetTypeEncodeIndex(const Il2CppTypeDefinition* typeDef)
  48. {
  49. InterpreterImage* image = GetImage(typeDef);
  50. return hybridclr::metadata::EncodeImageAndMetadataIndex(image->GetIndex(), image->GetTypeRawIndex(typeDef));
  51. }
  52. static Il2CppMetadataTypeHandle GetAssemblyTypeHandleFromRawIndex(const Il2CppImage* image, AssemblyTypeIndex index)
  53. {
  54. return GetImage(image)->GetAssemblyTypeHandleFromRawIndex(index);
  55. }
  56. static Il2CppMetadataTypeHandle GetAssemblyTypeHandleFromEncodeIndex(AssemblyTypeIndex index)
  57. {
  58. uint32_t imageIndex = DecodeImageIndex(index);
  59. return GetImage(imageIndex)->GetAssemblyTypeHandleFromRawIndex(DecodeMetadataIndex(index));
  60. }
  61. static Il2CppMetadataTypeHandle GetAssemblyExportedTypeHandleFromEncodeIndex(AssemblyTypeIndex index)
  62. {
  63. uint32_t imageIndex = DecodeImageIndex(index);
  64. return GetImage(imageIndex)->GetAssemblyExportedTypeHandleFromRawIndex(DecodeMetadataIndex(index));
  65. }
  66. static const Il2CppTypeDefinitionSizes* GetTypeDefinitionSizesFromEncodeIndex(TypeDefinitionIndex index)
  67. {
  68. uint32_t imageIndex = DecodeImageIndex(index);
  69. return GetImage(imageIndex)->GetTypeDefinitionSizesFromRawIndex(DecodeMetadataIndex(index));
  70. }
  71. static const Il2CppType* GetIl2CppTypeFromEncodeIndex(uint32_t index)
  72. {
  73. uint32_t imageIndex = DecodeImageIndex(index);
  74. IL2CPP_ASSERT(imageIndex > 0);
  75. uint32_t rawIndex = DecodeMetadataIndex(index);
  76. return GetImage(imageIndex)->GetIl2CppTypeFromRawIndex(rawIndex);
  77. }
  78. static Il2CppClass* GetTypeInfoFromTypeDefinitionEncodeIndex(TypeDefinitionIndex index)
  79. {
  80. uint32_t imageIndex = DecodeImageIndex(index);
  81. IL2CPP_ASSERT(imageIndex > 0);
  82. uint32_t rawIndex = DecodeMetadataIndex(index);
  83. return GetImage(imageIndex)->GetTypeInfoFromTypeDefinitionRawIndex(rawIndex);
  84. }
  85. static const Il2CppFieldDefinition* GetFieldDefinitionFromEncodeIndex(uint32_t index)
  86. {
  87. uint32_t imageIndex = DecodeImageIndex(index);
  88. return GetImage(imageIndex)->GetFieldDefinitionFromRawIndex(DecodeMetadataIndex(index));
  89. }
  90. static const Il2CppMethodDefinition* GetMethodDefinitionFromIndex(MethodIndex index)
  91. {
  92. uint32_t imageIndex = DecodeImageIndex(index);
  93. return GetImage(imageIndex)->GetMethodDefinitionFromRawIndex(DecodeMetadataIndex(index));
  94. }
  95. static uint32_t GetFieldOffset(const Il2CppClass* klass, int32_t fieldIndexInType, FieldInfo* field)
  96. {
  97. return GetImage(klass)->GetFieldOffset(klass, fieldIndexInType);
  98. }
  99. static const MethodInfo* GetMethodInfoFromMethodDefinitionIndex(uint32_t index)
  100. {
  101. uint32_t imageIndex = DecodeImageIndex(index);
  102. return GetImage(imageIndex)->GetMethodInfoFromMethodDefinitionRawIndex(DecodeMetadataIndex(index));
  103. }
  104. static const MethodInfo* GetMethodInfoFromMethodDefinition(const Il2CppMethodDefinition* methodDef)
  105. {
  106. uint32_t imageIndex = DecodeImageIndex(methodDef->nameIndex);
  107. return GetImage(imageIndex)->GetMethodInfoFromMethodDefinition(methodDef);
  108. }
  109. static const MethodInfo* GetMethodInfoFromVTableSlot(const Il2CppClass* klass, int32_t vTableSlot)
  110. {
  111. return GetImage(klass)->GetMethodInfoFromVTableSlot(klass, vTableSlot);
  112. }
  113. static const Il2CppMethodDefinition* GetMethodDefinitionFromVTableSlot(const Il2CppTypeDefinition* typeDefine, int32_t vTableSlot)
  114. {
  115. return GetImage(typeDefine)->GetMethodDefinitionFromVTableSlot(typeDefine, vTableSlot);
  116. }
  117. static Il2CppMethodPointer GetAdjustorThunk(const Il2CppImage* image, uint32_t token)
  118. {
  119. uint32_t imageIndex = DecodeImageIndex(image->token);
  120. return GetImage(imageIndex)->GetAdjustorThunk(token);
  121. }
  122. static Il2CppMethodPointer GetMethodPointer(const Il2CppImage* image, uint32_t token)
  123. {
  124. uint32_t imageIndex = DecodeImageIndex(image->token);
  125. return GetImage(imageIndex)->GetMethodPointer(token);
  126. }
  127. static InvokerMethod GetMethodInvoker(const Il2CppImage* image, uint32_t token)
  128. {
  129. uint32_t imageIndex = DecodeImageIndex(image->token);
  130. return GetImage(imageIndex)->GetMethodInvoker(token);
  131. }
  132. static const Il2CppParameterDefinition* GetParameterDefinitionFromIndex(const Il2CppImage* image, ParameterIndex index)
  133. {
  134. uint32_t imageIndex = DecodeImageIndex(image->token);
  135. return GetImage(imageIndex)->GetParameterDefinitionFromIndex(index);
  136. }
  137. static const Il2CppType* GetInterfaceFromIndex(const Il2CppClass* klass, TypeInterfaceIndex index)
  138. {
  139. return GetImage(klass)->GetInterfaceFromIndex(klass, index);
  140. }
  141. static const Il2CppType* GetInterfaceFromOffset(const Il2CppClass* klass, TypeInterfaceIndex offset)
  142. {
  143. return GetImage(klass)->GetInterfaceFromOffset(klass, offset);
  144. }
  145. static const Il2CppType* GetInterfaceFromOffset(const Il2CppTypeDefinition* typeDefine, TypeInterfaceIndex offset)
  146. {
  147. return GetImage(typeDefine)->GetInterfaceFromOffset(typeDefine, offset);
  148. }
  149. static Il2CppInterfaceOffsetInfo GetInterfaceOffsetInfo(const Il2CppTypeDefinition* typeDefine, TypeInterfaceOffsetIndex index)
  150. {
  151. return GetImage(typeDefine)->GetInterfaceOffsetInfo(typeDefine, index);
  152. }
  153. static Il2CppClass* GetNestedTypeFromOffset(const Il2CppClass* klass, TypeNestedTypeIndex offset)
  154. {
  155. return GetImage(klass)->GetNestedTypeFromOffset(klass, offset);
  156. }
  157. static Il2CppClass* GetNestedTypeFromOffset(const Il2CppTypeDefinition* typeDefinition, TypeNestedTypeIndex offset)
  158. {
  159. return GetImage(typeDefinition)->GetNestedTypeFromOffset(typeDefinition, offset);
  160. }
  161. static Il2CppMetadataTypeHandle GetNestedTypes(Il2CppMetadataTypeHandle handle, void** iter)
  162. {
  163. Il2CppTypeDefinition* typeDef = (Il2CppTypeDefinition*)handle;
  164. return (Il2CppMetadataTypeHandle)(GetImage(typeDef)->GetNestedTypes(typeDef, iter));
  165. }
  166. static const Il2CppGenericContainer* GetGenericContainerFromEncodeIndex(uint32_t index)
  167. {
  168. return GetImage(DecodeImageIndex(index))->GetGenericContainerByRawIndex(DecodeMetadataIndex(index));
  169. }
  170. static const Il2CppFieldDefaultValue* GetFieldDefaultValueEntry(uint32_t index)
  171. {
  172. return GetImage(DecodeImageIndex(index))->GetFieldDefaultValueEntryByRawIndex(DecodeMetadataIndex(index));
  173. }
  174. static const uint8_t* GetFieldOrParameterDefalutValue(uint32_t index)
  175. {
  176. return GetImage(DecodeImageIndex(index))->GetFieldOrParameterDefalutValueByRawIndex(DecodeMetadataIndex(index));
  177. }
  178. #if HYBRIDCLR_UNITY_2020
  179. static bool HasAttribute(const Il2CppImage* image, uint32_t token, Il2CppClass* attribute)
  180. {
  181. return GetImage(image)->HasAttributeByToken(token, attribute);
  182. }
  183. static std::tuple<void*, void*> GetCustomAttributeDataRange(const Il2CppImage* image, uint32_t token)
  184. {
  185. return GetImage(image)->GetCustomAttributeDataRange(token);
  186. }
  187. #endif
  188. static bool IsImplementedByInterpreter(MethodInfo* method)
  189. {
  190. Il2CppClass* klass = method->klass;
  191. Il2CppClass* parent = klass->parent;
  192. if (parent != il2cpp_defaults.multicastdelegate_class && parent != il2cpp_defaults.delegate_class)
  193. {
  194. return AOTHomologousImage::FindImageByAssembly(klass->image->assembly);
  195. }
  196. else
  197. {
  198. return strcmp(method->name, "Invoke") == 0;
  199. }
  200. }
  201. static LoadImageErrorCode LoadMetadataForAOTAssembly(const void* dllBytes, uint32_t dllSize, HomologousImageMode mode);
  202. private:
  203. };
  204. }
  205. }