marshalls.h 6.0 KB

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