gd_mono_utils.cpp 19 KB

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