Skip to content

Commit

Permalink
rewrite multiplayer spawner and synchronizer, making it a module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Franke committed Jul 10, 2022
1 parent d26442e commit 5ebf7af
Show file tree
Hide file tree
Showing 27 changed files with 921 additions and 1,528 deletions.
36 changes: 0 additions & 36 deletions core/multiplayer/multiplayer_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "core/os/os.h"
#endif

MultiplayerReplicationInterface *(*MultiplayerAPI::create_default_replication_interface)(MultiplayerAPI *p_multiplayer) = nullptr;
MultiplayerRPCInterface *(*MultiplayerAPI::create_default_rpc_interface)(MultiplayerAPI *p_multiplayer) = nullptr;
MultiplayerCacheInterface *(*MultiplayerAPI::create_default_cache_interface)(MultiplayerAPI *p_multiplayer) = nullptr;

Expand Down Expand Up @@ -85,7 +84,6 @@ void MultiplayerAPI::poll() {
return; // It's also possible that a packet or RPC caused a disconnection, so also check here.
}
}
replicator->on_network_process();
}

void MultiplayerAPI::clear() {
Expand Down Expand Up @@ -129,7 +127,6 @@ void MultiplayerAPI::set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) {
multiplayer_peer->connect("connection_failed", callable_mp(this, &MultiplayerAPI::_connection_failed));
multiplayer_peer->connect("server_disconnected", callable_mp(this, &MultiplayerAPI::_server_disconnected));
}
replicator->on_reset();
}

Ref<MultiplayerPeer> MultiplayerAPI::get_multiplayer_peer() const {
Expand Down Expand Up @@ -163,15 +160,6 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_
case NETWORK_COMMAND_RAW: {
_process_raw(p_from, p_packet, p_packet_len);
} break;
case NETWORK_COMMAND_SPAWN: {
replicator->on_spawn_receive(p_from, p_packet, p_packet_len);
} break;
case NETWORK_COMMAND_DESPAWN: {
replicator->on_despawn_receive(p_from, p_packet, p_packet_len);
} break;
case NETWORK_COMMAND_SYNC: {
replicator->on_sync_receive(p_from, p_packet, p_packet_len);
} break;
}
}

