gd_mono_field.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_class.h"
  33. #include "gd_mono_marshal.h"
  34. void GDMonoField::set_value_raw(MonoObject *p_object, void *p_ptr) {
  35. mono_field_set_value(p_object, mono_field, &p_ptr);
  36. }
  37. void GDMonoField::set_value(MonoObject *p_object, const Variant &p_value) {
  38. #define SET_FROM_STRUCT_AND_BREAK(m_type) \
  39. { \
  40. const m_type &val = p_value.operator ::m_type(); \
  41. MARSHALLED_OUT(m_type, val, raw); \
  42. mono_field_set_value(p_object, mono_field, raw); \
  43. break; \
  44. }
  45. #define SET_FROM_PRIMITIVE(m_type) \
  46. { \
  47. m_type val = p_value.operator m_type(); \
  48. mono_field_set_value(p_object, mono_field, &val); \
  49. break; \
  50. }
  51. #define SET_FROM_ARRAY_AND_BREAK(m_type) \
  52. { \
  53. MonoArray *managed = GDMonoMarshal::m_type##_to_mono_array(p_value.operator m_type()); \
  54. mono_field_set_value(p_object, mono_field, &managed); \
  55. break; \
  56. }
  57. switch (type.type_encoding) {
  58. case MONO_TYPE_BOOLEAN: {
  59. SET_FROM_PRIMITIVE(bool);
  60. } break;
  61. case MONO_TYPE_I1: {
  62. SET_FROM_PRIMITIVE(signed char);
  63. } break;
  64. case MONO_TYPE_I2: {
  65. SET_FROM_PRIMITIVE(signed short);
  66. } break;
  67. case MONO_TYPE_I4: {
  68. SET_FROM_PRIMITIVE(signed int);
  69. } break;
  70. case MONO_TYPE_I8: {
  71. SET_FROM_PRIMITIVE(int64_t);
  72. } break;
  73. case MONO_TYPE_U1: {
  74. SET_FROM_PRIMITIVE(unsigned char);
  75. } break;
  76. case MONO_TYPE_U2: {
  77. SET_FROM_PRIMITIVE(unsigned short);
  78. } break;
  79. case MONO_TYPE_U4: {
  80. SET_FROM_PRIMITIVE(unsigned int);
  81. } break;
  82. case MONO_TYPE_U8: {
  83. SET_FROM_PRIMITIVE(uint64_t);
  84. } break;
  85. case MONO_TYPE_R4: {
  86. SET_FROM_PRIMITIVE(float);
  87. } break;
  88. case MONO_TYPE_R8: {
  89. SET_FROM_PRIMITIVE(double);
  90. } break;
  91. case MONO_TYPE_STRING: {
  92. MonoString *mono_string = GDMonoMarshal::mono_string_from_godot(p_value);
  93. mono_field_set_value(p_object, mono_field, mono_string);
  94. } break;
  95. case MONO_TYPE_VALUETYPE: {
  96. GDMonoClass *tclass = type.type_class;
  97. if (tclass == CACHED_CLASS(Vector2))
  98. SET_FROM_STRUCT_AND_BREAK(Vector2);
  99. if (tclass == CACHED_CLASS(Rect2))
  100. SET_FROM_STRUCT_AND_BREAK(Rect2);
  101. if (tclass == CACHED_CLASS(Transform2D))
  102. SET_FROM_STRUCT_AND_BREAK(Transform2D);
  103. if (tclass == CACHED_CLASS(Vector3))
  104. SET_FROM_STRUCT_AND_BREAK(Vector3);
  105. if (tclass == CACHED_CLASS(Basis))
  106. SET_FROM_STRUCT_AND_BREAK(Basis);
  107. if (tclass == CACHED_CLASS(Quat))
  108. SET_FROM_STRUCT_AND_BREAK(Quat);
  109. if (tclass == CACHED_CLASS(Transform))
  110. SET_FROM_STRUCT_AND_BREAK(Transform);
  111. if (tclass == CACHED_CLASS(AABB))
  112. SET_FROM_STRUCT_AND_BREAK(AABB);
  113. if (tclass == CACHED_CLASS(Color))
  114. SET_FROM_STRUCT_AND_BREAK(Color);
  115. if (tclass == CACHED_CLASS(Plane))
  116. SET_FROM_STRUCT_AND_BREAK(Plane);
  117. if (mono_class_is_enum(tclass->get_raw()))
  118. SET_FROM_PRIMITIVE(signed int);
  119. ERR_EXPLAIN(String() + "Attempted to set the value of a field of unmarshallable type: " + tclass->get_name());
  120. ERR_FAIL();
  121. } break;
  122. case MONO_TYPE_ARRAY:
  123. case MONO_TYPE_SZARRAY: {
  124. MonoArrayType *array_type = mono_type_get_array_type(GDMonoClass::get_raw_type(type.type_class));
  125. if (array_type->eklass == CACHED_CLASS_RAW(MonoObject))
  126. SET_FROM_ARRAY_AND_BREAK(Array);
  127. if (array_type->eklass == CACHED_CLASS_RAW(uint8_t))
  128. SET_FROM_ARRAY_AND_BREAK(PoolByteArray);
  129. if (array_type->eklass == CACHED_CLASS_RAW(int32_t))
  130. SET_FROM_ARRAY_AND_BREAK(PoolIntArray);
  131. if (array_type->eklass == REAL_T_MONOCLASS)
  132. SET_FROM_ARRAY_AND_BREAK(PoolRealArray);
  133. if (array_type->eklass == CACHED_CLASS_RAW(String))
  134. SET_FROM_ARRAY_AND_BREAK(PoolStringArray);
  135. if (array_type->eklass == CACHED_CLASS_RAW(Vector2))
  136. SET_FROM_ARRAY_AND_BREAK(PoolVector2Array);
  137. if (array_type->eklass == CACHED_CLASS_RAW(Vector3))
  138. SET_FROM_ARRAY_AND_BREAK(PoolVector3Array);
  139. if (array_type->eklass == CACHED_CLASS_RAW(Color))
  140. SET_FROM_ARRAY_AND_BREAK(PoolColorArray);
  141. ERR_EXPLAIN(String() + "Attempted to convert Variant to a managed array of unmarshallable element type.");
  142. ERR_FAIL();
  143. } break;
  144. case MONO_TYPE_CLASS: {
  145. GDMonoClass *type_class = type.type_class;
  146. // GodotObject
  147. if (CACHED_CLASS(GodotObject)->is_assignable_from(type_class)) {
  148. MonoObject *managed = GDMonoUtils::unmanaged_get_managed(p_value.operator Object *());
  149. mono_field_set_value(p_object, mono_field, managed);
  150. break;
  151. }
  152. if (CACHED_CLASS(NodePath) == type_class) {
  153. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath());
  154. mono_field_set_value(p_object, mono_field, managed);
  155. break;
  156. }
  157. if (CACHED_CLASS(RID) == type_class) {
  158. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID());
  159. mono_field_set_value(p_object, mono_field, managed);
  160. break;
  161. }
  162. ERR_EXPLAIN(String() + "Attempted to set the value of a field of unmarshallable type: " + type_class->get_name());
  163. ERR_FAIL();
  164. } break;
  165. case MONO_TYPE_OBJECT: {
  166. // Variant
  167. switch (p_value.get_type()) {
  168. case Variant::BOOL: {
  169. SET_FROM_PRIMITIVE(bool);
  170. } break;
  171. case Variant::INT: {
  172. SET_FROM_PRIMITIVE(int);
  173. } break;
  174. case Variant::REAL: {
  175. #ifdef REAL_T_IS_DOUBLE
  176. SET_FROM_PRIMITIVE(double);
  177. #else
  178. SET_FROM_PRIMITIVE(float);
  179. #endif
  180. } break;
  181. case Variant::STRING: {
  182. MonoString *mono_string = GDMonoMarshal::mono_string_from_godot(p_value);
  183. mono_field_set_value(p_object, mono_field, mono_string);
  184. } break;
  185. case Variant::VECTOR2: SET_FROM_STRUCT_AND_BREAK(Vector2);
  186. case Variant::RECT2: SET_FROM_STRUCT_AND_BREAK(Rect2);
  187. case Variant::VECTOR3: SET_FROM_STRUCT_AND_BREAK(Vector3);
  188. case Variant::TRANSFORM2D: SET_FROM_STRUCT_AND_BREAK(Transform2D);
  189. case Variant::PLANE: SET_FROM_STRUCT_AND_BREAK(Plane);
  190. case Variant::QUAT: SET_FROM_STRUCT_AND_BREAK(Quat);
  191. case Variant::AABB: SET_FROM_STRUCT_AND_BREAK(AABB);
  192. case Variant::BASIS: SET_FROM_STRUCT_AND_BREAK(Basis);
  193. case Variant::TRANSFORM: SET_FROM_STRUCT_AND_BREAK(Transform);
  194. case Variant::COLOR: SET_FROM_STRUCT_AND_BREAK(Color);
  195. case Variant::NODE_PATH: {
  196. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath());
  197. mono_field_set_value(p_object, mono_field, managed);
  198. } break;
  199. case Variant::_RID: {
  200. MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID());
  201. mono_field_set_value(p_object, mono_field, managed);
  202. } break;
  203. case Variant::OBJECT: {
  204. MonoObject *managed = GDMonoUtils::unmanaged_get_managed(p_value.operator Object *());
  205. mono_field_set_value(p_object, mono_field, managed);
  206. break;
  207. }
  208. case Variant::DICTIONARY: {
  209. MonoObject *managed = GDMonoMarshal::Dictionary_to_mono_object(p_value.operator Dictionary());
  210. mono_field_set_value(p_object, mono_field, managed);
  211. } break;
  212. case Variant::ARRAY: SET_FROM_ARRAY_AND_BREAK(Array);
  213. case Variant::POOL_BYTE_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolByteArray);
  214. case Variant::POOL_INT_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolIntArray);
  215. case Variant::POOL_REAL_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolRealArray);
  216. case Variant::POOL_STRING_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolStringArray);
  217. case Variant::POOL_VECTOR2_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolVector2Array);
  218. case Variant::POOL_VECTOR3_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolVector3Array);
  219. case Variant::POOL_COLOR_ARRAY: SET_FROM_ARRAY_AND_BREAK(PoolColorArray);
  220. #undef SET_FROM_ARRAY_AND_BREAK
  221. default: break;
  222. }
  223. } break;
  224. case MONO_TYPE_GENERICINST: {
  225. if (CACHED_RAW_MONO_CLASS(Dictionary) == type.type_class->get_raw()) {
  226. MonoObject *managed = GDMonoMarshal::Dictionary_to_mono_object(p_value.operator Dictionary());
  227. mono_field_set_value(p_object, mono_field, managed);
  228. break;
  229. }
  230. } break;
  231. default: {
  232. ERR_PRINTS(String() + "Attempted to set the value of a field of unexpected type encoding: " + itos(type.type_encoding));
  233. } break;
  234. }
  235. #undef SET_FROM_STRUCT_AND_BREAK
  236. #undef SET_FROM_PRIMITIVE
  237. }
  238. bool GDMonoField::get_bool_value(MonoObject *p_object) {
  239. return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
  240. }
  241. int GDMonoField::get_int_value(MonoObject *p_object) {
  242. return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
  243. }
  244. String GDMonoField::get_string_value(MonoObject *p_object) {
  245. MonoObject *val = get_value(p_object);
  246. return val ? GDMonoMarshal::mono_string_to_godot((MonoString *)val) : String();
  247. }
  248. bool GDMonoField::has_attribute(GDMonoClass *p_attr_class) {
  249. ERR_FAIL_NULL_V(p_attr_class, false);
  250. if (!attrs_fetched)
  251. fetch_attributes();
  252. if (!attributes)
  253. return false;
  254. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_raw());
  255. }
  256. MonoObject *GDMonoField::get_attribute(GDMonoClass *p_attr_class) {
  257. ERR_FAIL_NULL_V(p_attr_class, NULL);
  258. if (!attrs_fetched)
  259. fetch_attributes();
  260. if (!attributes)
  261. return NULL;
  262. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_raw());
  263. }
  264. void GDMonoField::fetch_attributes() {
  265. ERR_FAIL_COND(attributes != NULL);
  266. attributes = mono_custom_attrs_from_field(owner->get_raw(), get_raw());
  267. attrs_fetched = true;
  268. }
  269. bool GDMonoField::is_static() {
  270. return mono_field_get_flags(mono_field) & MONO_FIELD_ATTR_STATIC;
  271. }
  272. GDMono::MemberVisibility GDMonoField::get_visibility() {
  273. switch (mono_field_get_flags(mono_field) & MONO_FIELD_ATTR_FIELD_ACCESS_MASK) {
  274. case MONO_FIELD_ATTR_PRIVATE:
  275. return GDMono::PRIVATE;
  276. case MONO_FIELD_ATTR_FAM_AND_ASSEM:
  277. return GDMono::PROTECTED_AND_INTERNAL;
  278. case MONO_FIELD_ATTR_ASSEMBLY:
  279. return GDMono::INTERNAL;
  280. case MONO_FIELD_ATTR_FAMILY:
  281. return GDMono::PROTECTED;
  282. case MONO_FIELD_ATTR_PUBLIC:
  283. return GDMono::PUBLIC;
  284. default:
  285. ERR_FAIL_V(GDMono::PRIVATE);
  286. }
  287. }
  288. GDMonoField::GDMonoField(MonoClassField *p_raw_field, GDMonoClass *p_owner) {
  289. owner = p_owner;
  290. mono_field = p_raw_field;
  291. name = mono_field_get_name(mono_field);
  292. MonoType *field_type = mono_field_get_type(mono_field);
  293. type.type_encoding = mono_type_get_type(field_type);
  294. MonoClass *field_type_class = mono_class_from_mono_type(field_type);
  295. type.type_class = GDMono::get_singleton()->get_class(field_type_class);
  296. attrs_fetched = false;
  297. attributes = NULL;
  298. }
  299. GDMonoField::~GDMonoField() {
  300. if (attributes) {
  301. mono_custom_attrs_free(attributes);
  302. }
  303. }