Debian Codegraph @

editor/file_system/dependency_editor.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* dependency_editor.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 "dependency_editor.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/file_access.h"
35 #include "core/io/resource_loader.h"
36 #include "editor/editor_node.h"
37 #include "editor/editor_string_names.h"
38 #include "editor/file_system/editor_file_system.h"
39 #include "editor/gui/editor_file_dialog.h"
40 #include "editor/gui/editor_quick_open_dialog.h"
41 #include "editor/settings/editor_settings.h"
42 #include "editor/themes/editor_scale.h"
43 #include "scene/gui/box_container.h"
44 #include "scene/gui/item_list.h"
45 #include "scene/gui/margin_container.h"
46 #include "scene/gui/popup_menu.h"
47 #include "scene/gui/tree.h"
48
49 static void _setup_search_file_dialog(EditorFileDialog *p_dialog, const String &p_file, const String &p_type) {
50 p_dialog->set_title(vformat(TTR("Search Replacement For: %s"), p_file.get_file()));
51
52 // Set directory to closest existing directory.
53 p_dialog->set_current_dir(p_file.get_base_dir());
54
55 p_dialog->clear_filters();
56 List<String> ext;
57 ResourceLoader::get_recognized_extensions_for_type(p_type, &ext);
58 for (const String &E : ext) {
59 p_dialog->add_filter("*." + E);
60 }
61 }
62
63 void DependencyEditor::_searched(const String &p_path) {
64 HashMap<String, String> dep_rename;
65 dep_rename[replacing] = p_path;
66
67 ResourceLoader::rename_dependencies(editing, dep_rename);
68
69 _update_list();
70 _update_file();
71 }
72
73 void DependencyEditor::_load_pressed(Object *p_item, int p_cell, int p_button, MouseButton p_mouse_button) {
74 if (p_mouse_button != MouseButton::LEFT) {
75 return;
76 }
77 TreeItem *ti = Object::cast_to<TreeItem>(p_item);
78 replacing = ti->get_text(1);
79
80 _setup_search_file_dialog(search, replacing, ti->get_metadata(0));
81 search->popup_file_dialog();
82 }
83
84 void DependencyEditor::_fix_and_find(EditorFileSystemDirectory *efsd, HashMap<String, HashMap<String, String>> &candidates) {
85 for (int i = 0; i < efsd->get_subdir_count(); i++) {
86 _fix_and_find(efsd->get_subdir(i), candidates);
87 }
88
89 for (int i = 0; i < efsd->get_file_count(); i++) {
90 String file = efsd->get_file(i);
91 if (!candidates.has(file)) {
92 continue;
93 }
94
95 String path = efsd->get_file_path(i);
96
97 for (KeyValue<String, String> &E : candidates[file]) {
98 if (E.value.is_empty()) {
99 E.value = path;
100 continue;
101 }
102
103 //must match the best, using subdirs
104 String existing = E.value.replace_first("res://", "");
105 String current = path.replace_first("res://", "");
106 String lost = E.key.replace_first("res://", "");
107
108 Vector<String> existingv = existing.split("/");
109 existingv.reverse();
110 Vector<String> currentv = current.split("/");
111 currentv.reverse();
112 Vector<String> lostv = lost.split("/");
113 lostv.reverse();
114
115 int existing_score = 0;
116 int current_score = 0;
117
118 for (int j = 0; j < lostv.size(); j++) {
119 if (j < existingv.size() && lostv[j] == existingv[j]) {
120 existing_score++;
121 }
122 if (j < currentv.size() && lostv[j] == currentv[j]) {
123 current_score++;
124 }
125 }
126
127 if (current_score > existing_score) {
128 //if it was the same, could track distance to new path but..
129
130 E.value = path; //replace by more accurate
131 }
132 }
133 }
134 }
135
136 void DependencyEditor::_fix_all() {
137 if (!EditorFileSystem::get_singleton()->get_filesystem()) {
138 return;
139 }
140
141 HashMap<String, HashMap<String, String>> candidates;
142
143 for (const String &E : missing) {
144 String base = E.get_file();
145 if (!candidates.has(base)) {
146 candidates[base] = HashMap<String, String>();
147 }
148
149 candidates[base][E] = "";
150 }
151
152 _fix_and_find(EditorFileSystem::get_singleton()->get_filesystem(), candidates);
153
154 HashMap<String, String> remaps;
155
156 for (KeyValue<String, HashMap<String, String>> &E : candidates) {
157 for (const KeyValue<String, String> &F : E.value) {
158 if (!F.value.is_empty()) {
159 remaps[F.key] = F.value;
160 }
161 }
162 }
163
164 if (remaps.size()) {
165 ResourceLoader::rename_dependencies(editing, remaps);
166
167 _update_list();
168 _update_file();
169 }
170 }
171
172 void DependencyEditor::_update_file() {
173 EditorFileSystem::get_singleton()->update_file(editing);
174 }
175
176 void DependencyEditor::_notification(int p_what) {
177 if (p_what == NOTIFICATION_THEME_CHANGED) {
178 warning_label->add_theme_color_override(SceneStringName(font_color), get_theme_color("warning_color", EditorStringName(Editor)));
179 }
180 }
181
182 static String _get_resolved_dep_path(const String &p_dep) {
183 if (p_dep.get_slice_count("::") < 3) {
184 return p_dep.get_slice("::", 0); // No UID, just return the path.
185 }
186
187 const String uid_text = p_dep.get_slice("::", 0);
188 ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(uid_text);
189
190 // Dependency is in UID format, obtain proper path.
191 if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
192 return ResourceUID::get_singleton()->get_id_path(uid);
193 }
194
195 // UID fallback path.
196 return p_dep.get_slice("::", 2);
197 }
198
199 static String _get_stored_dep_path(const String &p_dep) {
200 if (p_dep.get_slice_count("::") > 2) {
201 return p_dep.get_slice("::", 2);
202 }
203 return p_dep.get_slice("::", 0);
204 }
205
206 void DependencyEditor::_update_list() {
207 List<String> deps;
208 ResourceLoader::get_dependencies(editing, &deps, true);
209
210 tree->clear();
211 missing.clear();
212
213 TreeItem *root = tree->create_item();
214
215 Ref<Texture2D> folder = tree->get_theme_icon(SNAME("folder"), SNAME("FileDialog"));
216
217 bool broken = false;
218
219 for (const String &dep : deps) {
220 TreeItem *item = tree->create_item(root);
221
222 const String path = _get_resolved_dep_path(dep);
223 if (FileAccess::exists(path)) {
224 item->set_text(0, path.get_file());
225 item->set_text(1, path);
226 } else {
227 const String &stored_path = _get_stored_dep_path(dep);
228 item->set_text(0, stored_path.get_file());
229 item->set_text(1, stored_path);
230 item->set_custom_color(1, Color(1, 0.4, 0.3));
231 missing.push_back(stored_path);
232 broken = true;
233 }
234
235 const String type = dep.contains("::") ? dep.get_slice("::", 1) : "Resource";
236 Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(type);
237 item->set_icon(0, icon);
238 item->set_metadata(0, type);
239
240 item->add_button(1, folder, 0);
241 }
242
243 fixdeps->set_disabled(!broken);
244 }
245
246 void DependencyEditor::edit(const String &p_path) {
247 editing = p_path;
248 set_title(TTR("Dependencies For:") + " " + p_path.get_file());
249
250 _update_list();
251
252 if (EditorNode::get_singleton()->is_scene_open(p_path)) {
253 warning_label->show();
254 warning_label->set_text(vformat(TTR("Scene \"%s\" is currently being edited. Changes will only take effect when reloaded."), p_path.get_file()));
255 } else if (ResourceCache::has(p_path)) {
256 warning_label->show();
257 warning_label->set_text(vformat(TTR("Resource \"%s\" is in use. Changes will only take effect when reloaded."), p_path.get_file()));
258 } else {
259 warning_label->hide();
260 }
261 popup_centered_ratio(0.4);
262 }
263
264 DependencyEditor::DependencyEditor() {
265 VBoxContainer *vb = memnew(VBoxContainer);
266 vb->set_name(TTR("Dependencies"));
267 add_child(vb);
268
269 tree = memnew(Tree);
270 tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
271 tree->set_theme_type_variation("TreeTable");
272 tree->set_hide_folding(true);
273 tree->set_columns(2);
274 tree->set_column_titles_visible(true);
275 tree->set_column_title(0, TTR("Resource"));
276 tree->set_column_clip_content(0, true);
277 tree->set_column_title(1, TTR("Path"));
278 tree->set_column_clip_content(1, true);
279 tree->set_hide_root(true);
280 tree->connect("button_clicked", callable_mp(this, &DependencyEditor::_load_pressed));
281
282 HBoxContainer *hbc = memnew(HBoxContainer);
283 Label *label = memnew(Label(TTR("Dependencies:")));
284 label->set_theme_type_variation("HeaderSmall");
285
286 hbc->add_child(label);
287 hbc->add_spacer();
288 fixdeps = memnew(Button(TTR("Fix Broken")));
289 hbc->add_child(fixdeps);
290 fixdeps->connect(SceneStringName(pressed), callable_mp(this, &DependencyEditor::_fix_all));
291
292 vb->add_child(hbc);
293
294 MarginContainer *mc = memnew(MarginContainer);
295 mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
296
297 mc->add_child(tree);
298 vb->add_child(mc);
299
300 warning_label = memnew(Label);
301 warning_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
302 vb->add_child(warning_label);
303
304 set_title(TTR("Dependency Editor"));
305 search = memnew(EditorFileDialog);
306 search->connect("file_selected", callable_mp(this, &DependencyEditor::_searched));
307 search->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
308 add_child(search);
309 }
310
311 /////////////////////////////////////
312 void DependencyEditorOwners::_list_rmb_clicked(int p_item, const Vector2 &p_pos, MouseButton p_mouse_button_index) {
313 if (p_mouse_button_index != MouseButton::RIGHT) {
314 return;
315 }
316
317 file_options->clear();
318 file_options->reset_size();
319 if (p_item >= 0) {
320 PackedInt32Array selected_items = owners->get_selected_items();
321 bool only_scenes_selected = true;
322
323 for (int i = 0; i < selected_items.size(); i++) {
324 int item_idx = selected_items[i];
325 if (ResourceLoader::get_resource_type(owners->get_item_text(item_idx)) != "PackedScene") {
326 only_scenes_selected = false;
327 break;
328 }
329 }
330
331 if (only_scenes_selected) {
332 file_options->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTRN("Open Scene", "Open Scenes", selected_items.size()), FILE_MENU_OPEN);
333 } else if (selected_items.size() == 1) {
334 file_options->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTR("Open"), FILE_MENU_OPEN);
335 } else {
336 return;
337 }
338 }
339
340 file_options->set_position(owners->get_screen_position() + p_pos);
341 file_options->reset_size();
342 file_options->popup();
343 }
344
345 void DependencyEditorOwners::_select_file(int p_idx) {
346 String fpath = owners->get_item_text(p_idx);
347 EditorNode::get_singleton()->load_scene_or_resource(fpath);
348
349 hide();
350 emit_signal(SceneStringName(confirmed));
351 }
352
353 void DependencyEditorOwners::_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) {
354 if (p_mouse_button_index != MouseButton::LEFT) {
355 return;
356 }
357
358 owners->deselect_all();
359 }
360
361 void DependencyEditorOwners::_file_option(int p_option) {
362 switch (p_option) {
363 case FILE_MENU_OPEN: {
364 PackedInt32Array selected_items = owners->get_selected_items();
365 for (int i = 0; i < selected_items.size(); i++) {
366 int item_idx = selected_items[i];
367 if (item_idx < 0 || item_idx >= owners->get_item_count()) {
368 break;
369 }
370 _select_file(item_idx);
371 }
372 } break;
373 }
374 }
375
376 void DependencyEditorOwners::_fill_owners(EditorFileSystemDirectory *efsd) {
377 if (!efsd) {
378 return;
379 }
380
381 for (int i = 0; i < efsd->get_subdir_count(); i++) {
382 _fill_owners(efsd->get_subdir(i));
383 }
384
385 for (int i = 0; i < efsd->get_file_count(); i++) {
386 Vector<String> deps = efsd->get_file_deps(i);
387 bool found = false;
388 for (int j = 0; j < deps.size(); j++) {
389 if (deps[j] == editing) {
390 found = true;
391 break;
392 }
393 }
394 if (!found) {
395 continue;
396 }
397
398 Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(efsd->get_file_type(i));
399
400 owners->add_item(efsd->get_file_path(i), icon);
401 }
402 }
403
404 void DependencyEditorOwners::show(const String &p_path) {
405 editing = p_path;
406 owners->clear();
407 _fill_owners(EditorFileSystem::get_singleton()->get_filesystem());
408
409 int count = owners->get_item_count();
410 if (count > 0) {
411 empty->hide();
412 owners_count->set_text(vformat(TTR("Owners of: %s (Total: %d)"), p_path.get_file(), count));
413 owners_count->show();
414 owners_mc->show();
415 } else {
416 owners_count->hide();
417 owners_mc->hide();
418 empty->set_text(vformat(TTR("No owners found for: %s"), p_path.get_file()));
419 empty->show();
420 }
421
422 popup_centered_ratio(0.3);
423 }
424
425 DependencyEditorOwners::DependencyEditorOwners() {
426 file_options = memnew(PopupMenu);
427 add_child(file_options);
428 file_options->connect(SceneStringName(id_pressed), callable_mp(this, &DependencyEditorOwners::_file_option));
429
430 VBoxContainer *vbox = memnew(VBoxContainer);
431 add_child(vbox);
432
433 owners_count = memnew(Label);
434 owners_count->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
435 owners_count->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
436 owners_count->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
437 vbox->add_child(owners_count);
438
439 empty = memnew(Label);
440 empty->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
441 empty->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
442 empty->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
443 empty->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
444 empty->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
445 empty->set_v_size_flags(Control::SIZE_EXPAND_FILL);
446 empty->hide();
447 vbox->add_child(empty);
448
449 owners_mc = memnew(MarginContainer);
450 owners_mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
451 owners_mc->set_theme_type_variation("NoBorderHorizontalWindow");
452 vbox->add_child(owners_mc);
453
454 owners = memnew(ItemList);
455 owners->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
456 owners->set_select_mode(ItemList::SELECT_MULTI);
457 owners->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH);
458 owners->connect("item_clicked", callable_mp(this, &DependencyEditorOwners::_list_rmb_clicked));
459 owners->connect("item_activated", callable_mp(this, &DependencyEditorOwners::_select_file));
460 owners->connect("empty_clicked", callable_mp(this, &DependencyEditorOwners::_empty_clicked));
461 owners->set_allow_rmb_select(true);
462 owners_mc->add_child(owners);
463
464 set_title(TTRC("Owners List"));
465 }
466
467 ///////////////////////
468
469 void DependencyRemoveDialog::_find_files_in_removed_folder(EditorFileSystemDirectory *efsd, const String &p_folder) {
470 if (!efsd) {
471 return;
472 }
473
474 for (int i = 0; i < efsd->get_subdir_count(); ++i) {
475 _find_files_in_removed_folder(efsd->get_subdir(i), p_folder);
476 }
477 for (int i = 0; i < efsd->get_file_count(); i++) {
478 String file = efsd->get_file_path(i);
479 ERR_FAIL_COND(all_remove_files.has(file)); //We are deleting a directory which is contained in a directory we are deleting...
480 all_remove_files[file] = p_folder; //Point the file to the ancestor directory we are deleting so we know what to parent it under in the tree.
481 }
482 }
483
484 void DependencyRemoveDialog::_find_all_removed_dependencies(EditorFileSystemDirectory *efsd, Vector<RemovedDependency> &p_removed) {
485 if (!efsd) {
486 return;
487 }
488
489 for (int i = 0; i < efsd->get_subdir_count(); i++) {
490 _find_all_removed_dependencies(efsd->get_subdir(i), p_removed);
491 }
492
493 for (int i = 0; i < efsd->get_file_count(); i++) {
494 const String path = efsd->get_file_path(i);
495
496 //It doesn't matter if a file we are about to delete will have some of its dependencies removed too
497 if (all_remove_files.has(path)) {
498 continue;
499 }
500
501 Vector<String> all_deps = efsd->get_file_deps(i);
502 for (int j = 0; j < all_deps.size(); ++j) {
503 if (all_remove_files.has(all_deps[j])) {
504 RemovedDependency dep;
505 dep.file = path;
506 dep.file_type = efsd->get_file_type(i);
507 dep.dependency = all_deps[j];
508 dep.dependency_folder = all_remove_files[all_deps[j]];
509 p_removed.push_back(dep);
510 }
511 }
512 }
513 }
514
515 void DependencyRemoveDialog::_find_localization_remaps_of_removed_files(Vector<RemovedDependency> &p_removed) {
516 for (KeyValue<String, String> &files : all_remove_files) {
517 const String &path = files.key;
518
519 // Look for dependencies in the translation remaps.
520 if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
521 Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
522
523 if (remaps.has(path)) {
524 RemovedDependency dep;
525 dep.file = TTR("Localization remap");
526 dep.file_type = "";
527 dep.dependency = path;
528 dep.dependency_folder = files.value;
529 p_removed.push_back(dep);
530 }
531
532 for (const KeyValue<Variant, Variant> &remap_kv : remaps) {
533 PackedStringArray remapped_files = remap_kv.value;
534 for (const String &remapped_file : remapped_files) {
535 int splitter_pos = remapped_file.rfind_char(':');
536 String res_path = remapped_file.substr(0, splitter_pos);
537 if (res_path == path) {
538 String locale_name = remapped_file.substr(splitter_pos + 1);
539
540 RemovedDependency dep;
541 dep.file = vformat(TTR("Localization remap for path '%s' and locale '%s'."), remap_kv.key, locale_name);
542 dep.file_type = "";
543 dep.dependency = path;
544 dep.dependency_folder = files.value;
545 p_removed.push_back(dep);
546 }
547 }
548 }
549 }
550 }
551 }
552
553 void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<RemovedDependency> &p_removed) {
554 owners->clear();
555 owners->create_item(); // root
556
557 HashMap<String, TreeItem *> tree_items;
558 for (int i = 0; i < p_removed.size(); i++) {
559 RemovedDependency rd = p_removed[i];
560
561 //Ensure that the dependency is already in the tree
562 if (!tree_items.has(rd.dependency)) {
563 if (rd.dependency_folder.length() > 0) {
564 //Ensure the ancestor folder is already in the tree
565 if (!tree_items.has(rd.dependency_folder)) {
566 TreeItem *folder_item = owners->create_item(owners->get_root());
567 folder_item->set_text(0, rd.dependency_folder);
568 folder_item->set_icon(0, owners->get_editor_theme_icon(SNAME("Folder")));
569 tree_items[rd.dependency_folder] = folder_item;
570 }
571 TreeItem *dependency_item = owners->create_item(tree_items[rd.dependency_folder]);
572 dependency_item->set_text(0, rd.dependency);
573 dependency_item->set_icon(0, owners->get_editor_theme_icon(SNAME("Warning")));
574 tree_items[rd.dependency] = dependency_item;
575 } else {
576 TreeItem *dependency_item = owners->create_item(owners->get_root());
577 dependency_item->set_text(0, rd.dependency);
578 dependency_item->set_icon(0, owners->get_editor_theme_icon(SNAME("Warning")));
579 tree_items[rd.dependency] = dependency_item;
580 }
581 }
582
583 //List this file under this dependency
584 Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(rd.file_type);
585 TreeItem *file_item = owners->create_item(tree_items[rd.dependency]);
586 file_item->set_text(0, rd.file);
587 file_item->set_icon(0, icon);
588 }
589 }
590
591 void DependencyRemoveDialog::_show_files_to_delete_list() {
592 files_to_delete_list->clear();
593
594 for (const String &s : dirs_to_delete) {
595 String t = s.trim_prefix("res://");
596 files_to_delete_list->add_item(t, Ref<Texture2D>(), false);
597 }
598
599 for (const String &s : files_to_delete) {
600 String t = s.trim_prefix("res://");
601 files_to_delete_list->add_item(t, Ref<Texture2D>(), false);
602 }
603 }
604
605 void DependencyRemoveDialog::show(const Vector<String> &p_folders, const Vector<String> &p_files) {
606 all_remove_files.clear();
607 dirs_to_delete.clear();
608 files_to_delete.clear();
609 owners->clear();
610
611 for (int i = 0; i < p_folders.size(); ++i) {
612 String folder = p_folders[i].ends_with("/") ? p_folders[i] : (p_folders[i] + "/");
613 _find_files_in_removed_folder(EditorFileSystem::get_singleton()->get_filesystem_path(folder), folder);
614 dirs_to_delete.push_back(folder);
615 }
616 for (int i = 0; i < p_files.size(); ++i) {
617 all_remove_files[p_files[i]] = String();
618 files_to_delete.push_back(p_files[i]);
619 }
620
621 _show_files_to_delete_list();
622
623 Vector<RemovedDependency> removed_deps;
624 _find_all_removed_dependencies(EditorFileSystem::get_singleton()->get_filesystem(), removed_deps);
625 _find_localization_remaps_of_removed_files(removed_deps);
626 removed_deps.sort();
627 if (removed_deps.is_empty()) {
628 vb_owners->hide();
629 text->set_text(TTR("Remove the selected files from the project? (Cannot be undone.)\nDepending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently."));
630 reset_size();
631 popup_centered();
632 } else {
633 _build_removed_dependency_tree(removed_deps);
634 vb_owners->show();
635 text->set_text(TTR("The files being removed are required by other resources in order for them to work.\nRemove them anyway? (Cannot be undone.)\nDepending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently."));
636 popup_centered(Size2(500, 350));
637 }
638
639 EditorFileSystem::get_singleton()->scan_changes();
640 }
641
642 void DependencyRemoveDialog::ok_pressed() {
643 HashMap<String, StringName> setting_path_map;
644 for (const StringName &setting : path_project_settings) {
645 const String path = ResourceUID::ensure_path(GLOBAL_GET(setting));
646 setting_path_map[path] = setting;
647 }
648
649 bool project_settings_modified = false;
650
651 for (const KeyValue<String, String> &E : all_remove_files) {
652 String file = E.key;
653
654 if (ResourceCache::has(file)) {
655 Ref<Resource> res = ResourceCache::get_ref(file);
656 emit_signal(SNAME("resource_removed"), res);
657 res->set_path("");
658 }
659
660 // If the file we are deleting for e.g. the main scene, default environment,
661 // or audio bus layout, we must clear its definition in Project Settings.
662 const StringName *setting_name = setting_path_map.getptr(file);
663 if (setting_name) {
664 ProjectSettings::get_singleton()->set(*setting_name, "");
665 project_settings_modified = true;
666 }
667 }
668
669 for (const String &file : files_to_delete) {
670 const String path = OS::get_singleton()->get_resource_dir() + file.replace_first("res://", "/");
671 print_verbose("Moving to trash: " + path);
672 Error err = OS::get_singleton()->move_to_trash(path);
673 if (err != OK) {
674 EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + file + "\n");
675 } else {
676 emit_signal(SNAME("file_removed"), file);
677 }
678 }
679 if (project_settings_modified) {
680 ProjectSettings::get_singleton()->save();
681 }
682
683 if (dirs_to_delete.is_empty()) {
684 // If we only deleted files we should only need to tell the file system about the files we touched.
685 for (int i = 0; i < files_to_delete.size(); ++i) {
686 EditorFileSystem::get_singleton()->update_file(files_to_delete[i]);
687 }
688 } else {
689 for (int i = 0; i < dirs_to_delete.size(); ++i) {
690 String path = OS::get_singleton()->get_resource_dir() + dirs_to_delete[i].replace_first("res://", "/");
691 print_verbose("Moving to trash: " + path);
692 Error err = OS::get_singleton()->move_to_trash(path);
693 if (err != OK) {
694 EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + dirs_to_delete[i] + "\n");
695 } else {
696 emit_signal(SNAME("folder_removed"), dirs_to_delete[i]);
697 }
698 }
699
700 EditorFileSystem::get_singleton()->scan_changes();
701 }
702
703 // If some files/dirs would be deleted, favorite dirs need to be updated
704 Vector<String> previous_favorites = EditorSettings::get_singleton()->get_favorites();
705 Vector<String> new_favorites;
706
707 for (int i = 0; i < previous_favorites.size(); ++i) {
708 if (previous_favorites[i].ends_with("/")) {
709 if (!dirs_to_delete.has(previous_favorites[i])) {
710 new_favorites.push_back(previous_favorites[i]);
711 }
712 } else {
713 if (!files_to_delete.has(previous_favorites[i])) {
714 new_favorites.push_back(previous_favorites[i]);
715 }
716 }
717 }
718
719 if (new_favorites.size() < previous_favorites.size()) {
720 EditorSettings::get_singleton()->set_favorites(new_favorites);
721 }
722 }
723
724 void DependencyRemoveDialog::_bind_methods() {
725 ADD_SIGNAL(MethodInfo("resource_removed", PropertyInfo(Variant::OBJECT, "obj")));
726 ADD_SIGNAL(MethodInfo("file_removed", PropertyInfo(Variant::STRING, "file")));
727 ADD_SIGNAL(MethodInfo("folder_removed", PropertyInfo(Variant::STRING, "folder")));
728 }
729
730 DependencyRemoveDialog::DependencyRemoveDialog() {
731 set_ok_button_text(TTR("Remove"));
732
733 VBoxContainer *vb = memnew(VBoxContainer);
734 vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
735 add_child(vb);
736
737 text = memnew(Label);
738 text->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
739 vb->add_child(text);
740
741 Label *files_to_delete_label = memnew(Label);
742 files_to_delete_label->set_theme_type_variation("HeaderSmall");
743 files_to_delete_label->set_text(TTR("Files to be deleted:"));
744 vb->add_child(files_to_delete_label);
745
746 MarginContainer *mc = memnew(MarginContainer);
747 mc->set_theme_type_variation("NoBorderHorizontalWindow");
748 mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
749 vb->add_child(mc);
750
751 files_to_delete_list = memnew(ItemList);
752 files_to_delete_list->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH);
753 files_to_delete_list->set_custom_minimum_size(Size2(0, 94) * EDSCALE);
754 files_to_delete_list->set_accessibility_name(TTRC("Files to be deleted:"));
755 mc->add_child(files_to_delete_list);
756
757 vb_owners = memnew(VBoxContainer);
758 vb_owners->set_h_size_flags(Control::SIZE_EXPAND_FILL);
759 vb_owners->set_v_size_flags(Control::SIZE_EXPAND_FILL);
760 vb->add_child(vb_owners);
761
762 Label *owners_label = memnew(Label);
763 owners_label->set_theme_type_variation("HeaderSmall");
764 owners_label->set_text(TTR("Dependencies of files to be deleted:"));
765 vb_owners->add_child(owners_label);
766
767 mc = memnew(MarginContainer);
768 mc->set_theme_type_variation("NoBorderHorizontalWindow");
769 mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
770 vb_owners->add_child(mc);
771
772 owners = memnew(Tree);
773 owners->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
774 owners->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTH);
775 owners->set_hide_root(true);
776 owners->set_custom_minimum_size(Size2(0, 94) * EDSCALE);
777 owners->set_accessibility_name(TTRC("Dependencies"));
778 mc->add_child(owners);
779 owners->set_v_size_flags(Control::SIZE_EXPAND_FILL);
780
781 List<PropertyInfo> property_list;
782 ProjectSettings::get_singleton()->get_property_list(&property_list);
783 for (const PropertyInfo &pi : property_list) {
784 if (pi.type == Variant::STRING && pi.hint == PROPERTY_HINT_FILE) {
785 path_project_settings.push_back(pi.name);
786 }
787 }
788 }
789
790 //////////////
791 enum {
792 BUTTON_ID_SEARCH,
793 BUTTON_ID_OPEN_DEPS_EDITOR,
794 };
795
796 void DependencyErrorDialog::show(const String &p_for_file, const HashMap<String, HashSet<String>> &p_report) {
797 for_file = p_for_file;
798
799 // TRANSLATORS: The placeholder is a filename.
800 set_title(vformat(TTR("Error loading: %s"), p_for_file.get_file()));
801
802 HashMap<String, HashSet<String>> missing_to_owners;
803 for (const KeyValue<String, HashSet<String>> &E : p_report) {
804 for (const String &missing : E.value) {
805 missing_to_owners[missing].insert(E.key);
806 }
807 }
808
809 files->clear();
810 TreeItem *root = files->create_item(nullptr);
811 Ref<Texture2D> folder_icon = get_theme_icon(SNAME("folder"), SNAME("FileDialog"));
812
813 for (const KeyValue<String, HashSet<String>> &E : missing_to_owners) {
814 const String &missing_path = E.key.get_slice("::", 0);
815 const String &missing_type = E.key.get_slice("::", 1);
816
817 TreeItem *missing_ti = root->create_child();
818 missing_ti->set_text(0, missing_path);
819 missing_ti->set_metadata(0, E.key);
820 missing_ti->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);
821 missing_ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(missing_type));
822 missing_ti->set_icon(1, get_editor_theme_icon(icon_name_fail));
823 missing_ti->add_button(1, folder_icon, BUTTON_ID_SEARCH, false, TTRC("Search"));
824 missing_ti->set_collapsed(true);
825
826 for (const String &owner_path : E.value) {
827 TreeItem *owner_ti = missing_ti->create_child();
828 // TRANSLATORS: The placeholder is a file path.
829 owner_ti->set_text(0, vformat(TTR("Referenced by %s"), owner_path));
830 owner_ti->set_metadata(0, owner_path);
831 owner_ti->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED);
832 owner_ti->add_button(1, files->get_editor_theme_icon(SNAME("Edit")), BUTTON_ID_OPEN_DEPS_EDITOR, false, TTRC("Fix Dependencies"));
833 }
834 }
835
836 set_ok_button_text(TTRC("Open Anyway"));
837 popup_centered();
838 }
839
840 void DependencyErrorDialog::ok_pressed() {
841 EditorNode::get_singleton()->load_scene_or_resource(for_file, !errors_fixed);
842 }
843
844 void DependencyErrorDialog::_on_files_button_clicked(TreeItem *p_item, int p_column, int p_id, MouseButton p_button) {
845 switch (p_id) {
846 case BUTTON_ID_SEARCH: {
847 const String &meta = p_item->get_metadata(0);
848 const String &missing_path = meta.get_slice("::", 0);
849 const String &missing_type = meta.get_slice("::", 1);
850 if (replacement_file_dialog == nullptr) {
851 replacement_file_dialog = memnew(EditorFileDialog);
852 replacement_file_dialog->connect("file_selected", callable_mp(this, &DependencyErrorDialog::_on_replacement_file_selected));
853 replacement_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
854 add_child(replacement_file_dialog);
855 }
856 replacing_item = p_item;
857 _setup_search_file_dialog(replacement_file_dialog, missing_path, missing_type);
858 replacement_file_dialog->popup_file_dialog();
859 } break;
860
861 case BUTTON_ID_OPEN_DEPS_EDITOR: {
862 const String &owner_path = p_item->get_metadata(0);
863 if (deps_editor == nullptr) {
864 deps_editor = memnew(DependencyEditor);
865 deps_editor->connect(SceneStringName(visibility_changed), callable_mp(this, &DependencyErrorDialog::_check_for_resolved));
866 add_child(deps_editor);
867 }
868 deps_editor->edit(owner_path);
869 } break;
870 }
871 }
872
873 void DependencyErrorDialog::_on_replacement_file_selected(const String &p_path) {
874 const String &missing_path = String(replacing_item->get_metadata(0)).get_slice("::", 0);
875
876 for (TreeItem *owner_ti = replacing_item->get_first_child(); owner_ti; owner_ti = owner_ti->get_next()) {
877 const String &owner_path = owner_ti->get_metadata(0);
878 ResourceLoader::rename_dependencies(owner_path, { { missing_path, p_path } });
879 }
880
881 _check_for_resolved();
882 }
883
884 void DependencyErrorDialog::_check_for_resolved() {
885 if (deps_editor && deps_editor->is_visible()) {
886 return; // Only update when the dialog is closed.
887 }
888
889 errors_fixed = true;
890 HashMap<String, LocalVector<String>> owner_deps;
891
892 TreeItem *root = files->get_root();
893 for (TreeItem *missing_ti = root->get_first_child(); missing_ti; missing_ti = missing_ti->get_next()) {
894 bool all_owners_fixed = true;
895
896 for (TreeItem *owner_ti = missing_ti->get_first_child(); owner_ti; owner_ti = owner_ti->get_next()) {
897 const String &owner_path = owner_ti->get_metadata(0);
898
899 if (!owner_deps.has(owner_path)) {
900 List<String> deps;
901 ResourceLoader::get_dependencies(owner_path, &deps);
902
903 LocalVector<String> &stored_paths = owner_deps[owner_path];
904 for (const String &dep : deps) {
905 if (!errors_fixed && !FileAccess::exists(_get_resolved_dep_path(dep))) {
906 errors_fixed = false;
907 }
908 stored_paths.push_back(_get_stored_dep_path(dep));
909 }
910 }
911 const LocalVector<String> &stored_paths = owner_deps[owner_path];
912 const String &missing_path = String(missing_ti->get_metadata(0)).get_slice("::", 0);
913
914 if (stored_paths.has(missing_path)) {
915 all_owners_fixed = false;
916 break;
917 }
918 }
919
920 missing_ti->set_icon(1, get_editor_theme_icon(all_owners_fixed ? icon_name_check : icon_name_fail));
921 }
922
923 set_ok_button_text(errors_fixed ? TTRC("Open") : TTRC("Open Anyway"));
924 }
925
926 DependencyErrorDialog::DependencyErrorDialog() {
927 icon_name_fail = StringName("ImportFail");
928 icon_name_check = StringName("ImportCheck");
929
930 VBoxContainer *vb = memnew(VBoxContainer);
931 add_child(vb);
932
933 files = memnew(Tree);
934 files->set_hide_root(true);
935 files->set_select_mode(Tree::SELECT_ROW);
936 files->set_columns(2);
937 files->set_column_expand(1, false);
938 files->set_v_size_flags(Control::SIZE_EXPAND_FILL);
939 files->connect("button_clicked", callable_mp(this, &DependencyErrorDialog::_on_files_button_clicked));
940 vb->add_margin_child(TTRC("Load failed due to missing dependencies:"), files, true);
941
942 set_min_size(Size2(500, 320) * EDSCALE);
943 set_cancel_button_text(TTRC("Close"));
944
945 Label *text = memnew(Label(TTRC("Which action should be taken?")));
946 text->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
947 vb->add_child(text);
948 }
949
950 //////////////////////////////////////////////////////////////////////
951
952 void OrphanResourcesDialog::ok_pressed() {
953 paths.clear();
954
955 _find_to_delete(files->get_root(), paths);
956 if (paths.is_empty()) {
957 return;
958 }
959
960 delete_confirm->set_text(vformat(TTR("Permanently delete %d item(s)? (No undo!)"), paths.size()));
961 delete_confirm->popup_centered();
962 }
963
964 bool OrphanResourcesDialog::_fill_owners(EditorFileSystemDirectory *efsd, HashMap<String, int> &refs, TreeItem *p_parent) {
965 if (!efsd) {
966 return false;
967 }
968
969 bool has_children = false;
970
971 for (int i = 0; i < efsd->get_subdir_count(); i++) {
972 TreeItem *dir_item = nullptr;
973 if (p_parent) {
974 dir_item = files->create_item(p_parent);
975 dir_item->set_text(0, efsd->get_subdir(i)->get_name());
976 dir_item->set_icon(0, files->get_theme_icon(SNAME("folder"), SNAME("FileDialog")));
977 }
978 bool children = _fill_owners(efsd->get_subdir(i), refs, dir_item);
979
980 if (p_parent) {
981 if (!children) {
982 memdelete(dir_item);
983 } else {
984 has_children = true;
985 }
986 }
987 }
988
989 for (int i = 0; i < efsd->get_file_count(); i++) {
990 if (!p_parent) {
991 Vector<String> deps = efsd->get_file_deps(i);
992 for (int j = 0; j < deps.size(); j++) {
993 if (!refs.has(deps[j])) {
994 refs[deps[j]] = 1;
995 }
996 }
997 } else {
998 String path = efsd->get_file_path(i);
999 if (!refs.has(path)) {
1000 TreeItem *ti = files->create_item(p_parent);
1001 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1002 ti->set_text(0, efsd->get_file(i));
1003 ti->set_editable(0, true);
1004
1005 String type = efsd->get_file_type(i);
1006
1007 Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(type);
1008 ti->set_icon(0, icon);
1009 int ds = efsd->get_file_deps(i).size();
1010 ti->set_text(1, itos(ds));
1011 if (ds) {
1012 ti->add_button(1, files->get_editor_theme_icon(SNAME("GuiVisibilityVisible")), -1, false, TTR("Show Dependencies"));
1013 }
1014 ti->set_metadata(0, path);
1015 has_children = true;
1016 }
1017 }
1018 }
1019
1020 return has_children;
1021 }
1022
1023 void OrphanResourcesDialog::refresh() {
1024 HashMap<String, int> refs;
1025 _fill_owners(EditorFileSystem::get_singleton()->get_filesystem(), refs, nullptr);
1026 files->clear();
1027 TreeItem *root = files->create_item();
1028 _fill_owners(EditorFileSystem::get_singleton()->get_filesystem(), refs, root);
1029 }
1030
1031 void OrphanResourcesDialog::show() {
1032 refresh();
1033 popup_centered_ratio(0.4);
1034 }
1035
1036 void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &r_paths) {
1037 while (p_item) {
1038 if (p_item->get_cell_mode(0) == TreeItem::CELL_MODE_CHECK && p_item->is_checked(0)) {
1039 r_paths.push_back(p_item->get_metadata(0));
1040 }
1041
1042 if (p_item->get_first_child()) {
1043 _find_to_delete(p_item->get_first_child(), r_paths);
1044 }
1045
1046 p_item = p_item->get_next();
1047 }
1048 }
1049
1050 void OrphanResourcesDialog::_delete_confirm() {
1051 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1052 for (const String &E : paths) {
1053 da->remove(E);
1054 EditorFileSystem::get_singleton()->update_file(E);
1055 }
1056 refresh();
1057 }
1058
1059 void OrphanResourcesDialog::_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
1060 if (p_button != MouseButton::LEFT) {
1061 return;
1062 }
1063 TreeItem *ti = Object::cast_to<TreeItem>(p_item);
1064
1065 String path = ti->get_metadata(0);
1066 dep_edit->edit(path);
1067 }
1068
1069 OrphanResourcesDialog::OrphanResourcesDialog() {
1070 set_title(TTR("Orphan Resource Explorer"));
1071 delete_confirm = memnew(ConfirmationDialog);
1072 set_ok_button_text(TTR("Delete"));
1073 add_child(delete_confirm);
1074 dep_edit = memnew(DependencyEditor);
1075 add_child(dep_edit);
1076 delete_confirm->connect(SceneStringName(confirmed), callable_mp(this, &OrphanResourcesDialog::_delete_confirm));
1077 set_hide_on_ok(false);
1078
1079 VBoxContainer *vbc = memnew(VBoxContainer);
1080 add_child(vbc);
1081
1082 files = memnew(Tree);
1083 files->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1084 files->set_theme_type_variation("TreeTable");
1085 files->set_columns(2);
1086 files->set_column_titles_visible(true);
1087 files->set_column_custom_minimum_width(1, 100 * EDSCALE);
1088 files->set_column_expand(0, true);
1089 files->set_column_clip_content(0, true);
1090 files->set_column_expand(1, false);
1091 files->set_column_clip_content(1, true);
1092 files->set_column_title(0, TTR("Resource"));
1093 files->set_column_title(1, TTR("Owns"));
1094 files->set_hide_root(true);
1095 files->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTTOM);
1096 files->connect("button_clicked", callable_mp(this, &OrphanResourcesDialog::_button_pressed));
1097
1098 MarginContainer *mc = vbc->add_margin_child(TTRC("Resources Without Explicit Ownership:"), files, true);
1099 mc->set_theme_type_variation("NoBorderHorizontalWindow");
1100 mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1101 }