multiplayer_editor_plugin.cpp 7.1 KB

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