gd_mono_marshal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*************************************************************************/
  2. /* gd_mono_marshal.h */
  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. #ifndef GDMONOMARSHAL_H
  31. #define GDMONOMARSHAL_H
  32. #include "core/variant.h"
  33. #include "gd_mono.h"
  34. #include "gd_mono_utils.h"
  35. namespace GDMonoMarshal {
  36. template <typename T>
  37. T unbox(MonoObject *p_obj) {
  38. return *(T *)mono_object_unbox(p_obj);
  39. }
  40. #define BOX_DOUBLE(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(double), &x)
  41. #define BOX_FLOAT(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(float), &x)
  42. #define BOX_INT64(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int64_t), &x)
  43. #define BOX_INT32(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int32_t), &x)
  44. #define BOX_INT16(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int16_t), &x)
  45. #define BOX_INT8(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(int8_t), &x)
  46. #define BOX_UINT64(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint64_t), &x)
  47. #define BOX_UINT32(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint32_t), &x)
  48. #define BOX_UINT16(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint16_t), &x)
  49. #define BOX_UINT8(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(uint8_t), &x)
  50. #define BOX_BOOLEAN(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(bool), &x)
  51. #define BOX_PTR(x) mono_value_box(mono_domain_get(), CACHED_CLASS_RAW(IntPtr), x)
  52. #define BOX_ENUM(m_enum_class, x) mono_value_box(mono_domain_get(), m_enum_class, &x)
  53. Variant::Type managed_to_variant_type(const ManagedType &p_type);
  54. // String
  55. String mono_to_utf8_string(MonoString *p_mono_string);
  56. String mono_to_utf16_string(MonoString *p_mono_string);
  57. _FORCE_INLINE_ String mono_string_to_godot_not_null(MonoString *p_mono_string) {
  58. if (sizeof(CharType) == 2)
  59. return mono_to_utf16_string(p_mono_string);
  60. return mono_to_utf8_string(p_mono_string);
  61. }
  62. _FORCE_INLINE_ String mono_string_to_godot(MonoString *p_mono_string) {
  63. if (p_mono_string == NULL)
  64. return String();
  65. return mono_string_to_godot_not_null(p_mono_string);
  66. }
  67. _FORCE_INLINE_ MonoString *mono_from_utf8_string(const String &p_string) {
  68. return mono_string_new(mono_domain_get(), p_string.utf8().get_data());
  69. }
  70. _FORCE_INLINE_ MonoString *mono_from_utf16_string(const String &p_string) {
  71. return mono_string_from_utf16((mono_unichar2 *)p_string.c_str());
  72. }
  73. _FORCE_INLINE_ MonoString *mono_string_from_godot(const String &p_string) {
  74. if (sizeof(CharType) == 2)
  75. return mono_from_utf16_string(p_string);
  76. return mono_from_utf8_string(p_string);
  77. }
  78. // Variant
  79. MonoObject *variant_to_mono_object(const Variant *p_var, const ManagedType &p_type);
  80. MonoObject *variant_to_mono_object(const Variant *p_var);
  81. _FORCE_INLINE_ MonoObject *variant_to_mono_object(const Variant &p_var) {
  82. return variant_to_mono_object(&p_var);
  83. }
  84. _FORCE_INLINE_ MonoObject *variant_to_mono_object(const Variant &p_var, const ManagedType &p_type) {
  85. return variant_to_mono_object(&p_var, p_type);
  86. }
  87. Variant mono_object_to_variant(MonoObject *p_obj);
  88. // Array
  89. MonoArray *Array_to_mono_array(const Array &p_array);
  90. Array mono_array_to_Array(MonoArray *p_array);
  91. // PoolIntArray
  92. MonoArray *PoolIntArray_to_mono_array(const PoolIntArray &p_array);
  93. PoolIntArray mono_array_to_PoolIntArray(MonoArray *p_array);
  94. // PoolByteArray
  95. MonoArray *PoolByteArray_to_mono_array(const PoolByteArray &p_array);
  96. PoolByteArray mono_array_to_PoolByteArray(MonoArray *p_array);
  97. // PoolRealArray
  98. MonoArray *PoolRealArray_to_mono_array(const PoolRealArray &p_array);
  99. PoolRealArray mono_array_to_PoolRealArray(MonoArray *p_array);
  100. // PoolStringArray
  101. MonoArray *PoolStringArray_to_mono_array(const PoolStringArray &p_array);
  102. PoolStringArray mono_array_to_PoolStringArray(MonoArray *p_array);
  103. // PoolColorArray
  104. MonoArray *PoolColorArray_to_mono_array(const PoolColorArray &p_array);
  105. PoolColorArray mono_array_to_PoolColorArray(MonoArray *p_array);
  106. // PoolVector2Array
  107. MonoArray *PoolVector2Array_to_mono_array(const PoolVector2Array &p_array);
  108. PoolVector2Array mono_array_to_PoolVector2Array(MonoArray *p_array);
  109. // PoolVector3Array
  110. MonoArray *PoolVector3Array_to_mono_array(const PoolVector3Array &p_array);
  111. PoolVector3Array mono_array_to_PoolVector3Array(MonoArray *p_array);
  112. // Structures
  113. namespace InteropLayout {
  114. enum {
  115. MATCHES_float = (sizeof(float) == sizeof(uint32_t)),
  116. MATCHES_double = (sizeof(double) == sizeof(uint64_t)),
  117. #ifdef REAL_T_IS_DOUBLE
  118. MATCHES_real_t = (sizeof(real_t) == sizeof(uint64_t)),
  119. #else
  120. MATCHES_real_t = (sizeof(real_t) == sizeof(uint32_t)),
  121. #endif
  122. MATCHES_Vector2 = (MATCHES_real_t && (sizeof(Vector2) == (sizeof(real_t) * 2)) &&
  123. offsetof(Vector2, x) == (sizeof(real_t) * 0) &&
  124. offsetof(Vector2, y) == (sizeof(real_t) * 1)),
  125. MATCHES_Rect2 = (MATCHES_Vector2 && (sizeof(Rect2) == (sizeof(Vector2) * 2)) &&
  126. offsetof(Rect2, position) == (sizeof(Vector2) * 0) &&
  127. offsetof(Rect2, size) == (sizeof(Vector2) * 1)),
  128. MATCHES_Transform2D = (MATCHES_Vector2 && (sizeof(Transform2D) == (sizeof(Vector2) * 3))), // No field offset required, it stores an array
  129. MATCHES_Vector3 = (MATCHES_real_t && (sizeof(Vector3) == (sizeof(real_t) * 3)) &&
  130. offsetof(Vector3, x) == (sizeof(real_t) * 0) &&
  131. offsetof(Vector3, y) == (sizeof(real_t) * 1) &&
  132. offsetof(Vector3, z) == (sizeof(real_t) * 2)),
  133. MATCHES_Basis = (MATCHES_Vector3 && (sizeof(Basis) == (sizeof(Vector3) * 3))), // No field offset required, it stores an array
  134. MATCHES_Quat = (MATCHES_real_t && (sizeof(Quat) == (sizeof(real_t) * 4)) &&
  135. offsetof(Quat, x) == (sizeof(real_t) * 0) &&
  136. offsetof(Quat, y) == (sizeof(real_t) * 1) &&
  137. offsetof(Quat, z) == (sizeof(real_t) * 2) &&
  138. offsetof(Quat, w) == (sizeof(real_t) * 3)),
  139. MATCHES_Transform = (MATCHES_Basis && MATCHES_Vector3 && (sizeof(Transform) == (sizeof(Basis) + sizeof(Vector3))) &&
  140. offsetof(Transform, basis) == 0 &&
  141. offsetof(Transform, origin) == sizeof(Basis)),
  142. MATCHES_AABB = (MATCHES_Vector3 && (sizeof(AABB) == (sizeof(Vector3) * 2)) &&
  143. offsetof(AABB, position) == (sizeof(Vector3) * 0) &&
  144. offsetof(AABB, size) == (sizeof(Vector3) * 1)),
  145. MATCHES_Color = (MATCHES_float && (sizeof(Color) == (sizeof(float) * 4)) &&
  146. offsetof(Color, r) == (sizeof(float) * 0) &&
  147. offsetof(Color, g) == (sizeof(float) * 1) &&
  148. offsetof(Color, b) == (sizeof(float) * 2) &&
  149. offsetof(Color, a) == (sizeof(float) * 3)),
  150. MATCHES_Plane = (MATCHES_Vector3 && MATCHES_real_t && (sizeof(Plane) == (sizeof(Vector3) + sizeof(real_t))) &&
  151. offsetof(Plane, normal) == 0 &&
  152. offsetof(Plane, d) == sizeof(Vector3))
  153. };
  154. // In the future we may force this if we want to ref return these structs
  155. #ifdef GD_MONO_FORCE_INTEROP_STRUCT_COPY
  156. // Sometimes clang-format can be an ass
  157. GD_STATIC_ASSERT(MATCHES_Vector2 &&MATCHES_Rect2 &&MATCHES_Transform2D &&MATCHES_Vector3 &&
  158. MATCHES_Basis &&MATCHES_Quat &&MATCHES_Transform &&MATCHES_AABB &&MATCHES_Color &&MATCHES_Plane);
  159. #endif
  160. } // namespace InteropLayout
  161. #pragma pack(push, 1)
  162. struct M_Vector2 {
  163. real_t x, y;
  164. static _FORCE_INLINE_ Vector2 convert_to(const M_Vector2 &p_from) {
  165. return Vector2(p_from.x, p_from.y);
  166. }
  167. static _FORCE_INLINE_ M_Vector2 convert_from(const Vector2 &p_from) {
  168. M_Vector2 ret = { p_from.x, p_from.y };
  169. return ret;
  170. }
  171. };
  172. struct M_Rect2 {
  173. M_Vector2 position;
  174. M_Vector2 size;
  175. static _FORCE_INLINE_ Rect2 convert_to(const M_Rect2 &p_from) {
  176. return Rect2(M_Vector2::convert_to(p_from.position),
  177. M_Vector2::convert_to(p_from.size));
  178. }
  179. static _FORCE_INLINE_ M_Rect2 convert_from(const Rect2 &p_from) {
  180. M_Rect2 ret = { M_Vector2::convert_from(p_from.position), M_Vector2::convert_from(p_from.size) };
  181. return ret;
  182. }
  183. };
  184. struct M_Transform2D {
  185. M_Vector2 elements[3];
  186. static _FORCE_INLINE_ Transform2D convert_to(const M_Transform2D &p_from) {
  187. return Transform2D(p_from.elements[0].x, p_from.elements[0].y,
  188. p_from.elements[1].x, p_from.elements[1].y,
  189. p_from.elements[2].x, p_from.elements[2].y);
  190. }
  191. static _FORCE_INLINE_ M_Transform2D convert_from(const Transform2D &p_from) {
  192. M_Transform2D ret = {
  193. M_Vector2::convert_from(p_from.elements[0]),
  194. M_Vector2::convert_from(p_from.elements[1]),
  195. M_Vector2::convert_from(p_from.elements[2])
  196. };
  197. return ret;
  198. }
  199. };
  200. struct M_Vector3 {
  201. real_t x, y, z;
  202. static _FORCE_INLINE_ Vector3 convert_to(const M_Vector3 &p_from) {
  203. return Vector3(p_from.x, p_from.y, p_from.z);
  204. }
  205. static _FORCE_INLINE_ M_Vector3 convert_from(const Vector3 &p_from) {
  206. M_Vector3 ret = { p_from.x, p_from.y, p_from.z };
  207. return ret;
  208. }
  209. };
  210. struct M_Basis {
  211. M_Vector3 elements[3];
  212. static _FORCE_INLINE_ Basis convert_to(const M_Basis &p_from) {
  213. return Basis(M_Vector3::convert_to(p_from.elements[0]),
  214. M_Vector3::convert_to(p_from.elements[1]),
  215. M_Vector3::convert_to(p_from.elements[2]));
  216. }
  217. static _FORCE_INLINE_ M_Basis convert_from(const Basis &p_from) {
  218. M_Basis ret = {
  219. M_Vector3::convert_from(p_from.elements[0]),
  220. M_Vector3::convert_from(p_from.elements[1]),
  221. M_Vector3::convert_from(p_from.elements[2])
  222. };
  223. return ret;
  224. }
  225. };
  226. struct M_Quat {
  227. real_t x, y, z, w;
  228. static _FORCE_INLINE_ Quat convert_to(const M_Quat &p_from) {
  229. return Quat(p_from.x, p_from.y, p_from.z, p_from.w);
  230. }
  231. static _FORCE_INLINE_ M_Quat convert_from(const Quat &p_from) {
  232. M_Quat ret = { p_from.x, p_from.y, p_from.z, p_from.w };
  233. return ret;
  234. }
  235. };
  236. struct M_Transform {
  237. M_Basis basis;
  238. M_Vector3 origin;
  239. static _FORCE_INLINE_ Transform convert_to(const M_Transform &p_from) {
  240. return Transform(M_Basis::convert_to(p_from.basis), M_Vector3::convert_to(p_from.origin));
  241. }
  242. static _FORCE_INLINE_ M_Transform convert_from(const Transform &p_from) {
  243. M_Transform ret = { M_Basis::convert_from(p_from.basis), M_Vector3::convert_from(p_from.origin) };
  244. return ret;
  245. }
  246. };
  247. struct M_AABB {
  248. M_Vector3 position;
  249. M_Vector3 size;
  250. static _FORCE_INLINE_ AABB convert_to(const M_AABB &p_from) {
  251. return AABB(M_Vector3::convert_to(p_from.position), M_Vector3::convert_to(p_from.size));
  252. }
  253. static _FORCE_INLINE_ M_AABB convert_from(const AABB &p_from) {
  254. M_AABB ret = { M_Vector3::convert_from(p_from.position), M_Vector3::convert_from(p_from.size) };
  255. return ret;
  256. }
  257. };
  258. struct M_Color {
  259. float r, g, b, a;
  260. static _FORCE_INLINE_ Color convert_to(const M_Color &p_from) {
  261. return Color(p_from.r, p_from.g, p_from.b, p_from.a);
  262. }
  263. static _FORCE_INLINE_ M_Color convert_from(const Color &p_from) {
  264. M_Color ret = { p_from.r, p_from.g, p_from.b, p_from.a };
  265. return ret;
  266. }
  267. };
  268. struct M_Plane {
  269. M_Vector3 normal;
  270. real_t d;
  271. static _FORCE_INLINE_ Plane convert_to(const M_Plane &p_from) {
  272. return Plane(M_Vector3::convert_to(p_from.normal), p_from.d);
  273. }
  274. static _FORCE_INLINE_ M_Plane convert_from(const Plane &p_from) {
  275. M_Plane ret = { M_Vector3::convert_from(p_from.normal), p_from.d };
  276. return ret;
  277. }
  278. };
  279. #pragma pack(pop)
  280. #define DECL_TYPE_MARSHAL_TEMPLATES(m_type) \
  281. template <int> \
  282. _FORCE_INLINE_ m_type marshalled_in_##m_type##_impl(const M_##m_type *p_from); \
  283. \
  284. template <> \
  285. _FORCE_INLINE_ m_type marshalled_in_##m_type##_impl<0>(const M_##m_type *p_from) { \
  286. return M_##m_type::convert_to(*p_from); \
  287. } \
  288. \
  289. template <> \
  290. _FORCE_INLINE_ m_type marshalled_in_##m_type##_impl<1>(const M_##m_type *p_from) { \
  291. return *reinterpret_cast<const m_type *>(p_from); \
  292. } \
  293. \
  294. _FORCE_INLINE_ m_type marshalled_in_##m_type(const M_##m_type *p_from) { \
  295. return marshalled_in_##m_type##_impl<InteropLayout::MATCHES_##m_type>(p_from); \
  296. } \
  297. \
  298. template <int> \
  299. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type##_impl(const m_type &p_from); \
  300. \
  301. template <> \
  302. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type##_impl<0>(const m_type &p_from) { \
  303. return M_##m_type::convert_from(p_from); \
  304. } \
  305. \
  306. template <> \
  307. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type##_impl<1>(const m_type &p_from) { \
  308. return *reinterpret_cast<const M_##m_type *>(&p_from); \
  309. } \
  310. \
  311. _FORCE_INLINE_ M_##m_type marshalled_out_##m_type(const m_type &p_from) { \
  312. return marshalled_out_##m_type##_impl<InteropLayout::MATCHES_##m_type>(p_from); \
  313. }
  314. DECL_TYPE_MARSHAL_TEMPLATES(Vector2)
  315. DECL_TYPE_MARSHAL_TEMPLATES(Rect2)
  316. DECL_TYPE_MARSHAL_TEMPLATES(Transform2D)
  317. DECL_TYPE_MARSHAL_TEMPLATES(Vector3)
  318. DECL_TYPE_MARSHAL_TEMPLATES(Basis)
  319. DECL_TYPE_MARSHAL_TEMPLATES(Quat)
  320. DECL_TYPE_MARSHAL_TEMPLATES(Transform)
  321. DECL_TYPE_MARSHAL_TEMPLATES(AABB)
  322. DECL_TYPE_MARSHAL_TEMPLATES(Color)
  323. DECL_TYPE_MARSHAL_TEMPLATES(Plane)
  324. #define MARSHALLED_IN(m_type, m_from_ptr) (GDMonoMarshal::marshalled_in_##m_type(m_from_ptr))
  325. #define MARSHALLED_OUT(m_type, m_from) (GDMonoMarshal::marshalled_out_##m_type(m_from))
  326. } // namespace GDMonoMarshal
  327. #endif // GDMONOMARSHAL_H