gd_mono_utils.cpp 18 KB

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