gd_mono_utils.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_mono_utils.h"
  31. #include <mono/metadata/exception.h>
  32. #include "core/os/dir_access.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #include "core/reference.h"
  36. #include "../csharp_script.h"
  37. #include "../utils/macros.h"
  38. #include "../utils/mutex_utils.h"
  39. #include "gd_mono.h"
  40. #include "gd_mono_class.h"
  41. #include "gd_mono_marshal.h"
  42. namespace GDMonoUtils {
  43. MonoCache mono_cache;
  44. #define CACHE_AND_CHECK(m_var, m_val) \
  45. { \
  46. m_var = m_val; \
  47. if (!m_var) { \
  48. ERR_EXPLAIN("Mono Cache: Member " #m_var " is null"); \
  49. ERR_FAIL(); \
  50. } \
  51. }
  52. #define CACHE_CLASS_AND_CHECK(m_class, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.class_##m_class, m_val)
  53. #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)
  54. #define CACHE_RAW_MONO_CLASS_AND_CHECK(m_class, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.rawclass_##m_class, m_val)
  55. #define CACHE_FIELD_AND_CHECK(m_class, m_field, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.field_##m_class##_##m_field, m_val)
  56. #define CACHE_METHOD_AND_CHECK(m_class, m_method, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.method_##m_class##_##m_method, m_val)
  57. #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)
  58. #define CACHE_PROPERTY_AND_CHECK(m_class, m_property, m_val) CACHE_AND_CHECK(GDMonoUtils::mono_cache.property_##m_class##_##m_property, m_val)
  59. void MonoCache::clear_members() {
  60. class_MonoObject = NULL;
  61. class_bool = NULL;
  62. class_int8_t = NULL;
  63. class_int16_t = NULL;
  64. class_int32_t = NULL;
  65. class_int64_t = NULL;
  66. class_uint8_t = NULL;
  67. class_uint16_t = NULL;
  68. class_uint32_t = NULL;
  69. class_uint64_t = NULL;
  70. class_float = NULL;
  71. class_double = NULL;
  72. class_String = NULL;
  73. class_IntPtr = NULL;
  74. class_System_Collections_IEnumerable = NULL;
  75. class_System_Collections_IDictionary = NULL;
  76. #ifdef DEBUG_ENABLED
  77. class_System_Diagnostics_StackTrace = NULL;
  78. methodthunk_System_Diagnostics_StackTrace_GetFrames = NULL;
  79. method_System_Diagnostics_StackTrace_ctor_bool = NULL;
  80. method_System_Diagnostics_StackTrace_ctor_Exception_bool = NULL;
  81. #endif
  82. class_KeyNotFoundException = NULL;
  83. rawclass_Dictionary = NULL;
  84. class_Vector2 = NULL;
  85. class_Rect2 = NULL;
  86. class_Transform2D = NULL;
  87. class_Vector3 = NULL;
  88. class_Basis = NULL;
  89. class_Quat = NULL;
  90. class_Transform = NULL;
  91. class_AABB = NULL;
  92. class_Color = NULL;
  93. class_Plane = NULL;
  94. class_NodePath = NULL;
  95. class_RID = NULL;
  96. class_GodotObject = NULL;
  97. class_GodotResource = NULL;
  98. class_Node = NULL;
  99. class_Control = NULL;
  100. class_Spatial = NULL;
  101. class_WeakRef = NULL;
  102. class_Array = NULL;
  103. class_Dictionary = NULL;
  104. class_MarshalUtils = NULL;
  105. #ifdef DEBUG_ENABLED
  106. class_DebuggingUtils = NULL;
  107. methodthunk_DebuggingUtils_GetStackFrameInfo = NULL;
  108. #endif
  109. class_ExportAttribute = NULL;
  110. field_ExportAttribute_hint = NULL;
  111. field_ExportAttribute_hintString = NULL;
  112. class_SignalAttribute = NULL;
  113. class_ToolAttribute = NULL;
  114. class_RemoteAttribute = NULL;
  115. class_SyncAttribute = NULL;
  116. class_MasterAttribute = NULL;
  117. class_PuppetAttribute = NULL;
  118. class_SlaveAttribute = NULL;
  119. class_RemoteSyncAttribute = NULL;
  120. class_MasterSyncAttribute = NULL;
  121. class_PuppetSyncAttribute = NULL;
  122. class_GodotMethodAttribute = NULL;
  123. field_GodotMethodAttribute_methodName = NULL;
  124. field_GodotObject_ptr = NULL;
  125. field_NodePath_ptr = NULL;
  126. field_Image_ptr = NULL;
  127. field_RID_ptr = NULL;
  128. methodthunk_GodotObject_Dispose = NULL;
  129. methodthunk_Array_GetPtr = NULL;
  130. methodthunk_Dictionary_GetPtr = NULL;
  131. methodthunk_SignalAwaiter_SignalCallback = NULL;
  132. methodthunk_SignalAwaiter_FailureCallback = NULL;
  133. methodthunk_GodotTaskScheduler_Activate = NULL;
  134. // Start of MarshalUtils methods
  135. methodthunk_MarshalUtils_TypeIsGenericArray = NULL;
  136. methodthunk_MarshalUtils_TypeIsGenericDictionary = NULL;
  137. methodthunk_MarshalUtils_ArrayGetElementType = NULL;
  138. methodthunk_MarshalUtils_DictionaryGetKeyValueTypes = NULL;
  139. methodthunk_MarshalUtils_GenericIEnumerableIsAssignableFromType = NULL;
  140. methodthunk_MarshalUtils_GenericIDictionaryIsAssignableFromType = NULL;
  141. methodthunk_MarshalUtils_MakeGenericArrayType = NULL;
  142. methodthunk_MarshalUtils_MakeGenericDictionaryType = NULL;
  143. methodthunk_MarshalUtils_EnumerableToArray = NULL;
  144. methodthunk_MarshalUtils_IDictionaryToDictionary = NULL;
  145. methodthunk_MarshalUtils_GenericIDictionaryToDictionary = NULL;
  146. // End of MarshalUtils methods
  147. task_scheduler_handle = Ref<MonoGCHandle>();
  148. }
  149. void MonoCache::cleanup() {
  150. corlib_cache_updated = false;
  151. godot_api_cache_updated = false;
  152. }
  153. #define GODOT_API_CLASS(m_class) (GDMono::get_singleton()->get_core_api_assembly()->get_class(BINDINGS_NAMESPACE, #m_class))
  154. #define GODOT_API_NS_CLAS(m_ns, m_class) (GDMono::get_singleton()->get_core_api_assembly()->get_class(m_ns, #m_class))
  155. void update_corlib_cache() {
  156. CACHE_CLASS_AND_CHECK(MonoObject, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_object_class()));
  157. CACHE_CLASS_AND_CHECK(bool, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_boolean_class()));
  158. CACHE_CLASS_AND_CHECK(int8_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_sbyte_class()));
  159. CACHE_CLASS_AND_CHECK(int16_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_int16_class()));
  160. CACHE_CLASS_AND_CHECK(int32_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_int32_class()));
  161. CACHE_CLASS_AND_CHECK(int64_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_int64_class()));
  162. CACHE_CLASS_AND_CHECK(uint8_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_byte_class()));
  163. CACHE_CLASS_AND_CHECK(uint16_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_uint16_class()));
  164. CACHE_CLASS_AND_CHECK(uint32_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_uint32_class()));
  165. CACHE_CLASS_AND_CHECK(uint64_t, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_uint64_class()));
  166. CACHE_CLASS_AND_CHECK(float, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_single_class()));
  167. CACHE_CLASS_AND_CHECK(double, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_double_class()));
  168. CACHE_CLASS_AND_CHECK(String, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_string_class()));
  169. CACHE_CLASS_AND_CHECK(IntPtr, GDMono::get_singleton()->get_corlib_assembly()->get_class(mono_get_intptr_class()));
  170. CACHE_CLASS_AND_CHECK(System_Collections_IEnumerable, GDMono::get_singleton()->get_corlib_assembly()->get_class("System.Collections", "IEnumerable"));
  171. CACHE_CLASS_AND_CHECK(System_Collections_IDictionary, GDMono::get_singleton()->get_corlib_assembly()->get_class("System.Collections", "IDictionary"));
  172. #ifdef DEBUG_ENABLED
  173. CACHE_CLASS_AND_CHECK(System_Diagnostics_StackTrace, GDMono::get_singleton()->get_corlib_assembly()->get_class("System.Diagnostics", "StackTrace"));
  174. CACHE_METHOD_THUNK_AND_CHECK(System_Diagnostics_StackTrace, GetFrames, (StackTrace_GetFrames)CACHED_CLASS(System_Diagnostics_StackTrace)->get_method_thunk("GetFrames"));
  175. CACHE_METHOD_AND_CHECK(System_Diagnostics_StackTrace, ctor_bool, CACHED_CLASS(System_Diagnostics_StackTrace)->get_method_with_desc("System.Diagnostics.StackTrace:.ctor(bool)", true));
  176. 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));
  177. #endif
  178. CACHE_CLASS_AND_CHECK(KeyNotFoundException, GDMono::get_singleton()->get_corlib_assembly()->get_class("System.Collections.Generic", "KeyNotFoundException"));
  179. mono_cache.corlib_cache_updated = true;
  180. }
  181. void update_godot_api_cache() {
  182. CACHE_CLASS_AND_CHECK(Vector2, GODOT_API_CLASS(Vector2));
  183. CACHE_CLASS_AND_CHECK(Rect2, GODOT_API_CLASS(Rect2));
  184. CACHE_CLASS_AND_CHECK(Transform2D, GODOT_API_CLASS(Transform2D));
  185. CACHE_CLASS_AND_CHECK(Vector3, GODOT_API_CLASS(Vector3));
  186. CACHE_CLASS_AND_CHECK(Basis, GODOT_API_CLASS(Basis));
  187. CACHE_CLASS_AND_CHECK(Quat, GODOT_API_CLASS(Quat));
  188. CACHE_CLASS_AND_CHECK(Transform, GODOT_API_CLASS(Transform));
  189. CACHE_CLASS_AND_CHECK(AABB, GODOT_API_CLASS(AABB));
  190. CACHE_CLASS_AND_CHECK(Color, GODOT_API_CLASS(Color));
  191. CACHE_CLASS_AND_CHECK(Plane, GODOT_API_CLASS(Plane));
  192. CACHE_CLASS_AND_CHECK(NodePath, GODOT_API_CLASS(NodePath));
  193. CACHE_CLASS_AND_CHECK(RID, GODOT_API_CLASS(RID));
  194. CACHE_CLASS_AND_CHECK(GodotObject, GODOT_API_CLASS(Object));
  195. CACHE_CLASS_AND_CHECK(GodotResource, GODOT_API_CLASS(Resource));
  196. CACHE_CLASS_AND_CHECK(Node, GODOT_API_CLASS(Node));
  197. CACHE_CLASS_AND_CHECK(Control, GODOT_API_CLASS(Control));
  198. CACHE_CLASS_AND_CHECK(Spatial, GODOT_API_CLASS(Spatial));
  199. CACHE_CLASS_AND_CHECK(WeakRef, GODOT_API_CLASS(WeakRef));
  200. CACHE_CLASS_AND_CHECK(Array, GODOT_API_NS_CLAS(BINDINGS_NAMESPACE_COLLECTIONS, Array));
  201. CACHE_CLASS_AND_CHECK(Dictionary, GODOT_API_NS_CLAS(BINDINGS_NAMESPACE_COLLECTIONS, Dictionary));
  202. CACHE_CLASS_AND_CHECK(MarshalUtils, GODOT_API_CLASS(MarshalUtils));
  203. #ifdef DEBUG_ENABLED
  204. CACHE_CLASS_AND_CHECK(DebuggingUtils, GODOT_API_CLASS(DebuggingUtils));
  205. #endif
  206. // Attributes
  207. CACHE_CLASS_AND_CHECK(ExportAttribute, GODOT_API_CLASS(ExportAttribute));
  208. CACHE_FIELD_AND_CHECK(ExportAttribute, hint, CACHED_CLASS(ExportAttribute)->get_field("hint"));
  209. CACHE_FIELD_AND_CHECK(ExportAttribute, hintString, CACHED_CLASS(ExportAttribute)->get_field("hintString"));
  210. CACHE_CLASS_AND_CHECK(SignalAttribute, GODOT_API_CLASS(SignalAttribute));
  211. CACHE_CLASS_AND_CHECK(ToolAttribute, GODOT_API_CLASS(ToolAttribute));
  212. CACHE_CLASS_AND_CHECK(RemoteAttribute, GODOT_API_CLASS(RemoteAttribute));
  213. CACHE_CLASS_AND_CHECK(SyncAttribute, GODOT_API_CLASS(SyncAttribute));
  214. CACHE_CLASS_AND_CHECK(MasterAttribute, GODOT_API_CLASS(MasterAttribute));
  215. CACHE_CLASS_AND_CHECK(PuppetAttribute, GODOT_API_CLASS(PuppetAttribute));
  216. CACHE_CLASS_AND_CHECK(SlaveAttribute, GODOT_API_CLASS(SlaveAttribute));
  217. CACHE_CLASS_AND_CHECK(RemoteSyncAttribute, GODOT_API_CLASS(RemoteSyncAttribute));
  218. CACHE_CLASS_AND_CHECK(MasterSyncAttribute, GODOT_API_CLASS(MasterSyncAttribute));
  219. CACHE_CLASS_AND_CHECK(PuppetSyncAttribute, GODOT_API_CLASS(PuppetSyncAttribute));
  220. CACHE_CLASS_AND_CHECK(GodotMethodAttribute, GODOT_API_CLASS(GodotMethodAttribute));
  221. CACHE_FIELD_AND_CHECK(GodotMethodAttribute, methodName, CACHED_CLASS(GodotMethodAttribute)->get_field("methodName"));
  222. CACHE_FIELD_AND_CHECK(GodotObject, ptr, CACHED_CLASS(GodotObject)->get_field(BINDINGS_PTR_FIELD));
  223. CACHE_FIELD_AND_CHECK(NodePath, ptr, CACHED_CLASS(NodePath)->get_field(BINDINGS_PTR_FIELD));
  224. CACHE_FIELD_AND_CHECK(RID, ptr, CACHED_CLASS(RID)->get_field(BINDINGS_PTR_FIELD));
  225. CACHE_METHOD_THUNK_AND_CHECK(GodotObject, Dispose, (GodotObject_Dispose)CACHED_CLASS(GodotObject)->get_method_thunk("Dispose", 0));
  226. CACHE_METHOD_THUNK_AND_CHECK(Array, GetPtr, (Array_GetPtr)GODOT_API_NS_CLAS(BINDINGS_NAMESPACE_COLLECTIONS, Array)->get_method_thunk("GetPtr", 0));
  227. CACHE_METHOD_THUNK_AND_CHECK(Dictionary, GetPtr, (Dictionary_GetPtr)GODOT_API_NS_CLAS(BINDINGS_NAMESPACE_COLLECTIONS, Dictionary)->get_method_thunk("GetPtr", 0));
  228. CACHE_METHOD_THUNK_AND_CHECK(SignalAwaiter, SignalCallback, (SignalAwaiter_SignalCallback)GODOT_API_CLASS(SignalAwaiter)->get_method_thunk("SignalCallback", 1));
  229. CACHE_METHOD_THUNK_AND_CHECK(SignalAwaiter, FailureCallback, (SignalAwaiter_FailureCallback)GODOT_API_CLASS(SignalAwaiter)->get_method_thunk("FailureCallback", 0));
  230. CACHE_METHOD_THUNK_AND_CHECK(GodotTaskScheduler, Activate, (GodotTaskScheduler_Activate)GODOT_API_CLASS(GodotTaskScheduler)->get_method_thunk("Activate", 0));
  231. // Start of MarshalUtils methods
  232. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, TypeIsGenericArray, (TypeIsGenericArray)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("TypeIsGenericArray", 1));
  233. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, TypeIsGenericDictionary, (TypeIsGenericDictionary)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("TypeIsGenericDictionary", 1));
  234. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, ArrayGetElementType, (ArrayGetElementType)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("ArrayGetElementType", 2));
  235. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, DictionaryGetKeyValueTypes, (DictionaryGetKeyValueTypes)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("DictionaryGetKeyValueTypes", 3));
  236. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, ArrayGetElementType, (ArrayGetElementType)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("ArrayGetElementType", 2));
  237. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, DictionaryGetKeyValueTypes, (DictionaryGetKeyValueTypes)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("DictionaryGetKeyValueTypes", 3));
  238. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, GenericIEnumerableIsAssignableFromType, (GenericIEnumerableIsAssignableFromType)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("GenericIEnumerableIsAssignableFromType", 2));
  239. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, GenericIDictionaryIsAssignableFromType, (GenericIDictionaryIsAssignableFromType)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("GenericIDictionaryIsAssignableFromType", 3));
  240. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, MakeGenericArrayType, (MakeGenericArrayType)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("MakeGenericArrayType", 1));
  241. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, MakeGenericDictionaryType, (MakeGenericDictionaryType)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("MakeGenericDictionaryType", 2));
  242. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, EnumerableToArray, (EnumerableToArray)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("EnumerableToArray", 2));
  243. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, IDictionaryToDictionary, (IDictionaryToDictionary)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("IDictionaryToDictionary", 2));
  244. CACHE_METHOD_THUNK_AND_CHECK(MarshalUtils, GenericIDictionaryToDictionary, (GenericIDictionaryToDictionary)GODOT_API_CLASS(MarshalUtils)->get_method_thunk("GenericIDictionaryToDictionary", 2));
  245. // End of MarshalUtils methods
  246. #ifdef DEBUG_ENABLED
  247. CACHE_METHOD_THUNK_AND_CHECK(DebuggingUtils, GetStackFrameInfo, (DebugUtils_StackFrameInfo)GODOT_API_CLASS(DebuggingUtils)->get_method_thunk("GetStackFrameInfo", 4));
  248. #endif
  249. // TODO Move to CSharpLanguage::init() and do handle disposal
  250. MonoObject *task_scheduler = mono_object_new(SCRIPTS_DOMAIN, GODOT_API_CLASS(GodotTaskScheduler)->get_mono_ptr());
  251. GDMonoUtils::runtime_object_init(task_scheduler, GODOT_API_CLASS(GodotTaskScheduler));
  252. mono_cache.task_scheduler_handle = MonoGCHandle::create_strong(task_scheduler);
  253. mono_cache.godot_api_cache_updated = true;
  254. }
  255. void clear_cache() {
  256. mono_cache.cleanup();
  257. mono_cache.clear_members();
  258. }
  259. MonoObject *unmanaged_get_managed(Object *unmanaged) {
  260. if (!unmanaged)
  261. return NULL;
  262. if (unmanaged->get_script_instance()) {
  263. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(unmanaged->get_script_instance());
  264. if (cs_instance) {
  265. return cs_instance->get_mono_object();
  266. }
  267. }
  268. // If the owner does not have a CSharpInstance...
  269. void *data = unmanaged->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
  270. ERR_FAIL_NULL_V(data, NULL);
  271. CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->value();
  272. if (!script_binding.inited) {
  273. SCOPED_MUTEX_LOCK(CSharpLanguage::get_singleton()->get_language_bind_mutex());
  274. if (!script_binding.inited) { // Other thread may have set it up
  275. // Already had a binding that needs to be setup
  276. CSharpLanguage::get_singleton()->setup_csharp_script_binding(script_binding, unmanaged);
  277. ERR_FAIL_COND_V(!script_binding.inited, NULL);
  278. }
  279. }
  280. Ref<MonoGCHandle> &gchandle = script_binding.gchandle;
  281. ERR_FAIL_COND_V(gchandle.is_null(), NULL);
  282. MonoObject *target = gchandle->get_target();
  283. if (target)
  284. return target;
  285. CSharpLanguage::get_singleton()->release_script_gchandle(gchandle);
  286. // Create a new one
  287. #ifdef DEBUG_ENABLED
  288. CRASH_COND(script_binding.type_name == StringName());
  289. CRASH_COND(script_binding.wrapper_class == NULL);
  290. #endif
  291. MonoObject *mono_object = GDMonoUtils::create_managed_for_godot_object(script_binding.wrapper_class, script_binding.type_name, unmanaged);
  292. ERR_FAIL_NULL_V(mono_object, NULL);
  293. gchandle->set_handle(MonoGCHandle::new_strong_handle(mono_object), MonoGCHandle::STRONG_HANDLE);
  294. // Tie managed to unmanaged
  295. Reference *ref = Object::cast_to<Reference>(unmanaged);
  296. if (ref) {
  297. // Unsafe refcount increment. The managed instance also counts as a reference.
  298. // This way if the unmanaged world has no references to our owner
  299. // but the managed instance is alive, the refcount will be 1 instead of 0.
  300. // See: godot_icall_Reference_Dtor(MonoObject *p_obj, Object *p_ptr)
  301. ref->reference();
  302. }
  303. return mono_object;
  304. }
  305. void set_main_thread(MonoThread *p_thread) {
  306. mono_thread_set_main(p_thread);
  307. }
  308. void attach_current_thread() {
  309. ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
  310. MonoThread *mono_thread = mono_thread_attach(SCRIPTS_DOMAIN);
  311. ERR_FAIL_NULL(mono_thread);
  312. }
  313. void detach_current_thread() {
  314. ERR_FAIL_COND(!GDMono::get_singleton()->is_runtime_initialized());
  315. MonoThread *mono_thread = mono_thread_current();
  316. ERR_FAIL_NULL(mono_thread);
  317. mono_thread_detach(mono_thread);
  318. }
  319. MonoThread *get_current_thread() {
  320. return mono_thread_current();
  321. }
  322. void runtime_object_init(MonoObject *p_this_obj, GDMonoClass *p_class, MonoException **r_exc) {
  323. GDMonoMethod *ctor = p_class->get_method(".ctor", 0);
  324. ERR_FAIL_NULL(ctor);
  325. ctor->invoke_raw(p_this_obj, NULL, r_exc);
  326. }
  327. GDMonoClass *get_object_class(MonoObject *p_object) {
  328. return GDMono::get_singleton()->get_class(mono_object_get_class(p_object));
  329. }
  330. GDMonoClass *type_get_proxy_class(const StringName &p_type) {
  331. String class_name = p_type;
  332. if (class_name[0] == '_')
  333. class_name = class_name.substr(1, class_name.length());
  334. GDMonoClass *klass = GDMono::get_singleton()->get_core_api_assembly()->get_class(BINDINGS_NAMESPACE, class_name);
  335. if (klass && klass->is_static()) {
  336. // A static class means this is a Godot singleton class. If an instance is needed we use Godot.Object.
  337. return mono_cache.class_GodotObject;
  338. }
  339. #ifdef TOOLS_ENABLED
  340. if (!klass) {
  341. return GDMono::get_singleton()->get_editor_api_assembly()->get_class(BINDINGS_NAMESPACE, class_name);
  342. }
  343. #endif
  344. return klass;
  345. }
  346. GDMonoClass *get_class_native_base(GDMonoClass *p_class) {
  347. GDMonoClass *klass = p_class;
  348. do {
  349. const GDMonoAssembly *assembly = klass->get_assembly();
  350. if (assembly == GDMono::get_singleton()->get_core_api_assembly())
  351. return klass;
  352. #ifdef TOOLS_ENABLED
  353. if (assembly == GDMono::get_singleton()->get_editor_api_assembly())
  354. return klass;
  355. #endif
  356. } while ((klass = klass->get_parent_class()) != NULL);
  357. return NULL;
  358. }
  359. MonoObject *create_managed_for_godot_object(GDMonoClass *p_class, const StringName &p_native, Object *p_object) {
  360. String object_type = p_object->get_class_name();
  361. if (object_type[0] == '_')
  362. object_type = object_type.substr(1, object_type.length());
  363. if (!ClassDB::is_parent_class(object_type, p_native)) {
  364. ERR_EXPLAIN("Type inherits from native type '" + p_native + "', so it can't be instanced in object of type: '" + p_object->get_class() + "'");
  365. ERR_FAIL_V(NULL);
  366. }
  367. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, p_class->get_mono_ptr());
  368. ERR_FAIL_NULL_V(mono_object, NULL);
  369. CACHED_FIELD(GodotObject, ptr)->set_value_raw(mono_object, p_object);
  370. // Construct
  371. GDMonoUtils::runtime_object_init(mono_object, p_class);
  372. return mono_object;
  373. }
  374. MonoObject *create_managed_from(const NodePath &p_from) {
  375. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, CACHED_CLASS_RAW(NodePath));
  376. ERR_FAIL_NULL_V(mono_object, NULL);
  377. // Construct
  378. GDMonoUtils::runtime_object_init(mono_object, CACHED_CLASS(NodePath));
  379. CACHED_FIELD(NodePath, ptr)->set_value_raw(mono_object, memnew(NodePath(p_from)));
  380. return mono_object;
  381. }
  382. MonoObject *create_managed_from(const RID &p_from) {
  383. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, CACHED_CLASS_RAW(RID));
  384. ERR_FAIL_NULL_V(mono_object, NULL);
  385. // Construct
  386. GDMonoUtils::runtime_object_init(mono_object, CACHED_CLASS(RID));
  387. CACHED_FIELD(RID, ptr)->set_value_raw(mono_object, memnew(RID(p_from)));
  388. return mono_object;
  389. }
  390. MonoObject *create_managed_from(const Array &p_from, GDMonoClass *p_class) {
  391. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, p_class->get_mono_ptr());
  392. ERR_FAIL_NULL_V(mono_object, NULL);
  393. // Search constructor that takes a pointer as parameter
  394. MonoMethod *m;
  395. void *iter = NULL;
  396. while ((m = mono_class_get_methods(p_class->get_mono_ptr(), &iter))) {
  397. if (strcmp(mono_method_get_name(m), ".ctor") == 0) {
  398. MonoMethodSignature *sig = mono_method_signature(m);
  399. void *front = NULL;
  400. if (mono_signature_get_param_count(sig) == 1 &&
  401. mono_class_from_mono_type(mono_signature_get_params(sig, &front)) == CACHED_CLASS(IntPtr)->get_mono_ptr()) {
  402. break;
  403. }
  404. }
  405. }
  406. CRASH_COND(m == NULL);
  407. Array *new_array = memnew(Array(p_from));
  408. void *args[1] = { &new_array };
  409. MonoException *exc = NULL;
  410. GDMonoUtils::runtime_invoke(m, mono_object, args, &exc);
  411. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  412. return mono_object;
  413. }
  414. MonoObject *create_managed_from(const Dictionary &p_from, GDMonoClass *p_class) {
  415. MonoObject *mono_object = mono_object_new(SCRIPTS_DOMAIN, p_class->get_mono_ptr());
  416. ERR_FAIL_NULL_V(mono_object, NULL);
  417. // Search constructor that takes a pointer as parameter
  418. MonoMethod *m;
  419. void *iter = NULL;
  420. while ((m = mono_class_get_methods(p_class->get_mono_ptr(), &iter))) {
  421. if (strcmp(mono_method_get_name(m), ".ctor") == 0) {
  422. MonoMethodSignature *sig = mono_method_signature(m);
  423. void *front = NULL;
  424. if (mono_signature_get_param_count(sig) == 1 &&
  425. mono_class_from_mono_type(mono_signature_get_params(sig, &front)) == CACHED_CLASS(IntPtr)->get_mono_ptr()) {
  426. break;
  427. }
  428. }
  429. }
  430. CRASH_COND(m == NULL);
  431. Dictionary *new_dict = memnew(Dictionary(p_from));
  432. void *args[1] = { &new_dict };
  433. MonoException *exc = NULL;
  434. GDMonoUtils::runtime_invoke(m, mono_object, args, &exc);
  435. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  436. return mono_object;
  437. }
  438. MonoDomain *create_domain(const String &p_friendly_name) {
  439. MonoDomain *domain = mono_domain_create_appdomain((char *)p_friendly_name.utf8().get_data(), NULL);
  440. if (domain) {
  441. // Workaround to avoid this exception:
  442. // System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system.
  443. // ---> System.ArgumentException: The 'ExeConfigFilename' argument cannot be null.
  444. mono_domain_set_config(domain, ".", "");
  445. }
  446. return domain;
  447. }
  448. String get_exception_name_and_message(MonoException *p_exc) {
  449. String res;
  450. MonoClass *klass = mono_object_get_class((MonoObject *)p_exc);
  451. MonoType *type = mono_class_get_type(klass);
  452. char *full_name = mono_type_full_name(type);
  453. res += full_name;
  454. mono_free(full_name);
  455. res += ": ";
  456. MonoProperty *prop = mono_class_get_property_from_name(klass, "Message");
  457. MonoString *msg = (MonoString *)property_get_value(prop, (MonoObject *)p_exc, NULL, NULL);
  458. res += GDMonoMarshal::mono_string_to_godot(msg);
  459. return res;
  460. }
  461. void set_exception_message(MonoException *p_exc, String message) {
  462. MonoClass *klass = mono_object_get_class((MonoObject *)p_exc);
  463. MonoProperty *prop = mono_class_get_property_from_name(klass, "Message");
  464. MonoString *msg = GDMonoMarshal::mono_string_from_godot(message);
  465. void *params[1] = { msg };
  466. property_set_value(prop, (MonoObject *)p_exc, params, NULL);
  467. }
  468. void debug_print_unhandled_exception(MonoException *p_exc) {
  469. print_unhandled_exception(p_exc);
  470. debug_send_unhandled_exception_error(p_exc);
  471. }
  472. void debug_send_unhandled_exception_error(MonoException *p_exc) {
  473. #ifdef DEBUG_ENABLED
  474. if (!ScriptDebugger::get_singleton())
  475. return;
  476. _TLS_RECURSION_GUARD_;
  477. ScriptLanguage::StackInfo separator;
  478. separator.file = String();
  479. separator.func = "--- " + RTR("End of inner exception stack trace") + " ---";
  480. separator.line = 0;
  481. Vector<ScriptLanguage::StackInfo> si;
  482. String exc_msg;
  483. while (p_exc != NULL) {
  484. GDMonoClass *st_klass = CACHED_CLASS(System_Diagnostics_StackTrace);
  485. MonoObject *stack_trace = mono_object_new(mono_domain_get(), st_klass->get_mono_ptr());
  486. MonoBoolean need_file_info = true;
  487. void *ctor_args[2] = { p_exc, &need_file_info };
  488. MonoException *unexpected_exc = NULL;
  489. CACHED_METHOD(System_Diagnostics_StackTrace, ctor_Exception_bool)->invoke_raw(stack_trace, ctor_args, &unexpected_exc);
  490. if (unexpected_exc) {
  491. GDMonoInternals::unhandled_exception(unexpected_exc);
  492. GD_UNREACHABLE();
  493. }
  494. Vector<ScriptLanguage::StackInfo> _si;
  495. if (stack_trace != NULL) {
  496. _si = CSharpLanguage::get_singleton()->stack_trace_get_info(stack_trace);
  497. for (int i = _si.size() - 1; i >= 0; i--)
  498. si.insert(0, _si[i]);
  499. }
  500. exc_msg += (exc_msg.length() > 0 ? " ---> " : "") + GDMonoUtils::get_exception_name_and_message(p_exc);
  501. GDMonoClass *exc_class = GDMono::get_singleton()->get_class(mono_get_exception_class());
  502. GDMonoProperty *inner_exc_prop = exc_class->get_property("InnerException");
  503. CRASH_COND(inner_exc_prop == NULL);
  504. MonoObject *inner_exc = inner_exc_prop->get_value((MonoObject *)p_exc);
  505. if (inner_exc != NULL)
  506. si.insert(0, separator);
  507. p_exc = (MonoException *)inner_exc;
  508. }
  509. String file = si.size() ? si[0].file : __FILE__;
  510. String func = si.size() ? si[0].func : FUNCTION_STR;
  511. int line = si.size() ? si[0].line : __LINE__;
  512. String error_msg = "Unhandled exception";
  513. ScriptDebugger::get_singleton()->send_error(func, file, line, error_msg, exc_msg, ERR_HANDLER_ERROR, si);
  514. #endif
  515. }
  516. void debug_unhandled_exception(MonoException *p_exc) {
  517. GDMonoInternals::unhandled_exception(p_exc); // prints the exception as well
  518. GD_UNREACHABLE();
  519. }
  520. void print_unhandled_exception(MonoException *p_exc) {
  521. mono_print_unhandled_exception((MonoObject *)p_exc);
  522. }
  523. void set_pending_exception(MonoException *p_exc) {
  524. #ifdef HAS_PENDING_EXCEPTIONS
  525. if (get_runtime_invoke_count() == 0) {
  526. debug_unhandled_exception(p_exc);
  527. GD_UNREACHABLE();
  528. }
  529. if (!mono_runtime_set_pending_exception(p_exc, false)) {
  530. ERR_PRINTS("Exception thrown from managed code, but it could not be set as pending:");
  531. GDMonoUtils::debug_print_unhandled_exception(p_exc);
  532. }
  533. #else
  534. debug_unhandled_exception(p_exc);
  535. GD_UNREACHABLE();
  536. #endif
  537. }
  538. _THREAD_LOCAL_(int)
  539. current_invoke_count = 0;
  540. MonoObject *runtime_invoke(MonoMethod *p_method, void *p_obj, void **p_params, MonoException **r_exc) {
  541. GD_MONO_BEGIN_RUNTIME_INVOKE;
  542. MonoObject *ret = mono_runtime_invoke(p_method, p_obj, p_params, (MonoObject **)r_exc);
  543. GD_MONO_END_RUNTIME_INVOKE;
  544. return ret;
  545. }
  546. MonoObject *runtime_invoke_array(MonoMethod *p_method, void *p_obj, MonoArray *p_params, MonoException **r_exc) {
  547. GD_MONO_BEGIN_RUNTIME_INVOKE;
  548. MonoObject *ret = mono_runtime_invoke_array(p_method, p_obj, p_params, (MonoObject **)r_exc);
  549. GD_MONO_END_RUNTIME_INVOKE;
  550. return ret;
  551. }
  552. MonoString *object_to_string(MonoObject *p_obj, MonoException **r_exc) {
  553. GD_MONO_BEGIN_RUNTIME_INVOKE;
  554. MonoString *ret = mono_object_to_string(p_obj, (MonoObject **)r_exc);
  555. GD_MONO_END_RUNTIME_INVOKE;
  556. return ret;
  557. }
  558. void property_set_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc) {
  559. GD_MONO_BEGIN_RUNTIME_INVOKE;
  560. mono_property_set_value(p_prop, p_obj, p_params, (MonoObject **)r_exc);
  561. GD_MONO_END_RUNTIME_INVOKE;
  562. }
  563. MonoObject *property_get_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc) {
  564. GD_MONO_BEGIN_RUNTIME_INVOKE;
  565. MonoObject *ret = mono_property_get_value(p_prop, p_obj, p_params, (MonoObject **)r_exc);
  566. GD_MONO_END_RUNTIME_INVOKE;
  567. return ret;
  568. }
  569. uint64_t unbox_enum_value(MonoObject *p_boxed, MonoType *p_enum_basetype, bool &r_error) {
  570. r_error = false;
  571. switch (mono_type_get_type(p_enum_basetype)) {
  572. case MONO_TYPE_BOOLEAN:
  573. return (bool)GDMonoMarshal::unbox<MonoBoolean>(p_boxed) ? 1 : 0;
  574. case MONO_TYPE_CHAR:
  575. return GDMonoMarshal::unbox<uint16_t>(p_boxed);
  576. case MONO_TYPE_U1:
  577. return GDMonoMarshal::unbox<uint8_t>(p_boxed);
  578. case MONO_TYPE_U2:
  579. return GDMonoMarshal::unbox<uint16_t>(p_boxed);
  580. case MONO_TYPE_U4:
  581. return GDMonoMarshal::unbox<uint32_t>(p_boxed);
  582. case MONO_TYPE_U8:
  583. return GDMonoMarshal::unbox<uint64_t>(p_boxed);
  584. case MONO_TYPE_I1:
  585. return GDMonoMarshal::unbox<int8_t>(p_boxed);
  586. case MONO_TYPE_I2:
  587. return GDMonoMarshal::unbox<int16_t>(p_boxed);
  588. case MONO_TYPE_I4:
  589. return GDMonoMarshal::unbox<int32_t>(p_boxed);
  590. case MONO_TYPE_I8:
  591. return GDMonoMarshal::unbox<int64_t>(p_boxed);
  592. default:
  593. r_error = true;
  594. return 0;
  595. }
  596. }
  597. void dispose(MonoObject *p_mono_object, MonoException **r_exc) {
  598. invoke_method_thunk(CACHED_METHOD_THUNK(GodotObject, Dispose), p_mono_object, r_exc);
  599. }
  600. namespace Marshal {
  601. MonoBoolean type_is_generic_array(MonoReflectionType *p_reftype) {
  602. TypeIsGenericArray thunk = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericArray);
  603. MonoException *exc = NULL;
  604. MonoBoolean res = invoke_method_thunk(thunk, p_reftype, &exc);
  605. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  606. return res;
  607. }
  608. MonoBoolean type_is_generic_dictionary(MonoReflectionType *p_reftype) {
  609. TypeIsGenericDictionary thunk = CACHED_METHOD_THUNK(MarshalUtils, TypeIsGenericDictionary);
  610. MonoException *exc = NULL;
  611. MonoBoolean res = invoke_method_thunk(thunk, p_reftype, &exc);
  612. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  613. return res;
  614. }
  615. void array_get_element_type(MonoReflectionType *p_array_reftype, MonoReflectionType **r_elem_reftype) {
  616. ArrayGetElementType thunk = CACHED_METHOD_THUNK(MarshalUtils, ArrayGetElementType);
  617. MonoException *exc = NULL;
  618. invoke_method_thunk(thunk, p_array_reftype, r_elem_reftype, &exc);
  619. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  620. }
  621. void dictionary_get_key_value_types(MonoReflectionType *p_dict_reftype, MonoReflectionType **r_key_reftype, MonoReflectionType **r_value_reftype) {
  622. DictionaryGetKeyValueTypes thunk = CACHED_METHOD_THUNK(MarshalUtils, DictionaryGetKeyValueTypes);
  623. MonoException *exc = NULL;
  624. invoke_method_thunk(thunk, p_dict_reftype, r_key_reftype, r_value_reftype, &exc);
  625. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  626. }
  627. MonoBoolean generic_ienumerable_is_assignable_from(MonoReflectionType *p_reftype, MonoReflectionType **r_elem_reftype) {
  628. GenericIEnumerableIsAssignableFromType thunk = CACHED_METHOD_THUNK(MarshalUtils, GenericIEnumerableIsAssignableFromType);
  629. MonoException *exc = NULL;
  630. MonoBoolean res = invoke_method_thunk(thunk, p_reftype, r_elem_reftype, &exc);
  631. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  632. return res;
  633. }
  634. MonoBoolean generic_idictionary_is_assignable_from(MonoReflectionType *p_reftype, MonoReflectionType **r_key_reftype, MonoReflectionType **r_value_reftype) {
  635. GenericIDictionaryIsAssignableFromType thunk = CACHED_METHOD_THUNK(MarshalUtils, GenericIDictionaryIsAssignableFromType);
  636. MonoException *exc = NULL;
  637. MonoBoolean res = invoke_method_thunk(thunk, p_reftype, r_key_reftype, r_value_reftype, &exc);
  638. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  639. return res;
  640. }
  641. Array enumerable_to_array(MonoObject *p_enumerable) {
  642. Array result;
  643. EnumerableToArray thunk = CACHED_METHOD_THUNK(MarshalUtils, EnumerableToArray);
  644. MonoException *exc = NULL;
  645. invoke_method_thunk(thunk, p_enumerable, &result, &exc);
  646. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  647. return result;
  648. }
  649. Dictionary idictionary_to_dictionary(MonoObject *p_idictionary) {
  650. Dictionary result;
  651. IDictionaryToDictionary thunk = CACHED_METHOD_THUNK(MarshalUtils, IDictionaryToDictionary);
  652. MonoException *exc = NULL;
  653. invoke_method_thunk(thunk, p_idictionary, &result, &exc);
  654. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  655. return result;
  656. }
  657. Dictionary generic_idictionary_to_dictionary(MonoObject *p_generic_idictionary) {
  658. Dictionary result;
  659. GenericIDictionaryToDictionary thunk = CACHED_METHOD_THUNK(MarshalUtils, GenericIDictionaryToDictionary);
  660. MonoException *exc = NULL;
  661. invoke_method_thunk(thunk, p_generic_idictionary, &result, &exc);
  662. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  663. return result;
  664. }
  665. GDMonoClass *make_generic_array_type(MonoReflectionType *p_elem_reftype) {
  666. MakeGenericArrayType thunk = CACHED_METHOD_THUNK(MarshalUtils, MakeGenericArrayType);
  667. MonoException *exc = NULL;
  668. MonoReflectionType *reftype = invoke_method_thunk(thunk, p_elem_reftype, &exc);
  669. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  670. return GDMono::get_singleton()->get_class(mono_class_from_mono_type(mono_reflection_type_get_type(reftype)));
  671. }
  672. GDMonoClass *make_generic_dictionary_type(MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype) {
  673. MakeGenericDictionaryType thunk = CACHED_METHOD_THUNK(MarshalUtils, MakeGenericDictionaryType);
  674. MonoException *exc = NULL;
  675. MonoReflectionType *reftype = invoke_method_thunk(thunk, p_key_reftype, p_value_reftype, &exc);
  676. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  677. return GDMono::get_singleton()->get_class(mono_class_from_mono_type(mono_reflection_type_get_type(reftype)));
  678. }
  679. } // namespace Marshal
  680. // namespace Marshal
  681. } // namespace GDMonoUtils