multiplayer_editor_plugin.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* multiplayer_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "multiplayer_editor_plugin.h"
  31. #include "../multiplayer_synchronizer.h"
  32. #include "editor_network_profiler.h"
  33. #include "replication_editor.h"
  34. #include "editor/editor_node.h"
  35. bool MultiplayerEditorDebugger::has_capture(const String &p_capture) const {
  36. return p_capture == "multiplayer";
  37. }
  38. bool MultiplayerEditorDebugger::capture(const String &p_message, const Array &p_data, int p_session) {
  39. ERR_FAIL_COND_V(!profilers.has(p_session), false);
  40. EditorNetworkProfiler *profiler = profilers[p_session];
  41. if (p_message == "multiplayer:rpc") {
  42. MultiplayerDebugger::RPCFrame frame;
  43. frame.deserialize(p_data);
  44. for (int i = 0; i < frame.infos.size(); i++) {
  45. profiler->add_node_frame_data(frame.infos[i]);
  46. }
  47. return true;
  48. } else if (p_message == "multiplayer:bandwidth") {
  49. ERR_FAIL_COND_V(p_data.size() < 2, false);
  50. profiler->set_bandwidth(p_data[0], p_data[1]);
  51. return true;
  52. }
  53. return false;
  54. }
  55. void MultiplayerEditorDebugger::_profiler_activate(bool p_enable, int p_session_id) {
  56. Ref<EditorDebuggerSession> session = get_session(p_session_id);
  57. ERR_FAIL_COND(session.is_null());
  58. session->toggle_profiler("multiplayer", p_enable);
  59. session->toggle_profiler("rpc", p_enable);
  60. }
  61. void MultiplayerEditorDebugger::setup_session(int p_session_id) {
  62. Ref<EditorDebuggerSession> session = get_session(p_session_id);
  63. ERR_FAIL_COND(session.is_null());
  64. EditorNetworkProfiler *profiler = memnew(EditorNetworkProfiler);
  65. profiler->connect("enable_profiling", callable_mp(this, &MultiplayerEditorDebugger::_profiler_activate).bind(p_session_id));
  66. profiler->set_name(TTR("Network Profiler"));
  67. session->add_session_tab(profiler);
  68. profilers[p_session_id] = profiler;
  69. }
  70. MultiplayerEditorPlugin::MultiplayerEditorPlugin() {
  71. repl_editor = memnew(ReplicationEditor);
  72. button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Replication"), repl_editor);
  73. button->hide();
  74. repl_editor->get_pin()->connect("pressed", callable_mp(this, &MultiplayerEditorPlugin::_pinned));
  75. debugger.instantiate();
  76. }
  77. MultiplayerEditorPlugin::~MultiplayerEditorPlugin() {
  78. }
  79. void MultiplayerEditorPlugin::_notification(int p_what) {
  80. switch (p_what) {
  81. case NOTIFICATION_ENTER_TREE: {
  82. get_tree()->connect("node_removed", callable_mp(this, &MultiplayerEditorPlugin::_node_removed));
  83. add_debugger_plugin(debugger);
  84. } break;
  85. case NOTIFICATION_EXIT_TREE: {
  86. remove_debugger_plugin(debugger);
  87. }
  88. }
  89. }
  90. void MultiplayerEditorPlugin::_node_removed(Node *p_node) {
  91. if (p_node && p_node == repl_editor->get_current()) {
  92. repl_editor->edit(nullptr);
  93. if (repl_editor->is_visible_in_tree()) {
  94. EditorNode::get_singleton()->hide_bottom_panel();
  95. }
  96. button->hide();
  97. repl_editor->get_pin()->set_pressed(false);
  98. }
  99. }
  100. void MultiplayerEditorPlugin::_pinned() {
  101. if (!repl_editor->get_pin()->is_pressed()) {
  102. if (repl_editor->is_visible_in_tree()) {
  103. EditorNode::get_singleton()->hide_bottom_panel();
  104. }
  105. button->hide();
  106. }
  107. }
  108. void MultiplayerEditorPlugin::edit(Object *p_object) {
  109. repl_editor->edit(Object::cast_to<MultiplayerSynchronizer>(p_object));
  110. }
  111. bool MultiplayerEditorPlugin::handles(Object *p_object) const {
  112. return p_object->is_class("MultiplayerSynchronizer");
  113. }
  114. void MultiplayerEditorPlugin::make_visible(bool p_visible) {
  115. if (p_visible) {
  116. button->show();
  117. EditorNode::get_singleton()->make_bottom_panel_item_visible(repl_editor);
  118. } else if (!repl_editor->get_pin()->is_pressed()) {
  119. if (repl_editor->is_visible_in_tree()) {
  120. EditorNode::get_singleton()->hide_bottom_panel();
  121. }
  122. button->hide();
  123. }
  124. }