Commit e0fe7954 authored by Fabio Alessandrelli's avatar Fabio Alessandrelli Committed by Hein-Pieter van Braam-Stewart
Browse files

Add object encoding param to serialization methods

Network peers get_var/put_var
File get_var/store_var
GDScript/Mono/VisualScript bytes2var/var2bytes
Add MultiplayerAPI.allow_object_decoding member which deprecates PacketPeer.allow_object_decoding.

Break ABI compatibaility (API compatibility for GDNative).

(cherry picked from commit 393e62b9)
parent a1ad05df
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -1908,18 +1908,18 @@ bool _File::file_exists(const String &p_name) const {
	return FileAccess::exists(p_name);
}

void _File::store_var(const Variant &p_var) {
void _File::store_var(const Variant &p_var, bool p_full_objects) {

	ERR_FAIL_COND(!f);
	int len;
	Error err = encode_variant(p_var, NULL, len);
	Error err = encode_variant(p_var, NULL, len, p_full_objects);
	ERR_FAIL_COND(err != OK);

	PoolVector<uint8_t> buff;
	buff.resize(len);
	PoolVector<uint8_t>::Write w = buff.write();

	err = encode_variant(p_var, &w[0], len);
	err = encode_variant(p_var, &w[0], len, p_full_objects);
	ERR_FAIL_COND(err != OK);
	w = PoolVector<uint8_t>::Write();

@@ -1927,7 +1927,7 @@ void _File::store_var(const Variant &p_var) {
	store_buffer(buff);
}

Variant _File::get_var() const {
Variant _File::get_var(bool p_allow_objects) const {

	ERR_FAIL_COND_V(!f, Variant());
	uint32_t len = get_32();
@@ -1937,7 +1937,7 @@ Variant _File::get_var() const {
	PoolVector<uint8_t>::Read r = buff.read();

	Variant v;
	Error err = decode_variant(v, &r[0], len);
	Error err = decode_variant(v, &r[0], len, NULL, p_allow_objects);
	ERR_FAIL_COND_V(err != OK, Variant());

	return v;
@@ -1980,7 +1980,7 @@ void _File::_bind_methods() {
	ClassDB::bind_method(D_METHOD("get_endian_swap"), &_File::get_endian_swap);
	ClassDB::bind_method(D_METHOD("set_endian_swap", "enable"), &_File::set_endian_swap);
	ClassDB::bind_method(D_METHOD("get_error"), &_File::get_error);
	ClassDB::bind_method(D_METHOD("get_var"), &_File::get_var);
	ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &_File::get_var, DEFVAL(false));

	ClassDB::bind_method(D_METHOD("store_8", "value"), &_File::store_8);
	ClassDB::bind_method(D_METHOD("store_16", "value"), &_File::store_16);
@@ -1993,7 +1993,7 @@ void _File::_bind_methods() {
	ClassDB::bind_method(D_METHOD("store_line", "line"), &_File::store_line);
	ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &_File::store_csv_line, DEFVAL(","));
	ClassDB::bind_method(D_METHOD("store_string", "string"), &_File::store_string);
	ClassDB::bind_method(D_METHOD("store_var", "value"), &_File::store_var);
	ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &_File::store_var, DEFVAL(false));

	ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &_File::store_pascal_string);
	ClassDB::bind_method(D_METHOD("get_pascal_string"), &_File::get_pascal_string);
@@ -2223,17 +2223,17 @@ _Marshalls *_Marshalls::get_singleton() {
	return singleton;
}

String _Marshalls::variant_to_base64(const Variant &p_var) {
String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {

	int len;
	Error err = encode_variant(p_var, NULL, len);
	Error err = encode_variant(p_var, NULL, len, p_full_objects);
	ERR_FAIL_COND_V(err != OK, "");

	PoolVector<uint8_t> buff;
	buff.resize(len);
	PoolVector<uint8_t>::Write w = buff.write();

	err = encode_variant(p_var, &w[0], len);
	err = encode_variant(p_var, &w[0], len, p_full_objects);
	ERR_FAIL_COND_V(err != OK, "");

	int b64len = len / 3 * 4 + 4 + 1;
@@ -2249,7 +2249,7 @@ String _Marshalls::variant_to_base64(const Variant &p_var) {
	return ret;
};

Variant _Marshalls::base64_to_variant(const String &p_str) {
Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {

	int strlen = p_str.length();
	CharString cstr = p_str.ascii();
@@ -2261,7 +2261,7 @@ Variant _Marshalls::base64_to_variant(const String &p_str) {
	int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);

	Variant v;
	Error err = decode_variant(v, &w[0], len);
	Error err = decode_variant(v, &w[0], len, NULL, p_allow_objects);
	ERR_FAIL_COND_V(err != OK, Variant());

	return v;
@@ -2340,8 +2340,8 @@ String _Marshalls::base64_to_utf8(const String &p_str) {

void _Marshalls::_bind_methods() {

	ClassDB::bind_method(D_METHOD("variant_to_base64", "variant"), &_Marshalls::variant_to_base64);
	ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str"), &_Marshalls::base64_to_variant);
	ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &_Marshalls::variant_to_base64, DEFVAL(false));
	ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &_Marshalls::base64_to_variant, DEFVAL(false));

	ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &_Marshalls::raw_to_base64);
	ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &_Marshalls::base64_to_raw);
+4 −4
Original line number Diff line number Diff line
@@ -463,7 +463,7 @@ public:
	double get_double() const;
	real_t get_real() const;

	Variant get_var() const;
	Variant get_var(bool p_allow_objects = false) const;

	PoolVector<uint8_t> get_buffer(int p_length) const; ///< get an array of bytes
	String get_line() const;
@@ -500,7 +500,7 @@ public:

	void store_buffer(const PoolVector<uint8_t> &p_buffer); ///< store an array of bytes

	void store_var(const Variant &p_var);
	void store_var(const Variant &p_var, bool p_full_objects = false);

	bool file_exists(const String &p_name) const; ///< return true if a file exists

@@ -569,8 +569,8 @@ protected:
public:
	static _Marshalls *get_singleton();

	String variant_to_base64(const Variant &p_var);
	Variant base64_to_variant(const String &p_str);
	String variant_to_base64(const Variant &p_var, bool p_full_objects = false);
	Variant base64_to_variant(const String &p_str, bool p_allow_objects = false);

	String raw_to_base64(const PoolVector<uint8_t> &p_arr);
	PoolVector<uint8_t> base64_to_raw(const String &p_str);
+22 −7
Original line number Diff line number Diff line
@@ -299,7 +299,7 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_
		ERR_FAIL_COND(p_offset >= p_packet_len);

		int vlen;
		Error err = decode_variant(args.write[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen, network_peer->is_object_decoding_allowed());
		Error err = decode_variant(args.write[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen, allow_object_decoding || network_peer->is_object_decoding_allowed());
		ERR_EXPLAIN("Invalid packet received. Unable to decode RPC argument.");
		ERR_FAIL_COND(err != OK);

@@ -335,7 +335,7 @@ void MultiplayerAPI::_process_rset(Node *p_node, const StringName &p_name, int p
	ERR_FAIL_COND(!_can_call_mode(p_node, rset_mode, p_from));

	Variant value;
	Error err = decode_variant(value, &p_packet[p_offset], p_packet_len - p_offset, NULL, network_peer->is_object_decoding_allowed());
	Error err = decode_variant(value, &p_packet[p_offset], p_packet_len - p_offset, NULL, allow_object_decoding || network_peer->is_object_decoding_allowed());

	ERR_EXPLAIN("Invalid packet received. Unable to decode RSET value.");
	ERR_FAIL_COND(err != OK);
@@ -526,11 +526,11 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p

	if (p_set) {
		// Set argument.
		Error err = encode_variant(*p_arg[0], NULL, len, network_peer->is_object_decoding_allowed());
		Error err = encode_variant(*p_arg[0], NULL, len, allow_object_decoding || network_peer->is_object_decoding_allowed());
		ERR_EXPLAIN("Unable to encode RSET value. THIS IS LIKELY A BUG IN THE ENGINE!");
		ERR_FAIL_COND(err != OK);
		MAKE_ROOM(ofs + len);
		encode_variant(*p_arg[0], &(packet_cache.write[ofs]), len, network_peer->is_object_decoding_allowed());
		encode_variant(*p_arg[0], &(packet_cache.write[ofs]), len, allow_object_decoding || network_peer->is_object_decoding_allowed());
		ofs += len;

	} else {
@@ -539,11 +539,11 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p
		packet_cache.write[ofs] = p_argcount;
		ofs += 1;
		for (int i = 0; i < p_argcount; i++) {
			Error err = encode_variant(*p_arg[i], NULL, len, network_peer->is_object_decoding_allowed());
			Error err = encode_variant(*p_arg[i], NULL, len, allow_object_decoding || network_peer->is_object_decoding_allowed());
			ERR_EXPLAIN("Unable to encode RPC argument. THIS IS LIKELY A BUG IN THE ENGINE!");
			ERR_FAIL_COND(err != OK);
			MAKE_ROOM(ofs + len);
			encode_variant(*p_arg[i], &(packet_cache.write[ofs]), len, network_peer->is_object_decoding_allowed());
			encode_variant(*p_arg[i], &(packet_cache.write[ofs]), len, allow_object_decoding || network_peer->is_object_decoding_allowed());
			ofs += len;
		}
	}
@@ -818,6 +818,16 @@ Vector<int> MultiplayerAPI::get_network_connected_peers() const {
	return ret;
}

void MultiplayerAPI::set_allow_object_decoding(bool p_enable) {

	allow_object_decoding = p_enable;
}

bool MultiplayerAPI::is_object_decoding_allowed() const {

	return allow_object_decoding;
}

void MultiplayerAPI::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node);
	ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE));
@@ -838,6 +848,10 @@ void MultiplayerAPI::_bind_methods() {
	ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &MultiplayerAPI::get_network_connected_peers);
	ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &MultiplayerAPI::set_refuse_new_network_connections);
	ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &MultiplayerAPI::is_refusing_new_network_connections);
	ClassDB::bind_method(D_METHOD("set_allow_object_decoding", "enable"), &MultiplayerAPI::set_allow_object_decoding);
	ClassDB::bind_method(D_METHOD("is_object_decoding_allowed"), &MultiplayerAPI::is_object_decoding_allowed);

	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");

@@ -859,7 +873,8 @@ void MultiplayerAPI::_bind_methods() {
	BIND_ENUM_CONSTANT(RPC_MODE_PUPPETSYNC);
}

MultiplayerAPI::MultiplayerAPI() {
MultiplayerAPI::MultiplayerAPI() :
		allow_object_decoding(false) {
	rpc_sender_id = 0;
	root_node = NULL;
	clear();
+4 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ private:
	int last_send_cache_id;
	Vector<uint8_t> packet_cache;
	Node *root_node;
	bool allow_object_decoding;

protected:
	static void _bind_methods();
@@ -126,6 +127,9 @@ public:
	void set_refuse_new_network_connections(bool p_refuse);
	bool is_refusing_new_network_connections() const;

	void set_allow_object_decoding(bool p_enable);
	bool is_object_decoding_allowed() const;

	MultiplayerAPI();
	~MultiplayerAPI();
};
+9 −9
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ Error PacketPeer::put_packet_buffer(const PoolVector<uint8_t> &p_buffer) {
	return put_packet(&r[0], len);
}

Error PacketPeer::get_var(Variant &r_variant) {
Error PacketPeer::get_var(Variant &r_variant, bool p_allow_objects) {

	const uint8_t *buffer;
	int buffer_size;
@@ -87,13 +87,13 @@ Error PacketPeer::get_var(Variant &r_variant) {
	if (err)
		return err;

	return decode_variant(r_variant, buffer, buffer_size, NULL, allow_object_decoding);
	return decode_variant(r_variant, buffer, buffer_size, NULL, p_allow_objects || allow_object_decoding);
}

Error PacketPeer::put_var(const Variant &p_packet) {
Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) {

	int len;
	Error err = encode_variant(p_packet, NULL, len, allow_object_decoding); // compute len first
	Error err = encode_variant(p_packet, NULL, len, p_full_objects || allow_object_decoding); // compute len first
	if (err)
		return err;

@@ -102,15 +102,15 @@ Error PacketPeer::put_var(const Variant &p_packet) {

	uint8_t *buf = (uint8_t *)alloca(len);
	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, p_full_objects || allow_object_decoding);
	ERR_FAIL_COND_V(err, err);

	return put_packet(buf, len);
}

Variant PacketPeer::_bnd_get_var() {
Variant PacketPeer::_bnd_get_var(bool p_allow_objects) {
	Variant var;
	get_var(var);
	get_var(var, p_allow_objects);

	return var;
};
@@ -132,8 +132,8 @@ Error PacketPeer::_get_packet_error() const {

void PacketPeer::_bind_methods() {

	ClassDB::bind_method(D_METHOD("get_var"), &PacketPeer::_bnd_get_var);
	ClassDB::bind_method(D_METHOD("put_var", "var"), &PacketPeer::put_var);
	ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &PacketPeer::_bnd_get_var, DEFVAL(false));
	ClassDB::bind_method(D_METHOD("put_var", "var", "full_objects"), &PacketPeer::put_var, DEFVAL(false));
	ClassDB::bind_method(D_METHOD("get_packet"), &PacketPeer::_get_packet);
	ClassDB::bind_method(D_METHOD("put_packet", "buffer"), &PacketPeer::_put_packet);
	ClassDB::bind_method(D_METHOD("get_packet_error"), &PacketPeer::_get_packet_error);
Loading