Expand Down Expand Up @@ -402,12 +390,10 @@ Error MultiplayerAPI::decode_and_decompress_variants(Vector<Variant> &r_variants
void MultiplayerAPI::_add_peer(int p_id) {
connected_peers.insert(p_id);
cache->on_peer_change(p_id, true);
replicator->on_peer_change(p_id, true);
emit_signal(SNAME("peer_connected"), p_id);
}

void MultiplayerAPI::_del_peer(int p_id) {
replicator->on_peer_change(p_id, false);
cache->on_peer_change(p_id, false);
connected_peers.erase(p_id);
emit_signal(SNAME("peer_disconnected"), p_id);
Expand All @@ -422,7 +408,6 @@ void MultiplayerAPI::_connection_failed() {
}

void MultiplayerAPI::_server_disconnected() {
replicator->on_reset();
emit_signal(SNAME("server_disconnected"));
}

Expand Down Expand Up @@ -517,22 +502,6 @@ void MultiplayerAPI::rpcp(Object *p_obj, int p_peer_id, const StringName &p_meth
rpc->rpcp(p_obj, p_peer_id, p_method, p_arg, p_argcount);
}

Error MultiplayerAPI::spawn(Object *p_object, Variant p_config) {
return replicator->on_spawn(p_object, p_config);
}

Error MultiplayerAPI::despawn(Object *p_object, Variant p_config) {
return replicator->on_despawn(p_object, p_config);
}

Error MultiplayerAPI::replication_start(Object *p_object, Variant p_config) {
return replicator->on_replication_start(p_object, p_config);
}

Error MultiplayerAPI::replication_stop(Object *p_object, Variant p_config) {
return replicator->on_replication_stop(p_object, p_config);
}

void MultiplayerAPI::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_root_path", "path"), &MultiplayerAPI::set_root_path);
ClassDB::bind_method(D_METHOD("get_root_path"), &MultiplayerAPI::get_root_path);
Expand Down Expand Up @@ -567,11 +536,6 @@ void MultiplayerAPI::_bind_methods() {
}

MultiplayerAPI::MultiplayerAPI() {
if (create_default_replication_interface) {
replicator = Ref<MultiplayerReplicationInterface>(create_default_replication_interface(this));
} else {
replicator.instantiate();
}
if (create_default_rpc_interface) {
rpc = Ref<MultiplayerRPCInterface>(create_default_rpc_interface(this));
} else {
Expand Down
28 changes: 0 additions & 28 deletions core/multiplayer/multiplayer_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@

class MultiplayerAPI;

class MultiplayerReplicationInterface : public RefCounted {
GDCLASS(MultiplayerReplicationInterface, RefCounted);

public:
virtual void on_peer_change(int p_id, bool p_connected) {}
virtual void on_reset() {}
virtual Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) { return ERR_UNAVAILABLE; }
virtual Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) { return ERR_UNAVAILABLE; }
virtual Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len) { return ERR_UNAVAILABLE; }
virtual Error on_spawn(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
virtual Error on_despawn(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
virtual Error on_replication_start(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
virtual Error on_replication_stop(Object *p_obj, Variant p_config) { return ERR_UNAVAILABLE; }
virtual void on_network_process() {}

MultiplayerReplicationInterface() {}
};

class MultiplayerRPCInterface : public RefCounted {
GDCLASS(MultiplayerRPCInterface, RefCounted);

Expand Down Expand Up @@ -93,9 +75,6 @@ class MultiplayerAPI : public RefCounted {
NETWORK_COMMAND_SIMPLIFY_PATH,
NETWORK_COMMAND_CONFIRM_PATH,
NETWORK_COMMAND_RAW,
NETWORK_COMMAND_SPAWN,
NETWORK_COMMAND_DESPAWN,
NETWORK_COMMAND_SYNC,
};

// For each command, the 4 MSB can contain custom flags, as defined by subsystems.
Expand Down Expand Up @@ -123,7 +102,6 @@ class MultiplayerAPI : public RefCounted {
bool allow_object_decoding = false;

Ref<MultiplayerCacheInterface> cache;
Ref<MultiplayerReplicationInterface> replicator;
Ref<MultiplayerRPCInterface> rpc;

protected:
Expand All @@ -133,7 +111,6 @@ class MultiplayerAPI : public RefCounted {
void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);

public:
static MultiplayerReplicationInterface *(*create_default_replication_interface)(MultiplayerAPI *p_multiplayer);
static MultiplayerRPCInterface *(*create_default_rpc_interface)(MultiplayerAPI *p_multiplayer);
static MultiplayerCacheInterface *(*create_default_cache_interface)(MultiplayerAPI *p_multiplayer);

Expand All @@ -154,11 +131,6 @@ class MultiplayerAPI : public RefCounted {
// RPC API
void rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
String get_rpc_md5(const Object *p_obj) const;
// Replication API
Error spawn(Object *p_object, Variant p_config);
Error despawn(Object *p_object, Variant p_config);
Error replication_start(Object *p_object, Variant p_config);
Error replication_stop(Object *p_object, Variant p_config);
// Cache API
bool send_object_cache(Object *p_obj, NodePath p_path, int p_target, int &p_id);
Object *get_cached_object(int p_from, uint32_t p_cache_id);
Expand Down
67 changes: 0 additions & 67 deletions doc/classes/MultiplayerSpawner.xml

This file was deleted.

2 changes: 0 additions & 2 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@
#include "editor/plugins/polygon_2d_editor_plugin.h"
#include "editor/plugins/polygon_3d_editor_plugin.h"
#include "editor/plugins/ray_cast_2d_editor_plugin.h"
#include "editor/plugins/replication_editor_plugin.h"
#include "editor/plugins/resource_preloader_editor_plugin.h"
#include "editor/plugins/root_motion_editor_plugin.h"
#include "editor/plugins/script_editor_plugin.h"
Expand Down Expand Up @@ -7065,7 +7064,6 @@ EditorNode::EditorNode() {

// More visually meaningful to have this later.
raise_bottom_panel_item(AnimationPlayerEditor::get_singleton());
add_editor_plugin(memnew(ReplicationEditorPlugin));

add_editor_plugin(VersionControlEditorPlugin::get_singleton());
add_editor_plugin(memnew(ShaderEditorPlugin));
Expand Down
9 changes: 9 additions & 0 deletions modules/replication/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python

Import("env")
Import("env_modules")

env_replication = env_modules.Clone()

env_replication.add_source_files(env.modules_sources, "editor/*.cpp")
env_replication.add_source_files(env.modules_sources, "*.cpp")
18 changes: 18 additions & 0 deletions modules/replication/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def can_build(env, platform):
return True


def configure(env):
pass


def get_doc_classes():
return [
"MultiplayerSpawner",
"MultiplayerSynchronizer",
"SceneReplicationConfig",
]


def get_doc_path():
return "doc_classes"
54 changes: 54 additions & 0 deletions modules/replication/doc_classes/MultiplayerSpawner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="MultiplayerSpawner" inherits="Node" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<methods>
<method name="_spawn_custom" qualifiers="virtual">
<return type="Node" />
<argument index="0" name="custom_data" type="Variant" />
<description>
Virtual method called when spawning a custom node, either on the multiplayer authority or on a puppet.

Do not add custom nodes to the scene, as this is done automatically.
</description>
</method>
<method name="spawn_custom">
<return type="Node" />
<argument index="0" name="custom_data" type="Variant" default="null" />
<description>
Request, as the multiplayer authority, to spawn a node on all clients. Calls [method _spawn_custom] internally.
</description>
</method>
</methods>
<members>
<member name="spawn_limit" type="int" setter="set_spawn_limit" getter="get_spawn_limit" default="0">
Maxmium node count that can be spawned. Includes both custom spawns and spawned scenes. A value of 0 means no limit.
</member>
<member name="spawn_path" type="NodePath" setter="set_spawn_path" getter="get_spawn_path" default="NodePath(&quot;..&quot;)">
Parent node that listens for spawnable children. Custom spawned nodes are added here too.
</member>
<member name="spawnable_scenes" type="Array" setter="set_spawnable_scenes" getter="get_spawnable_scenes" default="[]">
Array of scenes that, when added as a child of [member spawn_path], are automatically spawned on multiplayer puppets.
</member>
</members>
<signals>
<signal name="despawned">
<argument index="0" name="scene_index" type="int" />
<argument index="1" name="node" type="Node" />
<description>
Signal emitted after the multiplayer authority despawns a scene instance. Not called for custom spawns.
</description>
</signal>
<signal name="spawned">
<argument index="0" name="scene_index" type="int" />
<argument index="1" name="node" type="Node" />
<description>
Signal emitted after the multiplayer authority spawns a scene instance. Not called for custom spawns.
</description>
</signal>
</signals>
</class>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="MultiplayerSynchronizer" inherits="Node" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="MultiplayerSynchronizer" inherits="Node" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="SceneReplicationConfig" inherits="Resource" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<class name="SceneReplicationConfig" inherits="Resource" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@

#include "replication_editor_plugin.h"

#ifdef TOOLS_ENABLED

#include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "editor/inspector_dock.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/tree.h"
#include "scene/multiplayer/multiplayer_synchronizer.h"

#include "../multiplayer_synchronizer.h"

void ReplicationEditor::_pick_node_filter_text_changed(const String &p_newtext) {
TreeItem *root_item = pick_node->get_scene_tree()->get_scene_tree()->get_root();
Expand Down Expand Up @@ -643,3 +646,5 @@ void ReplicationEditorPlugin::make_visible(bool p_visible) {
button->hide();
}
}

#endif // TOOLS_ENABLED
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
#ifndef REPLICATION_EDITOR_PLUGIN_H
#define REPLICATION_EDITOR_PLUGIN_H

#include "editor/editor_plugin.h"
#include "scene/resources/scene_replication_config.h"
#ifdef TOOLS_ENABLED

#include "editor/editor_plugin.h"
#include "editor/editor_spin_slider.h"
#include "editor/property_editor.h"
#include "editor/property_selector.h"

#include "../scene_replication_config.h"

class ConfirmationDialog;
class MultiplayerSynchronizer;
class Tree;
Expand Down Expand Up @@ -132,4 +134,6 @@ class ReplicationEditorPlugin : public EditorPlugin {
~ReplicationEditorPlugin();
};

#endif // TOOLS_ENABLED

#endif // REPLICATION_EDITOR_PLUGIN_H
File renamed without changes
File renamed without changes
Loading

0 comments on commit 5ebf7af

Please sign in to comment.