gd_mono_utils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "os/dir_access.h"
  33. #include "os/os.h"
  34. #include "project_settings.h"
  35. #include "reference.h"
  36. #include "../csharp_script.h"
  37. #include "../utils/macros.h"
  38. #include "gd_mono.h"
  39. #include "gd_mono_class.h"
  40. #include "gd_mono_marshal.h"
  41. namespace GDMonoUtils {
  42. MonoCache mono_cache;
  43. #define CACHE_AND_CHECK(m_var, m_val) \
  44. { \
  45. m_var = m_val; \
  46. if (!m_var) { \
  47. ERR_EXPLAIN("Mono Cache: Member " #m_var " is null"); \
  48. ERR_FAIL(); \
  49. } \
  50. }
  51. #define CACHE_CLASS_AND_CHECK(m_class, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.class_##m_class, m_val)
  52. #define CACHE_NS_CLASS_AND_CHECK(m_ns, m_class, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.class_##m_ns##_##m_class, m_val)
  53. #define CACHE_RAW_MONO_CLASS_AND_CHECK(m_class, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.rawclass_##m_class, m_val)
  54. #define CACHE_FIELD_AND_CHECK(m_class, m_field, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.field_##m_class##_##m_field, m_val)
  55. #define CACHE_METHOD_AND_CHECK(m_class, m_method, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.method_##m_class##_##m_method, m_val)
  56. #define CACHE_METHOD_THUNK_AND_CHECK(m_class, m_method, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.methodthunk_##m_class##_##m_method, m_val)
  57. void MonoCache::clear_members() {
  58. class_MonoObject = NULL;
  59. class_bool = NULL;
  60. class_int8_t = NULL;
  61. class_int16_t = NULL;
  62. class_int32_t = NULL;
  63. class_int64_t = NULL;
  64. class_uint8_t = NULL;
  65. class_uint16_t = NULL;
  66. class_uint32_t = NULL;
  67. class_uint64_t = NULL;
  68. class_float = NULL;
  69. class_double = NULL;
  70. class_String = NULL;
  71. class_IntPtr = NULL;
  72. #ifdef DEBUG_ENABLED
  73. class_System_Diagnostics_StackTrace = NULL;
  74. methodthunk_System_Diagnostics_StackTrace_GetFrames = NULL;
  75. method_System_Diagnostics_StackTrace_ctor_bool = NULL;
  76. method_System_Diagnostics_StackTrace_ctor_Exception_bool = NULL;
  77. #endif
  78. rawclass_Dictionary = NULL;
  79. class_Vector2 = NULL;
  80. class_Rect2 = NULL;
  81. class_Transform2D = NULL;
  82. class_Vector3 = NULL;
  83. class_Basis = NULL;
  84. class_Quat = NULL;
  85. class_Transform = NULL;
  86. class_AABB = NULL;
  87. class_Color = NULL;
  88. class_Plane = NULL;
  89. class_NodePath = NULL;
  90. class_RID = NULL;
  91. class_GodotObject = NULL;
  92. class_GodotReference = NULL;
  93. class_Node = NULL;
  94. class_Control = NULL;
  95. class_Spatial = NULL;
  96. class_WeakRef = NULL;
  97. class_MarshalUtils = NULL;
  98. #ifdef DEBUG_ENABLED
  99. class_DebuggingUtils = NULL;
  100. methodthunk_DebuggingUtils_GetStackFrameInfo = NULL;
  101. #endif
  102. class_ExportAttribute = NULL;
  103. field_ExportAttribute_hint = NULL;
  104. field_ExportAttribute_hintString = NULL;
  105. class_SignalAttribute = NULL;
  106. class_ToolAttribute = NULL;
  107. class_RemoteAttribute = NULL;
  108. class_SyncAttribute = NULL;
  109. class_MasterAttribute = NULL;
  110. class_SlaveAttribute = NULL;
  111. class_RemoteSyncAttribute = NULL;
  112. class_MasterSyncAttribute = NULL;
  113. class_SlaveSyncAttribute = NULL;
  114. class_GodotMethodAttribute = NULL;
  115. field_GodotMethodAttribute_methodName = NULL;
  116. field_GodotObject_ptr = NULL;
  117. field_NodePath_ptr = NULL;
  118. field_Image_ptr = NULL;
  119. field_RID_ptr = NULL;
  120. methodthunk_MarshalUtils_DictionaryToArrays = NULL;
  121. methodthunk_MarshalUtils_ArraysToDictionary = NULL;
  122. methodthunk_SignalAwaiter_SignalCallback = NULL;
  123. methodthunk_SignalAwaiter_FailureCallback = NULL;
  124. methodthunk_GodotTaskScheduler_Activate = NULL;
  125. task_scheduler_handle = Ref<MonoGCHandle>();
  126. }
  127. void MonoCache::cleanup() {
  128. corlib_cache_updated = false;
  129. godot_api_cache_updated = false;
  130. }
  131. #define GODOT_API_CLASS(m_class) (GDMono::get_singleton()->get_core_api_assembly()->get_class(BINDINGS_NAMESPACE, #m_class))
  132. void update_corlib_cache() {
  133. CACHE_CLASS_AND_CHECK(MonoObject, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_object_class()));
  134. CACHE_CLASS_AND_CHECK(bool, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_boolean_class()));
  135. CACHE_CLASS_AND_CHECK(int8_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_sbyte_class()));
  136. CACHE_CLASS_AND_CHECK(int16_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_int16_class()));
  137. CACHE_CLASS_AND_CHECK(int32_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_int32_class()));
  138. CACHE_CLASS_AND_CHECK(int64_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_int64_class()));
  139. CACHE_CLASS_AND_CHECK(uint8_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_byte_class()));
  140. CACHE_CLASS_AND_CHECK(uint16_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_uint16_class()));
  141. CACHE_CLASS_AND_CHECK(uint32_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_uint32_class()));
  142. CACHE_CLASS_AND_CHECK(uint64_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_uint64_class()));
  143. CACHE_CLASS_AND_CHECK(float, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_single_class()));
  144. CACHE_CLASS_AND_CHECK(double, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_double_class()));
  145. CACHE_CLASS_AND_CHECK(String, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_string_class()));
  146. CACHE_CLASS_AND_CHECK(IntPtr, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_intptr_class()));
  147. #ifdef DEBUG_ENABLED
  148. CACHE_CLASS_AND_CHECK(System_Diagnostics_StackTrace, GDMono::get_singleton()->get_corlib_assembly()->get_class("System.Diagnostics", "StackTrace"));
  149. CACHE_METHOD_THUNK_AND_CHECK(System_Diagnostics_StackTrace, GetFrames, (StackTrace_GetFrames)CACHED_CLASS(System_Diagnostics_StackTrace)->get_method("GetFrames")->get_thunk());
  150. CACHE_METHOD_AND_CHECK(System_Diagnostics_StackTrace, ctor_bool, CACHED_CLASS(System_Diagnostics_StackTrace)->get_method_with_desc("System.Diagnostics.StackTrace:.ctor(bool)", true));
  151. CACHE_METHOD_AND_CHECK(System_Diagnostics_StackTrace, ctor_Exception_bool, CACHED_CLASS(System_Diagnostics_StackTrace)->get_method_with_desc("System.Diagnostics.StackTrace:.ctor(System.Exception,bool)", true));
  152. #endif
  153. mono_cache.corlib_cache_updated = true;
  154. }
  155. void update_godot_api_cache() {
  156. CACHE_CLASS_AND_CHECK(Vector2, GODOT_API_CLASS(Vector2));
  157. CACHE_CLASS_AND_CHECK(Rect2, GODOT_API_CLASS(Rect2));
  158. CACHE_CLASS_AND_CHECK(Transform2D, GODOT_API_CLASS(Transform2D));
  159. CACHE_CLASS_AND_CHECK(Vector3, GODOT_API_CLASS(Vector3));
  160. CACHE_CLASS_AND_CHECK(Basis, GODOT_API_CLASS(Basis));
  161. CACHE_CLASS_AND_CHECK(Quat, GODOT_API_CLASS(Quat));
  162. CACHE_CLASS_AND_CHECK(Transform, GODOT_API_CLASS(Transform));
  163. CACHE_CLASS_AND_CHECK(AABB, GODOT_API_CLASS(AABB));
  164. CACHE_CLASS_AND_CHECK(Color, GODOT_API_CLASS(Color));
  165. CACHE_CLASS_AND_CHECK(Plane, GODOT_API_CLASS(Plane));
  166. CACHE_CLASS_AND_CHECK(NodePath, GODOT_API_CLASS(NodePath));
  167. CACHE_CLASS_AND_CHECK(RID, GODOT_API_CLASS(RID));
  168. CACHE_CLASS_AND_CHECK(GodotObject, GODOT_API_CLASS(Object));
  169. CACHE_CLASS_AND_CHECK(GodotReference, GODOT_API_CLASS(Reference));
  170. CACHE_CLASS_AND_CHECK(Node, GODOT_API_CLASS(Node));
  171. CACHE_CLASS_AND_CHECK(Control, GODOT_API_CLASS(Control));
  172. CACHE_CLASS_AND_CHECK(Spatial, GODOT_API_CLASS(Spatial));
  173. CACHE_CLASS_AND_CHECK(WeakRef, GODOT_API_CLASS(WeakRef));
  174. CACHE_CLASS_AND_CHECK(MarshalUtils, GODOT_API_CLASS(MarshalUtils));
  175. #ifdef DEBUG_ENABLED
  176. CACHE_CLASS_AND_CHECK(DebuggingUtils, GODOT_API_CLASS(DebuggingUtils));
  177. #endif
  178. // Attributes
  179. CACHE_CLASS_AND_CHECK(ExportAttribute, GODOT_API_CLASS(ExportAttribute));
  180. CACHE_FIELD_AND_CHECK(ExportAttribute, hint, CACHED_CLASS(ExportAttribute)->get_field("hint"));
  181. CACHE_FIELD_AND_CHECK(ExportAttribute, hintString, CACHED_CLASS(ExportAttribute)->get_field("hintString"));
  182. CACHE_CLASS_AND_CHECK(SignalAttribute, GODOT_API_CLASS(SignalAttribute));
  183. CACHE_CLASS_AND_CHECK(ToolAttribute, GODOT_API_CLASS(ToolAttribute));
  184. CACHE_CLASS_AND_CHECK(RemoteAttribute, GODOT_API_CLASS(RemoteAttribute));
  185. CACHE_CLASS_AND_CHECK(SyncAttribute, GODOT_API_CLASS(SyncAttribute));
  186. CACHE_CLASS_AND_CHECK(MasterAttribute, GODOT_API_CLASS(MasterAttribute));
  187. CACHE_CLASS_AND_CHECK(SlaveAttribute, GODOT_API_CLASS(SlaveAttribute));
  188. CACHE_CLASS_AND_CHECK(RemoteSyncAttribute, GODOT_API_CLASS(RemoteSyncAttribute));
  189. CACHE_CLASS_AND_CHECK(MasterSyncAttribute, GODOT_API_CLASS(MasterSyncAttribute));
  190. CACHE_CLASS_AND_CHECK(SlaveSyncAttribute, GODOT_API_CLASS(SlaveSyncAttribute));
  191. CACHE_CLASS_AND_CHECK(GodotMethodAttribute, GODOT_API_CLASS(GodotMethodAttribute));
  192. CACHE_FIELD_AND_CHECK(GodotMethodAttribute, methodName, CACHED_CLASS(GodotMethodAttribute)->get_field("methodName"));
  193. CACHE_FIELD_AND_CHECK(GodotObject, ptr, CACHED_CLASS(GodotObject)->get_field(BINDINGS_PTR_FIELD));
  194. CACHE_FIELD_AND_CHECK(NodePath, ptr, CACHED_CLASS(NodePath)->get_field(BINDINGS_PTR_FIELD));
  195. CACHE_FIELD_AND_CHECK(RID, ptr, CACHED_CLASS(RID)->get_field(BINDINGS_PTR_FIELD));
  196. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, DictionaryToArrays, (MarshalUtils_DictToArrays)CACHED_CLASS(MarshalUtils)->get_method("DictionaryToArrays", 3)->get_thunk());
  197. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, ArraysToDictionary, (MarshalUtils_ArraysToDict)CACHED_CLASS(MarshalUtils)->get_method("ArraysToDictionary", 2)->get_thunk());
  198. CACHE_METHOD_THUNK_AND_CHECK(SignalAwaiter, SignalCallback, (SignalAwaiter_SignalCallback)GODOT_API_CLASS(SignalAwaiter)->get_method("SignalCallback", 1)->get_thunk());
  199. CACHE_METHOD_THUNK_AND_CHECK(SignalAwaiter, FailureCallback, (SignalAwaiter_FailureCallback)GODOT_API_CLASS(SignalAwaiter)->get_method("FailureCallback", 0)->get_thunk());
  200. CACHE_METHOD_THUNK_AND_CHECK(GodotTaskScheduler, Activate, (GodotTaskScheduler_Activate)GODOT_API_CLASS(GodotTaskScheduler)->get_method("Activate", 0)->get_thunk());
  201. #ifdef DEBUG_ENABLED
  202. CACHE_METHOD_THUNK_AND_CHECK(DebuggingUtils, GetStackFrameInfo, (DebugUtils_StackFrameInfo)GODOT_API_CLASS(DebuggingUtils)->get_method("GetStackFrameInfo", 4)->get_thunk());
  203. #endif
  204. {
  205. /*
  206. * TODO Right now we only support Dictionary<object, object>.
  207. * It would be great if we could support other key/value types
  208. * without forcing the user to copy the entries.
  209. */
  210. GDMonoMethod *method_get_dict_type = CACHED_CLASS(MarshalUtils)->get_method("GetDictionaryType", 0);
  211. ERR_FAIL_NULL(method_get_dict_type);
  212. MonoReflectionType *dict_refl_type = (MonoReflectionType *)method_get_dict_type->invoke(NULL);
  213. ERR_FAIL_NULL(dict_refl_type);
  214. MonoType *dict_type = mono_reflection_type_get_type(dict_refl_type);
  215. ERR_FAIL_NULL(dict_type);
  216. CACHE_RAW_MONO_CLASS_AND_CHECK(Dictionary, mono_class_from_mono_type(dict_type));
  217. }
  218. MonoObject *task_scheduler = mono_object_new(SCRIPTS_DOMAIN, GODOT_API_CLASS(GodotTaskScheduler)->get_mono_ptr());
  219. mono_runtime_object_init(task_scheduler);
  220. mono_cache.task_scheduler_handle = MonoGCHandle::create_strong(task_scheduler);
  221. mono_cache.godot_api_cache_updated = true;
  222. }
  223. void clear_cache() {
  224. mono_cache.cleanup();
  225. mono_cache.clear_members();
  226. }
  227. MonoObject *unmanaged_get_managed(Object *unmanaged) {
  228. if (unmanaged) {
  229. if (unmanaged->get_script_instance()) {
  230. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(unmanaged->get_script_instance());
  231. if (cs_instance) {
  232. return cs_instance->get_mono_object();
  233. }
  234. }
  235. // Only called if the owner does not have a CSharpInstance
  236. void *data = unmanaged->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
  237. if (data) {
  238. return ((Map<Object *, Ref<MonoGCHandle> >::Element *)data)->value()->get_target();
  239. }
  240. }
  241. return NULL;
  242. }
  243. void set_main_thread(MonoThread *p_thread) {
  244. mono_thread_set_main(p_thread);
  245. }
  246. void attach_current_thread() {
  247. ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
  248. MonoThread *mono_thread = mono_thread_attach(SCRIPTS_DOMAIN);
  249. ERR_FAIL_NULL(mono_thread);
  250. }
  251. void detach_current_thread() {
  252. ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
  253. MonoThread *mono_thread = mono_thread_current();
  254. ERR_FAIL_NULL(mono_thread);
  255. mono_thread_detach(mono_thread);
  256. }
  257. MonoThread *get_current_thread() {
  258. return mono_thread_current();
  259. }
  260. GDMonoClass *get_object_class(MonoObject *p_object) {
  261. return GDMono::get_singleton()->get_class(mono_object_get_class(p_object));
  262. }
  263. GDMonoClass *type_get_proxy_class(const StringName &p_type) {
  264. String class_name = p_type;
  265. if (class_name[0] == '_')
  266. class_name = class_name.substr(1, class_name.length());
  267. GDMonoClass *klass = GDMono::get_singleton()->get_core_api_assembly()->get_class(BINDINGS_NAMESPACE, class_name);
  268. #ifdef TOOLS_ENABLED
  269. if (!klass) {
  270. return GDMono::get_singleton()->get_editor_api_assembly()->get_class(BINDINGS_NAMESPACE, class_name);
  271. }
  272. #endif
  273. return klass;
  274. }
  275. GDMonoClass *get_class_native_base(GDMonoClass *p_class) {
  276. GDMonoClass *klass = p_class;
  277. do {
  278. const GDMonoAssembly *assembly = klass->get_assembly();
  279. if (assembly == GDMono::get_singleton()->get_core_api_assembly())
  280. return klass;
  281. #ifdef TOOLS_ENABLED
  282. if (assembly == GDMono::get_singleton()->get_editor_api_assembly())
  283. return klass;
  284. #endif
  285. } while ((klass = klass->get_parent_class()) != NULL);
  286. return NULL;
  287. }
  288. MonoObject *create_managed_for_godot_object(GDMonoClass *p_class, const StringName &p_native, Object *p_object) {
  289. String object_type = p_object->get_class_name();
  290. if (object_type[0] == '_')
  291. object_type = object_type.substr(1, object_type.length());
  292. if (!ClassDB::is_parent_class(object_type, p_native)) {
  293. ERR_EXPLAIN("Type inherits from native type '" + p_native + "', so it can't be instanced in object of type: '" + p_object->get_class() + "'");
  294. ERR_FAIL_V(NULL);
  295. }
  296. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, p_class->get_mono_ptr());
  297. ERR_FAIL_NULL_V(mono_object, NULL);
  298. CACHED_FIELD(GodotObject, ptr)->set_value_raw(mono_object, p_object);
  299. // Construct
  300. mono_runtime_object_init(mono_object);
  301. return mono_object;
  302. }
  303. MonoObject *create_managed_from(const NodePath &p_from) {
  304. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, CACHED_CLASS_RAW(NodePath));
  305. ERR_FAIL_NULL_V(mono_object, NULL);
  306. // Construct
  307. mono_runtime_object_init(mono_object);
  308. CACHED_FIELD(NodePath, ptr)->set_value_raw(mono_object, memnew(NodePath(p_from)));
  309. return mono_object;
  310. }
  311. MonoObject *create_managed_from(const RID &p_from) {
  312. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, CACHED_CLASS_RAW(RID));
  313. ERR_FAIL_NULL_V(mono_object, NULL);
  314. // Construct
  315. mono_runtime_object_init(mono_object);
  316. CACHED_FIELD(RID, ptr)->set_value_raw(mono_object, memnew(RID(p_from)));
  317. return mono_object;
  318. }
  319. MonoDomain *create_domain(const String &p_friendly_name) {
  320. MonoDomain *domain = mono_domain_create_appdomain((char *)p_friendly_name.utf8().get_data(), NULL);
  321. if (domain) {
  322. // Workaround to avoid this exception:
  323. // System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system.
  324. // ---> System.ArgumentException: The 'ExeConfigFilename' argument cannot be null.
  325. mono_domain_set_config(domain, ".", "");
  326. }
  327. return domain;
  328. }
  329. String get_exception_name_and_message(MonoException *p_ex) {
  330. String res;
  331. MonoClass *klass = mono_object_get_class((MonoObject *)p_ex);
  332. MonoType *type = mono_class_get_type(klass);
  333. char *full_name = mono_type_full_name(type);
  334. res += full_name;
  335. mono_free(full_name);
  336. res += ": ";
  337. MonoProperty *prop = mono_class_get_property_from_name(klass, "Message");
  338. MonoString *msg = (MonoString *)mono_property_get_value(prop, (MonoObject *)p_ex, NULL, NULL);
  339. res += GDMonoMarshal::mono_string_to_godot(msg);
  340. return res;
  341. }
  342. void debug_print_unhandled_exception(MonoException *p_exc) {
  343. print_unhandled_exception(p_exc);
  344. debug_send_unhandled_exception_error(p_exc);
  345. }
  346. void debug_send_unhandled_exception_error(MonoException *p_exc) {
  347. #ifdef DEBUG_ENABLED
  348. if (!ScriptDebugger::get_singleton())
  349. return;
  350. _TLS_RECURSION_GUARD_;
  351. ScriptLanguage::StackInfo separator;
  352. separator.file = String();
  353. separator.func = "--- " + RTR("End of inner exception stack trace") + " ---";
  354. separator.line = 0;
  355. Vector<ScriptLanguage::StackInfo> si;
  356. String exc_msg;
  357. while (p_exc != NULL) {
  358. GDMonoClass *st_klass = CACHED_CLASS(System_Diagnostics_StackTrace);
  359. MonoObject *stack_trace = mono_object_new(mono_domain_get(), st_klass->get_mono_ptr());
  360. MonoBoolean need_file_info = true;
  361. void *ctor_args[2] = { p_exc, &need_file_info };
  362. MonoException *unexpected_exc = NULL;
  363. CACHED_METHOD(System_Diagnostics_StackTrace, ctor_Exception_bool)->invoke_raw(stack_trace, ctor_args, &unexpected_exc);
  364. if (unexpected_exc) {
  365. GDMonoInternals::unhandled_exception(unexpected_exc);
  366. _UNREACHABLE_();
  367. }
  368. Vector<ScriptLanguage::StackInfo> _si;
  369. if (stack_trace != NULL) {
  370. _si = CSharpLanguage::get_singleton()->stack_trace_get_info(stack_trace);
  371. for (int i = _si.size() - 1; i >= 0; i--)
  372. si.insert(0, _si[i]);
  373. }
  374. exc_msg += (exc_msg.length() > 0 ? " ---> " : "") + GDMonoUtils::get_exception_name_and_message(p_exc);
  375. GDMonoClass *exc_class = GDMono::get_singleton()->get_class(mono_get_exception_class());
  376. GDMonoProperty *inner_exc_prop = exc_class->get_property("InnerException");
  377. CRASH_COND(inner_exc_prop == NULL);
  378. MonoObject *inner_exc = inner_exc_prop->get_value((MonoObject *)p_exc);
  379. if (inner_exc != NULL)
  380. si.insert(0, separator);
  381. p_exc = (MonoException *)inner_exc;
  382. }
  383. String file = si.size() ? si[0].file : __FILE__;
  384. String func = si.size() ? si[0].func : FUNCTION_STR;
  385. int line = si.size() ? si[0].line : __LINE__;
  386. String error_msg = "Unhandled exception";
  387. ScriptDebugger::get_singleton()->send_error(func, file, line, error_msg, exc_msg, ERR_HANDLER_ERROR, si);
  388. #endif
  389. }
  390. void debug_unhandled_exception(MonoException *p_exc) {
  391. #ifdef DEBUG_ENABLED
  392. GDMonoUtils::debug_send_unhandled_exception_error(p_exc);
  393. if (ScriptDebugger::get_singleton())
  394. ScriptDebugger::get_singleton()->idle_poll();
  395. #endif
  396. GDMonoInternals::unhandled_exception(p_exc); // prints the exception as well
  397. _UNREACHABLE_();
  398. }
  399. void print_unhandled_exception(MonoException *p_exc) {
  400. mono_print_unhandled_exception((MonoObject *)p_exc);
  401. }
  402. void set_pending_exception(MonoException *p_exc) {
  403. #ifdef HAS_PENDING_EXCEPTIONS
  404. if (get_runtime_invoke_count() == 0) {
  405. debug_unhandled_exception(p_exc);
  406. _UNREACHABLE_();
  407. }
  408. if (!mono_runtime_set_pending_exception(p_exc, false)) {
  409. ERR_PRINTS("Exception thrown from managed code, but it could not be set as pending:");
  410. GDMonoUtils::debug_print_unhandled_exception(p_exc);
  411. }
  412. #else
  413. debug_unhandled_exception(p_exc);
  414. _UNREACHABLE_();
  415. #endif
  416. }
  417. _THREAD_LOCAL_(int)
  418. current_invoke_count = 0;
  419. } // namespace GDMonoUtils