marshalls.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_float(float p_float, uint8_t *p_arr) {
  76. MarshallFloat mf;
  77. mf.f = p_float;
  78. encode_uint32(mf.i, p_arr);
  79. return sizeof(uint32_t);
  80. }
  81. static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
  82. for (int i = 0; i < 8; i++) {
  83. *p_arr = p_uint & 0xFF;
  84. p_arr++;
  85. p_uint >>= 8;
  86. }
  87. return sizeof(uint64_t);
  88. }
  89. static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
  90. MarshallDouble md;
  91. md.d = p_double;
  92. encode_uint64(md.l, p_arr);
  93. return sizeof(uint64_t);
  94. }
  95. static inline unsigned int encode_uintr(uintr_t p_uint, uint8_t *p_arr) {
  96. for (size_t i = 0; i < sizeof(uintr_t); i++) {
  97. *p_arr = p_uint & 0xFF;
  98. p_arr++;
  99. p_uint >>= 8;
  100. }
  101. return sizeof(uintr_t);
  102. }
  103. static inline unsigned int encode_real(real_t p_real, uint8_t *p_arr) {
  104. MarshallReal mr;
  105. mr.r = p_real;
  106. encode_uintr(mr.i, p_arr);
  107. return sizeof(uintr_t);
  108. }
  109. static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
  110. int len = 0;
  111. while (*p_string) {
  112. if (p_data) {
  113. *p_data = (uint8_t)*p_string;
  114. p_data++;
  115. }
  116. p_string++;
  117. len++;
  118. }
  119. if (p_data) {
  120. *p_data = 0;
  121. }
  122. return len + 1;
  123. }
  124. static inline uint16_t decode_uint16(const uint8_t *p_arr) {
  125. uint16_t u = 0;
  126. for (int i = 0; i < 2; i++) {
  127. uint16_t b = *p_arr;
  128. b <<= (i * 8);
  129. u |= b;
  130. p_arr++;
  131. }
  132. return u;
  133. }
  134. static inline uint32_t decode_uint32(const uint8_t *p_arr) {
  135. uint32_t u = 0;
  136. for (int i = 0; i < 4; i++) {
  137. uint32_t b = *p_arr;
  138. b <<= (i * 8);
  139. u |= b;
  140. p_arr++;
  141. }
  142. return u;
  143. }
  144. static inline float decode_float(const uint8_t *p_arr) {
  145. MarshallFloat mf;
  146. mf.i = decode_uint32(p_arr);
  147. return mf.f;
  148. }
  149. static inline uint64_t decode_uint64(const uint8_t *p_arr) {
  150. uint64_t u = 0;
  151. for (int i = 0; i < 8; i++) {
  152. uint64_t b = (*p_arr) & 0xFF;
  153. b <<= (i * 8);
  154. u |= b;
  155. p_arr++;
  156. }
  157. return u;
  158. }
  159. static inline double decode_double(const uint8_t *p_arr) {
  160. MarshallDouble md;
  161. md.l = decode_uint64(p_arr);
  162. return md.d;
  163. }
  164. class EncodedObjectAsID : public RefCounted {
  165. GDCLASS(EncodedObjectAsID, RefCounted);
  166. ObjectID id;
  167. protected:
  168. static void _bind_methods();
  169. public:
  170. void set_object_id(ObjectID p_id);
  171. ObjectID get_object_id() const;
  172. EncodedObjectAsID() {}
  173. };
  174. 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);
  175. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
  176. Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count);
  177. #endif // MARSHALLS_H