Use same boolean for objects encode and decode.

In a very unintuitive move encode needed false to encode an object,
decode needed true to decode it.
They now need the same value: `true`.
This commit is contained in:
Fabio Alessandrelli 2019-03-28 09:40:31 +01:00
parent 174b19f768
commit e61a074a8e
3 changed files with 24 additions and 25 deletions

View File

@ -794,7 +794,7 @@ static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) {
} }
} }
Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_object_as_id) { Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects) {
uint8_t *buf = r_buffer; uint8_t *buf = r_buffer;
@ -819,7 +819,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
} }
} break; } break;
case Variant::OBJECT: { case Variant::OBJECT: {
if (p_object_as_id) { if (!p_full_objects) {
flags |= ENCODE_FLAG_OBJECT_AS_ID; flags |= ENCODE_FLAG_OBJECT_AS_ID;
} }
} break; } break;
@ -1086,22 +1086,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
} break; } break;
case Variant::OBJECT: { case Variant::OBJECT: {
if (p_object_as_id) { if (p_full_objects) {
if (buf) {
Object *obj = p_variant;
ObjectID id = 0;
if (obj && ObjectDB::instance_validate(obj)) {
id = obj->get_instance_id();
}
encode_uint64(id, buf);
}
r_len += 8;
} else {
Object *obj = p_variant; Object *obj = p_variant;
if (!obj) { if (!obj) {
if (buf) { if (buf) {
@ -1139,7 +1125,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
_encode_string(E->get().name, buf, r_len); _encode_string(E->get().name, buf, r_len);
int len; int len;
Error err = encode_variant(obj->get(E->get().name), buf, len, p_object_as_id); Error err = encode_variant(obj->get(E->get().name), buf, len, p_full_objects);
if (err) if (err)
return err; return err;
ERR_FAIL_COND_V(len % 4, ERR_BUG); ERR_FAIL_COND_V(len % 4, ERR_BUG);
@ -1148,6 +1134,19 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
buf += len; buf += len;
} }
} }
} else {
if (buf) {
Object *obj = p_variant;
ObjectID id = 0;
if (obj && ObjectDB::instance_validate(obj)) {
id = obj->get_instance_id();
}
encode_uint64(id, buf);
}
r_len += 8;
} }
} break; } break;
@ -1180,14 +1179,14 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
r_len++; //pad r_len++; //pad
*/ */
int len; int len;
encode_variant(E->get(), buf, len, p_object_as_id); encode_variant(E->get(), buf, len, p_full_objects);
ERR_FAIL_COND_V(len % 4, ERR_BUG); ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len; r_len += len;
if (buf) if (buf)
buf += len; buf += len;
Variant *v = d.getptr(E->get()); Variant *v = d.getptr(E->get());
ERR_FAIL_COND_V(!v, ERR_BUG); ERR_FAIL_COND_V(!v, ERR_BUG);
encode_variant(*v, buf, len, p_object_as_id); encode_variant(*v, buf, len, p_full_objects);
ERR_FAIL_COND_V(len % 4, ERR_BUG); ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len; r_len += len;
if (buf) if (buf)
@ -1209,7 +1208,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
for (int i = 0; i < v.size(); i++) { for (int i = 0; i < v.size(); i++) {
int len; int len;
encode_variant(v.get(i), buf, len, p_object_as_id); encode_variant(v.get(i), buf, len, p_full_objects);
ERR_FAIL_COND_V(len % 4, ERR_BUG); ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len; r_len += len;
if (buf) if (buf)

View File

@ -199,7 +199,7 @@ public:
EncodedObjectAsID(); EncodedObjectAsID();
}; };
Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = NULL, bool p_allow_objects = true); Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = NULL, bool p_allow_objects = false);
Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_object_as_id = false); Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false);
#endif #endif

View File

@ -93,7 +93,7 @@ Error PacketPeer::get_var(Variant &r_variant) {
Error PacketPeer::put_var(const Variant &p_packet) { Error PacketPeer::put_var(const Variant &p_packet) {
int len; int len;
Error err = encode_variant(p_packet, NULL, len, !allow_object_decoding); // compute len first Error err = encode_variant(p_packet, NULL, len, allow_object_decoding); // compute len first
if (err) if (err)
return err; return err;
@ -102,7 +102,7 @@ Error PacketPeer::put_var(const Variant &p_packet) {
uint8_t *buf = (uint8_t *)alloca(len); uint8_t *buf = (uint8_t *)alloca(len);
ERR_FAIL_COND_V(!buf, ERR_OUT_OF_MEMORY); ERR_FAIL_COND_V(!buf, ERR_OUT_OF_MEMORY);
err = encode_variant(p_packet, buf, len, !allow_object_decoding); err = encode_variant(p_packet, buf, len, allow_object_decoding);
ERR_FAIL_COND_V(err, err); ERR_FAIL_COND_V(err, err);
return put_packet(buf, len); return put_packet(buf, len);