il2cpp-api.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. #include "il2cpp-api.h"
  2. #include "il2cpp-object-internals.h"
  3. #include "il2cpp-runtime-stats.h"
  4. #include "gc/WriteBarrier.h"
  5. #include "os/StackTrace.h"
  6. #include "os/Image.h"
  7. #include "vm/AndroidRuntime.h"
  8. #include "vm/Array.h"
  9. #include "vm/Assembly.h"
  10. #include "vm/Class.h"
  11. #include "vm/Domain.h"
  12. #include "vm/Exception.h"
  13. #include "vm/Field.h"
  14. #include "vm/Image.h"
  15. #include "vm/InternalCalls.h"
  16. #include "vm/Liveness.h"
  17. #include "vm/MemoryInformation.h"
  18. #include "vm/Method.h"
  19. #include "vm/Monitor.h"
  20. #include "vm/Object.h"
  21. #include "vm/Path.h"
  22. #include "vm/PlatformInvoke.h"
  23. #include "vm/Profiler.h"
  24. #include "vm/Property.h"
  25. #include "vm/Reflection.h"
  26. #include "vm/Runtime.h"
  27. #include "vm/StackTrace.h"
  28. #include "vm/String.h"
  29. #include "vm/Thread.h"
  30. #include "vm/Type.h"
  31. #include "utils/Exception.h"
  32. #include "utils/Logging.h"
  33. #include "utils/Memory.h"
  34. #include "utils/MemoryPool.h"
  35. #include "utils/StringUtils.h"
  36. #include "utils/Runtime.h"
  37. #include "utils/Environment.h"
  38. #include "vm-utils/Debugger.h"
  39. #include "vm-utils/NativeSymbol.h"
  40. #include "gc/GarbageCollector.h"
  41. #include "gc/GCHandle.h"
  42. #include "gc/WriteBarrierValidation.h"
  43. #include <locale.h>
  44. #include <fstream>
  45. #include <string>
  46. using namespace il2cpp::vm;
  47. using il2cpp::utils::Memory;
  48. using namespace il2cpp::gc;
  49. #if IL2CPP_API_DYNAMIC_NO_DLSYM
  50. #include <map>
  51. struct SymbolCompare : public std::binary_function<char*, char*, bool>
  52. {
  53. bool operator()(const char* lhs, const char* rhs) const
  54. {
  55. return strcmp(lhs, rhs) < 0;
  56. }
  57. };
  58. typedef std::map<const char*, void*, SymbolCompare> SymbolTable;
  59. static SymbolTable s_SymbolTable;
  60. static void RegisterAPIFunction(const char* name, void* symbol)
  61. {
  62. s_SymbolTable.insert(std::make_pair(name, symbol));
  63. }
  64. void il2cpp_api_register_symbols(void)
  65. {
  66. #define DO_API(r, n, p) RegisterAPIFunction(#n, (void*)n);
  67. #define DO_API_NO_RETURN(r, n, p) DO_API(r, n, p)
  68. #include "il2cpp-api-functions.h"
  69. #undef DO_API
  70. #undef DO_API_NO_RETURN
  71. }
  72. void* il2cpp_api_lookup_symbol(const char* name)
  73. {
  74. SymbolTable::iterator it = s_SymbolTable.find(name);
  75. if (it != s_SymbolTable.end())
  76. {
  77. return it->second;
  78. }
  79. return NULL;
  80. }
  81. #endif // IL2CPP_API_DYNAMIC_NO_DLSYM
  82. int il2cpp_init(const char* domain_name)
  83. {
  84. // Use environment's default locale
  85. setlocale(LC_ALL, "");
  86. return Runtime::Init(domain_name);
  87. }
  88. int il2cpp_init_utf16(const Il2CppChar* domain_name)
  89. {
  90. return il2cpp_init(il2cpp::utils::StringUtils::Utf16ToUtf8(domain_name).c_str());
  91. }
  92. void il2cpp_shutdown()
  93. {
  94. Runtime::Shutdown();
  95. }
  96. void il2cpp_set_config_dir(const char *config_path)
  97. {
  98. il2cpp::vm::Runtime::SetConfigDir(config_path);
  99. }
  100. void il2cpp_set_data_dir(const char *data_path)
  101. {
  102. il2cpp::utils::Runtime::SetDataDir(data_path);
  103. }
  104. void il2cpp_set_temp_dir(const char *temp_dir)
  105. {
  106. il2cpp::vm::Path::SetTempPath(temp_dir);
  107. }
  108. void il2cpp_set_commandline_arguments(int argc, const char* const argv[], const char* basedir)
  109. {
  110. il2cpp::utils::Environment::SetMainArgs(argv, argc);
  111. }
  112. void il2cpp_set_commandline_arguments_utf16(int argc, const Il2CppChar* const argv[], const char* basedir)
  113. {
  114. il2cpp::utils::Environment::SetMainArgs(argv, argc);
  115. }
  116. void il2cpp_set_config_utf16(const Il2CppChar* executablePath)
  117. {
  118. il2cpp::vm::Runtime::SetConfigUtf16(executablePath);
  119. }
  120. void il2cpp_set_config(const char* executablePath)
  121. {
  122. il2cpp::vm::Runtime::SetConfig(executablePath);
  123. }
  124. void il2cpp_set_memory_callbacks(Il2CppMemoryCallbacks* callbacks)
  125. {
  126. Memory::SetMemoryCallbacks(callbacks);
  127. }
  128. void il2cpp_memory_pool_set_region_size(size_t size)
  129. {
  130. il2cpp::utils::MemoryPool::SetRegionSize(size);
  131. }
  132. size_t il2cpp_memory_pool_get_region_size()
  133. {
  134. return il2cpp::utils::MemoryPool::GetRegionSize();
  135. }
  136. const Il2CppImage* il2cpp_get_corlib()
  137. {
  138. return Image::GetCorlib();
  139. }
  140. void il2cpp_add_internal_call(const char* name, Il2CppMethodPointer method)
  141. {
  142. return InternalCalls::Add(name, method);
  143. }
  144. Il2CppMethodPointer il2cpp_resolve_icall(const char* name)
  145. {
  146. return InternalCalls::Resolve(name);
  147. }
  148. void* il2cpp_alloc(size_t size)
  149. {
  150. return IL2CPP_MALLOC(size);
  151. }
  152. void il2cpp_free(void* ptr)
  153. {
  154. IL2CPP_FREE(ptr);
  155. }
  156. // array
  157. Il2CppClass *il2cpp_array_class_get(Il2CppClass *element_class, uint32_t rank)
  158. {
  159. return Class::GetArrayClass(element_class, rank);
  160. }
  161. uint32_t il2cpp_array_length(Il2CppArray* array)
  162. {
  163. return Array::GetLength(array);
  164. }
  165. uint32_t il2cpp_array_get_byte_length(Il2CppArray *array)
  166. {
  167. return Array::GetByteLength(array);
  168. }
  169. Il2CppArray* il2cpp_array_new(Il2CppClass *elementTypeInfo, il2cpp_array_size_t length)
  170. {
  171. return Array::New(elementTypeInfo, length);
  172. }
  173. Il2CppArray* il2cpp_array_new_specific(Il2CppClass *arrayTypeInfo, il2cpp_array_size_t length)
  174. {
  175. return Array::NewSpecific(arrayTypeInfo, length);
  176. }
  177. Il2CppArray* il2cpp_array_new_full(Il2CppClass *array_class, il2cpp_array_size_t *lengths, il2cpp_array_size_t *lower_bounds)
  178. {
  179. return Array::NewFull(array_class, lengths, lower_bounds);
  180. }
  181. Il2CppClass* il2cpp_bounded_array_class_get(Il2CppClass *element_class, uint32_t rank, bool bounded)
  182. {
  183. return Class::GetBoundedArrayClass(element_class, rank, bounded);
  184. }
  185. int il2cpp_array_element_size(const Il2CppClass* klass)
  186. {
  187. return Array::GetElementSize(klass);
  188. }
  189. // assembly
  190. const Il2CppImage* il2cpp_assembly_get_image(const Il2CppAssembly *assembly)
  191. {
  192. return Assembly::GetImage(assembly);
  193. }
  194. // class
  195. const Il2CppType* il2cpp_class_enum_basetype(Il2CppClass *klass)
  196. {
  197. return Class::GetEnumBaseType(klass);
  198. }
  199. Il2CppClass* il2cpp_class_from_system_type(Il2CppReflectionType *type)
  200. {
  201. return Class::FromSystemType(type);
  202. }
  203. bool il2cpp_class_is_inited(const Il2CppClass *klass)
  204. {
  205. return klass->initialized;
  206. }
  207. bool il2cpp_class_is_generic(const Il2CppClass *klass)
  208. {
  209. return Class::IsGeneric(klass);
  210. }
  211. bool il2cpp_class_is_inflated(const Il2CppClass *klass)
  212. {
  213. return Class::IsInflated(klass);
  214. }
  215. bool il2cpp_class_is_assignable_from(Il2CppClass *klass, Il2CppClass *oklass)
  216. {
  217. return Class::IsAssignableFrom(klass, oklass);
  218. }
  219. bool il2cpp_class_is_subclass_of(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces)
  220. {
  221. return Class::IsSubclassOf(klass, klassc, check_interfaces);
  222. }
  223. bool il2cpp_class_has_parent(Il2CppClass *klass, Il2CppClass *klassc)
  224. {
  225. return Class::HasParent(klass, klassc);
  226. }
  227. Il2CppClass* il2cpp_class_from_il2cpp_type(const Il2CppType* type)
  228. {
  229. return Class::FromIl2CppType(type);
  230. }
  231. Il2CppClass* il2cpp_class_from_name(const Il2CppImage* image, const char* namespaze, const char *name)
  232. {
  233. return Class::FromName(image, namespaze, name);
  234. }
  235. Il2CppClass* il2cpp_class_get_element_class(Il2CppClass *klass)
  236. {
  237. return Class::GetElementClass(klass);
  238. }
  239. const EventInfo* il2cpp_class_get_events(Il2CppClass *klass, void* *iter)
  240. {
  241. return Class::GetEvents(klass, iter);
  242. }
  243. FieldInfo* il2cpp_class_get_fields(Il2CppClass *klass, void* *iter)
  244. {
  245. return Class::GetFields(klass, iter);
  246. }
  247. Il2CppClass* il2cpp_class_get_nested_types(Il2CppClass *klass, void* *iter)
  248. {
  249. return Class::GetNestedTypes(klass, iter);
  250. }
  251. Il2CppClass* il2cpp_class_get_interfaces(Il2CppClass *klass, void* *iter)
  252. {
  253. return Class::GetInterfaces(klass, iter);
  254. }
  255. const PropertyInfo* il2cpp_class_get_properties(Il2CppClass *klass, void* *iter)
  256. {
  257. return Class::GetProperties(klass, iter);
  258. }
  259. const PropertyInfo* il2cpp_class_get_property_from_name(Il2CppClass *klass, const char *name)
  260. {
  261. return Class::GetPropertyFromName(klass, name);
  262. }
  263. FieldInfo* il2cpp_class_get_field_from_name(Il2CppClass* klass, const char *name)
  264. {
  265. return Class::GetFieldFromName(klass, name);
  266. }
  267. const MethodInfo* il2cpp_class_get_methods(Il2CppClass *klass, void* *iter)
  268. {
  269. return Class::GetMethods(klass, iter);
  270. }
  271. const MethodInfo* il2cpp_class_get_method_from_name(Il2CppClass *klass, const char* name, int argsCount)
  272. {
  273. return Class::GetMethodFromName(klass, name, argsCount);
  274. }
  275. const char* il2cpp_class_get_name(Il2CppClass *klass)
  276. {
  277. return Class::GetName(klass);
  278. }
  279. const char* il2cpp_class_get_namespace(Il2CppClass *klass)
  280. {
  281. return Class::GetNamespace(klass);
  282. }
  283. Il2CppClass* il2cpp_class_get_parent(Il2CppClass *klass)
  284. {
  285. return Class::GetParent(klass);
  286. }
  287. Il2CppClass* il2cpp_class_get_declaring_type(Il2CppClass* klass)
  288. {
  289. return Class::GetDeclaringType(klass);
  290. }
  291. int32_t il2cpp_class_instance_size(Il2CppClass *klass)
  292. {
  293. return Class::GetInstanceSize(klass);
  294. }
  295. size_t il2cpp_class_num_fields(const Il2CppClass* klass)
  296. {
  297. return Class::GetNumFields(klass);
  298. }
  299. bool il2cpp_class_is_valuetype(const Il2CppClass* klass)
  300. {
  301. return Class::IsValuetype(klass);
  302. }
  303. bool il2cpp_class_is_blittable(const Il2CppClass* klass)
  304. {
  305. return Class::IsBlittable(klass);
  306. }
  307. int32_t il2cpp_class_value_size(Il2CppClass *klass, uint32_t *align)
  308. {
  309. return Class::GetValueSize(klass, align);
  310. }
  311. int il2cpp_class_get_flags(const Il2CppClass *klass)
  312. {
  313. return Class::GetFlags(klass);
  314. }
  315. bool il2cpp_class_is_abstract(const Il2CppClass *klass)
  316. {
  317. return Class::IsAbstract(klass);
  318. }
  319. bool il2cpp_class_is_interface(const Il2CppClass *klass)
  320. {
  321. return Class::IsInterface(klass);
  322. }
  323. int il2cpp_class_array_element_size(const Il2CppClass *klass)
  324. {
  325. return Class::GetArrayElementSize(klass);
  326. }
  327. Il2CppClass* il2cpp_class_from_type(const Il2CppType *type)
  328. {
  329. return Class::FromIl2CppType(type);
  330. }
  331. const Il2CppType* il2cpp_class_get_type(Il2CppClass *klass)
  332. {
  333. return Class::GetType(klass);
  334. }
  335. uint32_t il2cpp_class_get_type_token(Il2CppClass *klass)
  336. {
  337. return klass->token;
  338. }
  339. bool il2cpp_class_has_attribute(Il2CppClass *klass, Il2CppClass *attr_class)
  340. {
  341. return Class::HasAttribute(klass, attr_class);
  342. }
  343. bool il2cpp_class_has_references(Il2CppClass *klass)
  344. {
  345. return Class::HasReferences(klass);
  346. }
  347. bool il2cpp_class_is_enum(const Il2CppClass *klass)
  348. {
  349. return Class::IsEnum(klass);
  350. }
  351. const Il2CppImage* il2cpp_class_get_image(Il2CppClass* klass)
  352. {
  353. return Class::GetImage(klass);
  354. }
  355. const char *il2cpp_class_get_assemblyname(const Il2CppClass *klass)
  356. {
  357. return Class::GetAssemblyNameNoExtension(klass);
  358. }
  359. int il2cpp_class_get_rank(const Il2CppClass *klass)
  360. {
  361. return klass->rank;
  362. }
  363. uint32_t il2cpp_class_get_data_size(const Il2CppClass *klass)
  364. {
  365. return klass->static_fields_size;
  366. }
  367. void* il2cpp_class_get_static_field_data(const Il2CppClass *klass)
  368. {
  369. return klass->static_fields;
  370. }
  371. // testing only
  372. size_t il2cpp_class_get_bitmap_size(const Il2CppClass *klass)
  373. {
  374. return Class::GetBitmapSize(klass);
  375. }
  376. void il2cpp_class_get_bitmap(Il2CppClass *klass, size_t* bitmap)
  377. {
  378. size_t dummy = 0;
  379. Class::GetBitmap(klass, bitmap, dummy);
  380. }
  381. // stats
  382. extern Il2CppRuntimeStats il2cpp_runtime_stats;
  383. bool il2cpp_stats_dump_to_file(const char *path)
  384. {
  385. std::fstream fs;
  386. fs.open(path, std::fstream::out | std::fstream::trunc);
  387. fs << "New object count: " << il2cpp_stats_get_value(IL2CPP_STAT_NEW_OBJECT_COUNT) << "\n";
  388. fs << "Method count: " << il2cpp_stats_get_value(IL2CPP_STAT_METHOD_COUNT) << "\n";
  389. fs << "Class static data size: " << il2cpp_stats_get_value(IL2CPP_STAT_CLASS_STATIC_DATA_SIZE) << "\n";
  390. fs << "Inflated method count: " << il2cpp_stats_get_value(IL2CPP_STAT_INFLATED_METHOD_COUNT) << "\n";
  391. fs << "Inflated type count: " << il2cpp_stats_get_value(IL2CPP_STAT_INFLATED_TYPE_COUNT) << "\n";
  392. fs << "Initialized class count: " << il2cpp_stats_get_value(IL2CPP_STAT_INITIALIZED_CLASS_COUNT) << "\n";
  393. fs << "Generic instance count: " << il2cpp_stats_get_value(IL2CPP_STAT_GENERIC_INSTANCE_COUNT) << "\n";
  394. fs << "Generic class count: " << il2cpp_stats_get_value(IL2CPP_STAT_GENERIC_CLASS_COUNT) << "\n";
  395. fs.close();
  396. return true;
  397. }
  398. uint64_t il2cpp_stats_get_value(Il2CppStat stat)
  399. {
  400. switch (stat)
  401. {
  402. case IL2CPP_STAT_NEW_OBJECT_COUNT:
  403. return il2cpp_runtime_stats.new_object_count;
  404. case IL2CPP_STAT_INITIALIZED_CLASS_COUNT:
  405. return il2cpp_runtime_stats.initialized_class_count;
  406. /*case IL2CPP_STAT_GENERIC_VTABLE_COUNT:
  407. return il2cpp_runtime_stats.generic_vtable_count;
  408. case IL2CPP_STAT_USED_CLASS_COUNT:
  409. return il2cpp_runtime_stats.used_class_count;*/
  410. case IL2CPP_STAT_METHOD_COUNT:
  411. return il2cpp_runtime_stats.method_count;
  412. /*case IL2CPP_STAT_CLASS_VTABLE_SIZE:
  413. return il2cpp_runtime_stats.class_vtable_size;*/
  414. case IL2CPP_STAT_CLASS_STATIC_DATA_SIZE:
  415. return il2cpp_runtime_stats.class_static_data_size;
  416. case IL2CPP_STAT_GENERIC_INSTANCE_COUNT:
  417. return il2cpp_runtime_stats.generic_instance_count;
  418. case IL2CPP_STAT_GENERIC_CLASS_COUNT:
  419. return il2cpp_runtime_stats.generic_class_count;
  420. case IL2CPP_STAT_INFLATED_METHOD_COUNT:
  421. return il2cpp_runtime_stats.inflated_method_count;
  422. case IL2CPP_STAT_INFLATED_TYPE_COUNT:
  423. return il2cpp_runtime_stats.inflated_type_count;
  424. /*case IL2CPP_STAT_DELEGATE_CREATIONS:
  425. return il2cpp_runtime_stats.delegate_creations;
  426. case IL2CPP_STAT_MINOR_GC_COUNT:
  427. return il2cpp_runtime_stats.minor_gc_count;
  428. case IL2CPP_STAT_MAJOR_GC_COUNT:
  429. return il2cpp_runtime_stats.major_gc_count;
  430. case IL2CPP_STAT_MINOR_GC_TIME_USECS:
  431. return il2cpp_runtime_stats.minor_gc_time_usecs;
  432. case IL2CPP_STAT_MAJOR_GC_TIME_USECS:
  433. return il2cpp_runtime_stats.major_gc_time_usecs;*/
  434. }
  435. return 0;
  436. }
  437. // domain
  438. Il2CppDomain* il2cpp_domain_get()
  439. {
  440. return Domain::GetCurrent();
  441. }
  442. const Il2CppAssembly* il2cpp_domain_assembly_open(Il2CppDomain *domain, const char *name)
  443. {
  444. return Assembly::Load(name);
  445. }
  446. const Il2CppAssembly** il2cpp_domain_get_assemblies(const Il2CppDomain* domain, size_t* size)
  447. {
  448. il2cpp::vm::AssemblyVector* assemblies = Assembly::GetAllAssemblies();
  449. *size = assemblies->size();
  450. return &(*assemblies)[0];
  451. }
  452. // exception
  453. void il2cpp_raise_exception(Il2CppException* exc)
  454. {
  455. Exception::Raise(exc);
  456. }
  457. Il2CppException* il2cpp_exception_from_name_msg(const Il2CppImage* image, const char *name_space, const char *name, const char *msg)
  458. {
  459. return Exception::FromNameMsg(image, name_space, name, msg);
  460. }
  461. Il2CppException* il2cpp_get_exception_argument_null(const char *arg)
  462. {
  463. return Exception::GetArgumentNullException(arg);
  464. }
  465. void il2cpp_format_exception(const Il2CppException* ex, char* message, int message_size)
  466. {
  467. strncpy(message, il2cpp::utils::Exception::FormatException(ex).c_str(), message_size);
  468. }
  469. void il2cpp_format_stack_trace(const Il2CppException* ex, char* output, int output_size)
  470. {
  471. strncpy(output, il2cpp::utils::Exception::FormatStackTrace(ex).c_str(), output_size);
  472. }
  473. void il2cpp_unhandled_exception(Il2CppException* exc)
  474. {
  475. Runtime::UnhandledException(exc);
  476. }
  477. void il2cpp_native_stack_trace(const Il2CppException * ex, uintptr_t** addresses, int* numFrames, char** imageUUID, char** imageName)
  478. {
  479. #if IL2CPP_ENABLE_NATIVE_INSTRUCTION_POINTER_EMISSION && !IL2CPP_TINY
  480. if (ex == NULL || ex->native_trace_ips == NULL)
  481. {
  482. *numFrames = 0;
  483. *addresses = NULL;
  484. *imageUUID = NULL;
  485. *imageName = NULL;
  486. return;
  487. }
  488. *numFrames = il2cpp_array_length(ex->native_trace_ips);
  489. if (*numFrames <= 0)
  490. {
  491. *addresses = NULL;
  492. *imageUUID = NULL;
  493. *imageName = NULL;
  494. }
  495. else
  496. {
  497. *addresses = static_cast<uintptr_t*>(il2cpp_alloc((*numFrames) * sizeof(uintptr_t)));
  498. for (int i = 0; i < *numFrames; i++)
  499. {
  500. uintptr_t ptrAddr = il2cpp_array_get(ex->native_trace_ips, uintptr_t, i);
  501. (*addresses)[i] = ptrAddr;
  502. }
  503. *imageUUID = il2cpp::os::Image::GetImageUUID();
  504. *imageName = il2cpp::os::Image::GetImageName();
  505. }
  506. #endif
  507. }
  508. // field
  509. const char* il2cpp_field_get_name(FieldInfo *field)
  510. {
  511. return Field::GetName(field);
  512. }
  513. int il2cpp_field_get_flags(FieldInfo *field)
  514. {
  515. return Field::GetFlags(field);
  516. }
  517. Il2CppClass* il2cpp_field_get_parent(FieldInfo *field)
  518. {
  519. return Field::GetParent(field);
  520. }
  521. size_t il2cpp_field_get_offset(FieldInfo *field)
  522. {
  523. return Field::GetOffset(field);
  524. }
  525. const Il2CppType* il2cpp_field_get_type(FieldInfo *field)
  526. {
  527. return Field::GetType(field);
  528. }
  529. void il2cpp_field_get_value(Il2CppObject *obj, FieldInfo *field, void *value)
  530. {
  531. return Field::GetValue(obj, field, value);
  532. }
  533. Il2CppObject* il2cpp_field_get_value_object(FieldInfo *field, Il2CppObject *obj)
  534. {
  535. return Field::GetValueObject(field, obj);
  536. }
  537. bool il2cpp_field_has_attribute(FieldInfo *field, Il2CppClass *attr_class)
  538. {
  539. return Field::HasAttribute(field, attr_class);
  540. }
  541. void il2cpp_field_set_value(Il2CppObject *obj, FieldInfo *field, void *value)
  542. {
  543. Field::SetValue(obj, field, value);
  544. }
  545. void il2cpp_field_set_value_object(Il2CppObject* objectInstance, FieldInfo* field, Il2CppObject* value)
  546. {
  547. Field::SetInstanceFieldValueObject(objectInstance, field, value);
  548. }
  549. void il2cpp_field_static_get_value(FieldInfo *field, void *value)
  550. {
  551. Field::StaticGetValue(field, value);
  552. }
  553. void il2cpp_field_static_set_value(FieldInfo *field, void *value)
  554. {
  555. Field::StaticSetValue(field, value);
  556. }
  557. bool il2cpp_field_is_literal(FieldInfo *field)
  558. {
  559. return (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) != 0;
  560. }
  561. // gc
  562. void il2cpp_gc_collect(int maxGenerations)
  563. {
  564. GarbageCollector::Collect(maxGenerations);
  565. }
  566. int32_t il2cpp_gc_collect_a_little()
  567. {
  568. return GarbageCollector::CollectALittle();
  569. }
  570. void il2cpp_gc_start_incremental_collection()
  571. {
  572. GarbageCollector::StartIncrementalCollection();
  573. }
  574. void il2cpp_gc_enable()
  575. {
  576. GarbageCollector::Enable();
  577. }
  578. void il2cpp_gc_disable()
  579. {
  580. GarbageCollector::Disable();
  581. }
  582. bool il2cpp_gc_is_disabled()
  583. {
  584. return GarbageCollector::IsDisabled();
  585. }
  586. void il2cpp_gc_set_mode(Il2CppGCMode mode)
  587. {
  588. GarbageCollector::SetMode(mode);
  589. }
  590. bool il2cpp_gc_is_incremental()
  591. {
  592. return GarbageCollector::IsIncremental();
  593. }
  594. int64_t il2cpp_gc_get_max_time_slice_ns()
  595. {
  596. return GarbageCollector::GetMaxTimeSliceNs();
  597. }
  598. void il2cpp_gc_set_max_time_slice_ns(int64_t maxTimeSlice)
  599. {
  600. GarbageCollector::SetMaxTimeSliceNs(maxTimeSlice);
  601. }
  602. int64_t il2cpp_gc_get_used_size()
  603. {
  604. return GarbageCollector::GetUsedHeapSize();
  605. }
  606. int64_t il2cpp_gc_get_heap_size()
  607. {
  608. return GarbageCollector::GetAllocatedHeapSize();
  609. }
  610. void il2cpp_gc_foreach_heap(void(*func)(void* data, void* context), void* userData)
  611. {
  612. MemoryInformation::IterationContext ctx;
  613. ctx.callback = func;
  614. ctx.userData = userData;
  615. il2cpp::gc::GarbageCollector::ForEachHeapSection(&ctx, MemoryInformation::ReportGcHeapSection);
  616. }
  617. void il2cpp_stop_gc_world()
  618. {
  619. il2cpp::gc::GarbageCollector::StopWorld();
  620. }
  621. void il2cpp_start_gc_world()
  622. {
  623. il2cpp::gc::GarbageCollector::StartWorld();
  624. }
  625. void* il2cpp_gc_alloc_fixed(size_t size)
  626. {
  627. return il2cpp::gc::GarbageCollector::AllocateFixed(size, NULL);
  628. }
  629. void il2cpp_gc_free_fixed(void* address)
  630. {
  631. il2cpp::gc::GarbageCollector::FreeFixed(address);
  632. }
  633. // gchandle
  634. uint32_t il2cpp_gchandle_new(Il2CppObject *obj, bool pinned)
  635. {
  636. return GCHandle::New(obj, pinned);
  637. }
  638. uint32_t il2cpp_gchandle_new_weakref(Il2CppObject *obj, bool track_resurrection)
  639. {
  640. // Note that the call to Get will assert if an error occurred.
  641. return GCHandle::NewWeakref(obj, track_resurrection).Get();
  642. }
  643. Il2CppObject* il2cpp_gchandle_get_target(uint32_t gchandle)
  644. {
  645. return GCHandle::GetTarget(gchandle);
  646. }
  647. void il2cpp_gchandle_foreach_get_target(void(*func)(void*, void*), void* userData)
  648. {
  649. MemoryInformation::IterationContext ctx;
  650. ctx.callback = func;
  651. ctx.userData = userData;
  652. il2cpp::gc::GCHandle::WalkStrongGCHandleTargets(MemoryInformation::ReportGcHandleTarget, &ctx);
  653. }
  654. void il2cpp_gc_wbarrier_set_field(Il2CppObject *obj, void **targetAddress, void *object)
  655. {
  656. il2cpp::gc::WriteBarrier::GenericStore(targetAddress, object);
  657. }
  658. bool il2cpp_gc_has_strict_wbarriers()
  659. {
  660. #if IL2CPP_ENABLE_STRICT_WRITE_BARRIERS
  661. return true;
  662. #else
  663. return false;
  664. #endif
  665. }
  666. void il2cpp_gc_set_external_allocation_tracker(void(*func)(void*, size_t, int))
  667. {
  668. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  669. il2cpp::gc::WriteBarrierValidation::SetExternalAllocationTracker(func);
  670. #endif
  671. }
  672. void il2cpp_gc_set_external_wbarrier_tracker(void(*func)(void**))
  673. {
  674. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  675. il2cpp::gc::WriteBarrierValidation::SetExternalWriteBarrierTracker(func);
  676. #endif
  677. }
  678. void il2cpp_gchandle_free(uint32_t gchandle)
  679. {
  680. GCHandle::Free(gchandle);
  681. }
  682. // vm runtime info
  683. uint32_t il2cpp_object_header_size()
  684. {
  685. return static_cast<uint32_t>(sizeof(Il2CppObject));
  686. }
  687. uint32_t il2cpp_array_object_header_size()
  688. {
  689. return static_cast<uint32_t>(kIl2CppSizeOfArray);
  690. }
  691. uint32_t il2cpp_offset_of_array_length_in_array_object_header()
  692. {
  693. return kIl2CppOffsetOfArrayLength;
  694. }
  695. uint32_t il2cpp_offset_of_array_bounds_in_array_object_header()
  696. {
  697. return kIl2CppOffsetOfArrayBounds;
  698. }
  699. uint32_t il2cpp_allocation_granularity()
  700. {
  701. return static_cast<uint32_t>(2 * sizeof(void*));
  702. }
  703. // liveness
  704. void* il2cpp_unity_liveness_allocate_struct(Il2CppClass* filter, int max_object_count, il2cpp_register_object_callback callback, void* userdata, il2cpp_liveness_reallocate_callback reallocate)
  705. {
  706. return Liveness::AllocateStruct(filter, max_object_count, callback, userdata, reallocate);
  707. }
  708. void il2cpp_unity_liveness_calculation_from_root(Il2CppObject* root, void* state)
  709. {
  710. Liveness::FromRoot(root, state);
  711. }
  712. void il2cpp_unity_liveness_calculation_from_statics(void* state)
  713. {
  714. Liveness::FromStatics(state);
  715. }
  716. void il2cpp_unity_liveness_finalize(void* state)
  717. {
  718. Liveness::Finalize(state);
  719. }
  720. void il2cpp_unity_liveness_free_struct(void* state)
  721. {
  722. Liveness::FreeStruct(state);
  723. }
  724. // method
  725. const Il2CppType* il2cpp_method_get_return_type(const MethodInfo* method)
  726. {
  727. return Method::GetReturnType(method);
  728. }
  729. const MethodInfo* il2cpp_method_get_from_reflection(const Il2CppReflectionMethod *method)
  730. {
  731. return Reflection::GetMethod(method);
  732. }
  733. Il2CppReflectionMethod* il2cpp_method_get_object(const MethodInfo *method, Il2CppClass *refclass)
  734. {
  735. return Reflection::GetMethodObject(method, refclass);
  736. }
  737. const char* il2cpp_method_get_name(const MethodInfo *method)
  738. {
  739. return Method::GetName(method);
  740. }
  741. bool il2cpp_method_is_generic(const MethodInfo *method)
  742. {
  743. return Method::IsGeneric(method);
  744. }
  745. bool il2cpp_method_is_inflated(const MethodInfo *method)
  746. {
  747. return Method::IsInflated(method);
  748. }
  749. bool il2cpp_method_is_instance(const MethodInfo *method)
  750. {
  751. return Method::IsInstance(method);
  752. }
  753. uint32_t il2cpp_method_get_param_count(const MethodInfo *method)
  754. {
  755. return Method::GetParamCount(method);
  756. }
  757. const Il2CppType* il2cpp_method_get_param(const MethodInfo *method, uint32_t index)
  758. {
  759. return Method::GetParam(method, index);
  760. }
  761. Il2CppClass* il2cpp_method_get_class(const MethodInfo *method)
  762. {
  763. return Method::GetClass(method);
  764. }
  765. bool il2cpp_method_has_attribute(const MethodInfo *method, Il2CppClass *attr_class)
  766. {
  767. return Method::HasAttribute(method, attr_class);
  768. }
  769. Il2CppClass* il2cpp_method_get_declaring_type(const MethodInfo* method)
  770. {
  771. return Method::GetDeclaringType(method);
  772. }
  773. uint32_t il2cpp_method_get_flags(const MethodInfo *method, uint32_t *iflags)
  774. {
  775. if (iflags != 0)
  776. *iflags = Method::GetImplementationFlags(method);
  777. return Method::GetFlags(method);
  778. }
  779. uint32_t il2cpp_method_get_token(const MethodInfo *method)
  780. {
  781. return Method::GetToken(method);
  782. }
  783. const char *il2cpp_method_get_param_name(const MethodInfo *method, uint32_t index)
  784. {
  785. return Method::GetParamName(method, index);
  786. }
  787. // profiler
  788. #if IL2CPP_ENABLE_PROFILER
  789. void il2cpp_profiler_install(Il2CppProfiler *prof, Il2CppProfileFunc shutdown_callback)
  790. {
  791. Profiler::Install(prof, shutdown_callback);
  792. }
  793. void il2cpp_profiler_set_events(Il2CppProfileFlags events)
  794. {
  795. Profiler::SetEvents(events);
  796. }
  797. void il2cpp_profiler_install_enter_leave(Il2CppProfileMethodFunc enter, Il2CppProfileMethodFunc fleave)
  798. {
  799. Profiler::InstallEnterLeave(enter, fleave);
  800. }
  801. void il2cpp_profiler_install_allocation(Il2CppProfileAllocFunc callback)
  802. {
  803. Profiler::InstallAllocation(callback);
  804. }
  805. void il2cpp_profiler_install_gc(Il2CppProfileGCFunc callback, Il2CppProfileGCResizeFunc heap_resize_callback)
  806. {
  807. Profiler::InstallGC(callback, heap_resize_callback);
  808. }
  809. void il2cpp_profiler_install_fileio(Il2CppProfileFileIOFunc callback)
  810. {
  811. Profiler::InstallFileIO(callback);
  812. }
  813. void il2cpp_profiler_install_thread(Il2CppProfileThreadFunc start, Il2CppProfileThreadFunc end)
  814. {
  815. Profiler::InstallThread(start, end);
  816. }
  817. #endif
  818. // property
  819. const char* il2cpp_property_get_name(PropertyInfo *prop)
  820. {
  821. return Property::GetName(prop);
  822. }
  823. const MethodInfo* il2cpp_property_get_get_method(PropertyInfo *prop)
  824. {
  825. return Property::GetGetMethod(prop);
  826. }
  827. const MethodInfo* il2cpp_property_get_set_method(PropertyInfo *prop)
  828. {
  829. return Property::GetSetMethod(prop);
  830. }
  831. Il2CppClass* il2cpp_property_get_parent(PropertyInfo *prop)
  832. {
  833. return Property::GetParent(prop);
  834. }
  835. uint32_t il2cpp_property_get_flags(PropertyInfo *prop)
  836. {
  837. return Property::GetFlags(prop);
  838. }
  839. // object
  840. Il2CppClass* il2cpp_object_get_class(Il2CppObject* obj)
  841. {
  842. return Object::GetClass(obj);
  843. }
  844. uint32_t il2cpp_object_get_size(Il2CppObject* obj)
  845. {
  846. return Object::GetSize(obj);
  847. }
  848. const MethodInfo* il2cpp_object_get_virtual_method(Il2CppObject *obj, const MethodInfo *method)
  849. {
  850. return Object::GetVirtualMethod(obj, method);
  851. }
  852. Il2CppObject* il2cpp_object_new(const Il2CppClass *klass)
  853. {
  854. try
  855. {
  856. return Object::New(const_cast<Il2CppClass*>(klass));
  857. }
  858. catch (const Il2CppExceptionWrapper&)
  859. {
  860. // If a static constructor throws, that exception will occur here.
  861. // We don't want that to escape across the embedding API.
  862. return NULL;
  863. }
  864. }
  865. void* il2cpp_object_unbox(Il2CppObject* obj)
  866. {
  867. return Object::Unbox(obj);
  868. }
  869. Il2CppObject* il2cpp_value_box(Il2CppClass *klass, void* data)
  870. {
  871. return Object::Box(klass, data);
  872. }
  873. // monitor
  874. void il2cpp_monitor_enter(Il2CppObject* obj)
  875. {
  876. Monitor::Enter(obj);
  877. }
  878. bool il2cpp_monitor_try_enter(Il2CppObject* obj, uint32_t timeout)
  879. {
  880. return Monitor::TryEnter(obj, timeout);
  881. }
  882. void il2cpp_monitor_exit(Il2CppObject* obj)
  883. {
  884. Monitor::Exit(obj);
  885. }
  886. void il2cpp_monitor_pulse(Il2CppObject* obj)
  887. {
  888. Monitor::Pulse(obj);
  889. }
  890. void il2cpp_monitor_pulse_all(Il2CppObject* obj)
  891. {
  892. Monitor::PulseAll(obj);
  893. }
  894. void il2cpp_monitor_wait(Il2CppObject* obj)
  895. {
  896. Monitor::Wait(obj);
  897. }
  898. bool il2cpp_monitor_try_wait(Il2CppObject* obj, uint32_t timeout)
  899. {
  900. return Monitor::TryWait(obj, timeout);
  901. }
  902. // runtime
  903. Il2CppObject* il2cpp_runtime_invoke_convert_args(const MethodInfo *method, void *obj, Il2CppObject **params, int paramCount, Il2CppException **exc)
  904. {
  905. return Runtime::InvokeConvertArgs(method, obj, params, paramCount, exc);
  906. }
  907. Il2CppObject* il2cpp_runtime_invoke(const MethodInfo *method,
  908. void *obj, void **params, Il2CppException **exc)
  909. {
  910. return Runtime::Invoke(method, obj, params, exc);
  911. }
  912. void il2cpp_runtime_class_init(Il2CppClass* klass)
  913. {
  914. return Runtime::ClassInit(klass);
  915. }
  916. void il2cpp_runtime_object_init(Il2CppObject *obj)
  917. {
  918. Runtime::ObjectInit(obj);
  919. }
  920. void il2cpp_runtime_object_init_exception(Il2CppObject *obj, Il2CppException **exc)
  921. {
  922. Runtime::ObjectInitException(obj, exc);
  923. }
  924. void il2cpp_runtime_unhandled_exception_policy_set(Il2CppRuntimeUnhandledExceptionPolicy value)
  925. {
  926. Runtime::SetUnhandledExceptionPolicy(value);
  927. }
  928. // string
  929. int32_t il2cpp_string_length(Il2CppString* str)
  930. {
  931. return il2cpp::utils::StringUtils::GetLength(str);
  932. }
  933. Il2CppChar* il2cpp_string_chars(Il2CppString* str)
  934. {
  935. return il2cpp::utils::StringUtils::GetChars(str);
  936. }
  937. // Same as il2cpp_string_new_wrapper, because other normally takes a domain
  938. Il2CppString* il2cpp_string_new(const char* str)
  939. {
  940. return String::New(str);
  941. }
  942. Il2CppString* il2cpp_string_new_wrapper(const char* str)
  943. {
  944. return String::NewWrapper(str);
  945. }
  946. Il2CppString* il2cpp_string_new_utf16(const Il2CppChar *text, int32_t len)
  947. {
  948. return String::NewUtf16(text, len);
  949. }
  950. Il2CppString* il2cpp_string_new_len(const char* str, uint32_t length)
  951. {
  952. return String::NewLen(str, length);
  953. }
  954. Il2CppString* il2cpp_string_intern(Il2CppString* str)
  955. {
  956. return String::Intern(str);
  957. }
  958. Il2CppString* il2cpp_string_is_interned(Il2CppString* str)
  959. {
  960. return String::IsInterned(str);
  961. }
  962. // thread
  963. Il2CppThread *il2cpp_thread_current()
  964. {
  965. return Thread::Current();
  966. }
  967. Il2CppThread *il2cpp_thread_attach(Il2CppDomain *domain)
  968. {
  969. return Thread::Attach(domain);
  970. }
  971. void il2cpp_thread_detach(Il2CppThread *thread)
  972. {
  973. Thread::Detach(thread);
  974. }
  975. Il2CppThread **il2cpp_thread_get_all_attached_threads(size_t *size)
  976. {
  977. return Thread::GetAllAttachedThreads(*size);
  978. }
  979. bool il2cpp_is_vm_thread(Il2CppThread *thread)
  980. {
  981. return Thread::IsVmThread(thread);
  982. }
  983. // stacktrace
  984. void il2cpp_current_thread_walk_frame_stack(Il2CppFrameWalkFunc func, void* user_data)
  985. {
  986. StackTrace::WalkFrameStack(func, user_data);
  987. }
  988. void il2cpp_thread_walk_frame_stack(Il2CppThread *thread, Il2CppFrameWalkFunc func, void *user_data)
  989. {
  990. return StackTrace::WalkThreadFrameStack(thread, func, user_data);
  991. }
  992. bool il2cpp_current_thread_get_top_frame(Il2CppStackFrameInfo* frame)
  993. {
  994. IL2CPP_ASSERT(frame);
  995. return StackTrace::GetTopStackFrame(*frame);
  996. }
  997. bool il2cpp_thread_get_top_frame(Il2CppThread* thread, Il2CppStackFrameInfo* frame)
  998. {
  999. IL2CPP_ASSERT(frame);
  1000. return StackTrace::GetThreadTopStackFrame(thread, *frame);
  1001. }
  1002. bool il2cpp_current_thread_get_frame_at(int32_t offset, Il2CppStackFrameInfo* frame)
  1003. {
  1004. IL2CPP_ASSERT(frame);
  1005. return StackTrace::GetStackFrameAt(offset, *frame);
  1006. }
  1007. bool il2cpp_thread_get_frame_at(Il2CppThread* thread, int32_t offset, Il2CppStackFrameInfo* frame)
  1008. {
  1009. IL2CPP_ASSERT(frame);
  1010. return StackTrace::GetThreadStackFrameAt(thread, offset, *frame);
  1011. }
  1012. int32_t il2cpp_current_thread_get_stack_depth()
  1013. {
  1014. return static_cast<int32_t>(StackTrace::GetStackDepth());
  1015. }
  1016. int32_t il2cpp_thread_get_stack_depth(Il2CppThread *thread)
  1017. {
  1018. return StackTrace::GetThreadStackDepth(thread);
  1019. }
  1020. void il2cpp_set_default_thread_affinity(int64_t affinity_mask)
  1021. {
  1022. Thread::SetDefaultAffinityMask(affinity_mask);
  1023. }
  1024. void il2cpp_override_stack_backtrace(Il2CppBacktraceFunc stackBacktraceFunc)
  1025. {
  1026. il2cpp::os::StackTrace::OverrideStackBacktrace(stackBacktraceFunc);
  1027. }
  1028. // type
  1029. Il2CppObject* il2cpp_type_get_object(const Il2CppType *type)
  1030. {
  1031. return (Il2CppObject*)Reflection::GetTypeObject(type);
  1032. }
  1033. int il2cpp_type_get_type(const Il2CppType *type)
  1034. {
  1035. return Type::GetType(type);
  1036. }
  1037. Il2CppClass* il2cpp_type_get_class_or_element_class(const Il2CppType *type)
  1038. {
  1039. return Type::GetClassOrElementClass(type);
  1040. }
  1041. char* il2cpp_type_get_name(const Il2CppType *type)
  1042. {
  1043. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_IL);
  1044. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1045. memcpy(buffer, name.c_str(), name.length() + 1);
  1046. return buffer;
  1047. }
  1048. char* il2cpp_type_get_assembly_qualified_name(const Il2CppType * type)
  1049. {
  1050. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED);
  1051. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1052. memcpy(buffer, name.c_str(), name.length() + 1);
  1053. return buffer;
  1054. }
  1055. char* il2cpp_type_get_reflection_name(const Il2CppType *type)
  1056. {
  1057. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_REFLECTION);
  1058. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1059. memcpy(buffer, name.c_str(), name.length() + 1);
  1060. return buffer;
  1061. }
  1062. bool il2cpp_type_is_byref(const Il2CppType *type)
  1063. {
  1064. return type->byref;
  1065. }
  1066. uint32_t il2cpp_type_get_attrs(const Il2CppType *type)
  1067. {
  1068. return type->attrs;
  1069. }
  1070. bool il2cpp_type_equals(const Il2CppType* type, const Il2CppType *otherType)
  1071. {
  1072. return Type::IsEqualToType(type, otherType);
  1073. }
  1074. bool il2cpp_type_is_static(const Il2CppType *type)
  1075. {
  1076. return (type->attrs & FIELD_ATTRIBUTE_STATIC) != 0;
  1077. }
  1078. bool il2cpp_type_is_pointer_type(const Il2CppType *type)
  1079. {
  1080. return type->type == IL2CPP_TYPE_PTR;
  1081. }
  1082. // image
  1083. const Il2CppAssembly* il2cpp_image_get_assembly(const Il2CppImage *image)
  1084. {
  1085. return Image::GetAssembly(image);
  1086. }
  1087. const char* il2cpp_image_get_name(const Il2CppImage *image)
  1088. {
  1089. return Image::GetName(image);
  1090. }
  1091. const char* il2cpp_image_get_filename(const Il2CppImage *image)
  1092. {
  1093. return Image::GetFileName(image);
  1094. }
  1095. const MethodInfo* il2cpp_image_get_entry_point(const Il2CppImage *image)
  1096. {
  1097. return Image::GetEntryPoint(image);
  1098. }
  1099. size_t il2cpp_image_get_class_count(const Il2CppImage * image)
  1100. {
  1101. return Image::GetNumTypes(image);
  1102. }
  1103. const Il2CppClass* il2cpp_image_get_class(const Il2CppImage * image, size_t index)
  1104. {
  1105. return Image::GetType(image, static_cast<AssemblyTypeIndex>(index));
  1106. }
  1107. Il2CppManagedMemorySnapshot* il2cpp_capture_memory_snapshot()
  1108. {
  1109. return MemoryInformation::CaptureManagedMemorySnapshot();
  1110. }
  1111. void il2cpp_free_captured_memory_snapshot(Il2CppManagedMemorySnapshot* snapshot)
  1112. {
  1113. MemoryInformation::FreeCapturedManagedMemorySnapshot(snapshot);
  1114. }
  1115. void il2cpp_set_find_plugin_callback(Il2CppSetFindPlugInCallback method)
  1116. {
  1117. il2cpp::vm::PlatformInvoke::SetFindPluginCallback(method);
  1118. }
  1119. // Logging
  1120. void il2cpp_register_log_callback(Il2CppLogCallback method)
  1121. {
  1122. il2cpp::utils::Logging::SetLogCallback(method);
  1123. }
  1124. // Debugger
  1125. void il2cpp_debugger_set_agent_options(const char* options)
  1126. {
  1127. #if IL2CPP_MONO_DEBUGGER
  1128. il2cpp::utils::Debugger::SetAgentOptions(options);
  1129. #endif
  1130. }
  1131. bool il2cpp_is_debugger_attached()
  1132. {
  1133. return il2cpp::utils::Debugger::GetIsDebuggerAttached();
  1134. }
  1135. void il2cpp_register_debugger_agent_transport(Il2CppDebuggerTransport * debuggerTransport)
  1136. {
  1137. #if IL2CPP_MONO_DEBUGGER
  1138. il2cpp::utils::Debugger::RegisterTransport(debuggerTransport);
  1139. #endif
  1140. }
  1141. bool il2cpp_debug_get_method_info(const MethodInfo* method, Il2CppMethodDebugInfo* methodDebugInfo)
  1142. {
  1143. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  1144. return il2cpp::utils::NativeSymbol::GetMethodDebugInfo(method, methodDebugInfo);
  1145. #else
  1146. return false;
  1147. #endif
  1148. }
  1149. void il2cpp_unity_install_unitytls_interface(const void* unitytlsInterfaceStruct)
  1150. {
  1151. il2cpp::vm::Runtime::SetUnityTlsInterface(unitytlsInterfaceStruct);
  1152. }
  1153. // Custom Attributes
  1154. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_class(Il2CppClass *klass)
  1155. {
  1156. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(klass->image, klass->token));
  1157. }
  1158. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_method(const MethodInfo * method)
  1159. {
  1160. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(method->klass->image, method->token));
  1161. }
  1162. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_field(const FieldInfo * field)
  1163. {
  1164. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(field->parent->image, field->token));
  1165. }
  1166. bool il2cpp_custom_attrs_has_attr(Il2CppCustomAttrInfo *ainfo, Il2CppClass *attr_klass)
  1167. {
  1168. return Reflection::HasAttribute(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo), attr_klass);
  1169. }
  1170. Il2CppObject* il2cpp_custom_attrs_get_attr(Il2CppCustomAttrInfo *ainfo, Il2CppClass *attr_klass)
  1171. {
  1172. return Reflection::GetCustomAttribute(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo), attr_klass);
  1173. }
  1174. Il2CppArray* il2cpp_custom_attrs_construct(Il2CppCustomAttrInfo *ainfo)
  1175. {
  1176. return Reflection::ConstructCustomAttributes(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo));
  1177. }
  1178. void il2cpp_custom_attrs_free(Il2CppCustomAttrInfo *ainfo)
  1179. {
  1180. // nothing to free, we cache everything
  1181. }
  1182. void il2cpp_type_get_name_chunked(const Il2CppType * type, void(*chunkReportFunc)(void* data, void* userData), void* userData)
  1183. {
  1184. Type::GetNameChunkedRecurse(type, IL2CPP_TYPE_NAME_FORMAT_IL, chunkReportFunc, userData);
  1185. }
  1186. void il2cpp_class_set_userdata(Il2CppClass* klass, void* userdata)
  1187. {
  1188. klass->unity_user_data = userdata;
  1189. }
  1190. int il2cpp_class_get_userdata_offset()
  1191. {
  1192. return offsetof(struct Il2CppClass, unity_user_data);
  1193. }
  1194. void il2cpp_class_for_each(void(*klassReportFunc)(Il2CppClass* klass, void* userData), void* userData)
  1195. {
  1196. MemoryInformation::ReportIL2CppClasses(klassReportFunc, userData);
  1197. }
  1198. // Android
  1199. void il2cpp_unity_set_android_network_up_state_func(Il2CppAndroidUpStateFunc func)
  1200. {
  1201. AndroidRuntime::SetNetworkUpStateFunc(func);
  1202. }