gd_mono_marshal.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_mono_marshal.h"
  31. #include "gd_mono.h"
  32. #include "gd_mono_class.h"
  33. namespace GDMonoMarshal {
  34. #define RETURN_BOXED_STRUCT(m_t, m_var_in) \
  35. { \
  36. const m_t &m_in = m_var_in->operator ::m_t(); \
  37. MARSHALLED_OUT(m_t, m_in, raw); \
  38. return mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(m_t), raw); \
  39. }
  40. #define RETURN_UNBOXED_STRUCT(m_t, m_var_in) \
  41. { \
  42. float *raw = (float *)mono_object_unbox(m_var_in); \
  43. MARSHALLED_IN(m_t, raw, ret); \
  44. return ret; \
  45. }
  46. Variant::Type managed_to_variant_type(const ManagedType &p_type) {
  47. switch (p_type.type_encoding) {
  48. case MONO_TYPE_BOOLEAN:
  49. return Variant::BOOL;
  50. case MONO_TYPE_I1:
  51. return Variant::INT;
  52. case MONO_TYPE_I2:
  53. return Variant::INT;
  54. case MONO_TYPE_I4:
  55. return Variant::INT;
  56. case MONO_TYPE_I8:
  57. return Variant::INT;
  58. case MONO_TYPE_U1:
  59. return Variant::INT;
  60. case MONO_TYPE_U2:
  61. return Variant::INT;
  62. case MONO_TYPE_U4:
  63. return Variant::INT;
  64. case MONO_TYPE_U8:
  65. return Variant::INT;
  66. case MONO_TYPE_R4:
  67. return Variant::REAL;
  68. case MONO_TYPE_R8:
  69. return Variant::REAL;
  70. case MONO_TYPE_STRING: {
  71. return Variant::STRING;
  72. } break;
  73. case MONO_TYPE_VALUETYPE: {
  74. GDMonoClass *tclass = p_type.type_class;
  75. if (tclass == CACHED_CLASS(Vector2))
  76. return Variant::VECTOR2;
  77. if (tclass == CACHED_CLASS(Rect2))
  78. return Variant::RECT2;
  79. if (tclass == CACHED_CLASS(Transform2D))
  80. return Variant::TRANSFORM2D;
  81. if (tclass == CACHED_CLASS(Vector3))
  82. return Variant::VECTOR3;
  83. if (tclass == CACHED_CLASS(Basis))
  84. return Variant::BASIS;
  85. if (tclass == CACHED_CLASS(Quat))
  86. return Variant::QUAT;
  87. if (tclass == CACHED_CLASS(Transform))
  88. return Variant::TRANSFORM;
  89. if (tclass == CACHED_CLASS(AABB))
  90. return Variant::AABB;
  91. if (tclass == CACHED_CLASS(Color))
  92. return Variant::COLOR;
  93. if (tclass == CACHED_CLASS(Plane))
  94. return Variant::PLANE;
  95. if (mono_class_is_enum(tclass->get_mono_ptr()))
  96. return Variant::INT;
  97. } break;
  98. case MONO_TYPE_ARRAY:
  99. case MONO_TYPE_SZARRAY: {
  100. MonoArrayType *array_type = mono_type_get_array_type(p_type.type_class->get_mono_type());
  101. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  102. return Variant::ARRAY;
  103. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  104. return Variant::POOL_BYTE_ARRAY;
  105. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  106. return Variant::POOL_INT_ARRAY;
  107. if (array_type->eklass == REAL_T_MONOCLASS)
  108. return Variant::POOL_REAL_ARRAY;
  109. if (array_type->eklass == CACHED_CLASS_RAW(String))
  110. return Variant::POOL_STRING_ARRAY;
  111. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  112. return Variant::POOL_VECTOR2_ARRAY;
  113. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  114. return Variant::POOL_VECTOR3_ARRAY;
  115. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  116. return Variant::POOL_COLOR_ARRAY;
  117. } break;
  118. case MONO_TYPE_CLASS: {
  119. GDMonoClass *type_class = p_type.type_class;
  120. // GodotObject
  121. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  122. return Variant::OBJECT;
  123. }
  124. if (CACHED_CLASS(NodePath) == type_class) {
  125. return Variant::NODE_PATH;
  126. }
  127. if (CACHED_CLASS(RID) == type_class) {
  128. return Variant::_RID;
  129. }
  130. if (CACHED_CLASS(Dictionary) == type_class) {
  131. return Variant::DICTIONARY;
  132. }
  133. if (CACHED_CLASS(Array) == type_class) {
  134. return Variant::ARRAY;
  135. }
  136. } break;
  137. case MONO_TYPE_GENERICINST: {
  138. MonoReflectionType *reftype = mono_type_get_object(SCRIPTS_DOMAIN, p_type.type_class->get_mono_type());
  139. MonoException *exc = NULL;
  140. GDMonoUtils::IsDictionaryGenericType type_is_dict = CACHED_METHOD_THUNK(MarshalUtils, IsDictionaryGenericType);
  141. MonoBoolean is_dict = type_is_dict((MonoObject *)reftype, (MonoObject **)&exc);
  142. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  143. if (is_dict) {
  144. return Variant::DICTIONARY;
  145. }
  146. exc = NULL;
  147. GDMonoUtils::IsArrayGenericType type_is_array = CACHED_METHOD_THUNK(MarshalUtils, IsArrayGenericType);
  148. MonoBoolean is_array = type_is_array((MonoObject *)reftype, (MonoObject **)&exc);
  149. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  150. if (is_array) {
  151. return Variant::ARRAY;
  152. }
  153. } break;
  154. default: {
  155. } break;
  156. }
  157. // Unknown
  158. return Variant::NIL;
  159. }
  160. String mono_to_utf8_string(MonoString *p_mono_string) {
  161. MonoError error;
  162. char *utf8 = mono_string_to_utf8_checked(p_mono_string, &error);
  163. ERR_EXPLAIN("Conversion of MonoString to UTF8 failed.");
  164. ERR_FAIL_COND_V(!mono_error_ok(&error), String());
  165. String ret = String::utf8(utf8);
  166. mono_free(utf8);
  167. return ret;
  168. }
  169. String mono_to_utf16_string(MonoString *p_mono_string) {
  170. int len = mono_string_length(p_mono_string);
  171. String ret;
  172. if (len == 0)
  173. return ret;
  174. ret.resize(len + 1);
  175. ret.set(len, 0);
  176. CharType *src = (CharType *)mono_string_chars(p_mono_string);
  177. CharType *dst = &(ret.operator[](0));
  178. for (int i = 0; i < len; i++) {
  179. dst[i] = src[i];
  180. }
  181. return ret;
  182. }
  183. MonoObject *variant_to_mono_object(const Variant *p_var) {
  184. ManagedType type;
  185. type.type_encoding = MONO_TYPE_OBJECT;
  186. // type.type_class is not needed when we specify the MONO_TYPE_OBJECT encoding
  187. return variant_to_mono_object(p_var, type);
  188. }
  189. MonoObject *variant_to_mono_object(const Variant *p_var, const ManagedType &p_type) {
  190. switch (p_type.type_encoding) {
  191. case MONO_TYPE_BOOLEAN: {
  192. MonoBoolean val = p_var->operator bool();
  193. return BOX_BOOLEAN(val);
  194. }
  195. case MONO_TYPE_I1: {
  196. char val = p_var->operator signed char();
  197. return BOX_INT8(val);
  198. }
  199. case MONO_TYPE_I2: {
  200. short val = p_var->operator signed short();
  201. return BOX_INT16(val);
  202. }
  203. case MONO_TYPE_I4: {
  204. int val = p_var->operator signed int();
  205. return BOX_INT32(val);
  206. }
  207. case MONO_TYPE_I8: {
  208. int64_t val = p_var->operator int64_t();
  209. return BOX_INT64(val);
  210. }
  211. case MONO_TYPE_U1: {
  212. char val = p_var->operator unsigned char();
  213. return BOX_UINT8(val);
  214. }
  215. case MONO_TYPE_U2: {
  216. short val = p_var->operator unsigned short();
  217. return BOX_UINT16(val);
  218. }
  219. case MONO_TYPE_U4: {
  220. int val = p_var->operator unsigned int();
  221. return BOX_UINT32(val);
  222. }
  223. case MONO_TYPE_U8: {
  224. uint64_t val = p_var->operator uint64_t();
  225. return BOX_UINT64(val);
  226. }
  227. case MONO_TYPE_R4: {
  228. float val = p_var->operator float();
  229. return BOX_FLOAT(val);
  230. }
  231. case MONO_TYPE_R8: {
  232. double val = p_var->operator double();
  233. return BOX_DOUBLE(val);
  234. }
  235. case MONO_TYPE_STRING: {
  236. return (MonoObject *)mono_string_from_godot(p_var->operator String());
  237. } break;
  238. case MONO_TYPE_VALUETYPE: {
  239. GDMonoClass *tclass = p_type.type_class;
  240. if (tclass == CACHED_CLASS(Vector2))
  241. RETURN_BOXED_STRUCT(Vector2, p_var);
  242. if (tclass == CACHED_CLASS(Rect2))
  243. RETURN_BOXED_STRUCT(Rect2, p_var);
  244. if (tclass == CACHED_CLASS(Transform2D))
  245. RETURN_BOXED_STRUCT(Transform2D, p_var);
  246. if (tclass == CACHED_CLASS(Vector3))
  247. RETURN_BOXED_STRUCT(Vector3, p_var);
  248. if (tclass == CACHED_CLASS(Basis))
  249. RETURN_BOXED_STRUCT(Basis, p_var);
  250. if (tclass == CACHED_CLASS(Quat))
  251. RETURN_BOXED_STRUCT(Quat, p_var);
  252. if (tclass == CACHED_CLASS(Transform))
  253. RETURN_BOXED_STRUCT(Transform, p_var);
  254. if (tclass == CACHED_CLASS(AABB))
  255. RETURN_BOXED_STRUCT(AABB, p_var);
  256. if (tclass == CACHED_CLASS(Color))
  257. RETURN_BOXED_STRUCT(Color, p_var);
  258. if (tclass == CACHED_CLASS(Plane))
  259. RETURN_BOXED_STRUCT(Plane, p_var);
  260. if (mono_class_is_enum(tclass->get_mono_ptr())) {
  261. int val = p_var->operator signed int();
  262. return BOX_ENUM(tclass->get_mono_ptr(), val);
  263. }
  264. } break;
  265. case MONO_TYPE_ARRAY:
  266. case MONO_TYPE_SZARRAY: {
  267. MonoArrayType *array_type = mono_type_get_array_type(p_type.type_class->get_mono_type());
  268. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  269. return (MonoObject *)Array_to_mono_array(p_var->operator Array());
  270. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  271. return (MonoObject *)PoolByteArray_to_mono_array(p_var->operator PoolByteArray());
  272. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  273. return (MonoObject *)PoolIntArray_to_mono_array(p_var->operator PoolIntArray());
  274. if (array_type->eklass == REAL_T_MONOCLASS)
  275. return (MonoObject *)PoolRealArray_to_mono_array(p_var->operator PoolRealArray());
  276. if (array_type->eklass == CACHED_CLASS_RAW(String))
  277. return (MonoObject *)PoolStringArray_to_mono_array(p_var->operator PoolStringArray());
  278. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  279. return (MonoObject *)PoolVector2Array_to_mono_array(p_var->operator PoolVector2Array());
  280. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  281. return (MonoObject *)PoolVector3Array_to_mono_array(p_var->operator PoolVector3Array());
  282. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  283. return (MonoObject *)PoolColorArray_to_mono_array(p_var->operator PoolColorArray());
  284. ERR_EXPLAIN(String() + "Attempted to convert Variant to a managed array of unmarshallable element type.");
  285. ERR_FAIL_V(NULL);
  286. } break;
  287. case MONO_TYPE_CLASS: {
  288. GDMonoClass *type_class = p_type.type_class;
  289. // GodotObject
  290. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  291. return GDMonoUtils::unmanaged_get_managed(p_var->operator Object *());
  292. }
  293. if (CACHED_CLASS(NodePath) == type_class) {
  294. return GDMonoUtils::create_managed_from(p_var->operator NodePath());
  295. }
  296. if (CACHED_CLASS(RID) == type_class) {
  297. return GDMonoUtils::create_managed_from(p_var->operator RID());
  298. }
  299. if (CACHED_CLASS(Dictionary) == type_class) {
  300. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), CACHED_CLASS(Dictionary));
  301. }
  302. if (CACHED_CLASS(Array) == type_class) {
  303. return GDMonoUtils::create_managed_from(p_var->operator Array(), CACHED_CLASS(Array));
  304. }
  305. } break;
  306. case MONO_TYPE_OBJECT: {
  307. // Variant
  308. switch (p_var->get_type()) {
  309. case Variant::BOOL: {
  310. MonoBoolean val = p_var->operator bool();
  311. return BOX_BOOLEAN(val);
  312. }
  313. case Variant::INT: {
  314. int val = p_var->operator signed int();
  315. return BOX_INT32(val);
  316. }
  317. case Variant::REAL: {
  318. #ifdef REAL_T_IS_DOUBLE
  319. double val = p_var->operator double();
  320. return BOX_DOUBLE(val);
  321. #else
  322. float val = p_var->operator float();
  323. return BOX_FLOAT(val);
  324. #endif
  325. }
  326. case Variant::STRING:
  327. return (MonoObject *)mono_string_from_godot(p_var->operator String());
  328. case Variant::VECTOR2:
  329. RETURN_BOXED_STRUCT(Vector2, p_var);
  330. case Variant::RECT2:
  331. RETURN_BOXED_STRUCT(Rect2, p_var);
  332. case Variant::VECTOR3:
  333. RETURN_BOXED_STRUCT(Vector3, p_var);
  334. case Variant::TRANSFORM2D:
  335. RETURN_BOXED_STRUCT(Transform2D, p_var);
  336. case Variant::PLANE:
  337. RETURN_BOXED_STRUCT(Plane, p_var);
  338. case Variant::QUAT:
  339. RETURN_BOXED_STRUCT(Quat, p_var);
  340. case Variant::AABB:
  341. RETURN_BOXED_STRUCT(AABB, p_var);
  342. case Variant::BASIS:
  343. RETURN_BOXED_STRUCT(Basis, p_var);
  344. case Variant::TRANSFORM:
  345. RETURN_BOXED_STRUCT(Transform, p_var);
  346. case Variant::COLOR:
  347. RETURN_BOXED_STRUCT(Color, p_var);
  348. case Variant::NODE_PATH:
  349. return GDMonoUtils::create_managed_from(p_var->operator NodePath());
  350. case Variant::_RID:
  351. return GDMonoUtils::create_managed_from(p_var->operator RID());
  352. case Variant::OBJECT: {
  353. return GDMonoUtils::unmanaged_get_managed(p_var->operator Object *());
  354. }
  355. case Variant::DICTIONARY:
  356. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), CACHED_CLASS(Dictionary));
  357. case Variant::ARRAY:
  358. return GDMonoUtils::create_managed_from(p_var->operator Array(), CACHED_CLASS(Array));
  359. case Variant::POOL_BYTE_ARRAY:
  360. return (MonoObject *)PoolByteArray_to_mono_array(p_var->operator PoolByteArray());
  361. case Variant::POOL_INT_ARRAY:
  362. return (MonoObject *)PoolIntArray_to_mono_array(p_var->operator PoolIntArray());
  363. case Variant::POOL_REAL_ARRAY:
  364. return (MonoObject *)PoolRealArray_to_mono_array(p_var->operator PoolRealArray());
  365. case Variant::POOL_STRING_ARRAY:
  366. return (MonoObject *)PoolStringArray_to_mono_array(p_var->operator PoolStringArray());
  367. case Variant::POOL_VECTOR2_ARRAY:
  368. return (MonoObject *)PoolVector2Array_to_mono_array(p_var->operator PoolVector2Array());
  369. case Variant::POOL_VECTOR3_ARRAY:
  370. return (MonoObject *)PoolVector3Array_to_mono_array(p_var->operator PoolVector3Array());
  371. case Variant::POOL_COLOR_ARRAY:
  372. return (MonoObject *)PoolColorArray_to_mono_array(p_var->operator PoolColorArray());
  373. default:
  374. return NULL;
  375. }
  376. break;
  377. case MONO_TYPE_GENERICINST: {
  378. MonoReflectionType *reftype = mono_type_get_object(SCRIPTS_DOMAIN, p_type.type_class->get_mono_type());
  379. MonoException *exc = NULL;
  380. GDMonoUtils::IsDictionaryGenericType type_is_dict = CACHED_METHOD_THUNK(MarshalUtils, IsDictionaryGenericType);
  381. MonoBoolean is_dict = type_is_dict((MonoObject *)reftype, (MonoObject **)&exc);
  382. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  383. if (is_dict) {
  384. return GDMonoUtils::create_managed_from(p_var->operator Dictionary(), p_type.type_class);
  385. }
  386. exc = NULL;
  387. GDMonoUtils::IsArrayGenericType type_is_array = CACHED_METHOD_THUNK(MarshalUtils, IsArrayGenericType);
  388. MonoBoolean is_array = type_is_array((MonoObject *)reftype, (MonoObject **)&exc);
  389. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  390. if (is_array) {
  391. return GDMonoUtils::create_managed_from(p_var->operator Array(), p_type.type_class);
  392. }
  393. } break;
  394. } break;
  395. }
  396. ERR_EXPLAIN(String() + "Attempted to convert Variant to an unmarshallable managed type. Name: \'" +
  397. p_type.type_class->get_name() + "\' Encoding: " + itos(p_type.type_encoding));
  398. ERR_FAIL_V(NULL);
  399. }
  400. Variant mono_object_to_variant(MonoObject *p_obj) {
  401. if (!p_obj)
  402. return Variant();
  403. GDMonoClass *tclass = GDMono::get_singleton()->get_class(mono_object_get_class(p_obj));
  404. ERR_FAIL_COND_V(!tclass, Variant());
  405. MonoType *raw_type = tclass->get_mono_type();
  406. ManagedType type;
  407. type.type_encoding = mono_type_get_type(raw_type);
  408. type.type_class = tclass;
  409. switch (type.type_encoding) {
  410. case MONO_TYPE_BOOLEAN:
  411. return (bool)unbox<MonoBoolean>(p_obj);
  412. case MONO_TYPE_I1:
  413. return unbox<int8_t>(p_obj);
  414. case MONO_TYPE_I2:
  415. return unbox<int16_t>(p_obj);
  416. case MONO_TYPE_I4:
  417. return unbox<int32_t>(p_obj);
  418. case MONO_TYPE_I8:
  419. return unbox<int64_t>(p_obj);
  420. case MONO_TYPE_U1:
  421. return unbox<uint8_t>(p_obj);
  422. case MONO_TYPE_U2:
  423. return unbox<uint16_t>(p_obj);
  424. case MONO_TYPE_U4:
  425. return unbox<uint32_t>(p_obj);
  426. case MONO_TYPE_U8:
  427. return unbox<uint64_t>(p_obj);
  428. case MONO_TYPE_R4:
  429. return unbox<float>(p_obj);
  430. case MONO_TYPE_R8:
  431. return unbox<double>(p_obj);
  432. case MONO_TYPE_STRING: {
  433. if (p_obj == NULL)
  434. return Variant(); // NIL
  435. return mono_string_to_godot_not_null((MonoString *)p_obj);
  436. } break;
  437. case MONO_TYPE_VALUETYPE: {
  438. GDMonoClass *tclass = type.type_class;
  439. if (tclass == CACHED_CLASS(Vector2))
  440. RETURN_UNBOXED_STRUCT(Vector2, p_obj);
  441. if (tclass == CACHED_CLASS(Rect2))
  442. RETURN_UNBOXED_STRUCT(Rect2, p_obj);
  443. if (tclass == CACHED_CLASS(Transform2D))
  444. RETURN_UNBOXED_STRUCT(Transform2D, p_obj);
  445. if (tclass == CACHED_CLASS(Vector3))
  446. RETURN_UNBOXED_STRUCT(Vector3, p_obj);
  447. if (tclass == CACHED_CLASS(Basis))
  448. RETURN_UNBOXED_STRUCT(Basis, p_obj);
  449. if (tclass == CACHED_CLASS(Quat))
  450. RETURN_UNBOXED_STRUCT(Quat, p_obj);
  451. if (tclass == CACHED_CLASS(Transform))
  452. RETURN_UNBOXED_STRUCT(Transform, p_obj);
  453. if (tclass == CACHED_CLASS(AABB))
  454. RETURN_UNBOXED_STRUCT(AABB, p_obj);
  455. if (tclass == CACHED_CLASS(Color))
  456. RETURN_UNBOXED_STRUCT(Color, p_obj);
  457. if (tclass == CACHED_CLASS(Plane))
  458. RETURN_UNBOXED_STRUCT(Plane, p_obj);
  459. if (mono_class_is_enum(tclass->get_mono_ptr()))
  460. return unbox<int32_t>(p_obj);
  461. } break;
  462. case MONO_TYPE_ARRAY:
  463. case MONO_TYPE_SZARRAY: {
  464. MonoArrayType *array_type = mono_type_get_array_type(type.type_class->get_mono_type());
  465. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  466. return mono_array_to_Array((MonoArray *)p_obj);
  467. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  468. return mono_array_to_PoolByteArray((MonoArray *)p_obj);
  469. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  470. return mono_array_to_PoolIntArray((MonoArray *)p_obj);
  471. if (array_type->eklass == REAL_T_MONOCLASS)
  472. return mono_array_to_PoolRealArray((MonoArray *)p_obj);
  473. if (array_type->eklass == CACHED_CLASS_RAW(String))
  474. return mono_array_to_PoolStringArray((MonoArray *)p_obj);
  475. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  476. return mono_array_to_PoolVector2Array((MonoArray *)p_obj);
  477. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  478. return mono_array_to_PoolVector3Array((MonoArray *)p_obj);
  479. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  480. return mono_array_to_PoolColorArray((MonoArray *)p_obj);
  481. ERR_EXPLAIN(String() + "Attempted to convert a managed array of unmarshallable element type to Variant.");
  482. ERR_FAIL_V(Variant());
  483. } break;
  484. case MONO_TYPE_CLASS: {
  485. GDMonoClass *type_class = type.type_class;
  486. // GodotObject
  487. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  488. Object *ptr = unbox<Object *>(CACHED_FIELD(GodotObject, ptr)->get_value(p_obj));
  489. return ptr ? Variant(ptr) : Variant();
  490. }
  491. if (CACHED_CLASS(NodePath) == type_class) {
  492. NodePath *ptr = unbox<NodePath *>(CACHED_FIELD(NodePath, ptr)->get_value(p_obj));
  493. return ptr ? Variant(*ptr) : Variant();
  494. }
  495. if (CACHED_CLASS(RID) == type_class) {
  496. RID *ptr = unbox<RID *>(CACHED_FIELD(RID, ptr)->get_value(p_obj));
  497. return ptr ? Variant(*ptr) : Variant();
  498. }
  499. if (CACHED_CLASS(Array) == type_class) {
  500. MonoException *exc = NULL;
  501. GDMonoUtils::Array_GetPtr get_ptr = CACHED_METHOD_THUNK(Array, GetPtr);
  502. Array *ptr = get_ptr(p_obj, (MonoObject **)&exc);
  503. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  504. return ptr ? Variant(*ptr) : Variant();
  505. }
  506. if (CACHED_CLASS(Dictionary) == type_class) {
  507. MonoException *exc = NULL;
  508. GDMonoUtils::Dictionary_GetPtr get_ptr = CACHED_METHOD_THUNK(Dictionary, GetPtr);
  509. Dictionary *ptr = get_ptr(p_obj, (MonoObject **)&exc);
  510. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  511. return ptr ? Variant(*ptr) : Variant();
  512. }
  513. } break;
  514. case MONO_TYPE_GENERICINST: {
  515. MonoReflectionType *reftype = mono_type_get_object(SCRIPTS_DOMAIN, type.type_class->get_mono_type());
  516. MonoException *exc = NULL;
  517. GDMonoUtils::IsDictionaryGenericType type_is_dict = CACHED_METHOD_THUNK(MarshalUtils, IsDictionaryGenericType);
  518. MonoBoolean is_dict = type_is_dict((MonoObject *)reftype, (MonoObject **)&exc);
  519. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  520. if (is_dict) {
  521. MonoException *exc = NULL;
  522. MonoObject *ret = type.type_class->get_method("GetPtr")->invoke(p_obj, &exc);
  523. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  524. return *unbox<Dictionary *>(ret);
  525. }
  526. exc = NULL;
  527. GDMonoUtils::IsArrayGenericType type_is_array = CACHED_METHOD_THUNK(MarshalUtils, IsArrayGenericType);
  528. MonoBoolean is_array = type_is_array((MonoObject *)reftype, (MonoObject **)&exc);
  529. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  530. if (is_array) {
  531. MonoException *exc = NULL;
  532. MonoObject *ret = type.type_class->get_method("GetPtr")->invoke(p_obj, &exc);
  533. UNLIKELY_UNHANDLED_EXCEPTION(exc);
  534. return *unbox<Array *>(ret);
  535. }
  536. } break;
  537. }
  538. ERR_EXPLAIN(String() + "Attempted to convert an unmarshallable managed type to Variant. Name: \'" +
  539. type.type_class->get_name() + "\' Encoding: " + itos(type.type_encoding));
  540. ERR_FAIL_V(Variant());
  541. }
  542. MonoArray *Array_to_mono_array(const Array &p_array) {
  543. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(MonoObject), p_array.size());
  544. for (int i = 0; i < p_array.size(); i++) {
  545. MonoObject *boxed = variant_to_mono_object(p_array[i]);
  546. mono_array_setref(ret, i, boxed);
  547. }
  548. return ret;
  549. }
  550. Array mono_array_to_Array(MonoArray *p_array) {
  551. Array ret;
  552. if (!p_array)
  553. return ret;
  554. int length = mono_array_length(p_array);
  555. ret.resize(length);
  556. for (int i = 0; i < length; i++) {
  557. MonoObject *elem = mono_array_get(p_array, MonoObject *, i);
  558. ret[i] = mono_object_to_variant(elem);
  559. }
  560. return ret;
  561. }
  562. // TODO Optimize reading/writing from/to PoolArrays
  563. MonoArray *PoolIntArray_to_mono_array(const PoolIntArray &p_array) {
  564. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(int32_t), p_array.size());
  565. for (int i = 0; i < p_array.size(); i++) {
  566. mono_array_set(ret, int32_t, i, p_array[i]);
  567. }
  568. return ret;
  569. }
  570. PoolIntArray mono_array_to_PoolIntArray(MonoArray *p_array) {
  571. PoolIntArray ret;
  572. if (!p_array)
  573. return ret;
  574. int length = mono_array_length(p_array);
  575. ret.resize(length);
  576. for (int i = 0; i < length; i++) {
  577. int32_t elem = mono_array_get(p_array, int32_t, i);
  578. ret.set(i, elem);
  579. }
  580. return ret;
  581. }
  582. MonoArray *PoolByteArray_to_mono_array(const PoolByteArray &p_array) {
  583. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(uint8_t), p_array.size());
  584. for (int i = 0; i < p_array.size(); i++) {
  585. mono_array_set(ret, uint8_t, i, p_array[i]);
  586. }
  587. return ret;
  588. }
  589. PoolByteArray mono_array_to_PoolByteArray(MonoArray *p_array) {
  590. PoolByteArray ret;
  591. if (!p_array)
  592. return ret;
  593. int length = mono_array_length(p_array);
  594. ret.resize(length);
  595. for (int i = 0; i < length; i++) {
  596. uint8_t elem = mono_array_get(p_array, uint8_t, i);
  597. ret.set(i, elem);
  598. }
  599. return ret;
  600. }
  601. MonoArray *PoolRealArray_to_mono_array(const PoolRealArray &p_array) {
  602. MonoArray *ret = mono_array_new(mono_domain_get(), REAL_T_MONOCLASS, p_array.size());
  603. for (int i = 0; i < p_array.size(); i++) {
  604. mono_array_set(ret, real_t, i, p_array[i]);
  605. }
  606. return ret;
  607. }
  608. PoolRealArray mono_array_to_PoolRealArray(MonoArray *p_array) {
  609. PoolRealArray ret;
  610. if (!p_array)
  611. return ret;
  612. int length = mono_array_length(p_array);
  613. ret.resize(length);
  614. for (int i = 0; i < length; i++) {
  615. real_t elem = mono_array_get(p_array, real_t, i);
  616. ret.set(i, elem);
  617. }
  618. return ret;
  619. }
  620. MonoArray *PoolStringArray_to_mono_array(const PoolStringArray &p_array) {
  621. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(String), p_array.size());
  622. for (int i = 0; i < p_array.size(); i++) {
  623. MonoString *boxed = mono_string_from_godot(p_array[i]);
  624. mono_array_set(ret, MonoString *, i, boxed);
  625. }
  626. return ret;
  627. }
  628. PoolStringArray mono_array_to_PoolStringArray(MonoArray *p_array) {
  629. PoolStringArray ret;
  630. if (!p_array)
  631. return ret;
  632. int length = mono_array_length(p_array);
  633. ret.resize(length);
  634. for (int i = 0; i < length; i++) {
  635. MonoString *elem = mono_array_get(p_array, MonoString *, i);
  636. ret.set(i, mono_string_to_godot(elem));
  637. }
  638. return ret;
  639. }
  640. MonoArray *PoolColorArray_to_mono_array(const PoolColorArray &p_array) {
  641. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Color), p_array.size());
  642. for (int i = 0; i < p_array.size(); i++) {
  643. #ifdef YOLOCOPY
  644. mono_array_set(ret, Color, i, p_array[i]);
  645. #else
  646. real_t *raw = (real_t *)mono_array_addr_with_size(ret, sizeof(real_t) * 4, i);
  647. const Color &elem = p_array[i];
  648. raw[0] = elem.r;
  649. raw[1] = elem.g;
  650. raw[2] = elem.b;
  651. raw[3] = elem.a;
  652. #endif
  653. }
  654. return ret;
  655. }
  656. PoolColorArray mono_array_to_PoolColorArray(MonoArray *p_array) {
  657. PoolColorArray ret;
  658. if (!p_array)
  659. return ret;
  660. int length = mono_array_length(p_array);
  661. ret.resize(length);
  662. for (int i = 0; i < length; i++) {
  663. real_t *raw_elem = (real_t *)mono_array_addr_with_size(p_array, sizeof(real_t) * 4, i);
  664. MARSHALLED_IN(Color, raw_elem, elem);
  665. ret.set(i, elem);
  666. }
  667. return ret;
  668. }
  669. MonoArray *PoolVector2Array_to_mono_array(const PoolVector2Array &p_array) {
  670. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Vector2), p_array.size());
  671. for (int i = 0; i < p_array.size(); i++) {
  672. #ifdef YOLOCOPY
  673. mono_array_set(ret, Vector2, i, p_array[i]);
  674. #else
  675. real_t *raw = (real_t *)mono_array_addr_with_size(ret, sizeof(real_t) * 2, i);
  676. const Vector2 &elem = p_array[i];
  677. raw[0] = elem.x;
  678. raw[1] = elem.y;
  679. #endif
  680. }
  681. return ret;
  682. }
  683. PoolVector2Array mono_array_to_PoolVector2Array(MonoArray *p_array) {
  684. PoolVector2Array ret;
  685. if (!p_array)
  686. return ret;
  687. int length = mono_array_length(p_array);
  688. ret.resize(length);
  689. for (int i = 0; i < length; i++) {
  690. real_t *raw_elem = (real_t *)mono_array_addr_with_size(p_array, sizeof(real_t) * 2, i);
  691. MARSHALLED_IN(Vector2, raw_elem, elem);
  692. ret.set(i, elem);
  693. }
  694. return ret;
  695. }
  696. MonoArray *PoolVector3Array_to_mono_array(const PoolVector3Array &p_array) {
  697. MonoArray *ret = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(Vector3), p_array.size());
  698. for (int i = 0; i < p_array.size(); i++) {
  699. #ifdef YOLOCOPY
  700. mono_array_set(ret, Vector3, i, p_array[i]);
  701. #else
  702. real_t *raw = (real_t *)mono_array_addr_with_size(ret, sizeof(real_t) * 3, i);
  703. const Vector3 &elem = p_array[i];
  704. raw[0] = elem.x;
  705. raw[1] = elem.y;
  706. raw[2] = elem.z;
  707. #endif
  708. }
  709. return ret;
  710. }
  711. PoolVector3Array mono_array_to_PoolVector3Array(MonoArray *p_array) {
  712. PoolVector3Array ret;
  713. if (!p_array)
  714. return ret;
  715. int length = mono_array_length(p_array);
  716. ret.resize(length);
  717. for (int i = 0; i < length; i++) {
  718. real_t *raw_elem = (real_t *)mono_array_addr_with_size(p_array, sizeof(real_t) * 3, i);
  719. MARSHALLED_IN(Vector3, raw_elem, elem);
  720. ret.set(i, elem);
  721. }
  722. return ret;
  723. }
  724. } // namespace GDMonoMarshal