gd_mono_marshal.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /*************************************************************************/
  2. /* gd_mono_marshal.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_mono_marshal.h"
  31. #include "../signal_awaiter_utils.h"
  32. #include "gd_mono.h"
  33. #include "gd_mono_cache.h"
  34. #include "gd_mono_class.h"
  35. namespace GDMonoMarshal {
  36. Variant::Type managed_to_variant_type(const ManagedType &p_type, bool *r_nil_is_variant) {
  37. switch (p_type.type_encoding) {
  38. case MONO_TYPE_BOOLEAN:
  39. return Variant::BOOL;
  40. case MONO_TYPE_I1:
  41. return Variant::INT;
  42. case MONO_TYPE_I2:
  43. return Variant::INT;
  44. case MONO_TYPE_I4:
  45. return Variant::INT;
  46. case MONO_TYPE_I8:
  47. return Variant::INT;
  48. case MONO_TYPE_U1:
  49. return Variant::INT;
  50. case MONO_TYPE_U2:
  51. return Variant::INT;
  52. case MONO_TYPE_U4:
  53. return Variant::INT;
  54. case MONO_TYPE_U8:
  55. return Variant::INT;
  56. case MONO_TYPE_R4:
  57. return Variant::FLOAT;
  58. case MONO_TYPE_R8:
  59. return Variant::FLOAT;
  60. case MONO_TYPE_STRING: {
  61. return Variant::STRING;
  62. } break;
  63. case MONO_TYPE_VALUETYPE: {
  64. GDMonoClass *vtclass = p_type.type_class;
  65. if (vtclass == CACHED_CLASS(Vector2))
  66. return Variant::VECTOR2;
  67. if (vtclass == CACHED_CLASS(Vector2i))
  68. return Variant::VECTOR2I;
  69. if (vtclass == CACHED_CLASS(Rect2))
  70. return Variant::RECT2;
  71. if (vtclass == CACHED_CLASS(Rect2i))
  72. return Variant::RECT2I;
  73. if (vtclass == CACHED_CLASS(Transform2D))
  74. return Variant::TRANSFORM2D;
  75. if (vtclass == CACHED_CLASS(Vector3))
  76. return Variant::VECTOR3;
  77. if (vtclass == CACHED_CLASS(Vector3i))
  78. return Variant::VECTOR3I;
  79. if (vtclass == CACHED_CLASS(Basis))
  80. return Variant::BASIS;
  81. if (vtclass == CACHED_CLASS(Quat))
  82. return Variant::QUAT;
  83. if (vtclass == CACHED_CLASS(Transform))
  84. return Variant::TRANSFORM;
  85. if (vtclass == CACHED_CLASS(AABB))
  86. return Variant::AABB;
  87. if (vtclass == CACHED_CLASS(Color))
  88. return Variant::COLOR;
  89. if (vtclass == CACHED_CLASS(Plane))
  90. return Variant::PLANE;
  91. if (vtclass == CACHED_CLASS(Callable))
  92. return Variant::CALLABLE;
  93. if (vtclass == CACHED_CLASS(SignalInfo))
  94. return Variant::SIGNAL;
  95. if (mono_class_is_enum(vtclass->get_mono_ptr()))
  96. return Variant::INT;
  97. } break;
  98. case MONO_TYPE_ARRAY:
  99. case MONO_TYPE_SZARRAY: {
  100. MonoArrayType *array_type = mono_type_get_array_type(p_type.type_class->get_mono_type());
  101. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  102. return Variant::ARRAY;
  103. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  104. return Variant::PACKED_BYTE_ARRAY;
  105. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  106. return Variant::PACKED_INT32_ARRAY;
  107. if (array_type->eklass == CACHED_CLASS_RAW(int64_t))
  108. return Variant::PACKED_INT64_ARRAY;
  109. if (array_type->eklass == CACHED_CLASS_RAW(float))
  110. return Variant::PACKED_FLOAT32_ARRAY;
  111. if (array_type->eklass == CACHED_CLASS_RAW(double))
  112. return Variant::PACKED_FLOAT64_ARRAY;
  113. if (array_type->eklass == CACHED_CLASS_RAW(String))
  114. return Variant::PACKED_STRING_ARRAY;
  115. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  116. return Variant::PACKED_VECTOR2_ARRAY;
  117. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  118. return Variant::PACKED_VECTOR3_ARRAY;
  119. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  120. return Variant::PACKED_COLOR_ARRAY;
  121. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(array_type->eklass);
  122. if (CACHED_CLASS(GodotObject)->is_assignable_from(array_type_class))
  123. return Variant::ARRAY;
  124. } break;
  125. case MONO_TYPE_CLASS: {
  126. GDMonoClass *type_class = p_type.type_class;
  127. // GodotObject
  128. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  129. return Variant::OBJECT;
  130. }
  131. if (CACHED_CLASS(StringName) == type_class) {
  132. return Variant::STRING_NAME;
  133. }
  134. if (CACHED_CLASS(NodePath) == type_class) {
  135. return Variant::NODE_PATH;
  136. }
  137. if (CACHED_CLASS(RID) == type_class) {
  138. return Variant::_RID;
  139. }
  140. if (CACHED_CLASS(Dictionary) == type_class) {
  141. return Variant::DICTIONARY;
  142. }
  143. if (CACHED_CLASS(Array) == type_class) {
  144. return Variant::ARRAY;
  145. }
  146. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  147. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), type_class->get_mono_type());
  148. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype)) {
  149. return Variant::DICTIONARY;
  150. }
  151. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  152. return Variant::DICTIONARY;
  153. }
  154. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype)) {
  155. return Variant::ARRAY;
  156. }
  157. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  158. return Variant::ARRAY;
  159. }
  160. } break;
  161. case MONO_TYPE_OBJECT: {
  162. if (r_nil_is_variant)
  163. *r_nil_is_variant = true;
  164. return Variant::NIL;
  165. } break;
  166. case MONO_TYPE_GENERICINST: {
  167. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), p_type.type_class->get_mono_type());
  168. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  169. return Variant::DICTIONARY;
  170. }
  171. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  172. return Variant::ARRAY;
  173. }
  174. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  175. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype))
  176. return Variant::DICTIONARY;
  177. if (p_type.type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  178. return Variant::DICTIONARY;
  179. }
  180. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype))
  181. return Variant::ARRAY;
  182. if (p_type.type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  183. return Variant::ARRAY;
  184. }
  185. } break;
  186. default: {
  187. } break;
  188. }
  189. if (r_nil_is_variant)
  190. *r_nil_is_variant = false;
  191. // Unknown
  192. return Variant::NIL;
  193. }
  194. bool try_get_array_element_type(const ManagedType &p_array_type, ManagedType &r_elem_type) {
  195. switch (p_array_type.type_encoding) {
  196. case MONO_TYPE_ARRAY:
  197. case MONO_TYPE_SZARRAY: {
  198. MonoArrayType *array_type = mono_type_get_array_type(p_array_type.type_class->get_mono_type());
  199. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(array_type->eklass);
  200. r_elem_type = ManagedType::from_class(array_type_class);
  201. return true;
  202. } break;
  203. case MONO_TYPE_GENERICINST: {
  204. MonoReflectionType *array_reftype = mono_type_get_object(mono_domain_get(), p_array_type.type_class->get_mono_type());
  205. if (GDMonoUtils::Marshal::type_is_generic_array(array_reftype)) {
  206. MonoReflectionType *elem_reftype;
  207. GDMonoUtils::Marshal::array_get_element_type(array_reftype, &elem_reftype);
  208. r_elem_type = ManagedType::from_reftype(elem_reftype);
  209. return true;
  210. }
  211. MonoReflectionType *elem_reftype;
  212. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(array_reftype, &elem_reftype)) {
  213. r_elem_type = ManagedType::from_reftype(elem_reftype);
  214. return true;
  215. }
  216. } break;
  217. default: {
  218. } break;
  219. }
  220. return false;
  221. }
  222. bool try_get_dictionary_key_value_types(const ManagedType &p_dictionary_type, ManagedType &r_key_type, ManagedType &r_value_type) {
  223. switch (p_dictionary_type.type_encoding) {
  224. case MONO_TYPE_GENERICINST: {
  225. MonoReflectionType *dict_reftype = mono_type_get_object(mono_domain_get(), p_dictionary_type.type_class->get_mono_type());
  226. if (GDMonoUtils::Marshal::type_is_generic_dictionary(dict_reftype)) {
  227. MonoReflectionType *key_reftype;
  228. MonoReflectionType *value_reftype;
  229. GDMonoUtils::Marshal::dictionary_get_key_value_types(dict_reftype, &key_reftype, &value_reftype);
  230. r_key_type = ManagedType::from_reftype(key_reftype);
  231. r_value_type = ManagedType::from_reftype(value_reftype);
  232. return true;
  233. }
  234. MonoReflectionType *key_reftype, *value_reftype;
  235. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(dict_reftype, &key_reftype, &value_reftype)) {
  236. r_key_type = ManagedType::from_reftype(key_reftype);
  237. r_value_type = ManagedType::from_reftype(value_reftype);
  238. return true;
  239. }
  240. } break;
  241. default: {
  242. } break;
  243. }
  244. return false;
  245. }
  246. String mono_to_utf8_string(MonoString *p_mono_string) {
  247. MonoError error;
  248. char *utf8 = mono_string_to_utf8_checked(p_mono_string, &error);
  249. if (!mono_error_ok(&error)) {
  250. ERR_PRINT(String() + "Failed to convert MonoString* to UTF-8: '" + mono_error_get_message(&error) + "'.");
  251. mono_error_cleanup(&error);
  252. return String();
  253. }
  254. String ret = String::utf8(utf8);
  255. mono_free(utf8);
  256. return ret;
  257. }
  258. String mono_to_utf16_string(MonoString *p_mono_string) {
  259. int len = mono_string_length(p_mono_string);
  260. String ret;
  261. if (len == 0)
  262. return ret;
  263. ret.resize(len + 1);
  264. ret.set(len, 0);
  265. CharType *src = (CharType *)mono_string_chars(p_mono_string);
  266. CharType *dst = ret.ptrw();
  267. for (int i = 0; i < len; i++) {
  268. dst[i] = src[i];
  269. }
  270. return ret;
  271. }
  272. MonoObject *variant_to_mono_object(const Variant *p_var) {
  273. ManagedType type;
  274. type.type_encoding = MONO_TYPE_OBJECT;
  275. // type.type_class is not needed when we specify the MONO_TYPE_OBJECT encoding
  276. return variant_to_mono_object(p_var, type);
  277. }
  278. MonoObject *variant_to_mono_object(const Variant *p_var, const ManagedType &p_type) {
  279. switch (p_type.type_encoding) {
  280. case MONO_TYPE_BOOLEAN: {
  281. MonoBoolean val = p_var->operator bool();
  282. return BOX_BOOLEAN(val);
  283. }
  284. case MONO_TYPE_CHAR: {
  285. uint16_t val = p_var->operator unsigned short();
  286. return BOX_UINT16(val);
  287. }
  288. case MONO_TYPE_I1: {
  289. int8_t val = p_var->operator signed char();
  290. return BOX_INT8(val);
  291. }
  292. case MONO_TYPE_I2: {
  293. int16_t val = p_var->operator signed short();
  294. return BOX_INT16(val);
  295. }
  296. case MONO_TYPE_I4: {
  297. int32_t val = p_var->operator signed int();
  298. return BOX_INT32(val);
  299. }
  300. case MONO_TYPE_I8: {
  301. int64_t val = p_var->operator int64_t();
  302. return BOX_INT64(val);
  303. }
  304. case MONO_TYPE_U1: {
  305. uint8_t val = p_var->operator unsigned char();
  306. return BOX_UINT8(val);
  307. }
  308. case MONO_TYPE_U2: {
  309. uint16_t val = p_var->operator unsigned short();
  310. return BOX_UINT16(val);
  311. }
  312. case MONO_TYPE_U4: {
  313. uint32_t val = p_var->operator unsigned int();
  314. return BOX_UINT32(val);
  315. }
  316. case MONO_TYPE_U8: {
  317. uint64_t val = p_var->operator uint64_t();
  318. return BOX_UINT64(val);
  319. }
  320. case MONO_TYPE_R4: {
  321. float val = p_var->operator float();
  322. return BOX_FLOAT(val);
  323. }
  324. case MONO_TYPE_R8: {
  325. double val = p_var->operator double();
  326. return BOX_DOUBLE(val);
  327. }
  328. case MONO_TYPE_STRING: {
  329. if (p_var->get_type() == Variant::NIL)
  330. return nullptr; // Otherwise, Variant -> String would return the string "Null"
  331. return (MonoObject *)mono_string_from_godot(p_var->operator String());
  332. } break;
  333. case MONO_TYPE_VALUETYPE: {
  334. GDMonoClass *vtclass = p_type.type_class;
  335. if (vtclass == CACHED_CLASS(Vector2)) {
  336. GDMonoMarshal::M_Vector2 from = MARSHALLED_OUT(Vector2, p_var->operator ::Vector2());
  337. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector2), &from);
  338. }
  339. if (vtclass == CACHED_CLASS(Vector2i)) {
  340. GDMonoMarshal::M_Vector2i from = MARSHALLED_OUT(Vector2i, p_var->operator ::Vector2i());
  341. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector2i), &from);
  342. }
  343. if (vtclass == CACHED_CLASS(Rect2)) {
  344. GDMonoMarshal::M_Rect2 from = MARSHALLED_OUT(Rect2, p_var->operator ::Rect2());
  345. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Rect2), &from);
  346. }
  347. if (vtclass == CACHED_CLASS(Rect2i)) {
  348. GDMonoMarshal::M_Rect2i from = MARSHALLED_OUT(Rect2i, p_var->operator ::Rect2i());
  349. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Rect2i), &from);
  350. }
  351. if (vtclass == CACHED_CLASS(Transform2D)) {
  352. GDMonoMarshal::M_Transform2D from = MARSHALLED_OUT(Transform2D, p_var->operator ::Transform2D());
  353. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Transform2D), &from);
  354. }
  355. if (vtclass == CACHED_CLASS(Vector3)) {
  356. GDMonoMarshal::M_Vector3 from = MARSHALLED_OUT(Vector3, p_var->operator ::Vector3());
  357. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector3), &from);
  358. }
  359. if (vtclass == CACHED_CLASS(Vector3i)) {
  360. GDMonoMarshal::M_Vector3i from = MARSHALLED_OUT(Vector3i, p_var->operator ::Vector3i());
  361. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector3i), &from);
  362. }
  363. if (vtclass == CACHED_CLASS(Basis)) {
  364. GDMonoMarshal::M_Basis from = MARSHALLED_OUT(Basis, p_var->operator ::Basis());
  365. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Basis), &from);
  366. }
  367. if (vtclass == CACHED_CLASS(Quat)) {
  368. GDMonoMarshal::M_Quat from = MARSHALLED_OUT(Quat, p_var->operator ::Quat());
  369. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Quat), &from);
  370. }
  371. if (vtclass == CACHED_CLASS(Transform)) {
  372. GDMonoMarshal::M_Transform from = MARSHALLED_OUT(Transform, p_var->operator ::Transform());
  373. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Transform), &from);
  374. }
  375. if (vtclass == CACHED_CLASS(AABB)) {
  376. GDMonoMarshal::M_AABB from = MARSHALLED_OUT(AABB, p_var->operator ::AABB());
  377. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(AABB), &from);
  378. }
  379. if (vtclass == CACHED_CLASS(Color)) {
  380. GDMonoMarshal::M_Color from = MARSHALLED_OUT(Color, p_var->operator ::Color());
  381. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Color), &from);
  382. }
  383. if (vtclass == CACHED_CLASS(Plane)) {
  384. GDMonoMarshal::M_Plane from = MARSHALLED_OUT(Plane, p_var->operator ::Plane());
  385. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Plane), &from);
  386. }
  387. if (vtclass == CACHED_CLASS(Callable)) {
  388. GDMonoMarshal::M_Callable from = GDMonoMarshal::callable_to_managed(p_var->operator Callable());
  389. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Callable), &from);
  390. }
  391. if (vtclass == CACHED_CLASS(SignalInfo)) {
  392. GDMonoMarshal::M_SignalInfo from = GDMonoMarshal::signal_info_to_managed(p_var->operator Signal());
  393. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(SignalInfo), &from);
  394. }
  395. if (mono_class_is_enum(vtclass->get_mono_ptr())) {
  396. MonoType *enum_basetype = mono_class_enum_basetype(vtclass->get_mono_ptr());
  397. MonoClass *enum_baseclass = mono_class_from_mono_type(enum_basetype);
  398. switch (mono_type_get_type(enum_basetype)) {
  399. case MONO_TYPE_BOOLEAN: {
  400. MonoBoolean val = p_var->operator bool();
  401. return BOX_ENUM(enum_baseclass, val);
  402. }
  403. case MONO_TYPE_CHAR: {
  404. uint16_t val = p_var->operator unsigned short();
  405. return BOX_ENUM(enum_baseclass, val);
  406. }
  407. case MONO_TYPE_I1: {
  408. int8_t val = p_var->operator signed char();
  409. return BOX_ENUM(enum_baseclass, val);
  410. }
  411. case MONO_TYPE_I2: {
  412. int16_t val = p_var->operator signed short();
  413. return BOX_ENUM(enum_baseclass, val);
  414. }
  415. case MONO_TYPE_I4: {
  416. int32_t val = p_var->operator signed int();
  417. return BOX_ENUM(enum_baseclass, val);
  418. }
  419. case MONO_TYPE_I8: {
  420. int64_t val = p_var->operator int64_t();
  421. return BOX_ENUM(enum_baseclass, val);
  422. }
  423. case MONO_TYPE_U1: {
  424. uint8_t val = p_var->operator unsigned char();
  425. return BOX_ENUM(enum_baseclass, val);
  426. }
  427. case MONO_TYPE_U2: {
  428. uint16_t val = p_var->operator unsigned short();
  429. return BOX_ENUM(enum_baseclass, val);
  430. }
  431. case MONO_TYPE_U4: {
  432. uint32_t val = p_var->operator unsigned int();
  433. return BOX_ENUM(enum_baseclass, val);
  434. }
  435. case MONO_TYPE_U8: {
  436. uint64_t val = p_var->operator uint64_t();
  437. return BOX_ENUM(enum_baseclass, val);
  438. }
  439. default: {
  440. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to a managed enum value of unmarshallable base type.");
  441. }
  442. }
  443. }
  444. } break;
  445. case MONO_TYPE_ARRAY:
  446. case MONO_TYPE_SZARRAY: {
  447. MonoArrayType *array_type = mono_type_get_array_type(p_type.type_class->get_mono_type());
  448. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  449. return (MonoObject *)Array_to_mono_array(p_var->operator Array());
  450. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  451. return (MonoObject *)PackedByteArray_to_mono_array(p_var->operator PackedByteArray());
  452. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  453. return (MonoObject *)PackedInt32Array_to_mono_array(p_var->operator PackedInt32Array());
  454. if (array_type->eklass == CACHED_CLASS_RAW(int64_t))
  455. return (MonoObject *)PackedInt64Array_to_mono_array(p_var->operator PackedInt64Array());
  456. if (array_type->eklass == CACHED_CLASS_RAW(float))
  457. return (MonoObject *)PackedFloat32Array_to_mono_array(p_var->operator PackedFloat32Array());
  458. if (array_type->eklass == CACHED_CLASS_RAW(double))
  459. return (MonoObject *)PackedFloat64Array_to_mono_array(p_var->operator PackedFloat64Array());
  460. if (array_type->eklass == CACHED_CLASS_RAW(String))
  461. return (MonoObject *)PackedStringArray_to_mono_array(p_var->operator PackedStringArray());
  462. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  463. return (MonoObject *)PackedVector2Array_to_mono_array(p_var->operator PackedVector2Array());
  464. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  465. return (MonoObject *)PackedVector3Array_to_mono_array(p_var->operator PackedVector3Array());
  466. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  467. return (MonoObject *)PackedColorArray_to_mono_array(p_var->operator PackedColorArray());
  468. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(array_type->eklass);
  469. if (CACHED_CLASS(GodotObject)->is_assignable_from(array_type_class))
  470. return (MonoObject *)Array_to_mono_array(p_var->operator Array(), array_type_class);
  471. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to a managed array of unmarshallable element type.");
  472. } break;
  473. case MONO_TYPE_CLASS: {
  474. GDMonoClass *type_class = p_type.type_class;
  475. // GodotObject
  476. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  477. return GDMonoUtils::unmanaged_get_managed(p_var->operator Object *());
  478. }
  479. if (CACHED_CLASS(StringName) == type_class) {
  480. return GDMonoUtils::create_managed_from(p_var->operator StringName());
  481. }
  482. if (CACHED_CLASS(NodePath) == type_class) {
  483. return GDMonoUtils::create_managed_from(p_var->operator NodePath());
  484. }
  485. if (CACHED_CLASS(RID) == type_class) {
  486. return GDMonoUtils::create_managed_from(p_var->operator RID());
  487. }
  488. if (CACHED_CLASS(Dictionary) == type_class) {
  489. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), CACHED_CLASS(Dictionary));
  490. }
  491. if (CACHED_CLASS(Array) == type_class) {
  492. return GDMonoUtils::create_managed_from(p_var->operator Array(), CACHED_CLASS(Array));
  493. }
  494. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  495. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), type_class->get_mono_type());
  496. MonoReflectionType *key_reftype, *value_reftype;
  497. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype, &key_reftype, &value_reftype)) {
  498. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(),
  499. GDMonoUtils::Marshal::make_generic_dictionary_type(key_reftype, value_reftype));
  500. }
  501. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  502. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), CACHED_CLASS(Dictionary));
  503. }
  504. MonoReflectionType *elem_reftype;
  505. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype, &elem_reftype)) {
  506. return GDMonoUtils::create_managed_from(p_var->operator Array(),
  507. GDMonoUtils::Marshal::make_generic_array_type(elem_reftype));
  508. }
  509. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  510. if (GDMonoCache::tools_godot_api_check()) {
  511. return GDMonoUtils::create_managed_from(p_var->operator Array(), CACHED_CLASS(Array));
  512. } else {
  513. return (MonoObject *)GDMonoMarshal::Array_to_mono_array(p_var->operator Array());
  514. }
  515. }
  516. } break;
  517. case MONO_TYPE_OBJECT: {
  518. // Variant
  519. switch (p_var->get_type()) {
  520. case Variant::BOOL: {
  521. MonoBoolean val = p_var->operator bool();
  522. return BOX_BOOLEAN(val);
  523. }
  524. case Variant::INT: {
  525. int32_t val = p_var->operator signed int();
  526. return BOX_INT32(val);
  527. }
  528. case Variant::FLOAT: {
  529. #ifdef REAL_T_IS_DOUBLE
  530. double val = p_var->operator double();
  531. return BOX_DOUBLE(val);
  532. #else
  533. float val = p_var->operator float();
  534. return BOX_FLOAT(val);
  535. #endif
  536. }
  537. case Variant::STRING:
  538. return (MonoObject *)mono_string_from_godot(p_var->operator String());
  539. case Variant::VECTOR2: {
  540. GDMonoMarshal::M_Vector2 from = MARSHALLED_OUT(Vector2, p_var->operator ::Vector2());
  541. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector2), &from);
  542. }
  543. case Variant::VECTOR2I: {
  544. GDMonoMarshal::M_Vector2i from = MARSHALLED_OUT(Vector2i, p_var->operator ::Vector2i());
  545. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector2i), &from);
  546. }
  547. case Variant::RECT2: {
  548. GDMonoMarshal::M_Rect2 from = MARSHALLED_OUT(Rect2, p_var->operator ::Rect2());
  549. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Rect2), &from);
  550. }
  551. case Variant::RECT2I: {
  552. GDMonoMarshal::M_Rect2i from = MARSHALLED_OUT(Rect2i, p_var->operator ::Rect2i());
  553. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Rect2i), &from);
  554. }
  555. case Variant::VECTOR3: {
  556. GDMonoMarshal::M_Vector3 from = MARSHALLED_OUT(Vector3, p_var->operator ::Vector3());
  557. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector3), &from);
  558. }
  559. case Variant::VECTOR3I: {
  560. GDMonoMarshal::M_Vector3i from = MARSHALLED_OUT(Vector3i, p_var->operator ::Vector3i());
  561. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector3i), &from);
  562. }
  563. case Variant::TRANSFORM2D: {
  564. GDMonoMarshal::M_Transform2D from = MARSHALLED_OUT(Transform2D, p_var->operator ::Transform2D());
  565. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Transform2D), &from);
  566. }
  567. case Variant::PLANE: {
  568. GDMonoMarshal::M_Plane from = MARSHALLED_OUT(Plane, p_var->operator ::Plane());
  569. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Plane), &from);
  570. }
  571. case Variant::QUAT: {
  572. GDMonoMarshal::M_Quat from = MARSHALLED_OUT(Quat, p_var->operator ::Quat());
  573. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Quat), &from);
  574. }
  575. case Variant::AABB: {
  576. GDMonoMarshal::M_AABB from = MARSHALLED_OUT(AABB, p_var->operator ::AABB());
  577. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(AABB), &from);
  578. }
  579. case Variant::BASIS: {
  580. GDMonoMarshal::M_Basis from = MARSHALLED_OUT(Basis, p_var->operator ::Basis());
  581. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Basis), &from);
  582. }
  583. case Variant::TRANSFORM: {
  584. GDMonoMarshal::M_Transform from = MARSHALLED_OUT(Transform, p_var->operator ::Transform());
  585. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Transform), &from);
  586. }
  587. case Variant::COLOR: {
  588. GDMonoMarshal::M_Color from = MARSHALLED_OUT(Color, p_var->operator ::Color());
  589. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Color), &from);
  590. }
  591. case Variant::STRING_NAME:
  592. return GDMonoUtils::create_managed_from(p_var->operator StringName());
  593. case Variant::NODE_PATH:
  594. return GDMonoUtils::create_managed_from(p_var->operator NodePath());
  595. case Variant::_RID:
  596. return GDMonoUtils::create_managed_from(p_var->operator RID());
  597. case Variant::OBJECT:
  598. return GDMonoUtils::unmanaged_get_managed(p_var->operator Object *());
  599. case Variant::CALLABLE: {
  600. GDMonoMarshal::M_Callable from = GDMonoMarshal::callable_to_managed(p_var->operator Callable());
  601. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Callable), &from);
  602. }
  603. case Variant::SIGNAL: {
  604. GDMonoMarshal::M_SignalInfo from = GDMonoMarshal::signal_info_to_managed(p_var->operator Signal());
  605. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(SignalInfo), &from);
  606. }
  607. case Variant::DICTIONARY:
  608. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), CACHED_CLASS(Dictionary));
  609. case Variant::ARRAY:
  610. return GDMonoUtils::create_managed_from(p_var->operator Array(), CACHED_CLASS(Array));
  611. case Variant::PACKED_BYTE_ARRAY:
  612. return (MonoObject *)PackedByteArray_to_mono_array(p_var->operator PackedByteArray());
  613. case Variant::PACKED_INT32_ARRAY:
  614. return (MonoObject *)PackedInt32Array_to_mono_array(p_var->operator PackedInt32Array());
  615. case Variant::PACKED_INT64_ARRAY:
  616. return (MonoObject *)PackedInt64Array_to_mono_array(p_var->operator PackedInt64Array());
  617. case Variant::PACKED_FLOAT32_ARRAY:
  618. return (MonoObject *)PackedFloat32Array_to_mono_array(p_var->operator PackedFloat32Array());
  619. case Variant::PACKED_FLOAT64_ARRAY:
  620. return (MonoObject *)PackedFloat64Array_to_mono_array(p_var->operator PackedFloat64Array());
  621. case Variant::PACKED_STRING_ARRAY:
  622. return (MonoObject *)PackedStringArray_to_mono_array(p_var->operator PackedStringArray());
  623. case Variant::PACKED_VECTOR2_ARRAY:
  624. return (MonoObject *)PackedVector2Array_to_mono_array(p_var->operator PackedVector2Array());
  625. case Variant::PACKED_VECTOR3_ARRAY:
  626. return (MonoObject *)PackedVector3Array_to_mono_array(p_var->operator PackedVector3Array());
  627. case Variant::PACKED_COLOR_ARRAY:
  628. return (MonoObject *)PackedColorArray_to_mono_array(p_var->operator PackedColorArray());
  629. default:
  630. return nullptr;
  631. }
  632. break;
  633. case MONO_TYPE_GENERICINST: {
  634. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), p_type.type_class->get_mono_type());
  635. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  636. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), p_type.type_class);
  637. }
  638. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  639. return GDMonoUtils::create_managed_from(p_var->operator Array(), p_type.type_class);
  640. }
  641. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  642. MonoReflectionType *key_reftype, *value_reftype;
  643. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype, &key_reftype, &value_reftype)) {
  644. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(),
  645. GDMonoUtils::Marshal::make_generic_dictionary_type(key_reftype, value_reftype));
  646. }
  647. if (p_type.type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  648. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), CACHED_CLASS(Dictionary));
  649. }
  650. MonoReflectionType *elem_reftype;
  651. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype, &elem_reftype)) {
  652. return GDMonoUtils::create_managed_from(p_var->operator Array(),
  653. GDMonoUtils::Marshal::make_generic_array_type(elem_reftype));
  654. }
  655. if (p_type.type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  656. if (GDMonoCache::tools_godot_api_check()) {
  657. return GDMonoUtils::create_managed_from(p_var->operator Array(), CACHED_CLASS(Array));
  658. } else {
  659. return (MonoObject *)GDMonoMarshal::Array_to_mono_array(p_var->operator Array());
  660. }
  661. }
  662. } break;
  663. } break;
  664. }
  665. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to an unmarshallable managed type. Name: '" +
  666. p_type.type_class->get_name() + "' Encoding: " + itos(p_type.type_encoding) + ".");
  667. }
  668. Variant mono_object_to_variant_impl(MonoObject *p_obj, const ManagedType &p_type, bool p_fail_with_err = true) {
  669. ERR_FAIL_COND_V(!p_type.type_class, Variant());
  670. switch (p_type.type_encoding) {
  671. case MONO_TYPE_BOOLEAN:
  672. return (bool)unbox<MonoBoolean>(p_obj);
  673. case MONO_TYPE_CHAR:
  674. return unbox<uint16_t>(p_obj);
  675. case MONO_TYPE_I1:
  676. return unbox<int8_t>(p_obj);
  677. case MONO_TYPE_I2:
  678. return unbox<int16_t>(p_obj);
  679. case MONO_TYPE_I4:
  680. return unbox<int32_t>(p_obj);
  681. case MONO_TYPE_I8:
  682. return unbox<int64_t>(p_obj);
  683. case MONO_TYPE_U1:
  684. return unbox<uint8_t>(p_obj);
  685. case MONO_TYPE_U2:
  686. return unbox<uint16_t>(p_obj);
  687. case MONO_TYPE_U4:
  688. return unbox<uint32_t>(p_obj);
  689. case MONO_TYPE_U8:
  690. return unbox<uint64_t>(p_obj);
  691. case MONO_TYPE_R4:
  692. return unbox<float>(p_obj);
  693. case MONO_TYPE_R8:
  694. return unbox<double>(p_obj);
  695. case MONO_TYPE_STRING: {
  696. if (p_obj == nullptr)
  697. return Variant(); // NIL
  698. return mono_string_to_godot_not_null((MonoString *)p_obj);
  699. } break;
  700. case MONO_TYPE_VALUETYPE: {
  701. GDMonoClass *vtclass = p_type.type_class;
  702. if (vtclass == CACHED_CLASS(Vector2))
  703. return MARSHALLED_IN(Vector2, unbox_addr<GDMonoMarshal::M_Vector2>(p_obj));
  704. if (vtclass == CACHED_CLASS(Vector2i))
  705. return MARSHALLED_IN(Vector2i, unbox_addr<GDMonoMarshal::M_Vector2i>(p_obj));
  706. if (vtclass == CACHED_CLASS(Rect2))
  707. return MARSHALLED_IN(Rect2, unbox_addr<GDMonoMarshal::M_Rect2>(p_obj));
  708. if (vtclass == CACHED_CLASS(Rect2i))
  709. return MARSHALLED_IN(Rect2i, unbox_addr<GDMonoMarshal::M_Rect2i>(p_obj));
  710. if (vtclass == CACHED_CLASS(Transform2D))
  711. return MARSHALLED_IN(Transform2D, unbox_addr<GDMonoMarshal::M_Transform2D>(p_obj));
  712. if (vtclass == CACHED_CLASS(Vector3))
  713. return MARSHALLED_IN(Vector3, unbox_addr<GDMonoMarshal::M_Vector3>(p_obj));
  714. if (vtclass == CACHED_CLASS(Vector3i))
  715. return MARSHALLED_IN(Vector3i, unbox_addr<GDMonoMarshal::M_Vector3i>(p_obj));
  716. if (vtclass == CACHED_CLASS(Basis))
  717. return MARSHALLED_IN(Basis, unbox_addr<GDMonoMarshal::M_Basis>(p_obj));
  718. if (vtclass == CACHED_CLASS(Quat))
  719. return MARSHALLED_IN(Quat, unbox_addr<GDMonoMarshal::M_Quat>(p_obj));
  720. if (vtclass == CACHED_CLASS(Transform))
  721. return MARSHALLED_IN(Transform, unbox_addr<GDMonoMarshal::M_Transform>(p_obj));
  722. if (vtclass == CACHED_CLASS(AABB))
  723. return MARSHALLED_IN(AABB, unbox_addr<GDMonoMarshal::M_AABB>(p_obj));
  724. if (vtclass == CACHED_CLASS(Color))
  725. return MARSHALLED_IN(Color, unbox_addr<GDMonoMarshal::M_Color>(p_obj));
  726. if (vtclass == CACHED_CLASS(Plane))
  727. return MARSHALLED_IN(Plane, unbox_addr<GDMonoMarshal::M_Plane>(p_obj));
  728. if (vtclass == CACHED_CLASS(Callable))
  729. return managed_to_callable(unbox<GDMonoMarshal::M_Callable>(p_obj));
  730. if (vtclass == CACHED_CLASS(SignalInfo))
  731. return managed_to_signal_info(unbox<GDMonoMarshal::M_SignalInfo>(p_obj));
  732. if (mono_class_is_enum(vtclass->get_mono_ptr()))
  733. return unbox<int32_t>(p_obj);
  734. } break;
  735. case MONO_TYPE_ARRAY:
  736. case MONO_TYPE_SZARRAY: {
  737. MonoArrayType *array_type = mono_type_get_array_type(p_type.type_class->get_mono_type());
  738. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  739. return mono_array_to_Array((MonoArray *)p_obj);
  740. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  741. return mono_array_to_PackedByteArray((MonoArray *)p_obj);
  742. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  743. return mono_array_to_PackedInt32Array((MonoArray *)p_obj);
  744. if (array_type->eklass == CACHED_CLASS_RAW(int64_t))
  745. return mono_array_to_PackedInt64Array((MonoArray *)p_obj);
  746. if (array_type->eklass == CACHED_CLASS_RAW(float))
  747. return mono_array_to_PackedFloat32Array((MonoArray *)p_obj);
  748. if (array_type->eklass == CACHED_CLASS_RAW(double))
  749. return mono_array_to_PackedFloat64Array((MonoArray *)p_obj);
  750. if (array_type->eklass == CACHED_CLASS_RAW(String))
  751. return mono_array_to_PackedStringArray((MonoArray *)p_obj);
  752. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  753. return mono_array_to_PackedVector2Array((MonoArray *)p_obj);
  754. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  755. return mono_array_to_PackedVector3Array((MonoArray *)p_obj);
  756. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  757. return mono_array_to_PackedColorArray((MonoArray *)p_obj);
  758. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(array_type->eklass);
  759. if (CACHED_CLASS(GodotObject)->is_assignable_from(array_type_class))
  760. return mono_array_to_Array((MonoArray *)p_obj);
  761. if (p_fail_with_err) {
  762. ERR_FAIL_V_MSG(Variant(), "Attempted to convert a managed array of unmarshallable element type to Variant.");
  763. } else {
  764. return Variant();
  765. }
  766. } break;
  767. case MONO_TYPE_CLASS: {
  768. GDMonoClass *type_class = p_type.type_class;
  769. // GodotObject
  770. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  771. Object *ptr = unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_obj));
  772. if (ptr != nullptr) {
  773. Reference *ref = Object::cast_to<Reference>(ptr);
  774. return ref ? Variant(Ref<Reference>(ref)) : Variant(ptr);
  775. }
  776. return Variant();
  777. }
  778. if (CACHED_CLASS(StringName) == type_class) {
  779. StringName *ptr = unbox<StringName *>(CACHED_FIELD(StringName, ptr)->get_value(p_obj));
  780. return ptr ? Variant(*ptr) : Variant();
  781. }
  782. if (CACHED_CLASS(NodePath) == type_class) {
  783. NodePath *ptr = unbox<NodePath *>(CACHED_FIELD(NodePath, ptr)->get_value(p_obj));
  784. return ptr ? Variant(*ptr) : Variant();
  785. }
  786. if (CACHED_CLASS(RID) == type_class) {
  787. RID *ptr = unbox<RID *>(CACHED_FIELD(RID, ptr)->get_value(p_obj));
  788. return ptr ? Variant(*ptr) : Variant();
  789. }
  790. if (CACHED_CLASS(Array) == type_class) {
  791. MonoException *exc = nullptr;
  792. Array *ptr = CACHED_METHOD_THUNK(Array, GetPtr).invoke(p_obj, &exc);
  793. UNHANDLED_EXCEPTION(exc);
  794. return ptr ? Variant(*ptr) : Variant();
  795. }
  796. if (CACHED_CLASS(Dictionary) == type_class) {
  797. MonoException *exc = nullptr;
  798. Dictionary *ptr = CACHED_METHOD_THUNK(Dictionary, GetPtr).invoke(p_obj, &exc);
  799. UNHANDLED_EXCEPTION(exc);
  800. return ptr ? Variant(*ptr) : Variant();
  801. }
  802. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  803. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), type_class->get_mono_type());
  804. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype)) {
  805. return GDMonoUtils::Marshal::generic_idictionary_to_dictionary(p_obj);
  806. }
  807. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  808. return GDMonoUtils::Marshal::idictionary_to_dictionary(p_obj);
  809. }
  810. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype)) {
  811. return GDMonoUtils::Marshal::enumerable_to_array(p_obj);
  812. }
  813. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  814. return GDMonoUtils::Marshal::enumerable_to_array(p_obj);
  815. }
  816. } break;
  817. case MONO_TYPE_GENERICINST: {
  818. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), p_type.type_class->get_mono_type());
  819. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  820. MonoException *exc = nullptr;
  821. MonoObject *ret = p_type.type_class->get_method("GetPtr")->invoke(p_obj, &exc);
  822. UNHANDLED_EXCEPTION(exc);
  823. return *unbox<Dictionary *>(ret);
  824. }
  825. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  826. MonoException *exc = nullptr;
  827. MonoObject *ret = p_type.type_class->get_method("GetPtr")->invoke(p_obj, &exc);
  828. UNHANDLED_EXCEPTION(exc);
  829. return *unbox<Array *>(ret);
  830. }
  831. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  832. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype)) {
  833. return GDMonoUtils::Marshal::generic_idictionary_to_dictionary(p_obj);
  834. }
  835. if (p_type.type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  836. return GDMonoUtils::Marshal::idictionary_to_dictionary(p_obj);
  837. }
  838. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype)) {
  839. return GDMonoUtils::Marshal::enumerable_to_array(p_obj);
  840. }
  841. if (p_type.type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  842. return GDMonoUtils::Marshal::enumerable_to_array(p_obj);
  843. }
  844. } break;
  845. }
  846. if (p_fail_with_err) {
  847. ERR_FAIL_V_MSG(Variant(), "Attempted to convert an unmarshallable managed type to Variant. Name: '" +
  848. p_type.type_class->get_name() + "' Encoding: " + itos(p_type.type_encoding) + ".");
  849. } else {
  850. return Variant();
  851. }
  852. }
  853. Variant mono_object_to_variant(MonoObject *p_obj) {
  854. if (!p_obj)
  855. return Variant();
  856. ManagedType type = ManagedType::from_class(mono_object_get_class(p_obj));
  857. return mono_object_to_variant_impl(p_obj, type);
  858. }
  859. Variant mono_object_to_variant(MonoObject *p_obj, const ManagedType &p_type) {
  860. if (!p_obj)
  861. return Variant();
  862. return mono_object_to_variant_impl(p_obj, p_type);
  863. }
  864. Variant mono_object_to_variant_no_err(MonoObject *p_obj, const ManagedType &p_type) {
  865. if (!p_obj)
  866. return Variant();
  867. return mono_object_to_variant_impl(p_obj, p_type, /* fail_with_err: */ false);
  868. }
  869. String mono_object_to_variant_string(MonoObject *p_obj, MonoException **r_exc) {
  870. ManagedType type = ManagedType::from_class(mono_object_get_class(p_obj));
  871. Variant var = GDMonoMarshal::mono_object_to_variant_no_err(p_obj, type);
  872. if (var.get_type() == Variant::NIL && p_obj != nullptr) {
  873. // Cannot convert MonoObject* to Variant; fallback to 'ToString()'.
  874. MonoException *exc = nullptr;
  875. MonoString *mono_str = GDMonoUtils::object_to_string(p_obj, &exc);
  876. if (exc) {
  877. if (r_exc)
  878. *r_exc = exc;
  879. return String();
  880. }
  881. return GDMonoMarshal::mono_string_to_godot(mono_str);
  882. } else {
  883. return var.operator String();
  884. }
  885. }
  886. MonoArray *Array_to_mono_array(const Array &p_array) {
  887. int length = p_array.size();
  888. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(MonoObject), length);
  889. for (int i = 0; i < length; i++) {
  890. MonoObject *boxed = variant_to_mono_object(p_array[i]);
  891. mono_array_setref(ret, i, boxed);
  892. }
  893. return ret;
  894. }
  895. MonoArray *Array_to_mono_array(const Array &p_array, GDMonoClass *p_array_type_class) {
  896. int length = p_array.size();
  897. MonoArray *ret = mono_array_new(mono_domain_get(), p_array_type_class->get_mono_ptr(), length);
  898. for (int i = 0; i < length; i++) {
  899. MonoObject *boxed = variant_to_mono_object(p_array[i]);
  900. mono_array_setref(ret, i, boxed);
  901. }
  902. return ret;
  903. }
  904. Array mono_array_to_Array(MonoArray *p_array) {
  905. Array ret;
  906. if (!p_array)
  907. return ret;
  908. int length = mono_array_length(p_array);
  909. ret.resize(length);
  910. for (int i = 0; i < length; i++) {
  911. MonoObject *elem = mono_array_get(p_array, MonoObject *, i);
  912. ret[i] = mono_object_to_variant(elem);
  913. }
  914. return ret;
  915. }
  916. MonoArray *PackedInt32Array_to_mono_array(const PackedInt32Array &p_array) {
  917. const int32_t *src = p_array.ptr();
  918. int length = p_array.size();
  919. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(int32_t), length);
  920. int32_t *dst = (int32_t *)mono_array_addr(ret, int32_t, 0);
  921. memcpy(dst, src, length);
  922. return ret;
  923. }
  924. PackedInt32Array mono_array_to_PackedInt32Array(MonoArray *p_array) {
  925. PackedInt32Array ret;
  926. if (!p_array)
  927. return ret;
  928. int length = mono_array_length(p_array);
  929. ret.resize(length);
  930. int32_t *dst = ret.ptrw();
  931. const int32_t *src = (const int32_t *)mono_array_addr(p_array, int32_t, 0);
  932. memcpy(dst, src, length);
  933. return ret;
  934. }
  935. MonoArray *PackedInt64Array_to_mono_array(const PackedInt64Array &p_array) {
  936. const int64_t *src = p_array.ptr();
  937. int length = p_array.size();
  938. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(int64_t), length);
  939. int64_t *dst = (int64_t *)mono_array_addr(ret, int64_t, 0);
  940. memcpy(dst, src, length);
  941. return ret;
  942. }
  943. PackedInt64Array mono_array_to_PackedInt64Array(MonoArray *p_array) {
  944. PackedInt64Array ret;
  945. if (!p_array)
  946. return ret;
  947. int length = mono_array_length(p_array);
  948. ret.resize(length);
  949. int64_t *dst = ret.ptrw();
  950. const int64_t *src = (const int64_t *)mono_array_addr(p_array, int64_t, 0);
  951. memcpy(dst, src, length);
  952. return ret;
  953. }
  954. MonoArray *PackedByteArray_to_mono_array(const PackedByteArray &p_array) {
  955. const uint8_t *src = p_array.ptr();
  956. int length = p_array.size();
  957. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(uint8_t), length);
  958. uint8_t *dst = (uint8_t *)mono_array_addr(ret, uint8_t, 0);
  959. memcpy(dst, src, length);
  960. return ret;
  961. }
  962. PackedByteArray mono_array_to_PackedByteArray(MonoArray *p_array) {
  963. PackedByteArray ret;
  964. if (!p_array)
  965. return ret;
  966. int length = mono_array_length(p_array);
  967. ret.resize(length);
  968. uint8_t *dst = ret.ptrw();
  969. const uint8_t *src = (const uint8_t *)mono_array_addr(p_array, uint8_t, 0);
  970. memcpy(dst, src, length);
  971. return ret;
  972. }
  973. MonoArray *PackedFloat32Array_to_mono_array(const PackedFloat32Array &p_array) {
  974. const float *src = p_array.ptr();
  975. int length = p_array.size();
  976. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(float), length);
  977. float *dst = (float *)mono_array_addr(ret, float, 0);
  978. memcpy(dst, src, length);
  979. return ret;
  980. }
  981. PackedFloat32Array mono_array_to_PackedFloat32Array(MonoArray *p_array) {
  982. PackedFloat32Array ret;
  983. if (!p_array)
  984. return ret;
  985. int length = mono_array_length(p_array);
  986. ret.resize(length);
  987. float *dst = ret.ptrw();
  988. const float *src = (const float *)mono_array_addr(p_array, float, 0);
  989. memcpy(dst, src, length);
  990. return ret;
  991. }
  992. MonoArray *PackedFloat64Array_to_mono_array(const PackedFloat64Array &p_array) {
  993. const double *src = p_array.ptr();
  994. int length = p_array.size();
  995. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(double), length);
  996. double *dst = (double *)mono_array_addr(ret, double, 0);
  997. memcpy(dst, src, length);
  998. return ret;
  999. }
  1000. PackedFloat64Array mono_array_to_PackedFloat64Array(MonoArray *p_array) {
  1001. PackedFloat64Array ret;
  1002. if (!p_array)
  1003. return ret;
  1004. int length = mono_array_length(p_array);
  1005. ret.resize(length);
  1006. double *dst = ret.ptrw();
  1007. const double *src = (const double *)mono_array_addr(p_array, double, 0);
  1008. memcpy(dst, src, length);
  1009. return ret;
  1010. }
  1011. MonoArray *PackedStringArray_to_mono_array(const PackedStringArray &p_array) {
  1012. const String *r = p_array.ptr();
  1013. int length = p_array.size();
  1014. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(String), length);
  1015. for (int i = 0; i < length; i++) {
  1016. MonoString *boxed = mono_string_from_godot(r[i]);
  1017. mono_array_setref(ret, i, boxed);
  1018. }
  1019. return ret;
  1020. }
  1021. PackedStringArray mono_array_to_PackedStringArray(MonoArray *p_array) {
  1022. PackedStringArray ret;
  1023. if (!p_array)
  1024. return ret;
  1025. int length = mono_array_length(p_array);
  1026. ret.resize(length);
  1027. String *w = ret.ptrw();
  1028. for (int i = 0; i < length; i++) {
  1029. MonoString *elem = mono_array_get(p_array, MonoString *, i);
  1030. w[i] = mono_string_to_godot(elem);
  1031. }
  1032. return ret;
  1033. }
  1034. MonoArray *PackedColorArray_to_mono_array(const PackedColorArray &p_array) {
  1035. const Color *src = p_array.ptr();
  1036. int length = p_array.size();
  1037. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Color), length);
  1038. if constexpr (InteropLayout::MATCHES_Color) {
  1039. Color *dst = (Color *)mono_array_addr(ret, Color, 0);
  1040. memcpy(dst, src, length);
  1041. } else {
  1042. for (int i = 0; i < length; i++) {
  1043. M_Color *raw = (M_Color *)mono_array_addr_with_size(ret, sizeof(M_Color), i);
  1044. *raw = MARSHALLED_OUT(Color, src[i]);
  1045. }
  1046. }
  1047. return ret;
  1048. }
  1049. PackedColorArray mono_array_to_PackedColorArray(MonoArray *p_array) {
  1050. PackedColorArray ret;
  1051. if (!p_array)
  1052. return ret;
  1053. int length = mono_array_length(p_array);
  1054. ret.resize(length);
  1055. Color *dst = ret.ptrw();
  1056. if constexpr (InteropLayout::MATCHES_Color) {
  1057. const Color *src = (const Color *)mono_array_addr(p_array, Color, 0);
  1058. memcpy(dst, src, length);
  1059. } else {
  1060. for (int i = 0; i < length; i++) {
  1061. dst[i] = MARSHALLED_IN(Color, (M_Color *)mono_array_addr_with_size(p_array, sizeof(M_Color), i));
  1062. }
  1063. }
  1064. return ret;
  1065. }
  1066. MonoArray *PackedVector2Array_to_mono_array(const PackedVector2Array &p_array) {
  1067. const Vector2 *src = p_array.ptr();
  1068. int length = p_array.size();
  1069. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Vector2), length);
  1070. if constexpr (InteropLayout::MATCHES_Vector2) {
  1071. Vector2 *dst = (Vector2 *)mono_array_addr(ret, Vector2, 0);
  1072. memcpy(dst, src, length);
  1073. } else {
  1074. for (int i = 0; i < length; i++) {
  1075. M_Vector2 *raw = (M_Vector2 *)mono_array_addr_with_size(ret, sizeof(M_Vector2), i);
  1076. *raw = MARSHALLED_OUT(Vector2, src[i]);
  1077. }
  1078. }
  1079. return ret;
  1080. }
  1081. PackedVector2Array mono_array_to_PackedVector2Array(MonoArray *p_array) {
  1082. PackedVector2Array ret;
  1083. if (!p_array)
  1084. return ret;
  1085. int length = mono_array_length(p_array);
  1086. ret.resize(length);
  1087. Vector2 *dst = ret.ptrw();
  1088. if constexpr (InteropLayout::MATCHES_Vector2) {
  1089. const Vector2 *src = (const Vector2 *)mono_array_addr(p_array, Vector2, 0);
  1090. memcpy(dst, src, length);
  1091. } else {
  1092. for (int i = 0; i < length; i++) {
  1093. dst[i] = MARSHALLED_IN(Vector2, (M_Vector2 *)mono_array_addr_with_size(p_array, sizeof(M_Vector2), i));
  1094. }
  1095. }
  1096. return ret;
  1097. }
  1098. MonoArray *PackedVector3Array_to_mono_array(const PackedVector3Array &p_array) {
  1099. const Vector3 *src = p_array.ptr();
  1100. int length = p_array.size();
  1101. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Vector3), length);
  1102. if constexpr (InteropLayout::MATCHES_Vector3) {
  1103. Vector3 *dst = (Vector3 *)mono_array_addr(ret, Vector3, 0);
  1104. memcpy(dst, src, length);
  1105. } else {
  1106. for (int i = 0; i < length; i++) {
  1107. M_Vector3 *raw = (M_Vector3 *)mono_array_addr_with_size(ret, sizeof(M_Vector3), i);
  1108. *raw = MARSHALLED_OUT(Vector3, src[i]);
  1109. }
  1110. }
  1111. return ret;
  1112. }
  1113. PackedVector3Array mono_array_to_PackedVector3Array(MonoArray *p_array) {
  1114. PackedVector3Array ret;
  1115. if (!p_array)
  1116. return ret;
  1117. int length = mono_array_length(p_array);
  1118. ret.resize(length);
  1119. Vector3 *dst = ret.ptrw();
  1120. if constexpr (InteropLayout::MATCHES_Vector3) {
  1121. const Vector3 *src = (const Vector3 *)mono_array_addr(p_array, Vector3, 0);
  1122. memcpy(dst, src, length);
  1123. } else {
  1124. for (int i = 0; i < length; i++) {
  1125. dst[i] = MARSHALLED_IN(Vector3, (M_Vector3 *)mono_array_addr_with_size(p_array, sizeof(M_Vector3), i));
  1126. }
  1127. }
  1128. return ret;
  1129. }
  1130. Callable managed_to_callable(const M_Callable &p_managed_callable) {
  1131. if (p_managed_callable.delegate) {
  1132. // TODO: Use pooling for ManagedCallable instances.
  1133. CallableCustom *managed_callable = memnew(ManagedCallable(p_managed_callable.delegate));
  1134. return Callable(managed_callable);
  1135. } else {
  1136. Object *target = p_managed_callable.target ?
  1137. unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_managed_callable.target)) :
  1138. nullptr;
  1139. StringName *method_ptr = unbox<StringName *>(CACHED_FIELD(StringName, ptr)->get_value(p_managed_callable.method_string_name));
  1140. StringName method = method_ptr ? *method_ptr : StringName();
  1141. return Callable(target, method);
  1142. }
  1143. }
  1144. M_Callable callable_to_managed(const Callable &p_callable) {
  1145. if (p_callable.is_custom()) {
  1146. CallableCustom *custom = p_callable.get_custom();
  1147. CallableCustom::CompareEqualFunc compare_equal_func = custom->get_compare_equal_func();
  1148. if (compare_equal_func == ManagedCallable::compare_equal_func_ptr) {
  1149. ManagedCallable *managed_callable = static_cast<ManagedCallable *>(custom);
  1150. return {
  1151. nullptr, nullptr,
  1152. managed_callable->get_delegate()
  1153. };
  1154. } else if (compare_equal_func == SignalAwaiterCallable::compare_equal_func_ptr) {
  1155. SignalAwaiterCallable *signal_awaiter_callable = static_cast<SignalAwaiterCallable *>(custom);
  1156. return {
  1157. GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(signal_awaiter_callable->get_object())),
  1158. GDMonoUtils::create_managed_from(signal_awaiter_callable->get_signal()),
  1159. nullptr
  1160. };
  1161. } else if (compare_equal_func == EventSignalCallable::compare_equal_func_ptr) {
  1162. EventSignalCallable *event_signal_callable = static_cast<EventSignalCallable *>(custom);
  1163. return {
  1164. GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(event_signal_callable->get_object())),
  1165. GDMonoUtils::create_managed_from(event_signal_callable->get_signal()),
  1166. nullptr
  1167. };
  1168. }
  1169. // Some other CallableCustom. We only support ManagedCallable.
  1170. return { nullptr, nullptr, nullptr };
  1171. } else {
  1172. MonoObject *target_managed = GDMonoUtils::unmanaged_get_managed(p_callable.get_object());
  1173. MonoObject *method_string_name_managed = GDMonoUtils::create_managed_from(p_callable.get_method());
  1174. return { target_managed, method_string_name_managed, nullptr };
  1175. }
  1176. }
  1177. Signal managed_to_signal_info(const M_SignalInfo &p_managed_signal) {
  1178. Object *owner = p_managed_signal.owner ?
  1179. unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_managed_signal.owner)) :
  1180. nullptr;
  1181. StringName *name_ptr = unbox<StringName *>(CACHED_FIELD(StringName, ptr)->get_value(p_managed_signal.name_string_name));
  1182. StringName name = name_ptr ? *name_ptr : StringName();
  1183. return Signal(owner, name);
  1184. }
  1185. M_SignalInfo signal_info_to_managed(const Signal &p_signal) {
  1186. Object *owner = p_signal.get_object();
  1187. MonoObject *owner_managed = GDMonoUtils::unmanaged_get_managed(owner);
  1188. MonoObject *name_string_name_managed = GDMonoUtils::create_managed_from(p_signal.get_name());
  1189. return { owner_managed, name_string_name_managed };
  1190. }
  1191. } // namespace GDMonoMarshal