gd_mono_utils.cpp 23 KB

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