| 1 | /**************************************************************************/ |
| 2 | /* objectdb_profiler_panel.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 | |
| 31 | #include "objectdb_profiler_panel.h" |
| 32 | |
| 33 | #include "../snapshot_collector.h" |
| 34 | #include "data_viewers/class_view.h" |
| 35 | #include "data_viewers/node_view.h" |
| 36 | #include "data_viewers/object_view.h" |
| 37 | #include "data_viewers/refcounted_view.h" |
| 38 | #include "data_viewers/summary_view.h" |
| 39 | |
| 40 | #include "core/config/project_settings.h" |
| 41 | #include "core/os/time.h" |
| 42 | #include "editor/debugger/editor_debugger_node.h" |
| 43 | #include "editor/debugger/script_editor_debugger.h" |
| 44 | #include "editor/docks/inspector_dock.h" |
| 45 | #include "editor/editor_node.h" |
| 46 | #include "editor/inspector/editor_inspector.h" |
| 47 | #include "editor/themes/editor_scale.h" |
| 48 | #include "scene/gui/button.h" |
| 49 | #include "scene/gui/label.h" |
| 50 | #include "scene/gui/option_button.h" |
| 51 | #include "scene/gui/split_container.h" |
| 52 | #include "scene/gui/tab_container.h" |
| 53 | |
| 54 | // ObjectDB snapshots are very large. In remote_debugger_peer.cpp, the max in_buf and out_buf size is 8mb. |
| 55 | // Snapshots are typically larger than that, so we send them 6mb at a time. Leaving 2mb for other data. |
| 56 | const int SNAPSHOT_CHUNK_SIZE = 6 << 20; |
| 57 | |
| 58 | void ObjectDBProfilerPanel::_request_object_snapshot() { |
| 59 | take_snapshot->set_disabled(true); |
| 60 | take_snapshot->set_text(TTRC("Generating Snapshot")); |
| 61 | // Pause the game while the snapshot is taken so the state of the game isn't modified as we capture the snapshot. |
| 62 | if (EditorDebuggerNode::get_singleton()->get_current_debugger()->is_breaked()) { |
| 63 | requested_break_for_snapshot = false; |
| 64 | _begin_object_snapshot(); |
| 65 | } else { |
| 66 | awaiting_debug_break = true; |
| 67 | requested_break_for_snapshot = true; // We only need to resume the game if we are the ones who paused it. |
| 68 | EditorDebuggerNode::get_singleton()->debug_break(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void ObjectDBProfilerPanel::_on_debug_breaked(bool p_reallydid, bool p_can_debug, const String &p_reason, bool p_has_stackdump) { |
| 73 | if (p_reallydid && awaiting_debug_break) { |
| 74 | awaiting_debug_break = false; |
| 75 | _begin_object_snapshot(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void ObjectDBProfilerPanel::_begin_object_snapshot() { |
| 80 | Array args = { next_request_id++, SnapshotCollector::get_godot_version_string() }; |
| 81 | EditorDebuggerNode::get_singleton()->get_current_debugger()->send_message("snapshot:request_prepare_snapshot", args); |
| 82 | } |
| 83 | |
| 84 | bool ObjectDBProfilerPanel::handle_debug_message(const String &p_message, const Array &p_data, int p_index) { |
| 85 | if (p_message == "snapshot:snapshot_prepared") { |
| 86 | int request_id = p_data[0]; |
| 87 | int total_size = p_data[1]; |
| 88 | partial_snapshots[request_id] = PartialSnapshot(); |
| 89 | partial_snapshots[request_id].total_size = total_size; |
| 90 | Array args = { request_id, 0, SNAPSHOT_CHUNK_SIZE }; |
| 91 | take_snapshot->set_text(vformat(TTR("Receiving Snapshot (0/%s MiB)"), _to_mb(total_size))); |
| 92 | EditorDebuggerNode::get_singleton()->get_current_debugger()->send_message("snapshot:request_snapshot_chunk", args); |
| 93 | return true; |
| 94 | } |
| 95 | if (p_message == "snapshot:snapshot_chunk") { |
| 96 | int request_id = p_data[0]; |
| 97 | PartialSnapshot &chunk = partial_snapshots[request_id]; |
| 98 | chunk.data.append_array(p_data[1]); |
| 99 | take_snapshot->set_text(vformat(TTR("Receiving Snapshot (%s/%s MiB)"), _to_mb(chunk.data.size()), _to_mb(chunk.total_size))); |
| 100 | if (chunk.data.size() != chunk.total_size) { |
| 101 | Array args = { request_id, chunk.data.size(), chunk.data.size() + SNAPSHOT_CHUNK_SIZE }; |
| 102 | EditorDebuggerNode::get_singleton()->get_current_debugger()->send_message("snapshot:request_snapshot_chunk", args); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | take_snapshot->set_text(TTRC("Visualizing Snapshot")); |
| 107 | // Wait a frame just so the button has a chance to update its text so the user knows what's going on. |
| 108 | get_tree()->connect("process_frame", callable_mp(this, &ObjectDBProfilerPanel::receive_snapshot).bind(request_id), CONNECT_ONE_SHOT); |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | void ObjectDBProfilerPanel::receive_snapshot(int request_id) { |
| 115 | const Vector<uint8_t> &in_data = partial_snapshots[request_id].data; |
| 116 | Ref<DirAccess> snapshot_dir = _get_and_create_snapshot_storage_dir(); |
| 117 | if (snapshot_dir.is_valid()) { |
| 118 | Error err; |
| 119 | String base_snapshot_file_name = Time::get_singleton()->get_datetime_string_from_system(false).replace_char('T', '_').replace_char(':', '-'); |
| 120 | String snapshot_file_name = base_snapshot_file_name; |
| 121 | String current_dir = snapshot_dir->get_current_dir(); |
| 122 | String joined_dir = current_dir.path_join(snapshot_file_name) + ".odb_snapshot"; |
| 123 | |
| 124 | for (int i = 2; FileAccess::exists(joined_dir); i++) { |
| 125 | snapshot_file_name = base_snapshot_file_name + '_' + String::chr('0' + i); |
| 126 | joined_dir = current_dir.path_join(snapshot_file_name) + ".odb_snapshot"; |
| 127 | } |
| 128 | |
| 129 | Ref<FileAccess> file = FileAccess::open(joined_dir, FileAccess::WRITE, &err); |
| 130 | if (err == OK) { |
| 131 | file->store_buffer(in_data); |
| 132 | file->close(); // RAII could do this typically, but we want to read the file in _show_selected_snapshot, so we have to finalize the write before that. |
| 133 | |
| 134 | _add_snapshot_button(snapshot_file_name, joined_dir); |
| 135 | snapshot_list->deselect_all(); |
| 136 | snapshot_list->set_selected(snapshot_list->get_root()->get_first_child()); |
| 137 | snapshot_list->ensure_cursor_is_visible(); |
| 138 | _show_selected_snapshot(); |
| 139 | } else { |
| 140 | ERR_PRINT("Could not persist ObjectDB Snapshot: " + String(error_names[err])); |
| 141 | } |
| 142 | } |
| 143 | partial_snapshots.erase(request_id); |
| 144 | if (requested_break_for_snapshot) { |
| 145 | EditorDebuggerNode::get_singleton()->debug_continue(); |
| 146 | } |
| 147 | take_snapshot->set_disabled(false); |
| 148 | take_snapshot->set_text("Take ObjectDB Snapshot"); |
| 149 | } |
| 150 | |
| 151 | Ref<DirAccess> ObjectDBProfilerPanel::_get_and_create_snapshot_storage_dir() { |
| 152 | String profiles_dir = "user://"; |
| 153 | Ref<DirAccess> da = DirAccess::open(profiles_dir); |
| 154 | ERR_FAIL_COND_V_MSG(da.is_null(), nullptr, vformat("Could not open 'user://' directory: '%s'.", profiles_dir)); |
| 155 | Error err = da->change_dir("objectdb_snapshots"); |
| 156 | if (err != OK) { |
| 157 | Error err_mk = da->make_dir("objectdb_snapshots"); |
| 158 | Error err_ch = da->change_dir("objectdb_snapshots"); |
| 159 | ERR_FAIL_COND_V_MSG(err_mk != OK || err_ch != OK, nullptr, "Could not create ObjectDB Snapshots directory: " + da->get_current_dir()); |
| 160 | } |
| 161 | return da; |
| 162 | } |
| 163 | |
| 164 | TreeItem *ObjectDBProfilerPanel::_add_snapshot_button(const String &p_snapshot_file_name, const String &p_full_file_path) { |
| 165 | TreeItem *item = snapshot_list->create_item(snapshot_list->get_root()); |
| 166 | item->set_text(0, p_snapshot_file_name); |
| 167 | item->set_metadata(0, p_full_file_path); |
| 168 | item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); |
| 169 | item->move_before(snapshot_list->get_root()->get_first_child()); |
| 170 | _update_diff_items(); |
| 171 | _update_enabled_diff_items(); |
| 172 | return item; |
| 173 | } |
| 174 | |
| 175 | void ObjectDBProfilerPanel::_show_selected_snapshot() { |
| 176 | if (snapshot_list->get_selected()->get_text(0) == (String)diff_button->get_selected_metadata()) { |
| 177 | for (int i = 0; i < diff_button->get_item_count(); i++) { |
| 178 | if (diff_button->get_item_text(i) == current_snapshot->name) { |
| 179 | diff_button->select(i); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | show_snapshot(snapshot_list->get_selected()->get_text(0), diff_button->get_selected_metadata()); |
| 185 | _update_enabled_diff_items(); |
| 186 | } |
| 187 | |
| 188 | void ObjectDBProfilerPanel::_on_snapshot_deselected() { |
| 189 | snapshot_list->deselect_all(); |
| 190 | diff_button->select(0); |
| 191 | clear_snapshot(); |
| 192 | _update_enabled_diff_items(); |
| 193 | } |
| 194 | |
| 195 | Ref<GameStateSnapshot> ObjectDBProfilerPanel::get_snapshot(const String &p_snapshot_file_name) { |
| 196 | if (snapshot_cache.has(p_snapshot_file_name)) { |
| 197 | return snapshot_cache.get(p_snapshot_file_name); |
| 198 | } |
| 199 | |
| 200 | Ref<DirAccess> snapshot_dir = _get_and_create_snapshot_storage_dir(); |
| 201 | ERR_FAIL_COND_V_MSG(snapshot_dir.is_null(), nullptr, "Could not access ObjectDB Snapshot directory"); |
| 202 | |
| 203 | String full_file_path = snapshot_dir->get_current_dir().path_join(p_snapshot_file_name) + ".odb_snapshot"; |
| 204 | |
| 205 | Error err; |
| 206 | Ref<FileAccess> snapshot_file = FileAccess::open(full_file_path, FileAccess::READ, &err); |
| 207 | ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Could not open ObjectDB Snapshot file: " + full_file_path); |
| 208 | |
| 209 | Vector<uint8_t> content = snapshot_file->get_buffer(snapshot_file->get_length()); // We want to split on newlines, so normalize them. |
| 210 | ERR_FAIL_COND_V_MSG(content.is_empty(), nullptr, "ObjectDB Snapshot file is empty: " + full_file_path); |
| 211 | |
| 212 | Ref<GameStateSnapshot> snapshot = GameStateSnapshot::create_ref(p_snapshot_file_name, content); |
| 213 | if (snapshot.is_valid()) { |
| 214 | snapshot_cache.insert(p_snapshot_file_name, snapshot); |
| 215 | } |
| 216 | |
| 217 | return snapshot; |
| 218 | } |
| 219 | |
| 220 | void ObjectDBProfilerPanel::show_snapshot(const String &p_snapshot_file_name, const String &p_snapshot_diff_file_name) { |
| 221 | clear_snapshot(false); |
| 222 | |
| 223 | current_snapshot = get_snapshot(p_snapshot_file_name); |
| 224 | if (!p_snapshot_diff_file_name.is_empty()) { |
| 225 | diff_snapshot = get_snapshot(p_snapshot_diff_file_name); |
| 226 | } |
| 227 | |
| 228 | _update_view_tabs(); |
| 229 | _view_tab_changed(view_tabs->get_current_tab()); |
| 230 | } |
| 231 | |
| 232 | void ObjectDBProfilerPanel::_view_tab_changed(int p_tab_idx) { |
| 233 | // Populating tabs only on tab changed because we're handling a lot of data, |
| 234 | // and the editor freezes for a while if we try to populate every tab at once. |
| 235 | SnapshotView *view = cast_to<SnapshotView>(view_tabs->get_current_tab_control()); |
| 236 | GameStateSnapshot *snapshot = current_snapshot.ptr(); |
| 237 | GameStateSnapshot *diff = diff_snapshot.ptr(); |
| 238 | if (snapshot != nullptr && !view->is_showing_snapshot(snapshot, diff)) { |
| 239 | view->show_snapshot(snapshot, diff); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void ObjectDBProfilerPanel::clear_snapshot(bool p_update_view_tabs) { |
| 244 | for (SnapshotView *view : views) { |
| 245 | view->clear_snapshot(); |
| 246 | } |
| 247 | |
| 248 | const Object *edited_object = InspectorDock::get_inspector_singleton()->get_edited_object(); |
| 249 | if (Object::cast_to<SnapshotDataObject>(edited_object)) { |
| 250 | EditorNode::get_singleton()->push_item(nullptr); |
| 251 | } |
| 252 | |
| 253 | current_snapshot.unref(); |
| 254 | diff_snapshot.unref(); |
| 255 | |
| 256 | if (p_update_view_tabs) { |
| 257 | _update_view_tabs(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void ObjectDBProfilerPanel::set_enabled(bool p_enabled) { |
| 262 | take_snapshot->set_text(TTRC("Take ObjectDB Snapshot")); |
| 263 | take_snapshot->set_disabled(!p_enabled); |
| 264 | } |
| 265 | |
| 266 | void ObjectDBProfilerPanel::_snapshot_rmb(const Vector2 &p_pos, MouseButton p_button) { |
| 267 | if (p_button != MouseButton::RIGHT) { |
| 268 | return; |
| 269 | } |
| 270 | rmb_menu->clear(false); |
| 271 | |
| 272 | rmb_menu->add_icon_item(get_editor_theme_icon(SNAME("Rename")), TTRC("Rename"), OdbProfilerMenuOptions::ODB_MENU_RENAME); |
| 273 | rmb_menu->add_icon_item(get_editor_theme_icon(SNAME("Folder")), TTRC("Show in File Manager"), OdbProfilerMenuOptions::ODB_MENU_SHOW_IN_FOLDER); |
| 274 | rmb_menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTRC("Delete"), OdbProfilerMenuOptions::ODB_MENU_DELETE); |
| 275 | |
| 276 | rmb_menu->set_position(snapshot_list->get_screen_position() + p_pos); |
| 277 | rmb_menu->reset_size(); |
| 278 | rmb_menu->popup(); |
| 279 | } |
| 280 | |
| 281 | void ObjectDBProfilerPanel::_rmb_menu_pressed(int p_tool, bool p_confirm_override) { |
| 282 | String file_path = snapshot_list->get_selected()->get_metadata(0); |
| 283 | String global_path = ProjectSettings::get_singleton()->globalize_path(file_path); |
| 284 | switch (rmb_menu->get_item_id(p_tool)) { |
| 285 | case OdbProfilerMenuOptions::ODB_MENU_SHOW_IN_FOLDER: { |
| 286 | OS::get_singleton()->shell_show_in_file_manager(global_path, true); |
| 287 | break; |
| 288 | } |
| 289 | case OdbProfilerMenuOptions::ODB_MENU_DELETE: { |
| 290 | DirAccess::remove_file_or_error(global_path); |
| 291 | snapshot_list->get_root()->remove_child(snapshot_list->get_selected()); |
| 292 | if (snapshot_list->get_root()->get_child_count() > 0) { |
| 293 | snapshot_list->set_selected(snapshot_list->get_root()->get_first_child()); |
| 294 | } else { |
| 295 | // If we deleted the last snapshot, jump back to the summary tab and clear everything out. |
| 296 | clear_snapshot(); |
| 297 | } |
| 298 | _update_diff_items(); |
| 299 | break; |
| 300 | } |
| 301 | case OdbProfilerMenuOptions::ODB_MENU_RENAME: { |
| 302 | snapshot_list->edit_selected(true); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void ObjectDBProfilerPanel::_edit_snapshot_name() { |
| 309 | String new_snapshot_name = snapshot_list->get_selected()->get_text(0); |
| 310 | String full_file_with_path = snapshot_list->get_selected()->get_metadata(0); |
| 311 | Vector<String> full_path_parts = full_file_with_path.rsplit("/", false, 1); |
| 312 | String full_file_path = full_path_parts[0]; |
| 313 | String file_name = full_path_parts[1]; |
| 314 | String old_snapshot_name = file_name.split(".")[0]; |
| 315 | String new_full_file_path = full_file_path.path_join(new_snapshot_name) + ".odb_snapshot"; |
| 316 | |
| 317 | bool name_taken = false; |
| 318 | for (int i = 0; i < snapshot_list->get_root()->get_child_count(); i++) { |
| 319 | TreeItem *item = snapshot_list->get_root()->get_child(i); |
| 320 | if (item != snapshot_list->get_selected()) { |
| 321 | if (item->get_text(0) == new_snapshot_name) { |
| 322 | name_taken = true; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (name_taken || new_snapshot_name.contains_char(':') || new_snapshot_name.contains_char('\\') || new_snapshot_name.contains_char('/') || new_snapshot_name.begins_with(".") || new_snapshot_name.is_empty()) { |
| 329 | EditorNode::get_singleton()->show_warning(TTRC("Invalid snapshot name.")); |
| 330 | snapshot_list->get_selected()->set_text(0, old_snapshot_name); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | Error err = DirAccess::rename_absolute(full_file_with_path, new_full_file_path); |
| 335 | if (err != OK) { |
| 336 | EditorNode::get_singleton()->show_warning(TTRC("Snapshot rename failed")); |
| 337 | snapshot_list->get_selected()->set_text(0, old_snapshot_name); |
| 338 | } else { |
| 339 | snapshot_list->get_selected()->set_metadata(0, new_full_file_path); |
| 340 | } |
| 341 | |
| 342 | _update_diff_items(); |
| 343 | _show_selected_snapshot(); |
| 344 | } |
| 345 | |
| 346 | ObjectDBProfilerPanel::ObjectDBProfilerPanel() { |
| 347 | set_name(TTRC("ObjectDB Profiler")); |
| 348 | |
| 349 | snapshot_cache = LRUCache<String, Ref<GameStateSnapshot>>(SNAPSHOT_CACHE_MAX_SIZE); |
| 350 | |
| 351 | EditorDebuggerNode::get_singleton()->get_current_debugger()->connect("breaked", callable_mp(this, &ObjectDBProfilerPanel::_on_debug_breaked)); |
| 352 | |
| 353 | HSplitContainer *root_container = memnew(HSplitContainer); |
| 354 | root_container->set_anchors_preset(Control::LayoutPreset::PRESET_FULL_RECT); |
| 355 | root_container->set_v_size_flags(Control::SizeFlags::SIZE_EXPAND_FILL); |
| 356 | root_container->set_h_size_flags(Control::SizeFlags::SIZE_EXPAND_FILL); |
| 357 | root_container->set_split_offset(300 * EDSCALE); |
| 358 | add_child(root_container); |
| 359 | |
| 360 | VBoxContainer *snapshot_column = memnew(VBoxContainer); |
| 361 | root_container->add_child(snapshot_column); |
| 362 | |
| 363 | take_snapshot = memnew(Button(TTRC("Take ObjectDB Snapshot"))); |
| 364 | snapshot_column->add_child(take_snapshot); |
| 365 | take_snapshot->connect(SceneStringName(pressed), callable_mp(this, &ObjectDBProfilerPanel::_request_object_snapshot)); |
| 366 | |
| 367 | snapshot_list = memnew(Tree); |
| 368 | snapshot_list->create_item(); |
| 369 | snapshot_list->set_hide_folding(true); |
| 370 | snapshot_column->add_child(snapshot_list); |
| 371 | snapshot_list->set_select_mode(Tree::SelectMode::SELECT_ROW); |
| 372 | snapshot_list->set_hide_root(true); |
| 373 | snapshot_list->set_columns(1); |
| 374 | snapshot_list->set_column_titles_visible(true); |
| 375 | snapshot_list->set_column_title(0, "Snapshots"); |
| 376 | snapshot_list->set_column_expand(0, true); |
| 377 | snapshot_list->set_column_clip_content(0, true); |
| 378 | snapshot_list->connect(SceneStringName(item_selected), callable_mp(this, &ObjectDBProfilerPanel::_show_selected_snapshot)); |
| 379 | snapshot_list->connect("nothing_selected", callable_mp(this, &ObjectDBProfilerPanel::_on_snapshot_deselected)); |
| 380 | snapshot_list->connect("item_edited", callable_mp(this, &ObjectDBProfilerPanel::_edit_snapshot_name)); |
| 381 | snapshot_list->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 382 | snapshot_list->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 383 | snapshot_list->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT); |
| 384 | snapshot_list->set_theme_type_variation("TreeSecondary"); |
| 385 | |
| 386 | snapshot_list->set_allow_rmb_select(true); |
| 387 | snapshot_list->connect("item_mouse_selected", callable_mp(this, &ObjectDBProfilerPanel::_snapshot_rmb)); |
| 388 | |
| 389 | rmb_menu = memnew(PopupMenu); |
| 390 | add_child(rmb_menu); |
| 391 | rmb_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ObjectDBProfilerPanel::_rmb_menu_pressed).bind(false)); |
| 392 | |
| 393 | HBoxContainer *diff_button_and_label = memnew(HBoxContainer); |
| 394 | diff_button_and_label->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 395 | snapshot_column->add_child(diff_button_and_label); |
| 396 | Label *diff_against = memnew(Label(TTRC("Diff Against:"))); |
| 397 | diff_button_and_label->add_child(diff_against); |
| 398 | |
| 399 | diff_button = memnew(OptionButton); |
| 400 | diff_button->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 401 | diff_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 402 | diff_button->connect(SceneStringName(item_selected), callable_mp(this, &ObjectDBProfilerPanel::_show_selected_snapshot).unbind(1)); |
| 403 | diff_button_and_label->add_child(diff_button); |
| 404 | |
| 405 | // Tabs of various views right for each snapshot. |
| 406 | view_tabs = memnew(TabContainer); |
| 407 | view_tabs->set_theme_type_variation("TabContainerInner"); |
| 408 | root_container->add_child(view_tabs); |
| 409 | view_tabs->set_custom_minimum_size(Size2(300 * EDSCALE, 0)); |
| 410 | view_tabs->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); |
| 411 | view_tabs->connect("tab_changed", callable_mp(this, &ObjectDBProfilerPanel::_view_tab_changed)); |
| 412 | |
| 413 | add_view(memnew(SnapshotSummaryView)); |
| 414 | add_view(memnew(SnapshotClassView)); |
| 415 | add_view(memnew(SnapshotObjectView)); |
| 416 | add_view(memnew(SnapshotNodeView)); |
| 417 | add_view(memnew(SnapshotRefCountedView)); |
| 418 | |
| 419 | set_enabled(false); |
| 420 | |
| 421 | // Load all the snapshot names from disk. |
| 422 | Ref<DirAccess> snapshot_dir = _get_and_create_snapshot_storage_dir(); |
| 423 | if (snapshot_dir.is_valid()) { |
| 424 | for (const String &file_name : snapshot_dir->get_files()) { |
| 425 | Vector<String> name_parts = file_name.split("."); |
| 426 | ERR_CONTINUE_MSG(name_parts.size() != 2 || name_parts[1] != "odb_snapshot", "ObjectDB snapshot file did not have .odb_snapshot extension. Skipping: " + file_name); |
| 427 | _add_snapshot_button(name_parts[0], snapshot_dir->get_current_dir().path_join(file_name)); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void ObjectDBProfilerPanel::add_view(SnapshotView *p_to_add) { |
| 433 | views.push_back(p_to_add); |
| 434 | view_tabs->add_child(p_to_add); |
| 435 | _update_view_tabs(); |
| 436 | } |
| 437 | |
| 438 | void ObjectDBProfilerPanel::_update_view_tabs() { |
| 439 | bool has_snapshot = current_snapshot.is_valid(); |
| 440 | for (int i = 1; i < view_tabs->get_tab_count(); i++) { |
| 441 | view_tabs->set_tab_disabled(i, !has_snapshot); |
| 442 | } |
| 443 | |
| 444 | if (!has_snapshot) { |
| 445 | view_tabs->set_current_tab(0); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void ObjectDBProfilerPanel::_update_diff_items() { |
| 450 | diff_button->clear(); |
| 451 | diff_button->add_item(TTRC("None"), 0); |
| 452 | diff_button->set_item_metadata(0, String()); |
| 453 | diff_button->set_item_auto_translate_mode(0, Node::AUTO_TRANSLATE_MODE_ALWAYS); |
| 454 | |
| 455 | for (int i = 0; i < snapshot_list->get_root()->get_child_count(); i++) { |
| 456 | String name = snapshot_list->get_root()->get_child(i)->get_text(0); |
| 457 | diff_button->add_item(name); |
| 458 | diff_button->set_item_metadata(i + 1, name); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void ObjectDBProfilerPanel::_update_enabled_diff_items() { |
| 463 | TreeItem *selected_snapshot = snapshot_list->get_selected(); |
| 464 | if (selected_snapshot == nullptr) { |
| 465 | diff_button->set_disabled(true); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | diff_button->set_disabled(false); |
| 470 | |
| 471 | String snapshot_name = selected_snapshot->get_text(0); |
| 472 | for (int i = 0; i < diff_button->get_item_count(); i++) { |
| 473 | diff_button->set_item_disabled(i, diff_button->get_item_text(i) == snapshot_name); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | String ObjectDBProfilerPanel::_to_mb(int p_x) { |
| 478 | return String::num((double)p_x / (double)(1 << 20), 2); |
| 479 | } |