2
0

gd_mono_utils.cpp 23 KB

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