2022-02-04 16:24:16 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* scene_cache_interface.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#include "scene_cache_interface.h"
|
|
|
|
|
2023-06-13 16:56:21 +02:00
|
|
|
#include "scene_multiplayer.h"
|
|
|
|
|
2022-02-04 16:24:16 +01:00
|
|
|
#include "core/io/marshalls.h"
|
|
|
|
#include "scene/main/node.h"
|
|
|
|
#include "scene/main/window.h"
|
2023-12-10 14:40:18 +01:00
|
|
|
#include "scene/scene_string_names.h"
|
|
|
|
|
|
|
|
SceneCacheInterface::NodeCache &SceneCacheInterface::_track(Node *p_node) {
|
|
|
|
const ObjectID oid = p_node->get_instance_id();
|
|
|
|
NodeCache *nc = nodes_cache.getptr(oid);
|
|
|
|
if (!nc) {
|
|
|
|
nodes_cache[oid] = NodeCache();
|
|
|
|
p_node->connect(SceneStringNames::get_singleton()->tree_exited, callable_mp(this, &SceneCacheInterface::_remove_node_cache).bind(oid), Object::CONNECT_ONE_SHOT);
|
|
|
|
}
|
|
|
|
return nodes_cache[oid];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneCacheInterface::_remove_node_cache(ObjectID p_oid) {
|
|
|
|
NodeCache *nc = nodes_cache.getptr(p_oid);
|
|
|
|
if (!nc) {
|
|
|
|
return;
|
|
|
|
}
|
2024-03-29 22:35:03 +01:00
|
|
|
if (nc->cache_id) {
|
|
|
|
assigned_ids.erase(nc->cache_id);
|
|
|
|
}
|
2023-12-10 14:40:18 +01:00
|
|
|
for (KeyValue<int, int> &E : nc->recv_ids) {
|
|
|
|
PeerInfo *pinfo = peers_info.getptr(E.key);
|
|
|
|
ERR_CONTINUE(!pinfo);
|
|
|
|
pinfo->recv_nodes.erase(E.value);
|
|
|
|
}
|
|
|
|
for (KeyValue<int, bool> &E : nc->confirmed_peers) {
|
|
|
|
PeerInfo *pinfo = peers_info.getptr(E.key);
|
|
|
|
ERR_CONTINUE(!pinfo);
|
|
|
|
pinfo->sent_nodes.erase(p_oid);
|
|
|
|
}
|
|
|
|
nodes_cache.erase(p_oid);
|
|
|
|
}
|
2022-02-04 16:24:16 +01:00
|
|
|
|
|
|
|
void SceneCacheInterface::on_peer_change(int p_id, bool p_connected) {
|
|
|
|
if (p_connected) {
|
2023-12-10 14:40:18 +01:00
|
|
|
peers_info.insert(p_id, PeerInfo());
|
2022-02-04 16:24:16 +01:00
|
|
|
} else {
|
2023-12-10 14:40:18 +01:00
|
|
|
PeerInfo *pinfo = peers_info.getptr(p_id);
|
|
|
|
ERR_FAIL_NULL(pinfo); // Bug.
|
|
|
|
for (KeyValue<int, ObjectID> E : pinfo->recv_nodes) {
|
|
|
|
NodeCache *nc = nodes_cache.getptr(E.value);
|
|
|
|
ERR_CONTINUE(!nc);
|
|
|
|
nc->recv_ids.erase(E.key);
|
|
|
|
}
|
|
|
|
for (const ObjectID &oid : pinfo->sent_nodes) {
|
|
|
|
NodeCache *nc = nodes_cache.getptr(oid);
|
|
|
|
ERR_CONTINUE(!nc);
|
|
|
|
nc->confirmed_peers.erase(p_id);
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
2023-12-10 14:40:18 +01:00
|
|
|
peers_info.erase(p_id);
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneCacheInterface::process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
|
2023-12-10 14:40:18 +01:00
|
|
|
ERR_FAIL_COND(!peers_info.has(p_from)); // Bug.
|
|
|
|
ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small.");
|
2022-02-04 16:24:16 +01:00
|
|
|
Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL(root_node);
|
2022-02-04 16:24:16 +01:00
|
|
|
int ofs = 1;
|
|
|
|
|
|
|
|
String methods_md5;
|
|
|
|
methods_md5.parse_utf8((const char *)(p_packet + ofs), 32);
|
|
|
|
ofs += 33;
|
|
|
|
|
|
|
|
int id = decode_uint32(&p_packet[ofs]);
|
|
|
|
ofs += 4;
|
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
ERR_FAIL_COND_MSG(peers_info[p_from].recv_nodes.has(id), vformat("Duplicate remote cache ID %d for peer %d", id, p_from));
|
|
|
|
|
2022-02-04 16:24:16 +01:00
|
|
|
String paths;
|
|
|
|
paths.parse_utf8((const char *)(p_packet + ofs), p_packet_len - ofs);
|
|
|
|
|
2023-09-26 20:27:53 +02:00
|
|
|
const NodePath path = paths;
|
2022-02-04 16:24:16 +01:00
|
|
|
|
|
|
|
Node *node = root_node->get_node(path);
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL(node);
|
2022-02-04 16:24:16 +01:00
|
|
|
const bool valid_rpc_checksum = multiplayer->get_rpc_md5(node) == methods_md5;
|
|
|
|
if (valid_rpc_checksum == false) {
|
|
|
|
ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + path);
|
|
|
|
}
|
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
peers_info[p_from].recv_nodes.insert(id, node->get_instance_id());
|
|
|
|
NodeCache &cache = _track(node);
|
|
|
|
cache.recv_ids.insert(p_from, id);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2024-03-29 22:35:03 +01:00
|
|
|
// Send ack.
|
2022-02-04 16:24:16 +01:00
|
|
|
Vector<uint8_t> packet;
|
2024-03-29 22:35:03 +01:00
|
|
|
packet.resize(1 + 1 + 4);
|
2022-07-12 23:12:42 +02:00
|
|
|
packet.write[0] = SceneMultiplayer::NETWORK_COMMAND_CONFIRM_PATH;
|
2022-02-04 16:24:16 +01:00
|
|
|
packet.write[1] = valid_rpc_checksum;
|
2024-03-29 22:35:03 +01:00
|
|
|
encode_uint32(id, &packet.write[2]);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
|
|
|
Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
|
|
|
|
ERR_FAIL_COND(multiplayer_peer.is_null());
|
|
|
|
|
|
|
|
multiplayer_peer->set_transfer_channel(0);
|
2022-07-12 23:12:42 +02:00
|
|
|
multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
|
2022-10-08 20:50:19 +02:00
|
|
|
multiplayer->send_command(p_from, packet.ptr(), packet.size());
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SceneCacheInterface::process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) {
|
2024-03-29 22:35:03 +01:00
|
|
|
ERR_FAIL_COND_MSG(p_packet_len != 6, "Invalid packet received. Size too small.");
|
2023-09-26 20:27:53 +02:00
|
|
|
Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
|
|
|
|
ERR_FAIL_NULL(root_node);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
|
|
|
const bool valid_rpc_checksum = p_packet[1];
|
2024-03-29 22:35:03 +01:00
|
|
|
int id = decode_uint32(&p_packet[2]);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2024-03-29 22:35:03 +01:00
|
|
|
const ObjectID *oid = assigned_ids.getptr(id);
|
|
|
|
if (oid == nullptr) {
|
|
|
|
return; // May be trying to confirm a node that was removed.
|
|
|
|
}
|
2022-02-04 16:24:16 +01:00
|
|
|
|
|
|
|
if (valid_rpc_checksum == false) {
|
2024-03-29 22:35:03 +01:00
|
|
|
const Node *node = Object::cast_to<Node>(ObjectDB::get_instance(*oid));
|
|
|
|
ERR_FAIL_NULL(node); // Bug.
|
|
|
|
ERR_PRINT("The rpc node checksum failed. Make sure to have the same methods on both nodes. Node path: " + node->get_path());
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
2024-03-29 22:35:03 +01:00
|
|
|
NodeCache *cache = nodes_cache.getptr(*oid);
|
|
|
|
ERR_FAIL_NULL(cache); // Bug.
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
bool *confirmed = cache->confirmed_peers.getptr(p_from);
|
|
|
|
ERR_FAIL_NULL_MSG(confirmed, "Invalid packet received. Tries to confirm a node which was not requested.");
|
|
|
|
*confirmed = true;
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
Error SceneCacheInterface::_send_confirm_path(Node *p_node, NodeCache &p_cache, const List<int> &p_peers) {
|
2022-02-21 19:45:18 +01:00
|
|
|
// Encode function name.
|
2023-09-26 20:27:53 +02:00
|
|
|
const CharString path = String(multiplayer->get_root_path().rel_path_to(p_node->get_path())).utf8();
|
2022-02-21 19:45:18 +01:00
|
|
|
const int path_len = encode_cstring(path.get_data(), nullptr);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
// Extract MD5 from rpc methods list.
|
|
|
|
const String methods_md5 = multiplayer->get_rpc_md5(p_node);
|
|
|
|
const int methods_md5_len = 33; // 32 + 1 for the `0` that is added by the encoder.
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
Vector<uint8_t> packet;
|
|
|
|
packet.resize(1 + 4 + path_len + methods_md5_len);
|
|
|
|
int ofs = 0;
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-07-12 23:12:42 +02:00
|
|
|
packet.write[ofs] = SceneMultiplayer::NETWORK_COMMAND_SIMPLIFY_PATH;
|
2022-02-21 19:45:18 +01:00
|
|
|
ofs += 1;
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
ofs += encode_cstring(methods_md5.utf8().get_data(), &packet.write[ofs]);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
ofs += encode_uint32(p_cache.cache_id, &packet.write[ofs]);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
ofs += encode_cstring(path.get_data(), &packet.write[ofs]);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
Ref<MultiplayerPeer> multiplayer_peer = multiplayer->get_multiplayer_peer();
|
|
|
|
ERR_FAIL_COND_V(multiplayer_peer.is_null(), ERR_BUG);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
Error err = OK;
|
|
|
|
for (int peer_id : p_peers) {
|
|
|
|
multiplayer_peer->set_transfer_channel(0);
|
2022-07-12 23:12:42 +02:00
|
|
|
multiplayer_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
|
2022-10-08 20:50:19 +02:00
|
|
|
err = multiplayer->send_command(peer_id, packet.ptr(), packet.size());
|
2022-02-21 19:45:18 +01:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
// Insert into confirmed, but as false since it was not confirmed.
|
2023-12-10 14:40:18 +01:00
|
|
|
p_cache.confirmed_peers.insert(peer_id, false);
|
|
|
|
ERR_CONTINUE(!peers_info.has(peer_id));
|
|
|
|
peers_info[peer_id].sent_nodes.insert(p_node->get_instance_id());
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
2022-02-21 19:45:18 +01:00
|
|
|
return err;
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
2023-09-26 20:27:53 +02:00
|
|
|
bool SceneCacheInterface::is_cache_confirmed(Node *p_node, int p_peer) {
|
|
|
|
ERR_FAIL_NULL_V(p_node, false);
|
2023-12-10 14:40:18 +01:00
|
|
|
const ObjectID oid = p_node->get_instance_id();
|
|
|
|
NodeCache *cache = nodes_cache.getptr(oid);
|
|
|
|
bool *confirmed = cache ? cache->confirmed_peers.getptr(p_peer) : nullptr;
|
|
|
|
return confirmed && *confirmed;
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
2022-07-09 20:45:30 +02:00
|
|
|
int SceneCacheInterface::make_object_cache(Object *p_obj) {
|
2022-02-04 16:24:16 +01:00
|
|
|
Node *node = Object::cast_to<Node>(p_obj);
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL_V(node, -1);
|
2023-12-10 14:40:18 +01:00
|
|
|
NodeCache &cache = _track(node);
|
|
|
|
if (cache.cache_id == 0) {
|
|
|
|
cache.cache_id = last_send_cache_id++;
|
2024-03-29 22:35:03 +01:00
|
|
|
assigned_ids[cache.cache_id] = p_obj->get_instance_id();
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
2023-12-10 14:40:18 +01:00
|
|
|
return cache.cache_id;
|
2022-07-09 20:45:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SceneCacheInterface::send_object_cache(Object *p_obj, int p_peer_id, int &r_id) {
|
|
|
|
Node *node = Object::cast_to<Node>(p_obj);
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL_V(node, false);
|
2023-09-26 20:27:53 +02:00
|
|
|
// See if the path is cached.
|
2023-12-10 14:40:18 +01:00
|
|
|
NodeCache &cache = _track(node);
|
|
|
|
if (cache.cache_id == 0) {
|
|
|
|
cache.cache_id = last_send_cache_id++;
|
2024-03-29 22:35:03 +01:00
|
|
|
assigned_ids[cache.cache_id] = p_obj->get_instance_id();
|
2023-09-26 20:27:53 +02:00
|
|
|
}
|
2023-12-10 14:40:18 +01:00
|
|
|
r_id = cache.cache_id;
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2022-02-21 19:45:18 +01:00
|
|
|
bool has_all_peers = true;
|
|
|
|
List<int> peers_to_add; // If one is missing, take note to add it.
|
|
|
|
|
|
|
|
if (p_peer_id > 0) {
|
|
|
|
// Fast single peer check.
|
2023-12-10 14:40:18 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!peers_info.has(p_peer_id), false, "Peer doesn't exist: " + itos(p_peer_id));
|
|
|
|
|
|
|
|
bool *confirmed = cache.confirmed_peers.getptr(p_peer_id);
|
|
|
|
if (!confirmed) {
|
2022-02-21 19:45:18 +01:00
|
|
|
peers_to_add.push_back(p_peer_id); // Need to also be notified.
|
|
|
|
has_all_peers = false;
|
2023-12-10 14:40:18 +01:00
|
|
|
} else if (!(*confirmed)) {
|
2022-02-21 19:45:18 +01:00
|
|
|
has_all_peers = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Long and painful.
|
2023-12-10 14:40:18 +01:00
|
|
|
for (KeyValue<int, PeerInfo> &E : peers_info) {
|
|
|
|
if (p_peer_id < 0 && E.key == -p_peer_id) {
|
2022-02-21 19:45:18 +01:00
|
|
|
continue; // Continue, excluded.
|
|
|
|
}
|
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
bool *confirmed = cache.confirmed_peers.getptr(E.key);
|
|
|
|
if (!confirmed) {
|
|
|
|
peers_to_add.push_back(E.key); // Need to also be notified.
|
2022-02-21 19:45:18 +01:00
|
|
|
has_all_peers = false;
|
2023-12-10 14:40:18 +01:00
|
|
|
} else if (!(*confirmed)) {
|
2022-02-21 19:45:18 +01:00
|
|
|
has_all_peers = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peers_to_add.size()) {
|
2023-12-10 14:40:18 +01:00
|
|
|
_send_confirm_path(node, cache, peers_to_add);
|
2022-02-21 19:45:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return has_all_peers;
|
2022-02-04 16:24:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Object *SceneCacheInterface::get_cached_object(int p_from, uint32_t p_cache_id) {
|
|
|
|
Node *root_node = SceneTree::get_singleton()->get_root()->get_node(multiplayer->get_root_path());
|
2023-09-09 17:40:07 +02:00
|
|
|
ERR_FAIL_NULL_V(root_node, nullptr);
|
2023-12-10 14:40:18 +01:00
|
|
|
PeerInfo *pinfo = peers_info.getptr(p_from);
|
|
|
|
ERR_FAIL_NULL_V(pinfo, nullptr);
|
2022-02-04 16:24:16 +01:00
|
|
|
|
2023-12-10 14:40:18 +01:00
|
|
|
const ObjectID *oid = pinfo->recv_nodes.getptr(p_cache_id);
|
|
|
|
ERR_FAIL_NULL_V_MSG(oid, nullptr, vformat("ID %d not found in cache of peer %d.", p_cache_id, p_from));
|
|
|
|
Node *node = Object::cast_to<Node>(ObjectDB::get_instance(*oid));
|
|
|
|
ERR_FAIL_NULL_V_MSG(node, nullptr, vformat("Failed to get cached node from peer %d with cache ID %d.", p_from, p_cache_id));
|
2022-02-04 16:24:16 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneCacheInterface::clear() {
|
2023-12-10 14:40:18 +01:00
|
|
|
for (KeyValue<ObjectID, NodeCache> &E : nodes_cache) {
|
|
|
|
Object *obj = ObjectDB::get_instance(E.key);
|
|
|
|
ERR_CONTINUE(!obj);
|
|
|
|
obj->disconnect(SceneStringNames::get_singleton()->tree_exited, callable_mp(this, &SceneCacheInterface::_remove_node_cache));
|
|
|
|
}
|
|
|
|
peers_info.clear();
|
|
|
|
nodes_cache.clear();
|
2024-03-29 22:35:03 +01:00
|
|
|
assigned_ids.clear();
|
2022-02-04 16:24:16 +01:00
|
|
|
last_send_cache_id = 1;
|
|
|
|
}
|