gd_mono_marshal.cpp 57 KB

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