multiplayer_editor_plugin.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #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_command_palette.h"
  35. #include "editor/editor_interface.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/gui/editor_bottom_panel.h"
  38. void MultiplayerEditorDebugger::_bind_methods() {
  39. ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));
  40. }
  41. bool MultiplayerEditorDebugger::has_capture(const String &p_capture) const {
  42. return p_capture == "multiplayer";
  43. }
  44. void MultiplayerEditorDebugger::_open_request(const String &p_path) {
  45. emit_signal("open_request", p_path);
  46. }
  47. bool MultiplayerEditorDebugger::capture(const String &p_message, const Array &p_data, int p_session) {
  48. ERR_FAIL_COND_V(!profilers.has(p_session), false);
  49. EditorNetworkProfiler *profiler = profilers[p_session];
  50. if (p_message == "multiplayer:rpc") {
  51. MultiplayerDebugger::RPCFrame frame;
  52. frame.deserialize(p_data);
  53. for (int i = 0; i < frame.infos.size(); i++) {
  54. profiler->add_rpc_frame_data(frame.infos[i]);
  55. }
  56. return true;
  57. } else if (p_message == "multiplayer:syncs") {
  58. MultiplayerDebugger::ReplicationFrame frame;
  59. frame.deserialize(p_data);
  60. for (const KeyValue<ObjectID, MultiplayerDebugger::SyncInfo> &E : frame.infos) {
  61. profiler->add_sync_frame_data(E.value);
  62. }
  63. Array missing = profiler->pop_missing_node_data();
  64. if (missing.size()) {
  65. // Asks for the object information.
  66. get_session(p_session)->send_message("multiplayer:cache", missing);
  67. }
  68. return true;
  69. } else if (p_message == "multiplayer:cache") {
  70. ERR_FAIL_COND_V(p_data.size() % 3, false);
  71. for (int i = 0; i < p_data.size(); i += 3) {
  72. EditorNetworkProfiler::NodeInfo info;
  73. info.id = p_data[i].operator ObjectID();
  74. info.type = p_data[i + 1].operator String();
  75. info.path = p_data[i + 2].operator String();
  76. profiler->add_node_data(info);
  77. }
  78. return true;
  79. } else if (p_message == "multiplayer:bandwidth") {
  80. ERR_FAIL_COND_V(p_data.size() < 2, false);
  81. profiler->set_bandwidth(p_data[0], p_data[1]);
  82. return true;
  83. }
  84. return false;
  85. }
  86. void MultiplayerEditorDebugger::_profiler_activate(bool p_enable, int p_session_id) {
  87. Ref<EditorDebuggerSession> session = get_session(p_session_id);
  88. ERR_FAIL_COND(session.is_null());
  89. session->toggle_profiler("multiplayer:bandwidth", p_enable);
  90. session->toggle_profiler("multiplayer:rpc", p_enable);
  91. session->toggle_profiler("multiplayer:replication", p_enable);
  92. }
  93. void MultiplayerEditorDebugger::setup_session(int p_session_id) {
  94. Ref<EditorDebuggerSession> session = get_session(p_session_id);
  95. ERR_FAIL_COND(session.is_null());
  96. EditorNetworkProfiler *profiler = memnew(EditorNetworkProfiler);
  97. profiler->connect("enable_profiling", callable_mp(this, &MultiplayerEditorDebugger::_profiler_activate).bind(p_session_id));
  98. profiler->connect("open_request", callable_mp(this, &MultiplayerEditorDebugger::_open_request));
  99. profiler->set_name(TTR("Network Profiler"));
  100. session->connect("started", callable_mp(profiler, &EditorNetworkProfiler::started));
  101. session->connect("stopped", callable_mp(profiler, &EditorNetworkProfiler::stopped));
  102. session->add_session_tab(profiler);
  103. profilers[p_session_id] = profiler;
  104. }
  105. /// MultiplayerEditorPlugin
  106. MultiplayerEditorPlugin::MultiplayerEditorPlugin() {
  107. repl_editor = memnew(ReplicationEditor);
  108. button = EditorNode::get_bottom_panel()->add_item(TTR("Replication"), repl_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_replication_bottom_panel", TTR("Toggle Replication Bottom Panel")));
  109. button->hide();
  110. repl_editor->get_pin()->connect(SceneStringName(pressed), callable_mp(this, &MultiplayerEditorPlugin::_pinned));
  111. debugger.instantiate();
  112. debugger->connect("open_request", callable_mp(this, &MultiplayerEditorPlugin::_open_request));
  113. }
  114. void MultiplayerEditorPlugin::_open_request(const String &p_path) {
  115. EditorInterface::get_singleton()->open_scene_from_path(p_path);
  116. }
  117. void MultiplayerEditorPlugin::_notification(int p_what) {
  118. switch (p_what) {
  119. case NOTIFICATION_ENTER_TREE: {
  120. get_tree()->connect("node_removed", callable_mp(this, &MultiplayerEditorPlugin::_node_removed));
  121. add_debugger_plugin(debugger);
  122. } break;
  123. case NOTIFICATION_EXIT_TREE: {
  124. remove_debugger_plugin(debugger);
  125. }
  126. }
  127. }
  128. void MultiplayerEditorPlugin::_node_removed(Node *p_node) {
  129. if (p_node && p_node == repl_editor->get_current()) {
  130. repl_editor->edit(nullptr);
  131. if (repl_editor->is_visible_in_tree()) {
  132. EditorNode::get_bottom_panel()->hide_bottom_panel();
  133. }
  134. button->hide();
  135. repl_editor->get_pin()->set_pressed(false);
  136. }
  137. }
  138. void MultiplayerEditorPlugin::_pinned() {
  139. if (!repl_editor->get_pin()->is_pressed() && repl_editor->get_current() == nullptr) {
  140. if (repl_editor->is_visible_in_tree()) {
  141. EditorNode::get_bottom_panel()->hide_bottom_panel();
  142. }
  143. button->hide();
  144. }
  145. }
  146. void MultiplayerEditorPlugin::edit(Object *p_object) {
  147. repl_editor->edit(Object::cast_to<MultiplayerSynchronizer>(p_object));
  148. }
  149. bool MultiplayerEditorPlugin::handles(Object *p_object) const {
  150. return p_object->is_class("MultiplayerSynchronizer");
  151. }
  152. void MultiplayerEditorPlugin::make_visible(bool p_visible) {
  153. if (p_visible) {
  154. button->show();
  155. EditorNode::get_bottom_panel()->make_item_visible(repl_editor);
  156. } else if (!repl_editor->get_pin()->is_pressed()) {
  157. if (repl_editor->is_visible_in_tree()) {
  158. EditorNode::get_bottom_panel()->hide_bottom_panel();
  159. }
  160. button->hide();
  161. }
  162. }