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