summaryrefslogtreecommitdiffstats
path: root/modules/multiplayer/scene_replication_interface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/multiplayer/scene_replication_interface.cpp')
-rw-r--r--modules/multiplayer/scene_replication_interface.cpp72
1 files changed, 39 insertions, 33 deletions
diff --git a/modules/multiplayer/scene_replication_interface.cpp b/modules/multiplayer/scene_replication_interface.cpp
index 7f12fea4bf..c95e4ff9c9 100644
--- a/modules/multiplayer/scene_replication_interface.cpp
+++ b/modules/multiplayer/scene_replication_interface.cpp
@@ -89,6 +89,10 @@ void SceneReplicationInterface::_free_remotes(const PeerInfo &p_info) {
}
}
+bool SceneReplicationInterface::_has_authority(const Node *p_node) {
+ return multiplayer->has_multiplayer_peer() && p_node->get_multiplayer_authority() == multiplayer->get_unique_id();
+}
+
void SceneReplicationInterface::on_peer_change(int p_id, bool p_connected) {
if (p_connected) {
peers_info[p_id] = PeerInfo();
@@ -155,7 +159,7 @@ Error SceneReplicationInterface::on_spawn(Object *p_obj, Variant p_config) {
Node *node = Object::cast_to<Node>(p_obj);
ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(p_config.get_validated_object());
- ERR_FAIL_COND_V(!spawner, ERR_INVALID_PARAMETER);
+ ERR_FAIL_NULL_V(spawner, ERR_INVALID_PARAMETER);
// Track node.
const ObjectID oid = node->get_instance_id();
TrackedNode &tobj = _track(oid);
@@ -184,7 +188,7 @@ void SceneReplicationInterface::_node_ready(const ObjectID &p_oid) {
ERR_CONTINUE(!spawner);
spawned_nodes.insert(oid);
- if (multiplayer->has_multiplayer_peer() && spawner->is_multiplayer_authority()) {
+ if (_has_authority(spawner)) {
if (tobj.net_id == 0) {
tobj.net_id = ++last_net_id;
}
@@ -226,7 +230,7 @@ Error SceneReplicationInterface::on_replication_start(Object *p_obj, Variant p_c
Node *node = Object::cast_to<Node>(p_obj);
ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
MultiplayerSynchronizer *sync = Object::cast_to<MultiplayerSynchronizer>(p_config.get_validated_object());
- ERR_FAIL_COND_V(!sync, ERR_INVALID_PARAMETER);
+ ERR_FAIL_NULL_V(sync, ERR_INVALID_PARAMETER);
// Add to synchronizer list.
TrackedNode &tobj = _track(p_obj->get_instance_id());
@@ -248,9 +252,9 @@ Error SceneReplicationInterface::on_replication_start(Object *p_obj, Variant p_c
// Try to apply spawn state (before ready).
if (pending_buffer_size > 0) {
- ERR_FAIL_COND_V(!node || sync->get_replication_config().is_null(), ERR_UNCONFIGURED);
+ ERR_FAIL_COND_V(!node || !sync->get_replication_config_ptr(), ERR_UNCONFIGURED);
int consumed = 0;
- const List<NodePath> props = sync->get_replication_config()->get_spawn_properties();
+ const List<NodePath> props = sync->get_replication_config_ptr()->get_spawn_properties();
Vector<Variant> vars;
vars.resize(props.size());
Error err = MultiplayerAPI::decode_and_decompress_variants(vars, pending_buffer, pending_buffer_size, consumed);
@@ -270,7 +274,7 @@ Error SceneReplicationInterface::on_replication_stop(Object *p_obj, Variant p_co
Node *node = Object::cast_to<Node>(p_obj);
ERR_FAIL_COND_V(!node || p_config.get_type() != Variant::OBJECT, ERR_INVALID_PARAMETER);
MultiplayerSynchronizer *sync = Object::cast_to<MultiplayerSynchronizer>(p_config.get_validated_object());
- ERR_FAIL_COND_V(!sync, ERR_INVALID_PARAMETER);
+ ERR_FAIL_NULL_V(sync, ERR_INVALID_PARAMETER);
sync->disconnect("visibility_changed", callable_mp(this, &SceneReplicationInterface::_visibility_changed));
// Untrack synchronizer.
const ObjectID oid = node->get_instance_id();
@@ -291,9 +295,9 @@ Error SceneReplicationInterface::on_replication_stop(Object *p_obj, Variant p_co
void SceneReplicationInterface::_visibility_changed(int p_peer, ObjectID p_sid) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(p_sid);
- ERR_FAIL_COND(!sync); // Bug.
+ ERR_FAIL_NULL(sync); // Bug.
Node *node = sync->get_root_node();
- ERR_FAIL_COND(!node); // Bug.
+ ERR_FAIL_NULL(node); // Bug.
const ObjectID oid = node->get_instance_id();
if (spawned_nodes.has(oid) && p_peer != multiplayer->get_unique_id()) {
_update_spawn_visibility(p_peer, oid);
@@ -341,8 +345,8 @@ bool SceneReplicationInterface::is_rpc_visible(const ObjectID &p_oid, int p_peer
}
Error SceneReplicationInterface::_update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync) {
- ERR_FAIL_COND_V(!p_sync, ERR_BUG);
- if (!multiplayer->has_multiplayer_peer() || !p_sync->is_multiplayer_authority() || p_peer == multiplayer->get_unique_id()) {
+ ERR_FAIL_NULL_V(p_sync, ERR_BUG);
+ if (!_has_authority(p_sync) || p_peer == multiplayer->get_unique_id()) {
return OK;
}
@@ -380,17 +384,19 @@ Error SceneReplicationInterface::_update_sync_visibility(int p_peer, Multiplayer
Error SceneReplicationInterface::_update_spawn_visibility(int p_peer, const ObjectID &p_oid) {
const TrackedNode *tnode = tracked_nodes.getptr(p_oid);
- ERR_FAIL_COND_V(!tnode, ERR_BUG);
+ ERR_FAIL_NULL_V(tnode, ERR_BUG);
MultiplayerSpawner *spawner = get_id_as<MultiplayerSpawner>(tnode->spawner);
Node *node = get_id_as<Node>(p_oid);
- ERR_FAIL_COND_V(!node || !spawner || !spawner->is_multiplayer_authority(), ERR_BUG);
+ ERR_FAIL_NULL_V(node, ERR_BUG);
+ ERR_FAIL_NULL_V(spawner, ERR_BUG);
+ ERR_FAIL_COND_V(!_has_authority(spawner), ERR_BUG);
ERR_FAIL_COND_V(!tracked_nodes.has(p_oid), ERR_BUG);
const HashSet<ObjectID> synchronizers = tracked_nodes[p_oid].synchronizers;
bool is_visible = true;
for (const ObjectID &sid : synchronizers) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(sid);
ERR_CONTINUE(!sync);
- if (!sync->is_multiplayer_authority()) {
+ if (!_has_authority(sync)) {
continue;
}
// Spawn visibility is composed using OR when multiple synchronizers are present.
@@ -435,7 +441,7 @@ Error SceneReplicationInterface::_update_spawn_visibility(int p_peer, const Obje
for (int pid : to_spawn) {
ERR_CONTINUE(!peers_info.has(pid));
int path_id;
- multiplayer->get_path_cache()->send_object_cache(spawner, pid, path_id);
+ multiplayer_cache->send_object_cache(spawner, pid, path_id);
_send_raw(packet_cache.ptr(), len, pid, true);
peers_info[pid].spawn_nodes.insert(p_oid);
}
@@ -454,9 +460,9 @@ Error SceneReplicationInterface::_update_spawn_visibility(int p_peer, const Obje
Error SceneReplicationInterface::_send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable) {
ERR_FAIL_COND_V(!p_buffer || p_size < 1, ERR_INVALID_PARAMETER);
- ERR_FAIL_COND_V(!multiplayer->has_multiplayer_peer(), ERR_UNCONFIGURED);
Ref<MultiplayerPeer> peer = multiplayer->get_multiplayer_peer();
+ ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED);
peer->set_transfer_channel(0);
peer->set_transfer_mode(p_reliable ? MultiplayerPeer::TRANSFER_MODE_RELIABLE : MultiplayerPeer::TRANSFER_MODE_UNRELIABLE);
return multiplayer->send_command(p_peer, p_buffer, p_size);
@@ -467,7 +473,7 @@ Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpa
const ObjectID oid = p_node->get_instance_id();
const TrackedNode *tnode = tracked_nodes.getptr(oid);
- ERR_FAIL_COND_V(!tnode, ERR_INVALID_PARAMETER);
+ ERR_FAIL_NULL_V(tnode, ERR_INVALID_PARAMETER);
uint32_t nid = tnode->net_id;
ERR_FAIL_COND_V(!nid, ERR_UNCONFIGURED);
@@ -488,12 +494,12 @@ Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpa
const HashSet<ObjectID> synchronizers = tnode->synchronizers;
for (const ObjectID &sid : synchronizers) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(sid);
- if (!sync->is_multiplayer_authority()) {
+ if (!_has_authority(sync)) {
continue;
}
ERR_CONTINUE(!sync);
- ERR_FAIL_COND_V(sync->get_replication_config().is_null(), ERR_BUG);
- for (const NodePath &prop : sync->get_replication_config()->get_spawn_properties()) {
+ ERR_FAIL_NULL_V(sync->get_replication_config_ptr(), ERR_BUG);
+ for (const NodePath &prop : sync->get_replication_config_ptr()->get_spawn_properties()) {
state_props.push_back(prop);
}
// Ensure the synchronizer has an ID.
@@ -513,7 +519,7 @@ Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpa
}
// Encode scene ID, path ID, net ID, node name.
- int path_id = multiplayer->get_path_cache()->make_object_cache(p_spawner);
+ int path_id = multiplayer_cache->make_object_cache(p_spawner);
CharString cname = p_node->get_name().operator String().utf8();
int nlen = encode_cstring(cname.get_data(), nullptr);
MAKE_ROOM(1 + 1 + 4 + 4 + 4 + 4 * sync_ids.size() + 4 + nlen + (is_custom ? 4 + spawn_arg_size : 0) + state_size);
@@ -549,7 +555,7 @@ Error SceneReplicationInterface::_make_spawn_packet(Node *p_node, MultiplayerSpa
Error SceneReplicationInterface::_make_despawn_packet(Node *p_node, int &r_len) {
const ObjectID oid = p_node->get_instance_id();
const TrackedNode *tnode = tracked_nodes.getptr(oid);
- ERR_FAIL_COND_V(!tnode, ERR_INVALID_PARAMETER);
+ ERR_FAIL_NULL_V(tnode, ERR_INVALID_PARAMETER);
MAKE_ROOM(5);
uint8_t *ptr = packet_cache.ptrw();
ptr[0] = (uint8_t)SceneMultiplayer::NETWORK_COMMAND_DESPAWN;
@@ -567,8 +573,8 @@ Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_b
ofs += 1;
uint32_t node_target = decode_uint32(&p_buffer[ofs]);
ofs += 4;
- MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(multiplayer->get_path_cache()->get_cached_object(p_from, node_target));
- ERR_FAIL_COND_V(!spawner, ERR_DOES_NOT_EXIST);
+ MultiplayerSpawner *spawner = Object::cast_to<MultiplayerSpawner>(multiplayer_cache->get_cached_object(p_from, node_target));
+ ERR_FAIL_NULL_V(spawner, ERR_DOES_NOT_EXIST);
ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
uint32_t net_id = decode_uint32(&p_buffer[ofs]);
@@ -592,7 +598,7 @@ Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_b
// Check that we can spawn.
Node *parent = spawner->get_node_or_null(spawner->get_spawn_path());
- ERR_FAIL_COND_V(!parent, ERR_UNCONFIGURED);
+ ERR_FAIL_NULL_V(parent, ERR_UNCONFIGURED);
ERR_FAIL_COND_V(parent->has_node(name), ERR_INVALID_DATA);
Node *node = nullptr;
@@ -611,7 +617,7 @@ Error SceneReplicationInterface::on_spawn_receive(int p_from, const uint8_t *p_b
// Scene based spawn.
node = spawner->instantiate_scene(scene_id);
}
- ERR_FAIL_COND_V(!node, ERR_UNAUTHORIZED);
+ ERR_FAIL_NULL_V(node, ERR_UNAUTHORIZED);
node->set_name(name);
// Add and track remote
@@ -656,13 +662,13 @@ Error SceneReplicationInterface::on_despawn_receive(int p_from, const uint8_t *p
PeerInfo &pinfo = peers_info[p_from];
ERR_FAIL_COND_V(!pinfo.recv_nodes.has(net_id), ERR_UNAUTHORIZED);
Node *node = get_id_as<Node>(pinfo.recv_nodes[net_id]);
- ERR_FAIL_COND_V(!node, ERR_BUG);
+ ERR_FAIL_NULL_V(node, ERR_BUG);
pinfo.recv_nodes.erase(net_id);
const ObjectID oid = node->get_instance_id();
ERR_FAIL_COND_V(!tracked_nodes.has(oid), ERR_BUG);
MultiplayerSpawner *spawner = get_id_as<MultiplayerSpawner>(tracked_nodes[oid].spawner);
- ERR_FAIL_COND_V(!spawner, ERR_DOES_NOT_EXIST);
+ ERR_FAIL_NULL_V(spawner, ERR_DOES_NOT_EXIST);
ERR_FAIL_COND_V(p_from != spawner->get_multiplayer_authority(), ERR_UNAUTHORIZED);
if (node->get_parent() != nullptr) {
@@ -678,7 +684,7 @@ bool SceneReplicationInterface::_verify_synchronizer(int p_peer, MultiplayerSync
r_net_id = p_sync->get_net_id();
if (r_net_id == 0 || (r_net_id & 0x80000000)) {
int path_id = 0;
- bool verified = multiplayer->get_path_cache()->send_object_cache(p_sync, p_peer, path_id);
+ bool verified = multiplayer_cache->send_object_cache(p_sync, p_peer, path_id);
ERR_FAIL_COND_V_MSG(path_id < 0, false, "This should never happen!");
if (r_net_id == 0) {
// First time path based ID.
@@ -693,7 +699,7 @@ bool SceneReplicationInterface::_verify_synchronizer(int p_peer, MultiplayerSync
MultiplayerSynchronizer *SceneReplicationInterface::_find_synchronizer(int p_peer, uint32_t p_net_id) {
MultiplayerSynchronizer *sync = nullptr;
if (p_net_id & 0x80000000) {
- sync = Object::cast_to<MultiplayerSynchronizer>(multiplayer->get_path_cache()->get_cached_object(p_peer, p_net_id & 0x7FFFFFFF));
+ sync = Object::cast_to<MultiplayerSynchronizer>(multiplayer_cache->get_cached_object(p_peer, p_net_id & 0x7FFFFFFF));
} else if (peers_info[p_peer].recv_sync_ids.has(p_net_id)) {
const ObjectID &sid = peers_info[p_peer].recv_sync_ids[p_net_id];
sync = get_id_as<MultiplayerSynchronizer>(sid);
@@ -708,7 +714,7 @@ void SceneReplicationInterface::_send_delta(int p_peer, const HashSet<ObjectID>
int ofs = 1;
for (const ObjectID &oid : p_synchronizers) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
- ERR_CONTINUE(!sync || !sync->get_replication_config().is_valid() || !sync->is_multiplayer_authority());
+ ERR_CONTINUE(!sync || !sync->get_replication_config_ptr() || !_has_authority(sync));
uint32_t net_id;
if (!_verify_synchronizer(p_peer, sync, net_id)) {
continue;
@@ -803,7 +809,7 @@ void SceneReplicationInterface::_send_sync(int p_peer, const HashSet<ObjectID> p
// This is a lazy implementation, we could optimize much more here with by grouping by replication config.
for (const ObjectID &oid : p_synchronizers) {
MultiplayerSynchronizer *sync = get_id_as<MultiplayerSynchronizer>(oid);
- ERR_CONTINUE(!sync || !sync->get_replication_config().is_valid() || !sync->is_multiplayer_authority());
+ ERR_CONTINUE(!sync || !sync->get_replication_config_ptr() || !_has_authority(sync));
if (!sync->update_outbound_sync_time(p_usec)) {
continue; // nothing to sync.
}
@@ -818,7 +824,7 @@ void SceneReplicationInterface::_send_sync(int p_peer, const HashSet<ObjectID> p
int size;
Vector<Variant> vars;
Vector<const Variant *> varp;
- const List<NodePath> props = sync->get_replication_config()->get_sync_properties();
+ const List<NodePath> props = sync->get_replication_config_ptr()->get_sync_properties();
Error err = MultiplayerSynchronizer::get_state(props, node, vars, varp);
ERR_CONTINUE_MSG(err != OK, "Unable to retrieve sync state.");
err = MultiplayerAPI::encode_and_compress_variants(varp.ptrw(), varp.size(), nullptr, size);
@@ -877,7 +883,7 @@ Error SceneReplicationInterface::on_sync_receive(int p_from, const uint8_t *p_bu
ofs += size;
continue;
}
- const List<NodePath> props = sync->get_replication_config()->get_sync_properties();
+ const List<NodePath> props = sync->get_replication_config_ptr()->get_sync_properties();
Vector<Variant> vars;
vars.resize(props.size());
int consumed;