gd_mono_field.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*************************************************************************/
  2. /* gd_mono_field.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_mono_field.h"
  31. #include <mono/metadata/attrdefs.h>
  32. #include "gd_mono_cache.h"
  33. #include "gd_mono_class.h"
  34. #include "gd_mono_marshal.h"
  35. #include "gd_mono_utils.h"
  36. void GDMonoField::set_value(MonoObject *p_object, MonoObject *p_value) {
  37. mono_field_set_value(p_object, mono_field, p_value);
  38. }
  39. void GDMonoField::set_value_raw(MonoObject *p_object, void *p_ptr) {
  40. mono_field_set_value(p_object, mono_field, &p_ptr);
  41. }
  42. void GDMonoField::set_value_from_variant(MonoObject *p_object, const Variant &p_value) {
  43. #define SET_FROM_STRUCT(m_type) \
  44. { \
  45. GDMonoMarshal::M_##m_type from = MARSHALLED_OUT(m_type, p_value.operator ::m_type()); \
  46. mono_field_set_value(p_object, mono_field, &from); \
  47. }
  48. #define SET_FROM_ARRAY(m_type) \
  49. { \
  50. MonoArray *managed = GDMonoMarshal::m_type##_to_mono_array(p_value.operator ::m_type()); \
  51. mono_field_set_value(p_object, mono_field, managed); \
  52. }
  53. switch (type.type_encoding) {
  54. case MONO_TYPE_BOOLEAN: {
  55. MonoBoolean val = p_value.operator bool();
  56. mono_field_set_value(p_object, mono_field, &val);
  57. } break;
  58. case MONO_TYPE_CHAR: {
  59. int16_t val = p_value.operator unsigned short();
  60. mono_field_set_value(p_object, mono_field, &val);
  61. } break;
  62. case MONO_TYPE_I1: {
  63. int8_t val = p_value.operator signed char();
  64. mono_field_set_value(p_object, mono_field, &val);
  65. } break;
  66. case MONO_TYPE_I2: {
  67. int16_t val = p_value.operator signed short();
  68. mono_field_set_value(p_object, mono_field, &val);
  69. } break;
  70. case MONO_TYPE_I4: {
  71. int32_t val = p_value.operator signed int();
  72. mono_field_set_value(p_object, mono_field, &val);
  73. } break;
  74. case MONO_TYPE_I8: {
  75. int64_t val = p_value.operator int64_t();
  76. mono_field_set_value(p_object, mono_field, &val);
  77. } break;
  78. case MONO_TYPE_U1: {
  79. uint8_t val = p_value.operator unsigned char();
  80. mono_field_set_value(p_object, mono_field, &val);
  81. } break;
  82. case MONO_TYPE_U2: {
  83. uint16_t val = p_value.operator unsigned short();
  84. mono_field_set_value(p_object, mono_field, &val);
  85. } break;
  86. case MONO_TYPE_U4: {
  87. uint32_t val = p_value.operator unsigned int();
  88. mono_field_set_value(p_object, mono_field, &val);
  89. } break;
  90. case MONO_TYPE_U8: {
  91. uint64_t val = p_value.operator uint64_t();
  92. mono_field_set_value(p_object, mono_field, &val);
  93. } break;
  94. case MONO_TYPE_R4: {
  95. float val = p_value.operator float();
  96. mono_field_set_value(p_object, mono_field, &val);
  97. } break;
  98. case MONO_TYPE_R8: {
  99. double val = p_value.operator double();
  100. mono_field_set_value(p_object, mono_field, &val);
  101. } break;
  102. case MONO_TYPE_STRING: {
  103. if (p_value.get_type() == Variant::NIL) {
  104. // Otherwise, Variant -> String would return the string "Null"
  105. MonoString *mono_string = nullptr;
  106. mono_field_set_value(p_object, mono_field, mono_string);
  107. } else {
  108. MonoString *mono_string = GDMonoMarshal::mono_string_from_godot(p_value);
  109. mono_field_set_value(p_object, mono_field, mono_string);
  110. }
  111. } break;
  112. case MONO_TYPE_VALUETYPE: {
  113. GDMonoClass *tclass = type.type_class;
  114. if (tclass == CACHED_CLASS(Vector2)) {
  115. SET_FROM_STRUCT(Vector2);
  116. break;
  117. }
  118. if (tclass == CACHED_CLASS(Vector2i)) {
  119. SET_FROM_STRUCT(Vector2i);
  120. break;
  121. }
  122. if (tclass == CACHED_CLASS(Rect2)) {
  123. SET_FROM_STRUCT(Rect2);
  124. break;
  125. }
  126. if (tclass == CACHED_CLASS(Rect2i)) {
  127. SET_FROM_STRUCT(Rect2i);
  128. break;
  129. }
  130. if (tclass == CACHED_CLASS(Transform2D)) {
  131. SET_FROM_STRUCT(Transform2D);
  132. break;
  133. }
  134. if (tclass == CACHED_CLASS(Vector3)) {
  135. SET_FROM_STRUCT(Vector3);
  136. break;
  137. }
  138. if (tclass == CACHED_CLASS(Vector3i)) {
  139. SET_FROM_STRUCT(Vector3i);
  140. break;
  141. }
  142. if (tclass == CACHED_CLASS(Basis)) {
  143. SET_FROM_STRUCT(Basis);
  144. break;
  145. }
  146. if (tclass == CACHED_CLASS(Quat)) {
  147. SET_FROM_STRUCT(Quat);
  148. break;
  149. }
  150. if (tclass == CACHED_CLASS(Transform)) {
  151. SET_FROM_STRUCT(Transform);
  152. break;
  153. }
  154. if (tclass == CACHED_CLASS(AABB)) {
  155. SET_FROM_STRUCT(AABB);
  156. break;
  157. }
  158. if (tclass == CACHED_CLASS(Color)) {
  159. SET_FROM_STRUCT(Color);
  160. break;
  161. }
  162. if (tclass == CACHED_CLASS(Plane)) {
  163. SET_FROM_STRUCT(Plane);
  164. break;
  165. }
  166. if (tclass == CACHED_CLASS(Callable)) {
  167. GDMonoMarshal::M_Callable val = GDMonoMarshal::callable_to_managed(p_value.operator Callable());
  168. mono_field_set_value(p_object, mono_field, &val);
  169. break;
  170. }
  171. if (tclass == CACHED_CLASS(SignalInfo)) {
  172. GDMonoMarshal::M_SignalInfo val = GDMonoMarshal::signal_info_to_managed(p_value.operator Signal());
  173. mono_field_set_value(p_object, mono_field, &val);
  174. break;
  175. }
  176. if (mono_class_is_enum(tclass->get_mono_ptr())) {
  177. MonoType *enum_basetype = mono_class_enum_basetype(tclass->get_mono_ptr());
  178. switch (mono_type_get_type(enum_basetype)) {
  179. case MONO_TYPE_BOOLEAN: {
  180. MonoBoolean val = p_value.operator bool();
  181. mono_field_set_value(p_object, mono_field, &val);
  182. break;
  183. }
  184. case MONO_TYPE_CHAR: {
  185. uint16_t val = p_value.operator unsigned short();
  186. mono_field_set_value(p_object, mono_field, &val);
  187. break;
  188. }
  189. case MONO_TYPE_I1: {
  190. int8_t val = p_value.operator signed char();
  191. mono_field_set_value(p_object, mono_field, &val);
  192. break;
  193. }
  194. case MONO_TYPE_I2: {
  195. int16_t val = p_value.operator signed short();
  196. mono_field_set_value(p_object, mono_field, &val);
  197. break;
  198. }
  199. case MONO_TYPE_I4: {
  200. int32_t val = p_value.operator signed int();
  201. mono_field_set_value(p_object, mono_field, &val);
  202. break;
  203. }
  204. case MONO_TYPE_I8: {
  205. int64_t val = p_value.operator int64_t();
  206. mono_field_set_value(p_object, mono_field, &val);
  207. break;
  208. }
  209. case MONO_TYPE_U1: {
  210. uint8_t val = p_value.operator unsigned char();
  211. mono_field_set_value(p_object, mono_field, &val);
  212. break;
  213. }
  214. case MONO_TYPE_U2: {
  215. uint16_t val = p_value.operator unsigned short();
  216. mono_field_set_value(p_object, mono_field, &val);
  217. break;
  218. }
  219. case MONO_TYPE_U4: {
  220. uint32_t val = p_value.operator unsigned int();
  221. mono_field_set_value(p_object, mono_field, &val);
  222. break;
  223. }
  224. case MONO_TYPE_U8: {
  225. uint64_t val = p_value.operator uint64_t();
  226. mono_field_set_value(p_object, mono_field, &val);
  227. break;
  228. }
  229. default: {
  230. ERR_FAIL_MSG("Attempted to convert Variant to a managed enum value of unmarshallable base type.");
  231. }
  232. }
  233. break;
  234. }
  235. ERR_FAIL_MSG("Attempted to set the value of a field of unmarshallable type: '" + tclass->get_name() + "'.");
  236. } break;
  237. case MONO_TYPE_ARRAY:
  238. case MONO_TYPE_SZARRAY: {
  239. MonoArrayType *array_type = mono_type_get_array_type(type.type_class->get_mono_type());
  240. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject)) {
  241. SET_FROM_ARRAY(Array);
  242. break;
  243. }
  244. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t)) {
  245. SET_FROM_ARRAY(PackedByteArray);
  246. break;
  247. }
  248. if (array_type->eklass == CACHED_CLASS_RAW(int32_t)) {
  249. SET_FROM_ARRAY(PackedInt32Array);
  250. break;
  251. }
  252. if (array_type->eklass == CACHED_CLASS_RAW(int64_t)) {
  253. SET_FROM_ARRAY(PackedInt64Array);
  254. break;
  255. }
  256. if (array_type->eklass == CACHED_CLASS_RAW(float)) {
  257. SET_FROM_ARRAY(PackedFloat32Array);
  258. break;
  259. }
  260. if (array_type->eklass == CACHED_CLASS_RAW(double)) {
  261. SET_FROM_ARRAY(PackedFloat64Array);
  262. break;
  263. }
  264. if (array_type->eklass == CACHED_CLASS_RAW(String)) {
  265. SET_FROM_ARRAY(PackedStringArray);
  266. break;
  267. }
  268. if (array_type->eklass == CACHED_CLASS_RAW(Vector2)) {
  269. SET_FROM_ARRAY(PackedVector2Array);
  270. break;
  271. }
  272. if (array_type->eklass == CACHED_CLASS_RAW(Vector3)) {
  273. SET_FROM_ARRAY(PackedVector3Array);
  274. break;
  275. }
  276. if (array_type->eklass == CACHED_CLASS_RAW(Color)) {
  277. SET_FROM_ARRAY(PackedColorArray);
  278. break;
  279. }
  280. GDMonoClass *array_type_class = GDMono::get_singleton()->get_class(array_type->eklass);
  281. if (CACHED_CLASS(GodotObject)->is_assignable_from(array_type_class)) {
  282. MonoArray *managed = GDMonoMarshal::Array_to_mono_array(p_value.operator ::Array(), array_type_class);
  283. mono_field_set_value(p_object, mono_field, managed);
  284. break;
  285. }
  286. ERR_FAIL_MSG("Attempted to convert Variant to a managed array of unmarshallable element type.");
  287. } break;
  288. case MONO_TYPE_CLASS: {
  289. GDMonoClass *type_class = type.type_class;
  290. // GodotObject
  291. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  292. MonoObject *managed = GDMonoUtils::unmanaged_get_managed(p_value.operator Object *());
  293. mono_field_set_value(p_object, mono_field, managed);
  294. break;
  295. }
  296. if (CACHED_CLASS(StringName) == type_class) {
  297. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator StringName());
  298. mono_field_set_value(p_object, mono_field, managed);
  299. break;
  300. }
  301. if (CACHED_CLASS(NodePath) == type_class) {
  302. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath());
  303. mono_field_set_value(p_object, mono_field, managed);
  304. break;
  305. }
  306. if (CACHED_CLASS(RID) == type_class) {
  307. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID());
  308. mono_field_set_value(p_object, mono_field, managed);
  309. break;
  310. }
  311. if (CACHED_CLASS(Dictionary) == type_class) {
  312. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), CACHED_CLASS(Dictionary));
  313. mono_field_set_value(p_object, mono_field, managed);
  314. break;
  315. }
  316. if (CACHED_CLASS(Array) == type_class) {
  317. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), CACHED_CLASS(Array));
  318. mono_field_set_value(p_object, mono_field, managed);
  319. break;
  320. }
  321. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  322. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), type_class->get_mono_type());
  323. MonoReflectionType *key_reftype, *value_reftype;
  324. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype, &key_reftype, &value_reftype)) {
  325. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(),
  326. GDMonoUtils::Marshal::make_generic_dictionary_type(key_reftype, value_reftype));
  327. mono_field_set_value(p_object, mono_field, managed);
  328. break;
  329. }
  330. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  331. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), CACHED_CLASS(Dictionary));
  332. mono_field_set_value(p_object, mono_field, managed);
  333. break;
  334. }
  335. MonoReflectionType *elem_reftype;
  336. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype, &elem_reftype)) {
  337. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(),
  338. GDMonoUtils::Marshal::make_generic_array_type(elem_reftype));
  339. mono_field_set_value(p_object, mono_field, managed);
  340. break;
  341. }
  342. if (type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  343. if (GDMonoCache::tools_godot_api_check()) {
  344. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), CACHED_CLASS(Array));
  345. mono_field_set_value(p_object, mono_field, managed);
  346. break;
  347. } else {
  348. MonoObject *managed = (MonoObject *)GDMonoMarshal::Array_to_mono_array(p_value.operator Array());
  349. mono_field_set_value(p_object, mono_field, managed);
  350. break;
  351. }
  352. }
  353. ERR_FAIL_MSG("Attempted to set the value of a field of unmarshallable type: '" + type_class->get_name() + "'.");
  354. } break;
  355. case MONO_TYPE_OBJECT: {
  356. // Variant
  357. switch (p_value.get_type()) {
  358. case Variant::BOOL: {
  359. MonoBoolean val = p_value.operator bool();
  360. mono_field_set_value(p_object, mono_field, &val);
  361. } break;
  362. case Variant::INT: {
  363. int32_t val = p_value.operator signed int();
  364. mono_field_set_value(p_object, mono_field, &val);
  365. } break;
  366. case Variant::FLOAT: {
  367. #ifdef REAL_T_IS_DOUBLE
  368. double val = p_value.operator double();
  369. mono_field_set_value(p_object, mono_field, &val);
  370. #else
  371. float val = p_value.operator float();
  372. mono_field_set_value(p_object, mono_field, &val);
  373. #endif
  374. } break;
  375. case Variant::STRING: {
  376. MonoString *mono_string = GDMonoMarshal::mono_string_from_godot(p_value);
  377. mono_field_set_value(p_object, mono_field, mono_string);
  378. } break;
  379. case Variant::VECTOR2: {
  380. SET_FROM_STRUCT(Vector2);
  381. } break;
  382. case Variant::VECTOR2I: {
  383. SET_FROM_STRUCT(Vector2i);
  384. } break;
  385. case Variant::RECT2: {
  386. SET_FROM_STRUCT(Rect2);
  387. } break;
  388. case Variant::RECT2I: {
  389. SET_FROM_STRUCT(Rect2i);
  390. } break;
  391. case Variant::VECTOR3: {
  392. SET_FROM_STRUCT(Vector3);
  393. } break;
  394. case Variant::VECTOR3I: {
  395. SET_FROM_STRUCT(Vector3i);
  396. } break;
  397. case Variant::TRANSFORM2D: {
  398. SET_FROM_STRUCT(Transform2D);
  399. } break;
  400. case Variant::PLANE: {
  401. SET_FROM_STRUCT(Plane);
  402. } break;
  403. case Variant::QUAT: {
  404. SET_FROM_STRUCT(Quat);
  405. } break;
  406. case Variant::AABB: {
  407. SET_FROM_STRUCT(AABB);
  408. } break;
  409. case Variant::BASIS: {
  410. SET_FROM_STRUCT(Basis);
  411. } break;
  412. case Variant::TRANSFORM: {
  413. SET_FROM_STRUCT(Transform);
  414. } break;
  415. case Variant::COLOR: {
  416. SET_FROM_STRUCT(Color);
  417. } break;
  418. case Variant::STRING_NAME: {
  419. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator StringName());
  420. mono_field_set_value(p_object, mono_field, managed);
  421. } break;
  422. case Variant::NODE_PATH: {
  423. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath());
  424. mono_field_set_value(p_object, mono_field, managed);
  425. } break;
  426. case Variant::_RID: {
  427. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID());
  428. mono_field_set_value(p_object, mono_field, managed);
  429. } break;
  430. case Variant::OBJECT: {
  431. MonoObject *managed = GDMonoUtils::unmanaged_get_managed(p_value.operator Object *());
  432. mono_field_set_value(p_object, mono_field, managed);
  433. } break;
  434. case Variant::CALLABLE: {
  435. GDMonoMarshal::M_Callable val = GDMonoMarshal::callable_to_managed(p_value.operator Callable());
  436. mono_field_set_value(p_object, mono_field, &val);
  437. } break;
  438. case Variant::SIGNAL: {
  439. GDMonoMarshal::M_SignalInfo val = GDMonoMarshal::signal_info_to_managed(p_value.operator Signal());
  440. mono_field_set_value(p_object, mono_field, &val);
  441. } break;
  442. case Variant::DICTIONARY: {
  443. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), CACHED_CLASS(Dictionary));
  444. mono_field_set_value(p_object, mono_field, managed);
  445. } break;
  446. case Variant::ARRAY: {
  447. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), CACHED_CLASS(Array));
  448. mono_field_set_value(p_object, mono_field, managed);
  449. } break;
  450. case Variant::PACKED_BYTE_ARRAY: {
  451. SET_FROM_ARRAY(PackedByteArray);
  452. } break;
  453. case Variant::PACKED_INT32_ARRAY: {
  454. SET_FROM_ARRAY(PackedInt32Array);
  455. } break;
  456. case Variant::PACKED_INT64_ARRAY: {
  457. SET_FROM_ARRAY(PackedInt64Array);
  458. } break;
  459. case Variant::PACKED_FLOAT32_ARRAY: {
  460. SET_FROM_ARRAY(PackedFloat32Array);
  461. } break;
  462. case Variant::PACKED_FLOAT64_ARRAY: {
  463. SET_FROM_ARRAY(PackedFloat64Array);
  464. } break;
  465. case Variant::PACKED_STRING_ARRAY: {
  466. SET_FROM_ARRAY(PackedStringArray);
  467. } break;
  468. case Variant::PACKED_VECTOR2_ARRAY: {
  469. SET_FROM_ARRAY(PackedVector2Array);
  470. } break;
  471. case Variant::PACKED_VECTOR3_ARRAY: {
  472. SET_FROM_ARRAY(PackedVector3Array);
  473. } break;
  474. case Variant::PACKED_COLOR_ARRAY: {
  475. SET_FROM_ARRAY(PackedColorArray);
  476. } break;
  477. default: break;
  478. }
  479. } break;
  480. case MONO_TYPE_GENERICINST: {
  481. MonoReflectionType *reftype = mono_type_get_object(mono_domain_get(), type.type_class->get_mono_type());
  482. if (GDMonoUtils::Marshal::type_is_generic_dictionary(reftype)) {
  483. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), type.type_class);
  484. mono_field_set_value(p_object, mono_field, managed);
  485. break;
  486. }
  487. if (GDMonoUtils::Marshal::type_is_generic_array(reftype)) {
  488. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), type.type_class);
  489. mono_field_set_value(p_object, mono_field, managed);
  490. break;
  491. }
  492. // The order in which we check the following interfaces is very important (dictionaries and generics first)
  493. MonoReflectionType *key_reftype, *value_reftype;
  494. if (GDMonoUtils::Marshal::generic_idictionary_is_assignable_from(reftype, &key_reftype, &value_reftype)) {
  495. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(),
  496. GDMonoUtils::Marshal::make_generic_dictionary_type(key_reftype, value_reftype));
  497. mono_field_set_value(p_object, mono_field, managed);
  498. break;
  499. }
  500. if (type.type_class->implements_interface(CACHED_CLASS(System_Collections_IDictionary))) {
  501. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Dictionary(), CACHED_CLASS(Dictionary));
  502. mono_field_set_value(p_object, mono_field, managed);
  503. break;
  504. }
  505. MonoReflectionType *elem_reftype;
  506. if (GDMonoUtils::Marshal::generic_ienumerable_is_assignable_from(reftype, &elem_reftype)) {
  507. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(),
  508. GDMonoUtils::Marshal::make_generic_array_type(elem_reftype));
  509. mono_field_set_value(p_object, mono_field, managed);
  510. break;
  511. }
  512. if (type.type_class->implements_interface(CACHED_CLASS(System_Collections_IEnumerable))) {
  513. if (GDMonoCache::tools_godot_api_check()) {
  514. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator Array(), CACHED_CLASS(Array));
  515. mono_field_set_value(p_object, mono_field, managed);
  516. break;
  517. } else {
  518. MonoObject *managed = (MonoObject *)GDMonoMarshal::Array_to_mono_array(p_value.operator Array());
  519. mono_field_set_value(p_object, mono_field, managed);
  520. break;
  521. }
  522. }
  523. } break;
  524. default: {
  525. ERR_PRINT("Attempted to set the value of a field of unexpected type encoding: " + itos(type.type_encoding) + ".");
  526. } break;
  527. }
  528. #undef SET_FROM_ARRAY_AND_BREAK
  529. #undef SET_FROM_STRUCT_AND_BREAK
  530. }
  531. MonoObject *GDMonoField::get_value(MonoObject *p_object) {
  532. return mono_field_get_value_object(mono_domain_get(), mono_field, p_object);
  533. }
  534. bool GDMonoField::get_bool_value(MonoObject *p_object) {
  535. return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
  536. }
  537. int GDMonoField::get_int_value(MonoObject *p_object) {
  538. return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
  539. }
  540. String GDMonoField::get_string_value(MonoObject *p_object) {
  541. MonoObject *val = get_value(p_object);
  542. return GDMonoMarshal::mono_string_to_godot((MonoString *)val);
  543. }
  544. bool GDMonoField::has_attribute(GDMonoClass *p_attr_class) {
  545. ERR_FAIL_NULL_V(p_attr_class, false);
  546. if (!attrs_fetched)
  547. fetch_attributes();
  548. if (!attributes)
  549. return false;
  550. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
  551. }
  552. MonoObject *GDMonoField::get_attribute(GDMonoClass *p_attr_class) {
  553. ERR_FAIL_NULL_V(p_attr_class, nullptr);
  554. if (!attrs_fetched)
  555. fetch_attributes();
  556. if (!attributes)
  557. return nullptr;
  558. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
  559. }
  560. void GDMonoField::fetch_attributes() {
  561. ERR_FAIL_COND(attributes != nullptr);
  562. attributes = mono_custom_attrs_from_field(owner->get_mono_ptr(), mono_field);
  563. attrs_fetched = true;
  564. }
  565. bool GDMonoField::is_static() {
  566. return mono_field_get_flags(mono_field) & MONO_FIELD_ATTR_STATIC;
  567. }
  568. IMonoClassMember::Visibility GDMonoField::get_visibility() {
  569. switch (mono_field_get_flags(mono_field) & MONO_FIELD_ATTR_FIELD_ACCESS_MASK) {
  570. case MONO_FIELD_ATTR_PRIVATE:
  571. return IMonoClassMember::PRIVATE;
  572. case MONO_FIELD_ATTR_FAM_AND_ASSEM:
  573. return IMonoClassMember::PROTECTED_AND_INTERNAL;
  574. case MONO_FIELD_ATTR_ASSEMBLY:
  575. return IMonoClassMember::INTERNAL;
  576. case MONO_FIELD_ATTR_FAMILY:
  577. return IMonoClassMember::PROTECTED;
  578. case MONO_FIELD_ATTR_PUBLIC:
  579. return IMonoClassMember::PUBLIC;
  580. default:
  581. ERR_FAIL_V(IMonoClassMember::PRIVATE);
  582. }
  583. }
  584. GDMonoField::GDMonoField(MonoClassField *p_mono_field, GDMonoClass *p_owner) {
  585. owner = p_owner;
  586. mono_field = p_mono_field;
  587. name = mono_field_get_name(mono_field);
  588. MonoType *field_type = mono_field_get_type(mono_field);
  589. type.type_encoding = mono_type_get_type(field_type);
  590. MonoClass *field_type_class = mono_class_from_mono_type(field_type);
  591. type.type_class = GDMono::get_singleton()->get_class(field_type_class);
  592. attrs_fetched = false;
  593. attributes = nullptr;
  594. }
  595. GDMonoField::~GDMonoField() {
  596. if (attributes) {
  597. mono_custom_attrs_free(attributes);
  598. }
  599. }