gd_mono_utils.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /**************************************************************************/
  2. /* gd_mono_utils.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gd_mono_utils.h"
  31. #include <mono/metadata/debug-helpers.h>
  32. #include <mono/metadata/exception.h>
  33. #include "core/os/dir_access.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "core/reference.h"
  37. #ifdef TOOLS_ENABLED
  38. #include "editor/script_editor_debugger.h"
  39. #endif
  40. #include "../csharp_script.h"
  41. #include "../utils/macros.h"
  42. #include "gd_mono.h"
  43. #include "gd_mono_cache.h"
  44. #include "gd_mono_class.h"
  45. #include "gd_mono_marshal.h"
  46. #include "gd_mono_method_thunk.h"
  47. namespace GDMonoUtils {
  48. MonoObject *unmanaged_get_managed(Object *unmanaged) {
  49. if (!unmanaged)
  50. return NULL;
  51. if (unmanaged->get_script_instance()) {
  52. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(unmanaged->get_script_instance());
  53. if (cs_instance) {
  54. return cs_instance->get_mono_object();
  55. }
  56. }
  57. // If the owner does not have a CSharpInstance...
  58. void *data = unmanaged->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
  59. ERR_FAIL_NULL_V(data, NULL);
  60. CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->value();
  61. if (!script_binding.inited) {
  62. MutexLock lock(CSharpLanguage::get_singleton()->get_language_bind_mutex());
  63. if (!script_binding.inited) { // Other thread may have set it up
  64. // Already had a binding that needs to be setup
  65. CSharpLanguage::get_singleton()->setup_csharp_script_binding(script_binding, unmanaged);
  66. ERR_FAIL_COND_V(!script_binding.inited, NULL);
  67. }
  68. }
  69. Ref<MonoGCHandle> &gchandle = script_binding.gchandle;
  70. ERR_FAIL_COND_V(gchandle.is_null(), NULL);
  71. MonoObject *target = gchandle->get_target();
  72. if (target)
  73. return target;
  74. CSharpLanguage::get_singleton()->release_script_gchandle(gchandle);
  75. // Create a new one
  76. #ifdef DEBUG_ENABLED
  77. CRASH_COND(script_binding.type_name == StringName());
  78. CRASH_COND(script_binding.wrapper_class == NULL);
  79. #endif
  80. MonoObject *mono_object = GDMonoUtils::create_managed_for_godot_object(script_binding.wrapper_class, script_binding.type_name, unmanaged);
  81. ERR_FAIL_NULL_V(mono_object, NULL);
  82. gchandle->set_handle(MonoGCHandle::new_strong_handle(mono_object), MonoGCHandle::STRONG_HANDLE);
  83. // Tie managed to unmanaged
  84. Reference *ref = Object::cast_to<Reference>(unmanaged);
  85. if (ref) {
  86. // Unsafe refcount increment. The managed instance also counts as a reference.
  87. // This way if the unmanaged world has no references to our owner
  88. // but the managed instance is alive, the refcount will be 1 instead of 0.
  89. // See: godot_icall_Reference_Dtor(MonoObject *p_obj, Object *p_ptr)
  90. ref->reference();
  91. CSharpLanguage::get_singleton()->post_unsafe_reference(ref);
  92. }
  93. return mono_object;
  94. }
  95. void set_main_thread(MonoThread *p_thread) {
  96. mono_thread_set_main(p_thread);
  97. }
  98. MonoThread *attach_current_thread() {
  99. ERR_FAIL_COND_V(!GDMono::get_singleton() || !GDMono::get_singleton()->is_runtime_initialized(), NULL);
  100. MonoDomain *scripts_domain = GDMono::get_singleton()->get_scripts_domain();
  101. #ifndef GD_MONO_SINGLE_APPDOMAIN
  102. MonoThread *mono_thread = mono_thread_attach(scripts_domain ? scripts_domain : mono_get_root_domain());
  103. #else
  104. // The scripts domain is the root domain
  105. MonoThread *mono_thread = mono_thread_attach(scripts_domain);
  106. #endif
  107. ERR_FAIL_NULL_V(mono_thread, NULL);
  108. return mono_thread;
  109. }
  110. void detach_current_thread() {
  111. ERR_FAIL_COND(!GDMono::get_singleton() || !GDMono::get_singleton()->is_runtime_initialized());
  112. MonoThread *mono_thread = mono_thread_current();
  113. ERR_FAIL_NULL(mono_thread);
  114. mono_thread_detach(mono_thread);
  115. }
  116. void detach_current_thread(MonoThread *p_mono_thread) {
  117. ERR_FAIL_COND(!GDMono::get_singleton() || !GDMono::get_singleton()->is_runtime_initialized());
  118. ERR_FAIL_NULL(p_mono_thread);
  119. mono_thread_detach(p_mono_thread);
  120. }
  121. MonoThread *get_current_thread() {
  122. return mono_thread_current();
  123. }
  124. bool is_thread_attached() {
  125. return mono_domain_get() != NULL;
  126. }
  127. void runtime_object_init(MonoObject *p_this_obj, GDMonoClass *p_class, MonoException **r_exc) {
  128. GDMonoMethod *ctor = p_class->get_method(".ctor", 0);
  129. ERR_FAIL_NULL(ctor);
  130. ctor->invoke_raw(p_this_obj, NULL, r_exc);
  131. }
  132. GDMonoClass *get_object_class(MonoObject *p_object) {
  133. return GDMono::get_singleton()->get_class(mono_object_get_class(p_object));
  134. }
  135. GDMonoClass *type_get_proxy_class(const StringName &p_type) {
  136. String class_name = p_type;
  137. if (class_name[0] == '_')
  138. class_name = class_name.substr(1, class_name.length());
  139. GDMonoClass *klass = GDMono::get_singleton()->get_core_api_assembly()->get_class(BINDINGS_NAMESPACE, class_name);
  140. if (klass && klass->is_static()) {
  141. // A static class means this is a Godot singleton class. If an instance is needed we use Godot.Object.
  142. return GDMonoCache::cached_data.class_GodotObject;
  143. }
  144. #ifdef TOOLS_ENABLED
  145. if (!klass) {
  146. return GDMono::get_singleton()->get_editor_api_assembly()->get_class(BINDINGS_NAMESPACE, class_name);
  147. }
  148. #endif
  149. return klass;
  150. }
  151. GDMonoClass *get_class_native_base(GDMonoClass *p_class) {
  152. GDMonoClass *klass = p_class;
  153. do {
  154. const GDMonoAssembly *assembly = klass->get_assembly();
  155. if (assembly == GDMono::get_singleton()->get_core_api_assembly())
  156. return klass;
  157. #ifdef TOOLS_ENABLED
  158. if (assembly == GDMono::get_singleton()->get_editor_api_assembly())
  159. return klass;
  160. #endif
  161. } while ((klass = klass->get_parent_class()) != NULL);
  162. return NULL;
  163. }
  164. MonoObject *create_managed_for_godot_object(GDMonoClass *p_class, const StringName &p_native, Object *p_object) {
  165. bool parent_is_object_class = ClassDB::is_parent_class(p_object->get_class_name(), p_native);
  166. ERR_FAIL_COND_V_MSG(!parent_is_object_class, NULL,
  167. "Type inherits from native type '" + p_native + "', so it can't be instanced in object of type: '" + p_object->get_class() + "'.");
  168. MonoObject *mono_object = mono_object_new(mono_domain_get(), p_class->get_mono_ptr());
  169. ERR_FAIL_NULL_V(mono_object, NULL);
  170. CACHED_FIELD(GodotObject, ptr)->set_value_raw(mono_object, p_object);
  171. // Construct
  172. GDMonoUtils::runtime_object_init(mono_object, p_class);
  173. return mono_object;
  174. }
  175. MonoObject *create_managed_from(const NodePath &p_from) {
  176. MonoObject *mono_object = mono_object_new(mono_domain_get(), CACHED_CLASS_RAW(NodePath));
  177. ERR_FAIL_NULL_V(mono_object, NULL);
  178. // Construct
  179. GDMonoUtils::runtime_object_init(mono_object, CACHED_CLASS(NodePath));
  180. CACHED_FIELD(NodePath, ptr)->set_value_raw(mono_object, memnew(NodePath(p_from)));
  181. return mono_object;
  182. }
  183. MonoObject *create_managed_from(const RID &p_from) {
  184. MonoObject *mono_object = mono_object_new(mono_domain_get(), CACHED_CLASS_RAW(RID));
  185. ERR_FAIL_NULL_V(mono_object, NULL);
  186. // Construct
  187. GDMonoUtils::runtime_object_init(mono_object, CACHED_CLASS(RID));
  188. CACHED_FIELD(RID, ptr)->set_value_raw(mono_object, memnew(RID(p_from)));
  189. return mono_object;
  190. }
  191. MonoObject *create_managed_from(const Array &p_from, GDMonoClass *p_class) {
  192. MonoObject *mono_object = mono_object_new(mono_domain_get(), p_class->get_mono_ptr());
  193. ERR_FAIL_NULL_V(mono_object, NULL);
  194. // Search constructor that takes a pointer as parameter
  195. MonoMethod *m;
  196. void *iter = NULL;
  197. while ((m = mono_class_get_methods(p_class->get_mono_ptr(), &iter))) {
  198. if (strcmp(mono_method_get_name(m), ".ctor") == 0) {
  199. MonoMethodSignature *sig = mono_method_signature(m);
  200. void *front = NULL;
  201. if (mono_signature_get_param_count(sig) == 1 &&
  202. mono_class_from_mono_type(mono_signature_get_params(sig, &front)) == CACHED_CLASS(IntPtr)->get_mono_ptr()) {
  203. break;
  204. }
  205. }
  206. }
  207. CRASH_COND(m == NULL);
  208. Array *new_array = memnew(Array(p_from));
  209. void *args[1] = { &new_array };
  210. MonoException *exc = NULL;
  211. GDMonoUtils::runtime_invoke(m, mono_object, args, &exc);
  212. UNHANDLED_EXCEPTION(exc);
  213. return mono_object;
  214. }
  215. MonoObject *create_managed_from(const Dictionary &p_from, GDMonoClass *p_class) {
  216. MonoObject *mono_object = mono_object_new(mono_domain_get(), p_class->get_mono_ptr());
  217. ERR_FAIL_NULL_V(mono_object, NULL);
  218. // Search constructor that takes a pointer as parameter
  219. MonoMethod *m;
  220. void *iter = NULL;
  221. while ((m = mono_class_get_methods(p_class->get_mono_ptr(), &iter))) {
  222. if (strcmp(mono_method_get_name(m), ".ctor") == 0) {
  223. MonoMethodSignature *sig = mono_method_signature(m);
  224. void *front = NULL;
  225. if (mono_signature_get_param_count(sig) == 1 &&
  226. mono_class_from_mono_type(mono_signature_get_params(sig, &front)) == CACHED_CLASS(IntPtr)->get_mono_ptr()) {
  227. break;
  228. }
  229. }
  230. }
  231. CRASH_COND(m == NULL);
  232. Dictionary *new_dict = memnew(Dictionary(p_from));
  233. void *args[1] = { &new_dict };
  234. MonoException *exc = NULL;
  235. GDMonoUtils::runtime_invoke(m, mono_object, args, &exc);
  236. UNHANDLED_EXCEPTION(exc);
  237. return mono_object;
  238. }
  239. MonoDomain *create_domain(const String &p_friendly_name) {
  240. print_verbose("Mono: Creating domain '" + p_friendly_name + "'...");
  241. MonoDomain *domain = mono_domain_create_appdomain((char *)p_friendly_name.utf8().get_data(), NULL);
  242. if (domain) {
  243. // Workaround to avoid this exception:
  244. // System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system.
  245. // ---> System.ArgumentException: The 'ExeConfigFilename' argument cannot be null.
  246. mono_domain_set_config(domain, ".", "");
  247. }
  248. return domain;
  249. }
  250. String get_type_desc(MonoType *p_type) {
  251. return mono_type_full_name(p_type);
  252. }
  253. String get_type_desc(MonoReflectionType *p_reftype) {
  254. return get_type_desc(mono_reflection_type_get_type(p_reftype));
  255. }
  256. String get_exception_name_and_message(MonoException *p_exc) {
  257. String res;
  258. MonoClass *klass = mono_object_get_class((MonoObject *)p_exc);
  259. MonoType *type = mono_class_get_type(klass);
  260. char *full_name = mono_type_full_name(type);
  261. res += full_name;
  262. mono_free(full_name);
  263. res += ": ";
  264. MonoProperty *prop = mono_class_get_property_from_name(klass, "Message");
  265. MonoString *msg = (MonoString *)property_get_value(prop, (MonoObject *)p_exc, NULL, NULL);
  266. res += GDMonoMarshal::mono_string_to_godot(msg);
  267. return res;
  268. }
  269. void set_exception_message(MonoException *p_exc, String message) {
  270. MonoClass *klass = mono_object_get_class((MonoObject *)p_exc);
  271. MonoProperty *prop = mono_class_get_property_from_name(klass, "Message");
  272. MonoString *msg = GDMonoMarshal::mono_string_from_godot(message);
  273. void *params[1] = { msg };
  274. property_set_value(prop, (MonoObject *)p_exc, params, NULL);
  275. }
  276. void debug_print_unhandled_exception(MonoException *p_exc) {
  277. print_unhandled_exception(p_exc);
  278. debug_send_unhandled_exception_error(p_exc);
  279. }
  280. void debug_send_unhandled_exception_error(MonoException *p_exc) {
  281. #ifdef DEBUG_ENABLED
  282. if (!ScriptDebugger::get_singleton()) {
  283. #ifdef TOOLS_ENABLED
  284. if (Engine::get_singleton()->is_editor_hint()) {
  285. ERR_PRINT(GDMonoUtils::get_exception_name_and_message(p_exc));
  286. }
  287. #endif
  288. return;
  289. }
  290. _TLS_RECURSION_GUARD_;
  291. ScriptLanguage::StackInfo separator;
  292. separator.file = String();
  293. separator.func = "--- " + RTR("End of inner exception stack trace") + " ---";
  294. separator.line = 0;
  295. Vector<ScriptLanguage::StackInfo> si;
  296. String exc_msg;
  297. while (p_exc != NULL) {
  298. GDMonoClass *st_klass = CACHED_CLASS(System_Diagnostics_StackTrace);
  299. MonoObject *stack_trace = mono_object_new(mono_domain_get(), st_klass->get_mono_ptr());
  300. MonoBoolean need_file_info = true;
  301. void *ctor_args[2] = { p_exc, &need_file_info };
  302. MonoException *unexpected_exc = NULL;
  303. CACHED_METHOD(System_Diagnostics_StackTrace, ctor_Exception_bool)->invoke_raw(stack_trace, ctor_args, &unexpected_exc);
  304. if (unexpected_exc) {
  305. GDMonoInternals::unhandled_exception(unexpected_exc);
  306. return;
  307. }
  308. Vector<ScriptLanguage::StackInfo> _si;
  309. if (stack_trace != NULL) {
  310. _si = CSharpLanguage::get_singleton()->stack_trace_get_info(stack_trace);
  311. for (int i = _si.size() - 1; i >= 0; i--)
  312. si.insert(0, _si[i]);
  313. }
  314. exc_msg += (exc_msg.length() > 0 ? " ---> " : "") + GDMonoUtils::get_exception_name_and_message(p_exc);
  315. GDMonoClass *exc_class = GDMono::get_singleton()->get_class(mono_get_exception_class());
  316. GDMonoProperty *inner_exc_prop = exc_class->get_property("InnerException");
  317. CRASH_COND(inner_exc_prop == NULL);
  318. MonoObject *inner_exc = inner_exc_prop->get_value((MonoObject *)p_exc);
  319. if (inner_exc != NULL)
  320. si.insert(0, separator);
  321. p_exc = (MonoException *)inner_exc;
  322. }
  323. String file = si.size() ? si[0].file : __FILE__;
  324. String func = si.size() ? si[0].func : FUNCTION_STR;
  325. int line = si.size() ? si[0].line : __LINE__;
  326. String error_msg = "Unhandled exception";
  327. ScriptDebugger::get_singleton()->send_error(func, file, line, error_msg, exc_msg, ERR_HANDLER_ERROR, si);
  328. #endif
  329. }
  330. void debug_unhandled_exception(MonoException *p_exc) {
  331. GDMonoInternals::unhandled_exception(p_exc); // prints the exception as well
  332. }
  333. void print_unhandled_exception(MonoException *p_exc) {
  334. mono_print_unhandled_exception((MonoObject *)p_exc);
  335. }
  336. void set_pending_exception(MonoException *p_exc) {
  337. #ifdef NO_PENDING_EXCEPTIONS
  338. debug_unhandled_exception(p_exc);
  339. #else
  340. if (get_runtime_invoke_count() == 0) {
  341. debug_unhandled_exception(p_exc);
  342. return;
  343. }
  344. if (!mono_runtime_set_pending_exception(p_exc, false)) {
  345. ERR_PRINT("Exception thrown from managed code, but it could not be set as pending:");
  346. GDMonoUtils::debug_print_unhandled_exception(p_exc);
  347. }
  348. #endif
  349. }
  350. _THREAD_LOCAL_(int)
  351. current_invoke_count = 0;
  352. MonoObject *runtime_invoke(MonoMethod *p_method, void *p_obj, void **p_params, MonoException **r_exc) {
  353. GD_MONO_BEGIN_RUNTIME_INVOKE;
  354. MonoObject *ret = mono_runtime_invoke(p_method, p_obj, p_params, (MonoObject **)r_exc);
  355. GD_MONO_END_RUNTIME_INVOKE;
  356. return ret;
  357. }
  358. MonoString *object_to_string(MonoObject *p_obj, MonoException **r_exc) {
  359. GD_MONO_BEGIN_RUNTIME_INVOKE;
  360. MonoString *ret = mono_object_to_string(p_obj, (MonoObject **)r_exc);
  361. GD_MONO_END_RUNTIME_INVOKE;
  362. return ret;
  363. }
  364. void property_set_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc) {
  365. GD_MONO_BEGIN_RUNTIME_INVOKE;
  366. mono_property_set_value(p_prop, p_obj, p_params, (MonoObject **)r_exc);
  367. GD_MONO_END_RUNTIME_INVOKE;
  368. }
  369. MonoObject *property_get_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc) {
  370. GD_MONO_BEGIN_RUNTIME_INVOKE;
  371. MonoObject *ret = mono_property_get_value(p_prop, p_obj, p_params, (MonoObject **)r_exc);
  372. GD_MONO_END_RUNTIME_INVOKE;
  373. return ret;
  374. }
  375. uint64_t unbox_enum_value(MonoObject *p_boxed, MonoType *p_enum_basetype, bool &r_error) {
  376. r_error = false;
  377. switch (mono_type_get_type(p_enum_basetype)) {
  378. case MONO_TYPE_BOOLEAN:
  379. return (bool)GDMonoMarshal::unbox<MonoBoolean>(p_boxed) ? 1 : 0;
  380. case MONO_TYPE_CHAR:
  381. return GDMonoMarshal::unbox<uint16_t>(p_boxed);
  382. case MONO_TYPE_U1:
  383. return GDMonoMarshal::unbox<uint8_t>(p_boxed);
  384. case MONO_TYPE_U2:
  385. return GDMonoMarshal::unbox<uint16_t>(p_boxed);
  386. case MONO_TYPE_U4:
  387. return GDMonoMarshal::unbox<uint32_t>(p_boxed);
  388. case MONO_TYPE_U8:
  389. return GDMonoMarshal::unbox<uint64_t>(p_boxed);
  390. case MONO_TYPE_I1:
  391. return GDMonoMarshal::unbox<int8_t>(p_boxed);
  392. case MONO_TYPE_I2:
  393. return GDMonoMarshal::unbox<int16_t>(p_boxed);
  394. case MONO_TYPE_I4:
  395. return GDMonoMarshal::unbox<int32_t>(p_boxed);
  396. case MONO_TYPE_I8:
  397. return GDMonoMarshal::unbox<int64_t>(p_boxed);
  398. default:
  399. r_error = true;
  400. return 0;
  401. }
  402. }
  403. void dispose(MonoObject *p_mono_object, MonoException **r_exc) {
  404. CACHED_METHOD_THUNK(GodotObject, Dispose).invoke(p_mono_object, r_exc);
  405. }
  406. namespace Marshal {
  407. #ifdef MONO_GLUE_ENABLED
  408. #ifdef TOOLS_ENABLED
  409. #define NO_GLUE_RET(m_ret) \
  410. { \
  411. if (!GDMonoCache::cached_data.godot_api_cache_updated) \
  412. return m_ret; \
  413. }
  414. #else
  415. #define NO_GLUE_RET(m_ret) \
  416. {}
  417. #endif
  418. #else
  419. #define NO_GLUE_RET(m_ret) \
  420. { return m_ret; }
  421. #endif
  422. bool type_is_generic_array(MonoReflectionType *p_reftype) {
  423. NO_GLUE_RET(false);
  424. MonoException *exc = NULL;
  425. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericArray).invoke(p_reftype, &exc);
  426. UNHANDLED_EXCEPTION(exc);
  427. return (bool)res;
  428. }
  429. bool type_is_generic_dictionary(MonoReflectionType *p_reftype) {
  430. NO_GLUE_RET(false);
  431. MonoException *exc = NULL;
  432. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericDictionary).invoke(p_reftype, &exc);
  433. UNHANDLED_EXCEPTION(exc);
  434. return (bool)res;
  435. }
  436. bool type_is_system_generic_list(MonoReflectionType *p_reftype) {
  437. NO_GLUE_RET(false);
  438. MonoException *exc = NULL;
  439. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsSystemGenericList).invoke(p_reftype, &exc);
  440. UNHANDLED_EXCEPTION(exc);
  441. return (bool)res;
  442. }
  443. bool type_is_system_generic_dictionary(MonoReflectionType *p_reftype) {
  444. NO_GLUE_RET(false);
  445. MonoException *exc = NULL;
  446. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsSystemGenericDictionary).invoke(p_reftype, &exc);
  447. UNHANDLED_EXCEPTION(exc);
  448. return (bool)res;
  449. }
  450. bool type_is_generic_ienumerable(MonoReflectionType *p_reftype) {
  451. NO_GLUE_RET(false);
  452. MonoException *exc = NULL;
  453. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericIEnumerable).invoke(p_reftype, &exc);
  454. UNHANDLED_EXCEPTION(exc);
  455. return (bool)res;
  456. }
  457. bool type_is_generic_icollection(MonoReflectionType *p_reftype) {
  458. NO_GLUE_RET(false);
  459. MonoException *exc = NULL;
  460. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericICollection).invoke(p_reftype, &exc);
  461. UNHANDLED_EXCEPTION(exc);
  462. return (bool)res;
  463. }
  464. bool type_is_generic_idictionary(MonoReflectionType *p_reftype) {
  465. NO_GLUE_RET(false);
  466. MonoException *exc = NULL;
  467. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericIDictionary).invoke(p_reftype, &exc);
  468. UNHANDLED_EXCEPTION(exc);
  469. return (bool)res;
  470. }
  471. bool type_has_flags_attribute(MonoReflectionType *p_reftype) {
  472. NO_GLUE_RET(false);
  473. MonoException *exc = nullptr;
  474. MonoBoolean res = CACHED_METHOD_THUNK(MarshalUtils, TypeHasFlagsAttribute).invoke(p_reftype, &exc);
  475. UNHANDLED_EXCEPTION(exc);
  476. return (bool)res;
  477. }
  478. void get_generic_type_definition(MonoReflectionType *p_reftype, MonoReflectionType **r_generic_reftype) {
  479. MonoException *exc = nullptr;
  480. CACHED_METHOD_THUNK(MarshalUtils, GetGenericTypeDefinition).invoke(p_reftype, r_generic_reftype, &exc);
  481. UNHANDLED_EXCEPTION(exc);
  482. }
  483. void array_get_element_type(MonoReflectionType *p_array_reftype, MonoReflectionType **r_elem_reftype) {
  484. MonoException *exc = NULL;
  485. CACHED_METHOD_THUNK(MarshalUtils, ArrayGetElementType).invoke(p_array_reftype, r_elem_reftype, &exc);
  486. UNHANDLED_EXCEPTION(exc);
  487. }
  488. void dictionary_get_key_value_types(MonoReflectionType *p_dict_reftype, MonoReflectionType **r_key_reftype, MonoReflectionType **r_value_reftype) {
  489. MonoException *exc = NULL;
  490. CACHED_METHOD_THUNK(MarshalUtils, DictionaryGetKeyValueTypes).invoke(p_dict_reftype, r_key_reftype, r_value_reftype, &exc);
  491. UNHANDLED_EXCEPTION(exc);
  492. }
  493. GDMonoClass *make_generic_array_type(MonoReflectionType *p_elem_reftype) {
  494. NO_GLUE_RET(NULL);
  495. MonoException *exc = NULL;
  496. MonoReflectionType *reftype = CACHED_METHOD_THUNK(MarshalUtils, MakeGenericArrayType).invoke(p_elem_reftype, &exc);
  497. UNHANDLED_EXCEPTION(exc);
  498. return GDMono::get_singleton()->get_class(mono_class_from_mono_type(mono_reflection_type_get_type(reftype)));
  499. }
  500. GDMonoClass *make_generic_dictionary_type(MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype) {
  501. NO_GLUE_RET(NULL);
  502. MonoException *exc = NULL;
  503. MonoReflectionType *reftype = CACHED_METHOD_THUNK(MarshalUtils, MakeGenericDictionaryType).invoke(p_key_reftype, p_value_reftype, &exc);
  504. UNHANDLED_EXCEPTION(exc);
  505. return GDMono::get_singleton()->get_class(mono_class_from_mono_type(mono_reflection_type_get_type(reftype)));
  506. }
  507. } // namespace Marshal
  508. ScopeThreadAttach::ScopeThreadAttach() :
  509. mono_thread(NULL) {
  510. if (likely(GDMono::get_singleton()) && likely(GDMono::get_singleton()->is_runtime_initialized()) && unlikely(!mono_domain_get())) {
  511. mono_thread = GDMonoUtils::attach_current_thread();
  512. }
  513. }
  514. ScopeThreadAttach::~ScopeThreadAttach() {
  515. if (unlikely(mono_thread)) {
  516. GDMonoUtils::detach_current_thread(mono_thread);
  517. }
  518. }
  519. // namespace Marshal
  520. } // namespace GDMonoUtils