gd_mono_marshal.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. }
  68. if (vtclass == CACHED_CLASS(Vector2i)) {
  69. return Variant::VECTOR2I;
  70. }
  71. if (vtclass == CACHED_CLASS(Rect2)) {
  72. return Variant::RECT2;
  73. }
  74. if (vtclass == CACHED_CLASS(Rect2i)) {
  75. return Variant::RECT2I;
  76. }
  77. if (vtclass == CACHED_CLASS(Transform2D)) {
  78. return Variant::TRANSFORM2D;
  79. }
  80. if (vtclass == CACHED_CLASS(Vector3)) {
  81. return Variant::VECTOR3;
  82. }
  83. if (vtclass == CACHED_CLASS(Vector3i)) {
  84. return Variant::VECTOR3I;
  85. }
  86. if (vtclass == CACHED_CLASS(Vector4)) {
  87. return Variant::VECTOR4;
  88. }
  89. if (vtclass == CACHED_CLASS(Vector4i)) {
  90. return Variant::VECTOR4I;
  91. }
  92. if (vtclass == CACHED_CLASS(Basis)) {
  93. return Variant::BASIS;
  94. }
  95. if (vtclass == CACHED_CLASS(Quaternion)) {
  96. return Variant::QUATERNION;
  97. }
  98. if (vtclass == CACHED_CLASS(Transform3D)) {
  99. return Variant::TRANSFORM3D;
  100. }
  101. if (vtclass == CACHED_CLASS(Projection)) {
  102. return Variant::PROJECTION;
  103. }
  104. if (vtclass == CACHED_CLASS(AABB)) {
  105. return Variant::AABB;
  106. }
  107. if (vtclass == CACHED_CLASS(Color)) {
  108. return Variant::COLOR;
  109. }
  110. if (vtclass == CACHED_CLASS(Plane)) {
  111. return Variant::PLANE;
  112. }
  113. if (vtclass == CACHED_CLASS(Callable)) {
  114. return Variant::CALLABLE;
  115. }
  116. if (vtclass == CACHED_CLASS(SignalInfo)) {
  117. return Variant::SIGNAL;
  118. }
  119. if (mono_class_is_enum(vtclass->get_mono_ptr())) {
  120. return Variant::INT;
  121. }
  122. } break;
  123. case MONO_TYPE_ARRAY:
  124. case MONO_TYPE_SZARRAY: {
  125. MonoClass *elem_class = mono_class_get_element_class(p_type.type_class->get_mono_ptr());
  126. if (elem_class == CACHED_CLASS_RAW(MonoObject)) {
  127. return Variant::ARRAY;
  128. }
  129. if (elem_class == CACHED_CLASS_RAW(uint8_t)) {
  130. return Variant::PACKED_BYTE_ARRAY;
  131. }
  132. if (elem_class == CACHED_CLASS_RAW(int32_t)) {
  133. return Variant::PACKED_INT32_ARRAY;
  134. }
  135. if (elem_class == CACHED_CLASS_RAW(int64_t)) {
  136. return Variant::PACKED_INT64_ARRAY;
  137. }
  138. if (elem_class == CACHED_CLASS_RAW(float)) {
  139. return Variant::PACKED_FLOAT32_ARRAY;
  140. }
  141. if (elem_class == CACHED_CLASS_RAW(double)) {
  142. return Variant::PACKED_FLOAT64_ARRAY;
  143. }
  144. if (elem_class == CACHED_CLASS_RAW(String)) {
  145. return Variant::PACKED_STRING_ARRAY;
  146. }
  147. if (elem_class == CACHED_CLASS_RAW(Vector2)) {
  148. return Variant::PACKED_VECTOR2_ARRAY;
  149. }
  150. if (elem_class == CACHED_CLASS_RAW(Vector3)) {
  151. return Variant::PACKED_VECTOR3_ARRAY;
  152. }
  153. if (elem_class == CACHED_CLASS_RAW(Color)) {
  154. return Variant::PACKED_COLOR_ARRAY;
  155. }
  156. if (elem_class == CACHED_CLASS_RAW(StringName)) {
  157. return Variant::ARRAY;
  158. }
  159. if (elem_class == CACHED_CLASS_RAW(NodePath)) {
  160. return Variant::ARRAY;
  161. }
  162. if (elem_class == CACHED_CLASS_RAW(RID)) {
  163. return Variant::ARRAY;
  164. }
  165. if (mono_class_is_enum(elem_class)) {
  166. return Variant::ARRAY;
  167. }
  168. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(elem_class);
  169. if (CACHED_CLASS(GodotObject)->is_assignable_from(array_type_class)) {
  170. return Variant::ARRAY;
  171. }
  172. } break;
  173. case MONO_TYPE_CLASS: {
  174. GDMonoClass *type_class = p_type.type_class;
  175. // GodotObject
  176. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  177. return Variant::OBJECT;
  178. }
  179. if (CACHED_CLASS(StringName) == type_class) {
  180. return Variant::STRING_NAME;
  181. }
  182. if (CACHED_CLASS(NodePath) == type_class) {
  183. return Variant::NODE_PATH;
  184. }
  185. if (CACHED_CLASS(RID) == type_class) {
  186. return Variant::RID;
  187. }
  188. if (CACHED_CLASS(Dictionary) == type_class) {
  189. return Variant::DICTIONARY;
  190. }
  191. if (CACHED_CLASS(Array) == type_class) {
  192. return Variant::ARRAY;
  193. }
  194. // IDictionary
  195. if (p_type.type_class == CACHED_CLASS(System_Collections_IDictionary)) {
  196. return Variant::DICTIONARY;
  197. }
  198. // ICollection or IEnumerable
  199. if (p_type.type_class == CACHED_CLASS(System_Collections_ICollection) ||
  200. p_type.type_class == CACHED_CLASS(System_Collections_IEnumerable)) {
  201. return Variant::ARRAY;
  202. }
  203. } break;
  204. case MONO_TYPE_OBJECT: {
  205. if (r_nil_is_variant) {
  206. *r_nil_is_variant = true;
  207. }
  208. return Variant::NIL;
  209. } break;
  210. case MONO_TYPE_GENERICINST: {
  211. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), p_type.type_class->get_mono_type());
  212. // Godot.Collections.Dictionary<TKey, TValue>
  213. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  214. return Variant::DICTIONARY;
  215. }
  216. // Godot.Collections.Array<T>
  217. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  218. return Variant::ARRAY;
  219. }
  220. // System.Collections.Generic.Dictionary<TKey, TValue>
  221. if (GDMonoUtils::Marshal::type_is_system_generic_dictionary(reftype)) {
  222. return Variant::DICTIONARY;
  223. }
  224. // System.Collections.Generic.List<T>
  225. if (GDMonoUtils::Marshal::type_is_system_generic_list(reftype)) {
  226. return Variant::ARRAY;
  227. }
  228. // IDictionary<TKey, TValue>
  229. if (GDMonoUtils::Marshal::type_is_generic_idictionary(reftype)) {
  230. return Variant::DICTIONARY;
  231. }
  232. // ICollection<T> or IEnumerable<T>
  233. if (GDMonoUtils::Marshal::type_is_generic_icollection(reftype) || GDMonoUtils::Marshal::type_is_generic_ienumerable(reftype)) {
  234. return Variant::ARRAY;
  235. }
  236. // GodotObject
  237. GDMonoClass *type_class = p_type.type_class;
  238. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  239. return Variant::OBJECT;
  240. }
  241. } break;
  242. default: {
  243. } break;
  244. }
  245. if (r_nil_is_variant) {
  246. *r_nil_is_variant = false;
  247. }
  248. // Unknown
  249. return Variant::NIL;
  250. }
  251. bool try_get_array_element_type(const ManagedType &p_array_type, ManagedType &r_elem_type) {
  252. switch (p_array_type.type_encoding) {
  253. case MONO_TYPE_ARRAY:
  254. case MONO_TYPE_SZARRAY: {
  255. MonoClass *elem_class = mono_class_get_element_class(p_array_type.type_class->get_mono_ptr());
  256. r_elem_type = ManagedType::from_class(elem_class);
  257. return true;
  258. } break;
  259. case MONO_TYPE_GENERICINST: {
  260. MonoReflectionType *array_reftype = mono_type_get_object(mono_domain_get(), p_array_type.type_class->get_mono_type());
  261. if (GDMonoUtils::Marshal::type_is_generic_array(array_reftype) ||
  262. GDMonoUtils::Marshal::type_is_system_generic_list(array_reftype) ||
  263. GDMonoUtils::Marshal::type_is_generic_icollection(array_reftype) ||
  264. GDMonoUtils::Marshal::type_is_generic_ienumerable(array_reftype)) {
  265. MonoReflectionType *elem_reftype;
  266. GDMonoUtils::Marshal::array_get_element_type(array_reftype, &elem_reftype);
  267. r_elem_type = ManagedType::from_reftype(elem_reftype);
  268. return true;
  269. }
  270. } break;
  271. default: {
  272. } break;
  273. }
  274. return false;
  275. }
  276. MonoString *variant_to_mono_string(const Variant &p_var) {
  277. if (p_var.get_type() == Variant::NIL) {
  278. return nullptr; // Otherwise, Variant -> String would return the string "Null"
  279. }
  280. return mono_string_from_godot(p_var.operator String());
  281. }
  282. MonoArray *variant_to_mono_array(const Variant &p_var, GDMonoClass *p_type_class) {
  283. MonoArrayType *array_type = mono_type_get_array_type(p_type_class->get_mono_type());
  284. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject)) {
  285. return Array_to_mono_array(p_var.operator Array());
  286. }
  287. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t)) {
  288. return PackedByteArray_to_mono_array(p_var.operator PackedByteArray());
  289. }
  290. if (array_type->eklass == CACHED_CLASS_RAW(int32_t)) {
  291. return PackedInt32Array_to_mono_array(p_var.operator PackedInt32Array());
  292. }
  293. if (array_type->eklass == CACHED_CLASS_RAW(int64_t)) {
  294. return PackedInt64Array_to_mono_array(p_var.operator PackedInt64Array());
  295. }
  296. if (array_type->eklass == CACHED_CLASS_RAW(float)) {
  297. return PackedFloat32Array_to_mono_array(p_var.operator PackedFloat32Array());
  298. }
  299. if (array_type->eklass == CACHED_CLASS_RAW(double)) {
  300. return PackedFloat64Array_to_mono_array(p_var.operator PackedFloat64Array());
  301. }
  302. if (array_type->eklass == CACHED_CLASS_RAW(String)) {
  303. return PackedStringArray_to_mono_array(p_var.operator PackedStringArray());
  304. }
  305. if (array_type->eklass == CACHED_CLASS_RAW(Vector2)) {
  306. return PackedVector2Array_to_mono_array(p_var.operator PackedVector2Array());
  307. }
  308. if (array_type->eklass == CACHED_CLASS_RAW(Vector3)) {
  309. return PackedVector3Array_to_mono_array(p_var.operator PackedVector3Array());
  310. }
  311. if (array_type->eklass == CACHED_CLASS_RAW(Color)) {
  312. return PackedColorArray_to_mono_array(p_var.operator PackedColorArray());
  313. }
  314. if (array_type->eklass == CACHED_CLASS_RAW(StringName)) {
  315. return Array_to_mono_array(p_var.operator Array());
  316. }
  317. if (array_type->eklass == CACHED_CLASS_RAW(NodePath)) {
  318. return Array_to_mono_array(p_var.operator Array());
  319. }
  320. if (array_type->eklass == CACHED_CLASS_RAW(RID)) {
  321. return Array_to_mono_array(p_var.operator Array());
  322. }
  323. if (mono_class_is_assignable_from(CACHED_CLASS(GodotObject)->get_mono_ptr(), array_type->eklass)) {
  324. return Array_to_mono_array(p_var.operator ::Array(), array_type->eklass);
  325. }
  326. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to array of unsupported element type:" + GDMonoClass::get_full_name(array_type->eklass) + "'.");
  327. }
  328. MonoObject *variant_to_mono_object_of_class(const Variant &p_var, GDMonoClass *p_type_class) {
  329. // GodotObject
  330. if (CACHED_CLASS(GodotObject)->is_assignable_from(p_type_class)) {
  331. return GDMonoUtils::unmanaged_get_managed(p_var.operator Object *());
  332. }
  333. if (CACHED_CLASS(StringName) == p_type_class) {
  334. return GDMonoUtils::create_managed_from(p_var.operator StringName());
  335. }
  336. if (CACHED_CLASS(NodePath) == p_type_class) {
  337. return GDMonoUtils::create_managed_from(p_var.operator NodePath());
  338. }
  339. if (CACHED_CLASS(RID) == p_type_class) {
  340. return GDMonoUtils::create_managed_from(p_var.operator ::RID());
  341. }
  342. // Godot.Collections.Dictionary or IDictionary
  343. if (CACHED_CLASS(Dictionary) == p_type_class || CACHED_CLASS(System_Collections_IDictionary) == p_type_class) {
  344. return GDMonoUtils::create_managed_from(p_var.operator Dictionary(), CACHED_CLASS(Dictionary));
  345. }
  346. // Godot.Collections.Array or ICollection or IEnumerable
  347. if (CACHED_CLASS(Array) == p_type_class ||
  348. CACHED_CLASS(System_Collections_ICollection) == p_type_class ||
  349. CACHED_CLASS(System_Collections_IEnumerable) == p_type_class) {
  350. return GDMonoUtils::create_managed_from(p_var.operator Array(), CACHED_CLASS(Array));
  351. }
  352. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to unsupported type: '" + p_type_class->get_full_name() + "'.");
  353. }
  354. MonoObject *variant_to_mono_object_of_genericinst(const Variant &p_var, GDMonoClass *p_type_class) {
  355. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), p_type_class->get_mono_type());
  356. // Godot.Collections.Dictionary<TKey, TValue>
  357. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  358. return GDMonoUtils::create_managed_from(p_var.operator Dictionary(), p_type_class);
  359. }
  360. // Godot.Collections.Array<T>
  361. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  362. return GDMonoUtils::create_managed_from(p_var.operator Array(), p_type_class);
  363. }
  364. // System.Collections.Generic.Dictionary<TKey, TValue>
  365. if (GDMonoUtils::Marshal::type_is_system_generic_dictionary(reftype)) {
  366. MonoReflectionType *key_reftype = nullptr;
  367. MonoReflectionType *value_reftype = nullptr;
  368. GDMonoUtils::Marshal::dictionary_get_key_value_types(reftype, &key_reftype, &value_reftype);
  369. return Dictionary_to_system_generic_dict(p_var.operator Dictionary(), p_type_class, key_reftype, value_reftype);
  370. }
  371. // System.Collections.Generic.List<T>
  372. if (GDMonoUtils::Marshal::type_is_system_generic_list(reftype)) {
  373. MonoReflectionType *elem_reftype = nullptr;
  374. GDMonoUtils::Marshal::array_get_element_type(reftype, &elem_reftype);
  375. return Array_to_system_generic_list(p_var.operator Array(), p_type_class, elem_reftype);
  376. }
  377. // IDictionary<TKey, TValue>
  378. if (GDMonoUtils::Marshal::type_is_generic_idictionary(reftype)) {
  379. MonoReflectionType *key_reftype;
  380. MonoReflectionType *value_reftype;
  381. GDMonoUtils::Marshal::dictionary_get_key_value_types(reftype, &key_reftype, &value_reftype);
  382. GDMonoClass *godot_dict_class = GDMonoUtils::Marshal::make_generic_dictionary_type(key_reftype, value_reftype);
  383. return GDMonoUtils::create_managed_from(p_var.operator Dictionary(), godot_dict_class);
  384. }
  385. // ICollection<T> or IEnumerable<T>
  386. if (GDMonoUtils::Marshal::type_is_generic_icollection(reftype) || GDMonoUtils::Marshal::type_is_generic_ienumerable(reftype)) {
  387. MonoReflectionType *elem_reftype;
  388. GDMonoUtils::Marshal::array_get_element_type(reftype, &elem_reftype);
  389. GDMonoClass *godot_array_class = GDMonoUtils::Marshal::make_generic_array_type(elem_reftype);
  390. return GDMonoUtils::create_managed_from(p_var.operator Array(), godot_array_class);
  391. }
  392. // GodotObject
  393. if (CACHED_CLASS(GodotObject)->is_assignable_from(p_type_class)) {
  394. return GDMonoUtils::unmanaged_get_managed(p_var.operator Object *());
  395. }
  396. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to unsupported generic type: '" + p_type_class->get_full_name() + "'.");
  397. }
  398. MonoObject *variant_to_mono_object(const Variant &p_var) {
  399. // Variant
  400. switch (p_var.get_type()) {
  401. case Variant::BOOL: {
  402. MonoBoolean val = p_var.operator bool();
  403. return BOX_BOOLEAN(val);
  404. }
  405. case Variant::INT: {
  406. int64_t val = p_var.operator int64_t();
  407. return BOX_INT64(val);
  408. }
  409. case Variant::FLOAT: {
  410. #ifdef REAL_T_IS_DOUBLE
  411. double val = p_var.operator double();
  412. return BOX_DOUBLE(val);
  413. #else
  414. float val = p_var.operator float();
  415. return BOX_FLOAT(val);
  416. #endif
  417. }
  418. case Variant::STRING:
  419. return (MonoObject *)mono_string_from_godot(p_var.operator String());
  420. case Variant::VECTOR2: {
  421. GDMonoMarshal::M_Vector2 from = MARSHALLED_OUT(Vector2, p_var.operator ::Vector2());
  422. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector2), &from);
  423. }
  424. case Variant::VECTOR2I: {
  425. GDMonoMarshal::M_Vector2i from = MARSHALLED_OUT(Vector2i, p_var.operator ::Vector2i());
  426. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector2i), &from);
  427. }
  428. case Variant::RECT2: {
  429. GDMonoMarshal::M_Rect2 from = MARSHALLED_OUT(Rect2, p_var.operator ::Rect2());
  430. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Rect2), &from);
  431. }
  432. case Variant::RECT2I: {
  433. GDMonoMarshal::M_Rect2i from = MARSHALLED_OUT(Rect2i, p_var.operator ::Rect2i());
  434. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Rect2i), &from);
  435. }
  436. case Variant::VECTOR3: {
  437. GDMonoMarshal::M_Vector3 from = MARSHALLED_OUT(Vector3, p_var.operator ::Vector3());
  438. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector3), &from);
  439. }
  440. case Variant::VECTOR3I: {
  441. GDMonoMarshal::M_Vector3i from = MARSHALLED_OUT(Vector3i, p_var.operator ::Vector3i());
  442. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector3i), &from);
  443. }
  444. case Variant::TRANSFORM2D: {
  445. GDMonoMarshal::M_Transform2D from = MARSHALLED_OUT(Transform2D, p_var.operator ::Transform2D());
  446. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Transform2D), &from);
  447. }
  448. case Variant::VECTOR4: {
  449. GDMonoMarshal::M_Vector4 from = MARSHALLED_OUT(Vector4, p_var.operator ::Vector4());
  450. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector4), &from);
  451. }
  452. case Variant::VECTOR4I: {
  453. GDMonoMarshal::M_Vector4i from = MARSHALLED_OUT(Vector4i, p_var.operator ::Vector4i());
  454. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Vector4i), &from);
  455. }
  456. case Variant::PLANE: {
  457. GDMonoMarshal::M_Plane from = MARSHALLED_OUT(Plane, p_var.operator ::Plane());
  458. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Plane), &from);
  459. }
  460. case Variant::QUATERNION: {
  461. GDMonoMarshal::M_Quaternion from = MARSHALLED_OUT(Quaternion, p_var.operator ::Quaternion());
  462. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Quaternion), &from);
  463. }
  464. case Variant::AABB: {
  465. GDMonoMarshal::M_AABB from = MARSHALLED_OUT(AABB, p_var.operator ::AABB());
  466. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(AABB), &from);
  467. }
  468. case Variant::BASIS: {
  469. GDMonoMarshal::M_Basis from = MARSHALLED_OUT(Basis, p_var.operator ::Basis());
  470. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Basis), &from);
  471. }
  472. case Variant::TRANSFORM3D: {
  473. GDMonoMarshal::M_Transform3D from = MARSHALLED_OUT(Transform3D, p_var.operator ::Transform3D());
  474. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Transform3D), &from);
  475. }
  476. case Variant::PROJECTION: {
  477. GDMonoMarshal::M_Projection from = MARSHALLED_OUT(Projection, p_var.operator ::Projection());
  478. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Projection), &from);
  479. }
  480. case Variant::COLOR: {
  481. GDMonoMarshal::M_Color from = MARSHALLED_OUT(Color, p_var.operator ::Color());
  482. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Color), &from);
  483. }
  484. case Variant::STRING_NAME:
  485. return GDMonoUtils::create_managed_from(p_var.operator StringName());
  486. case Variant::NODE_PATH:
  487. return GDMonoUtils::create_managed_from(p_var.operator NodePath());
  488. case Variant::RID:
  489. return GDMonoUtils::create_managed_from(p_var.operator ::RID());
  490. case Variant::OBJECT:
  491. return GDMonoUtils::unmanaged_get_managed(p_var.operator Object *());
  492. case Variant::CALLABLE: {
  493. GDMonoMarshal::M_Callable from = GDMonoMarshal::callable_to_managed(p_var.operator Callable());
  494. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Callable), &from);
  495. }
  496. case Variant::SIGNAL: {
  497. GDMonoMarshal::M_SignalInfo from = GDMonoMarshal::signal_info_to_managed(p_var.operator Signal());
  498. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(SignalInfo), &from);
  499. }
  500. case Variant::DICTIONARY:
  501. return GDMonoUtils::create_managed_from(p_var.operator Dictionary(), CACHED_CLASS(Dictionary));
  502. case Variant::ARRAY:
  503. return GDMonoUtils::create_managed_from(p_var.operator Array(), CACHED_CLASS(Array));
  504. case Variant::PACKED_BYTE_ARRAY:
  505. return (MonoObject *)PackedByteArray_to_mono_array(p_var.operator PackedByteArray());
  506. case Variant::PACKED_INT32_ARRAY:
  507. return (MonoObject *)PackedInt32Array_to_mono_array(p_var.operator PackedInt32Array());
  508. case Variant::PACKED_INT64_ARRAY:
  509. return (MonoObject *)PackedInt64Array_to_mono_array(p_var.operator PackedInt64Array());
  510. case Variant::PACKED_FLOAT32_ARRAY:
  511. return (MonoObject *)PackedFloat32Array_to_mono_array(p_var.operator PackedFloat32Array());
  512. case Variant::PACKED_FLOAT64_ARRAY:
  513. return (MonoObject *)PackedFloat64Array_to_mono_array(p_var.operator PackedFloat64Array());
  514. case Variant::PACKED_STRING_ARRAY:
  515. return (MonoObject *)PackedStringArray_to_mono_array(p_var.operator PackedStringArray());
  516. case Variant::PACKED_VECTOR2_ARRAY:
  517. return (MonoObject *)PackedVector2Array_to_mono_array(p_var.operator PackedVector2Array());
  518. case Variant::PACKED_VECTOR3_ARRAY:
  519. return (MonoObject *)PackedVector3Array_to_mono_array(p_var.operator PackedVector3Array());
  520. case Variant::PACKED_COLOR_ARRAY:
  521. return (MonoObject *)PackedColorArray_to_mono_array(p_var.operator PackedColorArray());
  522. default:
  523. return nullptr;
  524. }
  525. }
  526. size_t variant_get_managed_unboxed_size(const ManagedType &p_type) {
  527. // This method prints no errors for unsupported types. It's called on all methods, not only
  528. // those that end up being invoked with Variant parameters.
  529. // For MonoObject* we return 0, as it doesn't need to be stored.
  530. constexpr size_t zero_for_mono_object = 0;
  531. switch (p_type.type_encoding) {
  532. case MONO_TYPE_BOOLEAN:
  533. return sizeof(MonoBoolean);
  534. case MONO_TYPE_CHAR:
  535. return sizeof(uint16_t);
  536. case MONO_TYPE_I1:
  537. return sizeof(int8_t);
  538. case MONO_TYPE_I2:
  539. return sizeof(int16_t);
  540. case MONO_TYPE_I4:
  541. return sizeof(int32_t);
  542. case MONO_TYPE_I8:
  543. return sizeof(int64_t);
  544. case MONO_TYPE_U1:
  545. return sizeof(uint8_t);
  546. case MONO_TYPE_U2:
  547. return sizeof(uint16_t);
  548. case MONO_TYPE_U4:
  549. return sizeof(uint32_t);
  550. case MONO_TYPE_U8:
  551. return sizeof(uint64_t);
  552. case MONO_TYPE_R4:
  553. return sizeof(float);
  554. case MONO_TYPE_R8:
  555. return sizeof(double);
  556. case MONO_TYPE_VALUETYPE: {
  557. GDMonoClass *vtclass = p_type.type_class;
  558. #define RETURN_CHECK_FOR_STRUCT(m_struct) \
  559. if (vtclass == CACHED_CLASS(m_struct)) { \
  560. return sizeof(M_##m_struct); \
  561. }
  562. RETURN_CHECK_FOR_STRUCT(Vector2);
  563. RETURN_CHECK_FOR_STRUCT(Vector2i);
  564. RETURN_CHECK_FOR_STRUCT(Rect2);
  565. RETURN_CHECK_FOR_STRUCT(Rect2i);
  566. RETURN_CHECK_FOR_STRUCT(Transform2D);
  567. RETURN_CHECK_FOR_STRUCT(Vector3);
  568. RETURN_CHECK_FOR_STRUCT(Vector3i);
  569. RETURN_CHECK_FOR_STRUCT(Basis);
  570. RETURN_CHECK_FOR_STRUCT(Quaternion);
  571. RETURN_CHECK_FOR_STRUCT(Transform3D);
  572. RETURN_CHECK_FOR_STRUCT(AABB);
  573. RETURN_CHECK_FOR_STRUCT(Color);
  574. RETURN_CHECK_FOR_STRUCT(Plane);
  575. RETURN_CHECK_FOR_STRUCT(Callable);
  576. RETURN_CHECK_FOR_STRUCT(SignalInfo);
  577. #undef RETURN_CHECK_FOR_STRUCT
  578. if (mono_class_is_enum(vtclass->get_mono_ptr())) {
  579. MonoType *enum_basetype = mono_class_enum_basetype(vtclass->get_mono_ptr());
  580. switch (mono_type_get_type(enum_basetype)) {
  581. case MONO_TYPE_BOOLEAN:
  582. return sizeof(MonoBoolean);
  583. case MONO_TYPE_CHAR:
  584. return sizeof(uint16_t);
  585. case MONO_TYPE_I1:
  586. return sizeof(int8_t);
  587. case MONO_TYPE_I2:
  588. return sizeof(int16_t);
  589. case MONO_TYPE_I4:
  590. return sizeof(int32_t);
  591. case MONO_TYPE_I8:
  592. return sizeof(int64_t);
  593. case MONO_TYPE_U1:
  594. return sizeof(uint8_t);
  595. case MONO_TYPE_U2:
  596. return sizeof(uint16_t);
  597. case MONO_TYPE_U4:
  598. return sizeof(uint32_t);
  599. case MONO_TYPE_U8:
  600. return sizeof(uint64_t);
  601. default: {
  602. // Enum with unsupported base type. We return nullptr MonoObject* on error.
  603. return zero_for_mono_object;
  604. }
  605. }
  606. }
  607. // Enum with unsupported value type. We return nullptr MonoObject* on error.
  608. } break;
  609. case MONO_TYPE_STRING:
  610. return zero_for_mono_object;
  611. case MONO_TYPE_ARRAY:
  612. case MONO_TYPE_SZARRAY:
  613. case MONO_TYPE_CLASS:
  614. case MONO_TYPE_GENERICINST:
  615. return zero_for_mono_object;
  616. case MONO_TYPE_OBJECT:
  617. return zero_for_mono_object;
  618. }
  619. // Unsupported type encoding. We return nullptr MonoObject* on error.
  620. return zero_for_mono_object;
  621. }
  622. void *variant_to_managed_unboxed(const Variant &p_var, const ManagedType &p_type, void *r_buffer, unsigned int &r_offset) {
  623. #define RETURN_TYPE_VAL(m_type, m_val) \
  624. *reinterpret_cast<m_type *>(r_buffer) = m_val; \
  625. r_offset += sizeof(m_type); \
  626. return r_buffer;
  627. switch (p_type.type_encoding) {
  628. case MONO_TYPE_BOOLEAN:
  629. RETURN_TYPE_VAL(MonoBoolean, (MonoBoolean)p_var.operator bool());
  630. case MONO_TYPE_CHAR:
  631. RETURN_TYPE_VAL(uint16_t, p_var.operator unsigned short());
  632. case MONO_TYPE_I1:
  633. RETURN_TYPE_VAL(int8_t, p_var.operator signed char());
  634. case MONO_TYPE_I2:
  635. RETURN_TYPE_VAL(int16_t, p_var.operator signed short());
  636. case MONO_TYPE_I4:
  637. RETURN_TYPE_VAL(int32_t, p_var.operator signed int());
  638. case MONO_TYPE_I8:
  639. RETURN_TYPE_VAL(int64_t, p_var.operator int64_t());
  640. case MONO_TYPE_U1:
  641. RETURN_TYPE_VAL(uint8_t, p_var.operator unsigned char());
  642. case MONO_TYPE_U2:
  643. RETURN_TYPE_VAL(uint16_t, p_var.operator unsigned short());
  644. case MONO_TYPE_U4:
  645. RETURN_TYPE_VAL(uint32_t, p_var.operator unsigned int());
  646. case MONO_TYPE_U8:
  647. RETURN_TYPE_VAL(uint64_t, p_var.operator uint64_t());
  648. case MONO_TYPE_R4:
  649. RETURN_TYPE_VAL(float, p_var.operator float());
  650. case MONO_TYPE_R8:
  651. RETURN_TYPE_VAL(double, p_var.operator double());
  652. case MONO_TYPE_VALUETYPE: {
  653. GDMonoClass *vtclass = p_type.type_class;
  654. #define RETURN_CHECK_FOR_STRUCT(m_struct) \
  655. if (vtclass == CACHED_CLASS(m_struct)) { \
  656. GDMonoMarshal::M_##m_struct from = MARSHALLED_OUT(m_struct, p_var.operator ::m_struct()); \
  657. RETURN_TYPE_VAL(M_##m_struct, from); \
  658. }
  659. RETURN_CHECK_FOR_STRUCT(Vector2);
  660. RETURN_CHECK_FOR_STRUCT(Vector2i);
  661. RETURN_CHECK_FOR_STRUCT(Rect2);
  662. RETURN_CHECK_FOR_STRUCT(Rect2i);
  663. RETURN_CHECK_FOR_STRUCT(Transform2D);
  664. RETURN_CHECK_FOR_STRUCT(Vector3);
  665. RETURN_CHECK_FOR_STRUCT(Vector3i);
  666. RETURN_CHECK_FOR_STRUCT(Basis);
  667. RETURN_CHECK_FOR_STRUCT(Quaternion);
  668. RETURN_CHECK_FOR_STRUCT(Transform3D);
  669. RETURN_CHECK_FOR_STRUCT(AABB);
  670. RETURN_CHECK_FOR_STRUCT(Color);
  671. RETURN_CHECK_FOR_STRUCT(Plane);
  672. #undef RETURN_CHECK_FOR_STRUCT
  673. if (vtclass == CACHED_CLASS(Callable)) {
  674. GDMonoMarshal::M_Callable from = GDMonoMarshal::callable_to_managed(p_var.operator Callable());
  675. RETURN_TYPE_VAL(M_Callable, from);
  676. }
  677. if (vtclass == CACHED_CLASS(SignalInfo)) {
  678. GDMonoMarshal::M_SignalInfo from = GDMonoMarshal::signal_info_to_managed(p_var.operator Signal());
  679. RETURN_TYPE_VAL(M_SignalInfo, from);
  680. }
  681. if (mono_class_is_enum(vtclass->get_mono_ptr())) {
  682. MonoType *enum_basetype = mono_class_enum_basetype(vtclass->get_mono_ptr());
  683. switch (mono_type_get_type(enum_basetype)) {
  684. case MONO_TYPE_BOOLEAN: {
  685. MonoBoolean val = p_var.operator bool();
  686. RETURN_TYPE_VAL(MonoBoolean, val);
  687. }
  688. case MONO_TYPE_CHAR: {
  689. uint16_t val = p_var.operator unsigned short();
  690. RETURN_TYPE_VAL(uint16_t, val);
  691. }
  692. case MONO_TYPE_I1: {
  693. int8_t val = p_var.operator signed char();
  694. RETURN_TYPE_VAL(int8_t, val);
  695. }
  696. case MONO_TYPE_I2: {
  697. int16_t val = p_var.operator signed short();
  698. RETURN_TYPE_VAL(int16_t, val);
  699. }
  700. case MONO_TYPE_I4: {
  701. int32_t val = p_var.operator signed int();
  702. RETURN_TYPE_VAL(int32_t, val);
  703. }
  704. case MONO_TYPE_I8: {
  705. int64_t val = p_var.operator int64_t();
  706. RETURN_TYPE_VAL(int64_t, val);
  707. }
  708. case MONO_TYPE_U1: {
  709. uint8_t val = p_var.operator unsigned char();
  710. RETURN_TYPE_VAL(uint8_t, val);
  711. }
  712. case MONO_TYPE_U2: {
  713. uint16_t val = p_var.operator unsigned short();
  714. RETURN_TYPE_VAL(uint16_t, val);
  715. }
  716. case MONO_TYPE_U4: {
  717. uint32_t val = p_var.operator unsigned int();
  718. RETURN_TYPE_VAL(uint32_t, val);
  719. }
  720. case MONO_TYPE_U8: {
  721. uint64_t val = p_var.operator uint64_t();
  722. RETURN_TYPE_VAL(uint64_t, val);
  723. }
  724. default: {
  725. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to enum value of unsupported base type: '" + GDMonoClass::get_full_name(mono_class_from_mono_type(enum_basetype)) + "'.");
  726. }
  727. }
  728. }
  729. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to unsupported value type: '" + p_type.type_class->get_full_name() + "'.");
  730. } break;
  731. #undef RETURN_TYPE_VAL
  732. case MONO_TYPE_STRING:
  733. return variant_to_mono_string(p_var);
  734. case MONO_TYPE_ARRAY:
  735. case MONO_TYPE_SZARRAY:
  736. return variant_to_mono_array(p_var, p_type.type_class);
  737. case MONO_TYPE_CLASS:
  738. return variant_to_mono_object_of_class(p_var, p_type.type_class);
  739. case MONO_TYPE_GENERICINST:
  740. return variant_to_mono_object_of_genericinst(p_var, p_type.type_class);
  741. case MONO_TYPE_OBJECT:
  742. return variant_to_mono_object(p_var);
  743. }
  744. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to unsupported type with encoding: " + itos(p_type.type_encoding) + ".");
  745. }
  746. MonoObject *variant_to_mono_object(const Variant &p_var, const ManagedType &p_type) {
  747. switch (p_type.type_encoding) {
  748. case MONO_TYPE_BOOLEAN: {
  749. MonoBoolean val = p_var.operator bool();
  750. return BOX_BOOLEAN(val);
  751. }
  752. case MONO_TYPE_CHAR: {
  753. uint16_t val = p_var.operator unsigned short();
  754. return BOX_UINT16(val);
  755. }
  756. case MONO_TYPE_I1: {
  757. int8_t val = p_var.operator signed char();
  758. return BOX_INT8(val);
  759. }
  760. case MONO_TYPE_I2: {
  761. int16_t val = p_var.operator signed short();
  762. return BOX_INT16(val);
  763. }
  764. case MONO_TYPE_I4: {
  765. int32_t val = p_var.operator signed int();
  766. return BOX_INT32(val);
  767. }
  768. case MONO_TYPE_I8: {
  769. int64_t val = p_var.operator int64_t();
  770. return BOX_INT64(val);
  771. }
  772. case MONO_TYPE_U1: {
  773. uint8_t val = p_var.operator unsigned char();
  774. return BOX_UINT8(val);
  775. }
  776. case MONO_TYPE_U2: {
  777. uint16_t val = p_var.operator unsigned short();
  778. return BOX_UINT16(val);
  779. }
  780. case MONO_TYPE_U4: {
  781. uint32_t val = p_var.operator unsigned int();
  782. return BOX_UINT32(val);
  783. }
  784. case MONO_TYPE_U8: {
  785. uint64_t val = p_var.operator uint64_t();
  786. return BOX_UINT64(val);
  787. }
  788. case MONO_TYPE_R4: {
  789. float val = p_var.operator float();
  790. return BOX_FLOAT(val);
  791. }
  792. case MONO_TYPE_R8: {
  793. double val = p_var.operator double();
  794. return BOX_DOUBLE(val);
  795. }
  796. case MONO_TYPE_VALUETYPE: {
  797. GDMonoClass *vtclass = p_type.type_class;
  798. #define RETURN_CHECK_FOR_STRUCT(m_struct) \
  799. if (vtclass == CACHED_CLASS(m_struct)) { \
  800. GDMonoMarshal::M_##m_struct from = MARSHALLED_OUT(m_struct, p_var.operator ::m_struct()); \
  801. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(m_struct), &from); \
  802. }
  803. RETURN_CHECK_FOR_STRUCT(Vector2);
  804. RETURN_CHECK_FOR_STRUCT(Vector2i);
  805. RETURN_CHECK_FOR_STRUCT(Rect2);
  806. RETURN_CHECK_FOR_STRUCT(Rect2i);
  807. RETURN_CHECK_FOR_STRUCT(Transform2D);
  808. RETURN_CHECK_FOR_STRUCT(Vector3);
  809. RETURN_CHECK_FOR_STRUCT(Vector3i);
  810. RETURN_CHECK_FOR_STRUCT(Basis);
  811. RETURN_CHECK_FOR_STRUCT(Quaternion);
  812. RETURN_CHECK_FOR_STRUCT(Transform3D);
  813. RETURN_CHECK_FOR_STRUCT(AABB);
  814. RETURN_CHECK_FOR_STRUCT(Color);
  815. RETURN_CHECK_FOR_STRUCT(Plane);
  816. #undef RETURN_CHECK_FOR_STRUCT
  817. if (vtclass == CACHED_CLASS(Callable)) {
  818. GDMonoMarshal::M_Callable from = GDMonoMarshal::callable_to_managed(p_var.operator Callable());
  819. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(Callable), &from);
  820. }
  821. if (vtclass == CACHED_CLASS(SignalInfo)) {
  822. GDMonoMarshal::M_SignalInfo from = GDMonoMarshal::signal_info_to_managed(p_var.operator Signal());
  823. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(SignalInfo), &from);
  824. }
  825. if (mono_class_is_enum(vtclass->get_mono_ptr())) {
  826. MonoType *enum_basetype = mono_class_enum_basetype(vtclass->get_mono_ptr());
  827. MonoClass *enum_baseclass = mono_class_from_mono_type(enum_basetype);
  828. switch (mono_type_get_type(enum_basetype)) {
  829. case MONO_TYPE_BOOLEAN: {
  830. MonoBoolean val = p_var.operator bool();
  831. return BOX_ENUM(enum_baseclass, val);
  832. }
  833. case MONO_TYPE_CHAR: {
  834. uint16_t val = p_var.operator unsigned short();
  835. return BOX_ENUM(enum_baseclass, val);
  836. }
  837. case MONO_TYPE_I1: {
  838. int8_t val = p_var.operator signed char();
  839. return BOX_ENUM(enum_baseclass, val);
  840. }
  841. case MONO_TYPE_I2: {
  842. int16_t val = p_var.operator signed short();
  843. return BOX_ENUM(enum_baseclass, val);
  844. }
  845. case MONO_TYPE_I4: {
  846. int32_t val = p_var.operator signed int();
  847. return BOX_ENUM(enum_baseclass, val);
  848. }
  849. case MONO_TYPE_I8: {
  850. int64_t val = p_var.operator int64_t();
  851. return BOX_ENUM(enum_baseclass, val);
  852. }
  853. case MONO_TYPE_U1: {
  854. uint8_t val = p_var.operator unsigned char();
  855. return BOX_ENUM(enum_baseclass, val);
  856. }
  857. case MONO_TYPE_U2: {
  858. uint16_t val = p_var.operator unsigned short();
  859. return BOX_ENUM(enum_baseclass, val);
  860. }
  861. case MONO_TYPE_U4: {
  862. uint32_t val = p_var.operator unsigned int();
  863. return BOX_ENUM(enum_baseclass, val);
  864. }
  865. case MONO_TYPE_U8: {
  866. uint64_t val = p_var.operator uint64_t();
  867. return BOX_ENUM(enum_baseclass, val);
  868. }
  869. default: {
  870. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to enum value of unsupported base type: '" + GDMonoClass::get_full_name(enum_baseclass) + "'.");
  871. }
  872. }
  873. }
  874. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to unsupported value type: '" + p_type.type_class->get_full_name() + "'.");
  875. } break;
  876. case MONO_TYPE_STRING:
  877. return (MonoObject *)variant_to_mono_string(p_var);
  878. case MONO_TYPE_ARRAY:
  879. case MONO_TYPE_SZARRAY:
  880. return (MonoObject *)variant_to_mono_array(p_var, p_type.type_class);
  881. case MONO_TYPE_CLASS:
  882. return variant_to_mono_object_of_class(p_var, p_type.type_class);
  883. case MONO_TYPE_GENERICINST:
  884. return variant_to_mono_object_of_genericinst(p_var, p_type.type_class);
  885. case MONO_TYPE_OBJECT:
  886. return variant_to_mono_object(p_var);
  887. }
  888. ERR_FAIL_V_MSG(nullptr, "Attempted to convert Variant to unsupported type with encoding: " + itos(p_type.type_encoding) + ".");
  889. }
  890. Variant mono_object_to_variant_impl(MonoObject *p_obj, const ManagedType &p_type, bool p_fail_with_err = true) {
  891. ERR_FAIL_COND_V(!p_type.type_class, Variant());
  892. #ifdef DEBUG_ENABLED
  893. CRASH_COND_MSG(p_type.type_encoding == MONO_TYPE_OBJECT, "Type of object should be known.");
  894. #endif
  895. switch (p_type.type_encoding) {
  896. case MONO_TYPE_BOOLEAN:
  897. return (bool)unbox<MonoBoolean>(p_obj);
  898. case MONO_TYPE_CHAR:
  899. return unbox<uint16_t>(p_obj);
  900. case MONO_TYPE_I1:
  901. return unbox<int8_t>(p_obj);
  902. case MONO_TYPE_I2:
  903. return unbox<int16_t>(p_obj);
  904. case MONO_TYPE_I4:
  905. return unbox<int32_t>(p_obj);
  906. case MONO_TYPE_I8:
  907. return unbox<int64_t>(p_obj);
  908. case MONO_TYPE_U1:
  909. return unbox<uint8_t>(p_obj);
  910. case MONO_TYPE_U2:
  911. return unbox<uint16_t>(p_obj);
  912. case MONO_TYPE_U4:
  913. return unbox<uint32_t>(p_obj);
  914. case MONO_TYPE_U8:
  915. return unbox<uint64_t>(p_obj);
  916. case MONO_TYPE_R4:
  917. return unbox<float>(p_obj);
  918. case MONO_TYPE_R8:
  919. return unbox<double>(p_obj);
  920. case MONO_TYPE_VALUETYPE: {
  921. GDMonoClass *vtclass = p_type.type_class;
  922. if (vtclass == CACHED_CLASS(Vector2)) {
  923. return MARSHALLED_IN(Vector2, unbox_addr<GDMonoMarshal::M_Vector2>(p_obj));
  924. }
  925. if (vtclass == CACHED_CLASS(Vector2i)) {
  926. return MARSHALLED_IN(Vector2i, unbox_addr<GDMonoMarshal::M_Vector2i>(p_obj));
  927. }
  928. if (vtclass == CACHED_CLASS(Rect2)) {
  929. return MARSHALLED_IN(Rect2, unbox_addr<GDMonoMarshal::M_Rect2>(p_obj));
  930. }
  931. if (vtclass == CACHED_CLASS(Rect2i)) {
  932. return MARSHALLED_IN(Rect2i, unbox_addr<GDMonoMarshal::M_Rect2i>(p_obj));
  933. }
  934. if (vtclass == CACHED_CLASS(Transform2D)) {
  935. return MARSHALLED_IN(Transform2D, unbox_addr<GDMonoMarshal::M_Transform2D>(p_obj));
  936. }
  937. if (vtclass == CACHED_CLASS(Vector3)) {
  938. return MARSHALLED_IN(Vector3, unbox_addr<GDMonoMarshal::M_Vector3>(p_obj));
  939. }
  940. if (vtclass == CACHED_CLASS(Vector3i)) {
  941. return MARSHALLED_IN(Vector3i, unbox_addr<GDMonoMarshal::M_Vector3i>(p_obj));
  942. }
  943. if (vtclass == CACHED_CLASS(Basis)) {
  944. return MARSHALLED_IN(Basis, unbox_addr<GDMonoMarshal::M_Basis>(p_obj));
  945. }
  946. if (vtclass == CACHED_CLASS(Quaternion)) {
  947. return MARSHALLED_IN(Quaternion, unbox_addr<GDMonoMarshal::M_Quaternion>(p_obj));
  948. }
  949. if (vtclass == CACHED_CLASS(Transform3D)) {
  950. return MARSHALLED_IN(Transform3D, unbox_addr<GDMonoMarshal::M_Transform3D>(p_obj));
  951. }
  952. if (vtclass == CACHED_CLASS(AABB)) {
  953. return MARSHALLED_IN(AABB, unbox_addr<GDMonoMarshal::M_AABB>(p_obj));
  954. }
  955. if (vtclass == CACHED_CLASS(Color)) {
  956. return MARSHALLED_IN(Color, unbox_addr<GDMonoMarshal::M_Color>(p_obj));
  957. }
  958. if (vtclass == CACHED_CLASS(Plane)) {
  959. return MARSHALLED_IN(Plane, unbox_addr<GDMonoMarshal::M_Plane>(p_obj));
  960. }
  961. if (vtclass == CACHED_CLASS(Callable)) {
  962. return managed_to_callable(unbox<GDMonoMarshal::M_Callable>(p_obj));
  963. }
  964. if (vtclass == CACHED_CLASS(SignalInfo)) {
  965. return managed_to_signal_info(unbox<GDMonoMarshal::M_SignalInfo>(p_obj));
  966. }
  967. if (mono_class_is_enum(vtclass->get_mono_ptr())) {
  968. return unbox<int32_t>(p_obj);
  969. }
  970. } break;
  971. case MONO_TYPE_STRING: {
  972. if (p_obj == nullptr) {
  973. return Variant(); // NIL
  974. }
  975. return mono_string_to_godot_not_null((MonoString *)p_obj);
  976. } break;
  977. case MONO_TYPE_ARRAY:
  978. case MONO_TYPE_SZARRAY: {
  979. MonoArrayType *array_type = mono_type_get_array_type(p_type.type_class->get_mono_type());
  980. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject)) {
  981. return mono_array_to_Array((MonoArray *)p_obj);
  982. }
  983. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t)) {
  984. return mono_array_to_PackedByteArray((MonoArray *)p_obj);
  985. }
  986. if (array_type->eklass == CACHED_CLASS_RAW(int32_t)) {
  987. return mono_array_to_PackedInt32Array((MonoArray *)p_obj);
  988. }
  989. if (array_type->eklass == CACHED_CLASS_RAW(int64_t)) {
  990. return mono_array_to_PackedInt64Array((MonoArray *)p_obj);
  991. }
  992. if (array_type->eklass == CACHED_CLASS_RAW(float)) {
  993. return mono_array_to_PackedFloat32Array((MonoArray *)p_obj);
  994. }
  995. if (array_type->eklass == CACHED_CLASS_RAW(double)) {
  996. return mono_array_to_PackedFloat64Array((MonoArray *)p_obj);
  997. }
  998. if (array_type->eklass == CACHED_CLASS_RAW(String)) {
  999. return mono_array_to_PackedStringArray((MonoArray *)p_obj);
  1000. }
  1001. if (array_type->eklass == CACHED_CLASS_RAW(Vector2)) {
  1002. return mono_array_to_PackedVector2Array((MonoArray *)p_obj);
  1003. }
  1004. if (array_type->eklass == CACHED_CLASS_RAW(Vector3)) {
  1005. return mono_array_to_PackedVector3Array((MonoArray *)p_obj);
  1006. }
  1007. if (array_type->eklass == CACHED_CLASS_RAW(Color)) {
  1008. return mono_array_to_PackedColorArray((MonoArray *)p_obj);
  1009. }
  1010. if (array_type->eklass == CACHED_CLASS_RAW(StringName)) {
  1011. return mono_array_to_Array((MonoArray *)p_obj);
  1012. }
  1013. if (array_type->eklass == CACHED_CLASS_RAW(NodePath)) {
  1014. return mono_array_to_Array((MonoArray *)p_obj);
  1015. }
  1016. if (array_type->eklass == CACHED_CLASS_RAW(RID)) {
  1017. return mono_array_to_Array((MonoArray *)p_obj);
  1018. }
  1019. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(array_type->eklass);
  1020. if (CACHED_CLASS(GodotObject)->is_assignable_from(array_type_class)) {
  1021. return mono_array_to_Array((MonoArray *)p_obj);
  1022. }
  1023. if (p_fail_with_err) {
  1024. ERR_FAIL_V_MSG(Variant(), "Attempted to convert a managed array of unmarshallable element type to Variant.");
  1025. } else {
  1026. return Variant();
  1027. }
  1028. } break;
  1029. case MONO_TYPE_CLASS: {
  1030. GDMonoClass *type_class = p_type.type_class;
  1031. // GodotObject
  1032. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  1033. Object *ptr = unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_obj));
  1034. if (ptr != nullptr) {
  1035. RefCounted *rc = Object::cast_to<RefCounted>(ptr);
  1036. return rc ? Variant(Ref<RefCounted>(rc)) : Variant(ptr);
  1037. }
  1038. return Variant();
  1039. }
  1040. if (CACHED_CLASS(StringName) == type_class) {
  1041. StringName *ptr = unbox<StringName *>(CACHED_FIELD(StringName, ptr)->get_value(p_obj));
  1042. return ptr ? Variant(*ptr) : Variant();
  1043. }
  1044. if (CACHED_CLASS(NodePath) == type_class) {
  1045. NodePath *ptr = unbox<NodePath *>(CACHED_FIELD(NodePath, ptr)->get_value(p_obj));
  1046. return ptr ? Variant(*ptr) : Variant();
  1047. }
  1048. if (CACHED_CLASS(RID) == type_class) {
  1049. RID *ptr = unbox<RID *>(CACHED_FIELD(RID, ptr)->get_value(p_obj));
  1050. return ptr ? Variant(*ptr) : Variant();
  1051. }
  1052. // Godot.Collections.Dictionary
  1053. if (CACHED_CLASS(Dictionary) == type_class) {
  1054. MonoException *exc = nullptr;
  1055. Dictionary *ptr = CACHED_METHOD_THUNK(Dictionary, GetPtr).invoke(p_obj, &exc);
  1056. UNHANDLED_EXCEPTION(exc);
  1057. return ptr ? Variant(*ptr) : Variant();
  1058. }
  1059. // Godot.Collections.Array
  1060. if (CACHED_CLASS(Array) == type_class) {
  1061. MonoException *exc = nullptr;
  1062. Array *ptr = CACHED_METHOD_THUNK(Array, GetPtr).invoke(p_obj, &exc);
  1063. UNHANDLED_EXCEPTION(exc);
  1064. return ptr ? Variant(*ptr) : Variant();
  1065. }
  1066. } break;
  1067. case MONO_TYPE_GENERICINST: {
  1068. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), p_type.type_class->get_mono_type());
  1069. // Godot.Collections.Dictionary<TKey, TValue>
  1070. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  1071. MonoException *exc = nullptr;
  1072. MonoObject *ret = p_type.type_class->get_method("GetPtr")->invoke(p_obj, &exc);
  1073. UNHANDLED_EXCEPTION(exc);
  1074. return *unbox<Dictionary *>(ret);
  1075. }
  1076. // Godot.Collections.Array<T>
  1077. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  1078. MonoException *exc = nullptr;
  1079. MonoObject *ret = p_type.type_class->get_method("GetPtr")->invoke(p_obj, &exc);
  1080. UNHANDLED_EXCEPTION(exc);
  1081. return *unbox<Array *>(ret);
  1082. }
  1083. // System.Collections.Generic.Dictionary<TKey, TValue>
  1084. if (GDMonoUtils::Marshal::type_is_system_generic_dictionary(reftype)) {
  1085. MonoReflectionType *key_reftype = nullptr;
  1086. MonoReflectionType *value_reftype = nullptr;
  1087. GDMonoUtils::Marshal::dictionary_get_key_value_types(reftype, &key_reftype, &value_reftype);
  1088. return system_generic_dict_to_Dictionary(p_obj, p_type.type_class, key_reftype, value_reftype);
  1089. }
  1090. // System.Collections.Generic.List<T>
  1091. if (GDMonoUtils::Marshal::type_is_system_generic_list(reftype)) {
  1092. MonoReflectionType *elem_reftype = nullptr;
  1093. GDMonoUtils::Marshal::array_get_element_type(reftype, &elem_reftype);
  1094. return system_generic_list_to_Array_variant(p_obj, p_type.type_class, elem_reftype);
  1095. }
  1096. // GodotObject
  1097. GDMonoClass *type_class = p_type.type_class;
  1098. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  1099. Object *ptr = unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_obj));
  1100. if (ptr != nullptr) {
  1101. RefCounted *rc = Object::cast_to<RefCounted>(ptr);
  1102. return rc ? Variant(Ref<RefCounted>(rc)) : Variant(ptr);
  1103. }
  1104. return Variant();
  1105. }
  1106. } break;
  1107. }
  1108. if (p_fail_with_err) {
  1109. ERR_FAIL_V_MSG(Variant(), "Attempted to convert an unmarshallable managed type to Variant. Name: '" + p_type.type_class->get_name() + "' Encoding: " + itos(p_type.type_encoding) + ".");
  1110. } else {
  1111. return Variant();
  1112. }
  1113. }
  1114. Variant mono_object_to_variant(MonoObject *p_obj) {
  1115. if (!p_obj) {
  1116. return Variant();
  1117. }
  1118. ManagedType type = ManagedType::from_class(mono_object_get_class(p_obj));
  1119. return mono_object_to_variant_impl(p_obj, type);
  1120. }
  1121. Variant mono_object_to_variant(MonoObject *p_obj, const ManagedType &p_type) {
  1122. if (!p_obj) {
  1123. return Variant();
  1124. }
  1125. return mono_object_to_variant_impl(p_obj, p_type);
  1126. }
  1127. Variant mono_object_to_variant_no_err(MonoObject *p_obj, const ManagedType &p_type) {
  1128. if (!p_obj) {
  1129. return Variant();
  1130. }
  1131. return mono_object_to_variant_impl(p_obj, p_type, /* fail_with_err: */ false);
  1132. }
  1133. String mono_object_to_variant_string(MonoObject *p_obj, MonoException **r_exc) {
  1134. if (p_obj == nullptr) {
  1135. return String("null");
  1136. }
  1137. ManagedType type = ManagedType::from_class(mono_object_get_class(p_obj));
  1138. Variant var = GDMonoMarshal::mono_object_to_variant_no_err(p_obj, type);
  1139. if (var.get_type() == Variant::NIL) { // `&& p_obj != nullptr` but omitted because always true
  1140. // Cannot convert MonoObject* to Variant; fallback to 'ToString()'.
  1141. MonoException *exc = nullptr;
  1142. MonoString *mono_str = GDMonoUtils::object_to_string(p_obj, &exc);
  1143. if (exc) {
  1144. if (r_exc) {
  1145. *r_exc = exc;
  1146. }
  1147. return String();
  1148. }
  1149. return GDMonoMarshal::mono_string_to_godot(mono_str);
  1150. } else {
  1151. return var.operator String();
  1152. }
  1153. }
  1154. MonoObject *Dictionary_to_system_generic_dict(const Dictionary &p_dict, GDMonoClass *p_class, MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype) {
  1155. String ctor_desc = ":.ctor(System.Collections.Generic.IDictionary`2<" + GDMonoUtils::get_type_desc(p_key_reftype) +
  1156. ", " + GDMonoUtils::get_type_desc(p_value_reftype) + ">)";
  1157. GDMonoMethod *ctor = p_class->get_method_with_desc(ctor_desc, true);
  1158. CRASH_COND(ctor == nullptr);
  1159. MonoObject *mono_object = mono_object_new(mono_domain_get(), p_class->get_mono_ptr());
  1160. ERR_FAIL_NULL_V(mono_object, nullptr);
  1161. GDMonoClass *godot_dict_class = GDMonoUtils::Marshal::make_generic_dictionary_type(p_key_reftype, p_value_reftype);
  1162. MonoObject *godot_dict = GDMonoUtils::create_managed_from(p_dict, godot_dict_class);
  1163. void *ctor_args[1] = { godot_dict };
  1164. MonoException *exc = nullptr;
  1165. ctor->invoke_raw(mono_object, ctor_args, &exc);
  1166. UNHANDLED_EXCEPTION(exc);
  1167. return mono_object;
  1168. }
  1169. Dictionary system_generic_dict_to_Dictionary(MonoObject *p_obj, [[maybe_unused]] GDMonoClass *p_class, MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype) {
  1170. GDMonoClass *godot_dict_class = GDMonoUtils::Marshal::make_generic_dictionary_type(p_key_reftype, p_value_reftype);
  1171. String ctor_desc = ":.ctor(System.Collections.Generic.IDictionary`2<" + GDMonoUtils::get_type_desc(p_key_reftype) +
  1172. ", " + GDMonoUtils::get_type_desc(p_value_reftype) + ">)";
  1173. GDMonoMethod *godot_dict_ctor = godot_dict_class->get_method_with_desc(ctor_desc, true);
  1174. CRASH_COND(godot_dict_ctor == nullptr);
  1175. MonoObject *godot_dict = mono_object_new(mono_domain_get(), godot_dict_class->get_mono_ptr());
  1176. ERR_FAIL_NULL_V(godot_dict, Dictionary());
  1177. void *ctor_args[1] = { p_obj };
  1178. MonoException *exc = nullptr;
  1179. godot_dict_ctor->invoke_raw(godot_dict, ctor_args, &exc);
  1180. UNHANDLED_EXCEPTION(exc);
  1181. exc = nullptr;
  1182. MonoObject *ret = godot_dict_class->get_method("GetPtr")->invoke(godot_dict, &exc);
  1183. UNHANDLED_EXCEPTION(exc);
  1184. return *unbox<Dictionary *>(ret);
  1185. }
  1186. MonoObject *Array_to_system_generic_list(const Array &p_array, GDMonoClass *p_class, MonoReflectionType *p_elem_reftype) {
  1187. MonoType *elem_type = mono_reflection_type_get_type(p_elem_reftype);
  1188. String ctor_desc = ":.ctor(System.Collections.Generic.IEnumerable`1<" + GDMonoUtils::get_type_desc(elem_type) + ">)";
  1189. GDMonoMethod *ctor = p_class->get_method_with_desc(ctor_desc, true);
  1190. CRASH_COND(ctor == nullptr);
  1191. MonoObject *mono_object = mono_object_new(mono_domain_get(), p_class->get_mono_ptr());
  1192. ERR_FAIL_NULL_V(mono_object, nullptr);
  1193. GDMonoClass *godot_array_class = GDMonoUtils::Marshal::make_generic_array_type(p_elem_reftype);
  1194. MonoObject *godot_array = GDMonoUtils::create_managed_from(p_array, godot_array_class);
  1195. void *ctor_args[1] = { godot_array };
  1196. MonoException *exc = nullptr;
  1197. ctor->invoke_raw(mono_object, ctor_args, &exc);
  1198. UNHANDLED_EXCEPTION(exc);
  1199. return mono_object;
  1200. }
  1201. Variant system_generic_list_to_Array_variant(MonoObject *p_obj, GDMonoClass *p_class, [[maybe_unused]] MonoReflectionType *p_elem_reftype) {
  1202. GDMonoMethod *to_array = p_class->get_method("ToArray", 0);
  1203. CRASH_COND(to_array == nullptr);
  1204. MonoException *exc = nullptr;
  1205. MonoObject *array = to_array->invoke_raw(p_obj, nullptr, &exc);
  1206. UNHANDLED_EXCEPTION(exc);
  1207. ERR_FAIL_NULL_V(array, Variant());
  1208. ManagedType type = ManagedType::from_class(mono_object_get_class(array));
  1209. bool result_is_array = type.type_encoding != MONO_TYPE_SZARRAY && type.type_encoding != MONO_TYPE_ARRAY;
  1210. ERR_FAIL_COND_V(result_is_array, Variant());
  1211. return mono_object_to_variant(array, type);
  1212. }
  1213. MonoArray *Array_to_mono_array(const Array &p_array) {
  1214. int length = p_array.size();
  1215. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(MonoObject), length);
  1216. for (int i = 0; i < length; i++) {
  1217. MonoObject *boxed = variant_to_mono_object(p_array[i]);
  1218. mono_array_setref(ret, i, boxed);
  1219. }
  1220. return ret;
  1221. }
  1222. MonoArray *Array_to_mono_array(const Array &p_array, MonoClass *p_array_type_class) {
  1223. int length = p_array.size();
  1224. MonoArray *ret = mono_array_new(mono_domain_get(), p_array_type_class, length);
  1225. for (int i = 0; i < length; i++) {
  1226. MonoObject *boxed = variant_to_mono_object(p_array[i]);
  1227. mono_array_setref(ret, i, boxed);
  1228. }
  1229. return ret;
  1230. }
  1231. Array mono_array_to_Array(MonoArray *p_array) {
  1232. Array ret;
  1233. if (!p_array) {
  1234. return ret;
  1235. }
  1236. int length = mono_array_length(p_array);
  1237. ret.resize(length);
  1238. for (int i = 0; i < length; i++) {
  1239. MonoObject *elem = mono_array_get(p_array, MonoObject *, i);
  1240. ret[i] = mono_object_to_variant(elem);
  1241. }
  1242. return ret;
  1243. }
  1244. MonoArray *PackedInt32Array_to_mono_array(const PackedInt32Array &p_array) {
  1245. const int32_t *src = p_array.ptr();
  1246. int length = p_array.size();
  1247. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(int32_t), length);
  1248. int32_t *dst = mono_array_addr(ret, int32_t, 0);
  1249. memcpy(dst, src, length * sizeof(int32_t));
  1250. return ret;
  1251. }
  1252. PackedInt32Array mono_array_to_PackedInt32Array(MonoArray *p_array) {
  1253. PackedInt32Array ret;
  1254. if (!p_array) {
  1255. return ret;
  1256. }
  1257. int length = mono_array_length(p_array);
  1258. ret.resize(length);
  1259. int32_t *dst = ret.ptrw();
  1260. const int32_t *src = mono_array_addr(p_array, int32_t, 0);
  1261. memcpy(dst, src, length * sizeof(int32_t));
  1262. return ret;
  1263. }
  1264. MonoArray *PackedInt64Array_to_mono_array(const PackedInt64Array &p_array) {
  1265. const int64_t *src = p_array.ptr();
  1266. int length = p_array.size();
  1267. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(int64_t), length);
  1268. int64_t *dst = mono_array_addr(ret, int64_t, 0);
  1269. memcpy(dst, src, length * sizeof(int64_t));
  1270. return ret;
  1271. }
  1272. PackedInt64Array mono_array_to_PackedInt64Array(MonoArray *p_array) {
  1273. PackedInt64Array ret;
  1274. if (!p_array) {
  1275. return ret;
  1276. }
  1277. int length = mono_array_length(p_array);
  1278. ret.resize(length);
  1279. int64_t *dst = ret.ptrw();
  1280. const int64_t *src = mono_array_addr(p_array, int64_t, 0);
  1281. memcpy(dst, src, length * sizeof(int64_t));
  1282. return ret;
  1283. }
  1284. MonoArray *PackedByteArray_to_mono_array(const PackedByteArray &p_array) {
  1285. const uint8_t *src = p_array.ptr();
  1286. int length = p_array.size();
  1287. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(uint8_t), length);
  1288. uint8_t *dst = mono_array_addr(ret, uint8_t, 0);
  1289. memcpy(dst, src, length * sizeof(uint8_t));
  1290. return ret;
  1291. }
  1292. PackedByteArray mono_array_to_PackedByteArray(MonoArray *p_array) {
  1293. PackedByteArray ret;
  1294. if (!p_array) {
  1295. return ret;
  1296. }
  1297. int length = mono_array_length(p_array);
  1298. ret.resize(length);
  1299. uint8_t *dst = ret.ptrw();
  1300. const uint8_t *src = mono_array_addr(p_array, uint8_t, 0);
  1301. memcpy(dst, src, length * sizeof(uint8_t));
  1302. return ret;
  1303. }
  1304. MonoArray *PackedFloat32Array_to_mono_array(const PackedFloat32Array &p_array) {
  1305. const float *src = p_array.ptr();
  1306. int length = p_array.size();
  1307. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(float), length);
  1308. float *dst = mono_array_addr(ret, float, 0);
  1309. memcpy(dst, src, length * sizeof(float));
  1310. return ret;
  1311. }
  1312. PackedFloat32Array mono_array_to_PackedFloat32Array(MonoArray *p_array) {
  1313. PackedFloat32Array ret;
  1314. if (!p_array) {
  1315. return ret;
  1316. }
  1317. int length = mono_array_length(p_array);
  1318. ret.resize(length);
  1319. float *dst = ret.ptrw();
  1320. const float *src = mono_array_addr(p_array, float, 0);
  1321. memcpy(dst, src, length * sizeof(float));
  1322. return ret;
  1323. }
  1324. MonoArray *PackedFloat64Array_to_mono_array(const PackedFloat64Array &p_array) {
  1325. const double *src = p_array.ptr();
  1326. int length = p_array.size();
  1327. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(double), length);
  1328. double *dst = mono_array_addr(ret, double, 0);
  1329. memcpy(dst, src, length * sizeof(double));
  1330. return ret;
  1331. }
  1332. PackedFloat64Array mono_array_to_PackedFloat64Array(MonoArray *p_array) {
  1333. PackedFloat64Array ret;
  1334. if (!p_array) {
  1335. return ret;
  1336. }
  1337. int length = mono_array_length(p_array);
  1338. ret.resize(length);
  1339. double *dst = ret.ptrw();
  1340. const double *src = mono_array_addr(p_array, double, 0);
  1341. memcpy(dst, src, length * sizeof(double));
  1342. return ret;
  1343. }
  1344. MonoArray *PackedStringArray_to_mono_array(const PackedStringArray &p_array) {
  1345. const String *r = p_array.ptr();
  1346. int length = p_array.size();
  1347. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(String), length);
  1348. for (int i = 0; i < length; i++) {
  1349. MonoString *boxed = mono_string_from_godot(r[i]);
  1350. mono_array_setref(ret, i, boxed);
  1351. }
  1352. return ret;
  1353. }
  1354. PackedStringArray mono_array_to_PackedStringArray(MonoArray *p_array) {
  1355. PackedStringArray ret;
  1356. if (!p_array) {
  1357. return ret;
  1358. }
  1359. int length = mono_array_length(p_array);
  1360. ret.resize(length);
  1361. String *w = ret.ptrw();
  1362. for (int i = 0; i < length; i++) {
  1363. MonoString *elem = mono_array_get(p_array, MonoString *, i);
  1364. w[i] = mono_string_to_godot(elem);
  1365. }
  1366. return ret;
  1367. }
  1368. MonoArray *PackedColorArray_to_mono_array(const PackedColorArray &p_array) {
  1369. const Color *src = p_array.ptr();
  1370. int length = p_array.size();
  1371. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Color), length);
  1372. if constexpr (InteropLayout::MATCHES_Color) {
  1373. Color *dst = mono_array_addr(ret, Color, 0);
  1374. memcpy(dst, src, length * sizeof(Color));
  1375. } else {
  1376. for (int i = 0; i < length; i++) {
  1377. M_Color *raw = (M_Color *)mono_array_addr_with_size(ret, sizeof(M_Color), i);
  1378. *raw = MARSHALLED_OUT(Color, src[i]);
  1379. }
  1380. }
  1381. return ret;
  1382. }
  1383. PackedColorArray mono_array_to_PackedColorArray(MonoArray *p_array) {
  1384. PackedColorArray ret;
  1385. if (!p_array) {
  1386. return ret;
  1387. }
  1388. int length = mono_array_length(p_array);
  1389. ret.resize(length);
  1390. Color *dst = ret.ptrw();
  1391. if constexpr (InteropLayout::MATCHES_Color) {
  1392. const Color *src = mono_array_addr(p_array, Color, 0);
  1393. memcpy(dst, src, length * sizeof(Color));
  1394. } else {
  1395. for (int i = 0; i < length; i++) {
  1396. dst[i] = MARSHALLED_IN(Color, (M_Color *)mono_array_addr_with_size(p_array, sizeof(M_Color), i));
  1397. }
  1398. }
  1399. return ret;
  1400. }
  1401. MonoArray *PackedVector2Array_to_mono_array(const PackedVector2Array &p_array) {
  1402. const Vector2 *src = p_array.ptr();
  1403. int length = p_array.size();
  1404. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Vector2), length);
  1405. if constexpr (InteropLayout::MATCHES_Vector2) {
  1406. Vector2 *dst = mono_array_addr(ret, Vector2, 0);
  1407. memcpy(dst, src, length * sizeof(Vector2));
  1408. } else {
  1409. for (int i = 0; i < length; i++) {
  1410. M_Vector2 *raw = (M_Vector2 *)mono_array_addr_with_size(ret, sizeof(M_Vector2), i);
  1411. *raw = MARSHALLED_OUT(Vector2, src[i]);
  1412. }
  1413. }
  1414. return ret;
  1415. }
  1416. PackedVector2Array mono_array_to_PackedVector2Array(MonoArray *p_array) {
  1417. PackedVector2Array ret;
  1418. if (!p_array) {
  1419. return ret;
  1420. }
  1421. int length = mono_array_length(p_array);
  1422. ret.resize(length);
  1423. Vector2 *dst = ret.ptrw();
  1424. if constexpr (InteropLayout::MATCHES_Vector2) {
  1425. const Vector2 *src = mono_array_addr(p_array, Vector2, 0);
  1426. memcpy(dst, src, length * sizeof(Vector2));
  1427. } else {
  1428. for (int i = 0; i < length; i++) {
  1429. dst[i] = MARSHALLED_IN(Vector2, (M_Vector2 *)mono_array_addr_with_size(p_array, sizeof(M_Vector2), i));
  1430. }
  1431. }
  1432. return ret;
  1433. }
  1434. MonoArray *PackedVector3Array_to_mono_array(const PackedVector3Array &p_array) {
  1435. const Vector3 *src = p_array.ptr();
  1436. int length = p_array.size();
  1437. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Vector3), length);
  1438. if constexpr (InteropLayout::MATCHES_Vector3) {
  1439. Vector3 *dst = mono_array_addr(ret, Vector3, 0);
  1440. memcpy(dst, src, length * sizeof(Vector3));
  1441. } else {
  1442. for (int i = 0; i < length; i++) {
  1443. M_Vector3 *raw = (M_Vector3 *)mono_array_addr_with_size(ret, sizeof(M_Vector3), i);
  1444. *raw = MARSHALLED_OUT(Vector3, src[i]);
  1445. }
  1446. }
  1447. return ret;
  1448. }
  1449. PackedVector3Array mono_array_to_PackedVector3Array(MonoArray *p_array) {
  1450. PackedVector3Array ret;
  1451. if (!p_array) {
  1452. return ret;
  1453. }
  1454. int length = mono_array_length(p_array);
  1455. ret.resize(length);
  1456. Vector3 *dst = ret.ptrw();
  1457. if constexpr (InteropLayout::MATCHES_Vector3) {
  1458. const Vector3 *src = mono_array_addr(p_array, Vector3, 0);
  1459. memcpy(dst, src, length * sizeof(Vector3));
  1460. } else {
  1461. for (int i = 0; i < length; i++) {
  1462. dst[i] = MARSHALLED_IN(Vector3, (M_Vector3 *)mono_array_addr_with_size(p_array, sizeof(M_Vector3), i));
  1463. }
  1464. }
  1465. return ret;
  1466. }
  1467. Callable managed_to_callable(const M_Callable &p_managed_callable) {
  1468. if (p_managed_callable.delegate) {
  1469. // TODO: Use pooling for ManagedCallable instances.
  1470. CallableCustom *managed_callable = memnew(ManagedCallable(p_managed_callable.delegate));
  1471. return Callable(managed_callable);
  1472. } else {
  1473. Object *target = p_managed_callable.target
  1474. ? unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_managed_callable.target))
  1475. : nullptr;
  1476. StringName *method_ptr = p_managed_callable.method_string_name
  1477. ? unbox<StringName *>(CACHED_FIELD(StringName, ptr)->get_value(p_managed_callable.method_string_name))
  1478. : nullptr;
  1479. StringName method = method_ptr ? *method_ptr : StringName();
  1480. return Callable(target, method);
  1481. }
  1482. }
  1483. M_Callable callable_to_managed(const Callable &p_callable) {
  1484. if (p_callable.is_custom()) {
  1485. CallableCustom *custom = p_callable.get_custom();
  1486. CallableCustom::CompareEqualFunc compare_equal_func = custom->get_compare_equal_func();
  1487. if (compare_equal_func == ManagedCallable::compare_equal_func_ptr) {
  1488. ManagedCallable *managed_callable = static_cast<ManagedCallable *>(custom);
  1489. return {
  1490. nullptr, nullptr,
  1491. managed_callable->get_delegate()
  1492. };
  1493. } else if (compare_equal_func == SignalAwaiterCallable::compare_equal_func_ptr) {
  1494. SignalAwaiterCallable *signal_awaiter_callable = static_cast<SignalAwaiterCallable *>(custom);
  1495. return {
  1496. GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(signal_awaiter_callable->get_object())),
  1497. GDMonoUtils::create_managed_from(signal_awaiter_callable->get_signal()),
  1498. nullptr
  1499. };
  1500. } else if (compare_equal_func == EventSignalCallable::compare_equal_func_ptr) {
  1501. EventSignalCallable *event_signal_callable = static_cast<EventSignalCallable *>(custom);
  1502. return {
  1503. GDMonoUtils::unmanaged_get_managed(ObjectDB::get_instance(event_signal_callable->get_object())),
  1504. GDMonoUtils::create_managed_from(event_signal_callable->get_signal()),
  1505. nullptr
  1506. };
  1507. }
  1508. // Some other CallableCustom. We only support ManagedCallable.
  1509. return { nullptr, nullptr, nullptr };
  1510. } else {
  1511. MonoObject *target_managed = GDMonoUtils::unmanaged_get_managed(p_callable.get_object());
  1512. MonoObject *method_string_name_managed = GDMonoUtils::create_managed_from(p_callable.get_method());
  1513. return { target_managed, method_string_name_managed, nullptr };
  1514. }
  1515. }
  1516. Signal managed_to_signal_info(const M_SignalInfo &p_managed_signal) {
  1517. Object *owner = p_managed_signal.owner
  1518. ? unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_managed_signal.owner))
  1519. : nullptr;
  1520. StringName *name_ptr = p_managed_signal.name_string_name
  1521. ? unbox<StringName *>(CACHED_FIELD(StringName, ptr)->get_value(p_managed_signal.name_string_name))
  1522. : nullptr;
  1523. StringName name = name_ptr ? *name_ptr : StringName();
  1524. return Signal(owner, name);
  1525. }
  1526. M_SignalInfo signal_info_to_managed(const Signal &p_signal) {
  1527. Object *owner = p_signal.get_object();
  1528. MonoObject *owner_managed = GDMonoUtils::unmanaged_get_managed(owner);
  1529. MonoObject *name_string_name_managed = GDMonoUtils::create_managed_from(p_signal.get_name());
  1530. return { owner_managed, name_string_name_managed };
  1531. }
  1532. } // namespace GDMonoMarshal