marshalls.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* marshalls.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 MARSHALLS_H
  31. #define MARSHALLS_H
  32. #include "core/math/math_defs.h"
  33. #include "core/object/ref_counted.h"
  34. #include "core/typedefs.h"
  35. #include "core/variant/variant.h"
  36. // uintr_t is only for pairing with real_t, and we only need it in here.
  37. #ifdef REAL_T_IS_DOUBLE
  38. typedef uint64_t uintr_t;
  39. #else
  40. typedef uint32_t uintr_t;
  41. #endif
  42. /**
  43. * Miscellaneous helpers for marshaling data types, and encoding
  44. * in an endian independent way
  45. */
  46. union MarshallFloat {
  47. uint32_t i; ///< int
  48. float f; ///< float
  49. };
  50. union MarshallDouble {
  51. uint64_t l; ///< long long
  52. double d; ///< double
  53. };
  54. // Behaves like one of the above, depending on compilation setting.
  55. union MarshallReal {
  56. uintr_t i;
  57. real_t r;
  58. };
  59. static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
  60. for (int i = 0; i < 2; i++) {
  61. *p_arr = p_uint & 0xFF;
  62. p_arr++;
  63. p_uint >>= 8;
  64. }
  65. return sizeof(uint16_t);
  66. }
  67. static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
  68. for (int i = 0; i < 4; i++) {
  69. *p_arr = p_uint & 0xFF;
  70. p_arr++;
  71. p_uint >>= 8;
  72. }
  73. return sizeof(uint32_t);
  74. }
  75. static inline unsigned int encode_half(float p_float, uint8_t *p_arr) {
  76. encode_uint16(Math::make_half_float(p_float), p_arr);
  77. return sizeof(uint16_t);
  78. }
  79. static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
  80. MarshallFloat mf;
  81. mf.f = p_float;
  82. encode_uint32(mf.i, p_arr);
  83. return sizeof(uint32_t);
  84. }
  85. static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
  86. for (int i = 0; i < 8; i++) {
  87. *p_arr = p_uint & 0xFF;
  88. p_arr++;
  89. p_uint >>= 8;
  90. }
  91. return sizeof(uint64_t);
  92. }
  93. static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
  94. MarshallDouble md;
  95. md.d = p_double;
  96. encode_uint64(md.l, p_arr);
  97. return sizeof(uint64_t);
  98. }
  99. static inline unsigned int encode_uintr(uintr_t p_uint, uint8_t *p_arr) {
  100. for (size_t i = 0; i < sizeof(uintr_t); i++) {
  101. *p_arr = p_uint & 0xFF;
  102. p_arr++;
  103. p_uint >>= 8;
  104. }
  105. return sizeof(uintr_t);
  106. }
  107. static inline unsigned int encode_real(real_t p_real, uint8_t *p_arr) {
  108. MarshallReal mr;
  109. mr.r = p_real;
  110. encode_uintr(mr.i, p_arr);
  111. return sizeof(uintr_t);
  112. }
  113. static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
  114. int len = 0;
  115. while (*p_string) {
  116. if (p_data) {
  117. *p_data = (uint8_t)*p_string;
  118. p_data++;
  119. }
  120. p_string++;
  121. len++;
  122. }
  123. if (p_data) {
  124. *p_data = 0;
  125. }
  126. return len + 1;
  127. }
  128. static inline uint16_t decode_uint16(const uint8_t *p_arr) {
  129. uint16_t u = 0;
  130. for (int i = 0; i < 2; i++) {
  131. uint16_t b = *p_arr;
  132. b <<= (i * 8);
  133. u |= b;
  134. p_arr++;
  135. }
  136. return u;
  137. }
  138. static inline uint32_t decode_uint32(const uint8_t *p_arr) {
  139. uint32_t u = 0;
  140. for (int i = 0; i < 4; i++) {
  141. uint32_t b = *p_arr;
  142. b <<= (i * 8);
  143. u |= b;
  144. p_arr++;
  145. }
  146. return u;
  147. }
  148. static inline float decode_half(const uint8_t *p_arr) {
  149. return Math::half_to_float(decode_uint16(p_arr));
  150. }
  151. static inline float decode_float(const uint8_t *p_arr) {
  152. MarshallFloat mf;
  153. mf.i = decode_uint32(p_arr);
  154. return mf.f;
  155. }
  156. static inline uint64_t decode_uint64(const uint8_t *p_arr) {
  157. uint64_t u = 0;
  158. for (int i = 0; i < 8; i++) {
  159. uint64_t b = (*p_arr) & 0xFF;
  160. b <<= (i * 8);
  161. u |= b;
  162. p_arr++;
  163. }
  164. return u;
  165. }
  166. static inline double decode_double(const uint8_t *p_arr) {
  167. MarshallDouble md;
  168. md.l = decode_uint64(p_arr);
  169. return md.d;
  170. }
  171. class EncodedObjectAsID : public RefCounted {
  172. GDCLASS(EncodedObjectAsID, RefCounted);
  173. ObjectID id;
  174. protected:
  175. static void _bind_methods();
  176. public:
  177. void set_object_id(ObjectID p_id);
  178. ObjectID get_object_id() const;
  179. EncodedObjectAsID() {}
  180. };
  181. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false, int p_depth = 0);
  182. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
  183. Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count);
  184. #endif // MARSHALLS_H