2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* marshalls.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef MARSHALLS_H
|
|
|
|
#define MARSHALLS_H
|
|
|
|
|
2020-03-17 03:11:41 +00:00
|
|
|
#include "core/math/math_defs.h"
|
2021-06-04 16:03:15 +00:00
|
|
|
#include "core/object/ref_counted.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/typedefs.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/variant/variant.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-17 03:11:41 +00:00
|
|
|
// uintr_t is only for pairing with real_t, and we only need it in here.
|
|
|
|
#ifdef REAL_T_IS_DOUBLE
|
|
|
|
typedef uint64_t uintr_t;
|
|
|
|
#else
|
|
|
|
typedef uint32_t uintr_t;
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
2017-03-24 20:45:31 +00:00
|
|
|
* Miscellaneous helpers for marshalling data types, and encoding
|
2014-02-10 01:10:30 +00:00
|
|
|
* in an endian independent way
|
|
|
|
*/
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
union MarshallFloat {
|
|
|
|
uint32_t i; ///< int
|
|
|
|
float f; ///< float
|
|
|
|
};
|
|
|
|
|
|
|
|
union MarshallDouble {
|
|
|
|
uint64_t l; ///< long long
|
|
|
|
double d; ///< double
|
|
|
|
};
|
|
|
|
|
2020-03-17 03:11:41 +00:00
|
|
|
// Behaves like one of the above, depending on compilation setting.
|
|
|
|
union MarshallReal {
|
|
|
|
uintr_t i;
|
|
|
|
real_t r;
|
|
|
|
};
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
*p_arr = p_uint & 0xFF;
|
|
|
|
p_arr++;
|
|
|
|
p_uint >>= 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return sizeof(uint16_t);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
*p_arr = p_uint & 0xFF;
|
|
|
|
p_arr++;
|
|
|
|
p_uint >>= 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return sizeof(uint32_t);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
|
|
|
|
MarshallFloat mf;
|
2017-03-05 15:44:50 +00:00
|
|
|
mf.f = p_float;
|
|
|
|
encode_uint32(mf.i, p_arr);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return sizeof(uint32_t);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
*p_arr = p_uint & 0xFF;
|
|
|
|
p_arr++;
|
|
|
|
p_uint >>= 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return sizeof(uint64_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
|
|
|
|
MarshallDouble md;
|
2017-03-05 15:44:50 +00:00
|
|
|
md.d = p_double;
|
|
|
|
encode_uint64(md.l, p_arr);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return sizeof(uint64_t);
|
|
|
|
}
|
|
|
|
|
2020-03-17 03:11:41 +00:00
|
|
|
static inline unsigned int encode_uintr(uintr_t p_uint, uint8_t *p_arr) {
|
|
|
|
for (size_t i = 0; i < sizeof(uintr_t); i++) {
|
|
|
|
*p_arr = p_uint & 0xFF;
|
|
|
|
p_arr++;
|
|
|
|
p_uint >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeof(uintr_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int encode_real(real_t p_real, uint8_t *p_arr) {
|
|
|
|
MarshallReal mr;
|
|
|
|
mr.r = p_real;
|
|
|
|
encode_uintr(mr.i, p_arr);
|
|
|
|
|
|
|
|
return sizeof(uintr_t);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
|
|
|
|
int len = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
while (*p_string) {
|
|
|
|
if (p_data) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*p_data = (uint8_t)*p_string;
|
2014-02-10 01:10:30 +00:00
|
|
|
p_data++;
|
|
|
|
}
|
|
|
|
p_string++;
|
|
|
|
len++;
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_data) {
|
2020-05-10 10:56:01 +00:00
|
|
|
*p_data = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
return len + 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t decode_uint16(const uint8_t *p_arr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
uint16_t u = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
uint16_t b = *p_arr;
|
2017-03-05 15:44:50 +00:00
|
|
|
b <<= (i * 8);
|
|
|
|
u |= b;
|
2014-02-10 01:10:30 +00:00
|
|
|
p_arr++;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t decode_uint32(const uint8_t *p_arr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t u = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t b = *p_arr;
|
2017-03-05 15:44:50 +00:00
|
|
|
b <<= (i * 8);
|
|
|
|
u |= b;
|
2014-02-10 01:10:30 +00:00
|
|
|
p_arr++;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float decode_float(const uint8_t *p_arr) {
|
|
|
|
MarshallFloat mf;
|
|
|
|
mf.i = decode_uint32(p_arr);
|
|
|
|
return mf.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t decode_uint64(const uint8_t *p_arr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
uint64_t u = 0;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
uint64_t b = (*p_arr) & 0xFF;
|
|
|
|
b <<= (i * 8);
|
|
|
|
u |= b;
|
2014-02-10 01:10:30 +00:00
|
|
|
p_arr++;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double decode_double(const uint8_t *p_arr) {
|
|
|
|
MarshallDouble md;
|
2017-03-05 15:44:50 +00:00
|
|
|
md.l = decode_uint64(p_arr);
|
2014-02-10 01:10:30 +00:00
|
|
|
return md.d;
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:03:15 +00:00
|
|
|
class EncodedObjectAsID : public RefCounted {
|
|
|
|
GDCLASS(EncodedObjectAsID, RefCounted);
|
2017-08-18 13:59:31 +00:00
|
|
|
|
|
|
|
ObjectID id;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void set_object_id(ObjectID p_id);
|
|
|
|
ObjectID get_object_id() const;
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
EncodedObjectAsID() {}
|
2017-08-18 13:59:31 +00:00
|
|
|
};
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false);
|
2021-07-30 13:43:01 +00:00
|
|
|
Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-25 10:10:34 +00:00
|
|
|
#endif // MARSHALLS_H
|