gd_mono_marshal.cpp 59 KB

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