Il2CppCompatibleDef.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include "codegen/il2cpp-codegen-metadata.h"
  4. #include "il2cpp-class-internals.h"
  5. #include "vm/Array.h"
  6. #include "vm/Type.h"
  7. #include "vm/Runtime.h"
  8. #include "vm/GlobalMetadataFileInternals.h"
  9. #include "icalls/mscorlib/System/Type.h"
  10. #include "gc/GarbageCollector.h"
  11. #if HYBRIDCLR_UNITY_2020
  12. #include "icalls/mscorlib/System/MonoType.h"
  13. #elif HYBRIDCLR_UNITY_2021 || HYBRIDCLR_UNITY_2022
  14. #include "icalls/mscorlib/System/RuntimeType.h"
  15. #include "icalls/mscorlib/System/RuntimeTypeHandle.h"
  16. #elif HYBRIDCLR_UNITY_2019
  17. #include "icalls/mscorlib/System/MonoType.h"
  18. #if IL2CPP_SIZEOF_VOID_P == 8
  19. #define PLATFORM_ARCH_64 1
  20. #else
  21. #define PLATFORM_ARCH_64 0
  22. #endif
  23. #elif !defined(HYBRIDCLR_UNITY_VERSION)
  24. #error "please run 'HybridCLR/Generate/All' before building"
  25. #else
  26. #error "unsupported unity version"
  27. #endif
  28. #if IL2CPP_BYTE_ORDER != IL2CPP_LITTLE_ENDIAN
  29. #error "only support litten endian"
  30. #endif
  31. #if PLATFORM_ARCH_64
  32. #define HYBRIDCLR_ARCH_64 1
  33. #else
  34. #define HYBRIDCLR_ARCH_64 0
  35. #endif
  36. #define PTR_SIZE IL2CPP_SIZEOF_VOID_P
  37. #if HYBRIDCLR_ARCH_64 || HYBRIDCLR_TARGET_X86
  38. #define SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS 1
  39. #else
  40. #define SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS 0
  41. #endif
  42. #ifndef ENABLE_PLACEHOLDER_DLL
  43. #define ENABLE_PLACEHOLDER_DLL 1
  44. #endif
  45. #if IL2CPP_ENABLE_WRITE_BARRIERS
  46. #define HYBRIDCLR_ENABLE_WRITE_BARRIERS 1
  47. #else
  48. #define HYBRIDCLR_ENABLE_WRITE_BARRIERS 0
  49. #endif
  50. #if UNITY_ENGINE_TUANJIE
  51. #define HYBRIDCLR_MALLOC(size) IL2CPP_MALLOC(size, IL2CPP_MEM_META_POOL)
  52. #define HYBRIDCLR_MALLOC_ALIGNED(size, alignment) IL2CPP_MALLOC_ALIGNED(size, alignment, IL2CPP_MEM_META_POOL)
  53. #define HYBRIDCLR_MALLOC_ZERO(size) IL2CPP_MALLOC_ZERO(size, IL2CPP_MEM_META_POOL)
  54. #define HYBRIDCLR_CALLOC(count, size) IL2CPP_CALLOC(count, size, IL2CPP_MEM_META_POOL)
  55. #define HYBRIDCLR_FREE(ptr) IL2CPP_FREE(ptr, IL2CPP_MEM_META_POOL)
  56. #define HYBRIDCLR_FREE_ALIGNED(ptr) IL2CPP_FREE_ALIGNED(ptr, IL2CPP_MEM_META_POOL)
  57. #define HYBRIDCLR_METADATA_MALLOC(size) il2cpp::vm::MetadataMalloc(size, IL2CPP_MSTAT_TYPE)
  58. #define HYBRIDCLR_METADATA_CALLOC(count, size) il2cpp::vm::MetadataCalloc(count, size, IL2CPP_MSTAT_TYPE)
  59. #else
  60. #define HYBRIDCLR_MALLOC(size) IL2CPP_MALLOC(size)
  61. #define HYBRIDCLR_MALLOC_ALIGNED(size, alignment) IL2CPP_MALLOC_ALIGNED(size, alignment)
  62. #define HYBRIDCLR_MALLOC_ZERO(size) IL2CPP_MALLOC_ZERO(size)
  63. #define HYBRIDCLR_CALLOC(count, size) IL2CPP_CALLOC(count, size)
  64. #define HYBRIDCLR_FREE(ptr) IL2CPP_FREE(ptr)
  65. #define HYBRIDCLR_FREE_ALIGNED(ptr) IL2CPP_FREE_ALIGNED(ptr)
  66. #define HYBRIDCLR_METADATA_MALLOC(size) il2cpp::vm::MetadataMalloc(size)
  67. #define HYBRIDCLR_METADATA_CALLOC(count, size) il2cpp::vm::MetadataCalloc(count, size)
  68. #endif
  69. namespace hybridclr
  70. {
  71. extern const char* g_placeHolderAssemblies[];
  72. Il2CppMethodPointer InitAndGetInterpreterDirectlyCallMethodPointerSlow(MethodInfo* method);
  73. inline Il2CppMethodPointer InitAndGetInterpreterDirectlyCallMethodPointer(const MethodInfo* method)
  74. {
  75. Il2CppMethodPointer methodPointer = method->methodPointerCallByInterp;
  76. if (methodPointer)
  77. {
  78. return methodPointer;
  79. }
  80. if (method->initInterpCallMethodPointer)
  81. {
  82. return methodPointer;
  83. }
  84. return InitAndGetInterpreterDirectlyCallMethodPointerSlow(const_cast<MethodInfo*>(method));
  85. }
  86. inline Il2CppMethodPointer InitAndGetInterpreterDirectlyCallVirtualMethodPointer(const MethodInfo* method)
  87. {
  88. Il2CppMethodPointer methodPointer = method->virtualMethodPointerCallByInterp;
  89. if (methodPointer)
  90. {
  91. return methodPointer;
  92. }
  93. if (method->initInterpCallMethodPointer)
  94. {
  95. return methodPointer;
  96. }
  97. InitAndGetInterpreterDirectlyCallMethodPointerSlow(const_cast<MethodInfo*>(method));
  98. return method->virtualMethodPointerCallByInterp;
  99. }
  100. inline void HYBRIDCLR_SET_WRITE_BARRIER(void** ptr)
  101. {
  102. #if HYBRIDCLR_ENABLE_WRITE_BARRIERS
  103. il2cpp::gc::GarbageCollector::SetWriteBarrier(ptr);
  104. #endif
  105. }
  106. inline void HYBRIDCLR_SET_WRITE_BARRIER(void** ptr, size_t size)
  107. {
  108. #if HYBRIDCLR_ENABLE_WRITE_BARRIERS
  109. il2cpp::gc::GarbageCollector::SetWriteBarrier(ptr, size);
  110. #endif
  111. }
  112. }
  113. #if HYBRIDCLR_UNITY_2019 || HYBRIDCLR_UNITY_2020
  114. inline bool IS_CLASS_VALUE_TYPE(const Il2CppClass* klass)
  115. {
  116. return klass->valuetype;
  117. }
  118. inline bool IS_CCTOR_FINISH_OR_NO_CCTOR(const Il2CppClass* klass)
  119. {
  120. return (klass->cctor_finished) || !(klass->has_cctor);
  121. }
  122. inline const Il2CppType* GET_METHOD_PARAMETER_TYPE(const ParameterInfo& param)
  123. {
  124. return param.parameter_type;
  125. }
  126. inline uint32_t GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(const Il2CppCustomAttributeTypeRange& tr)
  127. {
  128. return tr.start;
  129. }
  130. inline void SET_IL2CPPTYPE_VALUE_TYPE(Il2CppType& type, bool v)
  131. {
  132. }
  133. inline void COPY_IL2CPPTYPE_VALUE_TYPE_FLAG(Il2CppType& dst, const Il2CppType& src)
  134. {
  135. }
  136. #define GET_ARRAY_ELEMENT_ADDRESS load_array_elema
  137. #define VALUE_TYPE_METHOD_POINTER_IS_ADJUST_METHOD 1
  138. namespace hybridclr
  139. {
  140. inline Il2CppReflectionType* GetReflectionTypeFromName(Il2CppString* name)
  141. {
  142. return il2cpp::icalls::mscorlib::System::Type::internal_from_name(name, true, false);
  143. }
  144. inline void ConstructDelegate(Il2CppDelegate* delegate, Il2CppObject* target, const MethodInfo* method)
  145. {
  146. delegate->method_ptr = InitAndGetInterpreterDirectlyCallVirtualMethodPointer(method);
  147. delegate->method = method;
  148. delegate->target = target;
  149. //il2cpp::vm::Type::ConstructDelegate(delegate, target, InitAndGetInterpreterDirectlyCallMethodPointer(method), method);
  150. }
  151. inline const MethodInfo* GetGenericVirtualMethod(const MethodInfo* result, const MethodInfo* inflateMethod)
  152. {
  153. return il2cpp::vm::Runtime::GetGenericVirtualMethod(result, inflateMethod);
  154. }
  155. inline void* GetNulllableDataOffset(void* nullableObj, Il2CppClass* nullableClass)
  156. {
  157. uint32_t field_offset = nullableClass->fields[0].offset - sizeof(Il2CppObject); // offset of value field
  158. return (uint8_t*)nullableObj + field_offset;
  159. }
  160. inline uint8_t* GetNulllableHasValueOffset(void* nullableObj, Il2CppClass* nullableClass)
  161. {
  162. uint32_t field_offset = nullableClass->fields[1].offset - sizeof(Il2CppObject); // offset of has_value field
  163. return (uint8_t*)nullableObj + field_offset;
  164. }
  165. inline Il2CppString* GetKlassFullName(const Il2CppType* type)
  166. {
  167. Il2CppReflectionType* refType = il2cpp::icalls::mscorlib::System::Type::internal_from_handle((intptr_t)type);
  168. return il2cpp::icalls::mscorlib::System::MonoType::getFullName(refType, false, false);
  169. }
  170. }
  171. #elif HYBRIDCLR_UNITY_2021 || HYBRIDCLR_UNITY_2022
  172. inline bool IS_CLASS_VALUE_TYPE(const Il2CppClass* klass)
  173. {
  174. return klass->byval_arg.valuetype;
  175. }
  176. inline bool IS_CCTOR_FINISH_OR_NO_CCTOR(const Il2CppClass* klass)
  177. {
  178. return klass->cctor_finished_or_no_cctor;
  179. }
  180. inline const Il2CppType* GET_METHOD_PARAMETER_TYPE(const Il2CppType* param)
  181. {
  182. return param;
  183. }
  184. inline uint32_t GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(const Il2CppCustomAttributeTypeRange& tr)
  185. {
  186. return tr.startOffset;
  187. }
  188. inline void SET_IL2CPPTYPE_VALUE_TYPE(Il2CppType& type, bool v)
  189. {
  190. type.valuetype = v;
  191. }
  192. inline void COPY_IL2CPPTYPE_VALUE_TYPE_FLAG(Il2CppType& dst, const Il2CppType& src)
  193. {
  194. dst.valuetype = src.valuetype;
  195. }
  196. #define GET_ARRAY_ELEMENT_ADDRESS il2cpp_array_addr_with_size
  197. #define VALUE_TYPE_METHOD_POINTER_IS_ADJUST_METHOD 0
  198. namespace hybridclr
  199. {
  200. inline Il2CppReflectionType* GetReflectionTypeFromName(Il2CppString* name)
  201. {
  202. return il2cpp::icalls::mscorlib::System::RuntimeTypeHandle::internal_from_name(name, nullptr, nullptr, true, false, false);
  203. }
  204. inline void ConstructDelegate(Il2CppDelegate* delegate, Il2CppObject* target, const MethodInfo* method)
  205. {
  206. delegate->target = target;
  207. delegate->method = method;
  208. delegate->invoke_impl = InitAndGetInterpreterDirectlyCallVirtualMethodPointer(method);
  209. delegate->invoke_impl_this = target;
  210. }
  211. inline const MethodInfo* GetGenericVirtualMethod(const MethodInfo* result, const MethodInfo* inflateMethod)
  212. {
  213. #if HYBRIDCLR_UNITY_2021
  214. VirtualInvokeData vid;
  215. il2cpp::vm::Runtime::GetGenericVirtualMethod(result, inflateMethod, &vid);
  216. return vid.method;
  217. #else
  218. return il2cpp::metadata::GenericMethod::GetGenericVirtualMethod(result, inflateMethod);
  219. #endif
  220. }
  221. inline void* GetNulllableDataOffset(void* nullableObj, Il2CppClass* nullableClass)
  222. {
  223. uint32_t field_offset = nullableClass->fields[1].offset - sizeof(Il2CppObject); // offset of value field
  224. return (uint8_t*)nullableObj + field_offset;
  225. }
  226. inline uint8_t* GetNulllableHasValueOffset(void* nullableObj, Il2CppClass* nullableClass)
  227. {
  228. uint32_t field_offset = nullableClass->fields[0].offset - sizeof(Il2CppObject); // offset of has_value field
  229. return (uint8_t*)nullableObj + field_offset;
  230. }
  231. inline Il2CppString* GetKlassFullName(const Il2CppType* type)
  232. {
  233. Il2CppReflectionType* refType = il2cpp::icalls::mscorlib::System::Type::internal_from_handle((intptr_t)type);
  234. return il2cpp::icalls::mscorlib::System::RuntimeType::getFullName((Il2CppReflectionRuntimeType*)refType, false, false);
  235. }
  236. }
  237. #endif