Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/editor/multiplayer_editor_plugin.cpp
10278 views
1
/**************************************************************************/
2
/* multiplayer_editor_plugin.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "multiplayer_editor_plugin.h"
32
33
#include "../multiplayer_synchronizer.h"
34
#include "editor_network_profiler.h"
35
#include "replication_editor.h"
36
37
#include "editor/editor_interface.h"
38
#include "editor/editor_node.h"
39
#include "editor/gui/editor_bottom_panel.h"
40
#include "editor/settings/editor_command_palette.h"
41
42
void MultiplayerEditorDebugger::_bind_methods() {
43
ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));
44
}
45
46
bool MultiplayerEditorDebugger::has_capture(const String &p_capture) const {
47
return p_capture == "multiplayer";
48
}
49
50
void MultiplayerEditorDebugger::_open_request(const String &p_path) {
51
emit_signal("open_request", p_path);
52
}
53
54
bool MultiplayerEditorDebugger::capture(const String &p_message, const Array &p_data, int p_session) {
55
ERR_FAIL_COND_V(!profilers.has(p_session), false);
56
EditorNetworkProfiler *profiler = profilers[p_session];
57
if (p_message == "multiplayer:rpc") {
58
MultiplayerDebugger::RPCFrame frame;
59
frame.deserialize(p_data);
60
for (int i = 0; i < frame.infos.size(); i++) {
61
profiler->add_rpc_frame_data(frame.infos[i]);
62
}
63
return true;
64
} else if (p_message == "multiplayer:syncs") {
65
MultiplayerDebugger::ReplicationFrame frame;
66
frame.deserialize(p_data);
67
for (const KeyValue<ObjectID, MultiplayerDebugger::SyncInfo> &E : frame.infos) {
68
profiler->add_sync_frame_data(E.value);
69
}
70
Array missing = profiler->pop_missing_node_data();
71
if (missing.size()) {
72
// Asks for the object information.
73
get_session(p_session)->send_message("multiplayer:cache", missing);
74
}
75
return true;
76
} else if (p_message == "multiplayer:cache") {
77
ERR_FAIL_COND_V(p_data.size() % 3, false);
78
for (int i = 0; i < p_data.size(); i += 3) {
79
EditorNetworkProfiler::NodeInfo info;
80
info.id = p_data[i].operator ObjectID();
81
info.type = p_data[i + 1].operator String();
82
info.path = p_data[i + 2].operator String();
83
profiler->add_node_data(info);
84
}
85
return true;
86
} else if (p_message == "multiplayer:bandwidth") {
87
ERR_FAIL_COND_V(p_data.size() < 2, false);
88
profiler->set_bandwidth(p_data[0], p_data[1]);
89
return true;
90
}
91
return false;
92
}
93
94
void MultiplayerEditorDebugger::_profiler_activate(bool p_enable, int p_session_id) {
95
Ref<EditorDebuggerSession> session = get_session(p_session_id);
96
ERR_FAIL_COND(session.is_null());
97
session->toggle_profiler("multiplayer:bandwidth", p_enable);
98
session->toggle_profiler("multiplayer:rpc", p_enable);
99
session->toggle_profiler("multiplayer:replication", p_enable);
100
}
101
102
void MultiplayerEditorDebugger::setup_session(int p_session_id) {
103
Ref<EditorDebuggerSession> session = get_session(p_session_id);
104
ERR_FAIL_COND(session.is_null());
105
EditorNetworkProfiler *profiler = memnew(EditorNetworkProfiler);
106
profiler->connect("enable_profiling", callable_mp(this, &MultiplayerEditorDebugger::_profiler_activate).bind(p_session_id));
107
profiler->connect("open_request", callable_mp(this, &MultiplayerEditorDebugger::_open_request));
108
profiler->set_name(TTR("Network Profiler"));
109
session->connect("started", callable_mp(profiler, &EditorNetworkProfiler::started));
110
session->connect("stopped", callable_mp(profiler, &EditorNetworkProfiler::stopped));
111
session->add_session_tab(profiler);
112
profilers[p_session_id] = profiler;
113
}
114
115
/// MultiplayerEditorPlugin
116
117
MultiplayerEditorPlugin::MultiplayerEditorPlugin() {
118
repl_editor = memnew(ReplicationEditor);
119
button = EditorNode::get_bottom_panel()->add_item(TTRC("Replication"), repl_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_replication_bottom_panel", TTRC("Toggle Replication Bottom Panel")));
120
button->hide();
121
repl_editor->get_pin()->connect(SceneStringName(pressed), callable_mp(this, &MultiplayerEditorPlugin::_pinned));
122
debugger.instantiate();
123
debugger->connect("open_request", callable_mp(this, &MultiplayerEditorPlugin::_open_request));
124
}
125
126
void MultiplayerEditorPlugin::_open_request(const String &p_path) {
127
EditorInterface::get_singleton()->open_scene_from_path(p_path);
128
}
129
130
void MultiplayerEditorPlugin::_notification(int p_what) {
131
switch (p_what) {
132
case NOTIFICATION_ENTER_TREE: {
133
get_tree()->connect("node_removed", callable_mp(this, &MultiplayerEditorPlugin::_node_removed));
134
add_debugger_plugin(debugger);
135
} break;
136
case NOTIFICATION_EXIT_TREE: {
137
remove_debugger_plugin(debugger);
138
}
139
}
140
}
141
142
void MultiplayerEditorPlugin::_node_removed(Node *p_node) {
143
if (p_node && p_node == repl_editor->get_current()) {
144
repl_editor->edit(nullptr);
145
if (repl_editor->is_visible_in_tree()) {
146
EditorNode::get_bottom_panel()->hide_bottom_panel();
147
}
148
button->hide();
149
repl_editor->get_pin()->set_pressed(false);
150
}
151
}
152
153
void MultiplayerEditorPlugin::_pinned() {
154
if (!repl_editor->get_pin()->is_pressed() && repl_editor->get_current() == nullptr) {
155
if (repl_editor->is_visible_in_tree()) {
156
EditorNode::get_bottom_panel()->hide_bottom_panel();
157
}
158
button->hide();
159
}
160
}
161
162
void MultiplayerEditorPlugin::edit(Object *p_object) {
163
repl_editor->edit(Object::cast_to<MultiplayerSynchronizer>(p_object));
164
}
165
166
bool MultiplayerEditorPlugin::handles(Object *p_object) const {
167
return p_object->is_class("MultiplayerSynchronizer");
168
}
169
170
void MultiplayerEditorPlugin::make_visible(bool p_visible) {
171
if (p_visible) {
172
button->show();
173
EditorNode::get_bottom_panel()->make_item_visible(repl_editor);
174
} else if (!repl_editor->get_pin()->is_pressed()) {
175
if (repl_editor->is_visible_in_tree()) {
176
EditorNode::get_bottom_panel()->hide_bottom_panel();
177
}
178
button->hide();
179
}
180
}
181
182