| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  multiplayer_editor_plugin.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 "multiplayer_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../multiplayer_synchronizer.h"
 | 
					
						
							|  |  |  | #include "editor_network_profiler.h"
 | 
					
						
							|  |  |  | #include "replication_editor.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-08 00:08:07 +01:00
										 |  |  | #include "editor/editor_command_palette.h"
 | 
					
						
							| 
									
										
										
										
											2023-04-05 17:14:46 +02:00
										 |  |  | #include "editor/editor_interface.h"
 | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2024-01-30 17:22:22 -05:00
										 |  |  | #include "editor/gui/editor_bottom_panel.h"
 | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | void MultiplayerEditorDebugger::_bind_methods() { | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path"))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | bool MultiplayerEditorDebugger::has_capture(const String &p_capture) const { | 
					
						
							|  |  |  | 	return p_capture == "multiplayer"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | void MultiplayerEditorDebugger::_open_request(const String &p_path) { | 
					
						
							|  |  |  | 	emit_signal("open_request", p_path); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | bool MultiplayerEditorDebugger::capture(const String &p_message, const Array &p_data, int p_session) { | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(!profilers.has(p_session), false); | 
					
						
							|  |  |  | 	EditorNetworkProfiler *profiler = profilers[p_session]; | 
					
						
							|  |  |  | 	if (p_message == "multiplayer:rpc") { | 
					
						
							|  |  |  | 		MultiplayerDebugger::RPCFrame frame; | 
					
						
							|  |  |  | 		frame.deserialize(p_data); | 
					
						
							|  |  |  | 		for (int i = 0; i < frame.infos.size(); i++) { | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | 			profiler->add_rpc_frame_data(frame.infos[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else if (p_message == "multiplayer:syncs") { | 
					
						
							|  |  |  | 		MultiplayerDebugger::ReplicationFrame frame; | 
					
						
							|  |  |  | 		frame.deserialize(p_data); | 
					
						
							|  |  |  | 		for (const KeyValue<ObjectID, MultiplayerDebugger::SyncInfo> &E : frame.infos) { | 
					
						
							|  |  |  | 			profiler->add_sync_frame_data(E.value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		Array missing = profiler->pop_missing_node_data(); | 
					
						
							|  |  |  | 		if (missing.size()) { | 
					
						
							|  |  |  | 			// Asks for the object information.
 | 
					
						
							|  |  |  | 			get_session(p_session)->send_message("multiplayer:cache", missing); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else if (p_message == "multiplayer:cache") { | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(p_data.size() % 3, false); | 
					
						
							|  |  |  | 		for (int i = 0; i < p_data.size(); i += 3) { | 
					
						
							|  |  |  | 			EditorNetworkProfiler::NodeInfo info; | 
					
						
							|  |  |  | 			info.id = p_data[i].operator ObjectID(); | 
					
						
							|  |  |  | 			info.type = p_data[i + 1].operator String(); | 
					
						
							|  |  |  | 			info.path = p_data[i + 2].operator String(); | 
					
						
							|  |  |  | 			profiler->add_node_data(info); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} else if (p_message == "multiplayer:bandwidth") { | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(p_data.size() < 2, false); | 
					
						
							|  |  |  | 		profiler->set_bandwidth(p_data[0], p_data[1]); | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorDebugger::_profiler_activate(bool p_enable, int p_session_id) { | 
					
						
							|  |  |  | 	Ref<EditorDebuggerSession> session = get_session(p_session_id); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(session.is_null()); | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | 	session->toggle_profiler("multiplayer:bandwidth", p_enable); | 
					
						
							|  |  |  | 	session->toggle_profiler("multiplayer:rpc", p_enable); | 
					
						
							|  |  |  | 	session->toggle_profiler("multiplayer:replication", p_enable); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorDebugger::setup_session(int p_session_id) { | 
					
						
							|  |  |  | 	Ref<EditorDebuggerSession> session = get_session(p_session_id); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(session.is_null()); | 
					
						
							|  |  |  | 	EditorNetworkProfiler *profiler = memnew(EditorNetworkProfiler); | 
					
						
							|  |  |  | 	profiler->connect("enable_profiling", callable_mp(this, &MultiplayerEditorDebugger::_profiler_activate).bind(p_session_id)); | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | 	profiler->connect("open_request", callable_mp(this, &MultiplayerEditorDebugger::_open_request)); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 	profiler->set_name(TTR("Network Profiler")); | 
					
						
							| 
									
										
										
										
											2024-09-10 19:40:42 +02:00
										 |  |  | 	session->connect("started", callable_mp(profiler, &EditorNetworkProfiler::started)); | 
					
						
							|  |  |  | 	session->connect("stopped", callable_mp(profiler, &EditorNetworkProfiler::stopped)); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 	session->add_session_tab(profiler); | 
					
						
							|  |  |  | 	profilers[p_session_id] = profiler; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | /// MultiplayerEditorPlugin
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | MultiplayerEditorPlugin::MultiplayerEditorPlugin() { | 
					
						
							|  |  |  | 	repl_editor = memnew(ReplicationEditor); | 
					
						
							| 
									
										
										
										
											2024-10-18 19:07:44 +08:00
										 |  |  | 	button = EditorNode::get_bottom_panel()->add_item(TTR("Replication"), repl_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_replication_bottom_panel", TTRC("Toggle Replication Bottom Panel"))); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 	button->hide(); | 
					
						
							| 
									
										
										
										
											2024-05-14 09:40:21 +02:00
										 |  |  | 	repl_editor->get_pin()->connect(SceneStringName(pressed), callable_mp(this, &MultiplayerEditorPlugin::_pinned)); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 	debugger.instantiate(); | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | 	debugger->connect("open_request", callable_mp(this, &MultiplayerEditorPlugin::_open_request)); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 03:31:23 +01:00
										 |  |  | void MultiplayerEditorPlugin::_open_request(const String &p_path) { | 
					
						
							| 
									
										
										
										
											2023-04-05 17:14:46 +02:00
										 |  |  | 	EditorInterface::get_singleton()->open_scene_from_path(p_path); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorPlugin::_notification(int p_what) { | 
					
						
							|  |  |  | 	switch (p_what) { | 
					
						
							|  |  |  | 		case NOTIFICATION_ENTER_TREE: { | 
					
						
							|  |  |  | 			get_tree()->connect("node_removed", callable_mp(this, &MultiplayerEditorPlugin::_node_removed)); | 
					
						
							|  |  |  | 			add_debugger_plugin(debugger); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case NOTIFICATION_EXIT_TREE: { | 
					
						
							|  |  |  | 			remove_debugger_plugin(debugger); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorPlugin::_node_removed(Node *p_node) { | 
					
						
							|  |  |  | 	if (p_node && p_node == repl_editor->get_current()) { | 
					
						
							|  |  |  | 		repl_editor->edit(nullptr); | 
					
						
							|  |  |  | 		if (repl_editor->is_visible_in_tree()) { | 
					
						
							| 
									
										
										
										
											2024-01-30 17:22:22 -05:00
										 |  |  | 			EditorNode::get_bottom_panel()->hide_bottom_panel(); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		button->hide(); | 
					
						
							|  |  |  | 		repl_editor->get_pin()->set_pressed(false); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorPlugin::_pinned() { | 
					
						
							| 
									
										
										
										
											2024-03-25 23:12:26 +08:00
										 |  |  | 	if (!repl_editor->get_pin()->is_pressed() && repl_editor->get_current() == nullptr) { | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 		if (repl_editor->is_visible_in_tree()) { | 
					
						
							| 
									
										
										
										
											2024-01-30 17:22:22 -05:00
										 |  |  | 			EditorNode::get_bottom_panel()->hide_bottom_panel(); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		button->hide(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 	repl_editor->edit(Object::cast_to<MultiplayerSynchronizer>(p_object)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool MultiplayerEditorPlugin::handles(Object *p_object) const { | 
					
						
							|  |  |  | 	return p_object->is_class("MultiplayerSynchronizer"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MultiplayerEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							|  |  |  | 		button->show(); | 
					
						
							| 
									
										
										
										
											2024-01-30 17:22:22 -05:00
										 |  |  | 		EditorNode::get_bottom_panel()->make_item_visible(repl_editor); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 	} else if (!repl_editor->get_pin()->is_pressed()) { | 
					
						
							|  |  |  | 		if (repl_editor->is_visible_in_tree()) { | 
					
						
							| 
									
										
										
										
											2024-01-30 17:22:22 -05:00
										 |  |  | 			EditorNode::get_bottom_panel()->hide_bottom_panel(); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:50:19 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		button->hide(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |