Explorar el Código

Merge pull request #56442 from PucklaMotzer09/remap_files_moved

Rémi Verschelde hace 3 años
padre
commit
2d372d9e10

+ 6 - 0
core/io/resource_loader.cpp

@@ -819,6 +819,12 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem
 		if (r_translation_remapped) {
 			*r_translation_remapped = true;
 		}
+
+		// Fallback to p_path if new_path does not exist.
+		if (!FileAccess::exists(new_path)) {
+			WARN_PRINT(vformat("Translation remap '%s' does not exist. Falling back to '%s'.", new_path, p_path));
+			new_path = p_path;
+		}
 	}
 
 	if (path_remaps.has(new_path)) {

+ 40 - 0
editor/dependency_editor.cpp

@@ -417,6 +417,45 @@ void DependencyRemoveDialog::_find_all_removed_dependencies(EditorFileSystemDire
 	}
 }
 
+void DependencyRemoveDialog::_find_localization_remaps_of_removed_files(Vector<RemovedDependency> &p_removed) {
+	for (KeyValue<String, String> &files : all_remove_files) {
+		const String &path = files.key;
+
+		// Look for dependencies in the translation remaps.
+		if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
+			Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
+
+			if (remaps.has(path)) {
+				RemovedDependency dep;
+				dep.file = TTR("Localization remap");
+				dep.file_type = "";
+				dep.dependency = path;
+				dep.dependency_folder = files.value;
+				p_removed.push_back(dep);
+			}
+
+			Array remap_keys = remaps.keys();
+			for (int j = 0; j < remap_keys.size(); j++) {
+				PackedStringArray remapped_files = remaps[remap_keys[j]];
+				for (int k = 0; k < remapped_files.size(); k++) {
+					int splitter_pos = remapped_files[k].rfind(":");
+					String res_path = remapped_files[k].substr(0, splitter_pos);
+					if (res_path == path) {
+						String locale_name = remapped_files[k].substr(splitter_pos + 1);
+
+						RemovedDependency dep;
+						dep.file = vformat(TTR("Localization remap for path '%s' and locale '%s'."), remap_keys[j], locale_name);
+						dep.file_type = "";
+						dep.dependency = path;
+						dep.dependency_folder = files.value;
+						p_removed.push_back(dep);
+					}
+				}
+			}
+		}
+	}
+}
+
 void DependencyRemoveDialog::_build_removed_dependency_tree(const Vector<RemovedDependency> &p_removed) {
 	owners->clear();
 	owners->create_item(); // root
@@ -473,6 +512,7 @@ void DependencyRemoveDialog::show(const Vector<String> &p_folders, const Vector<
 
 	Vector<RemovedDependency> removed_deps;
 	_find_all_removed_dependencies(EditorFileSystem::get_singleton()->get_filesystem(), removed_deps);
+	_find_localization_remaps_of_removed_files(removed_deps);
 	removed_deps.sort();
 	if (removed_deps.is_empty()) {
 		owners->hide();

+ 1 - 0
editor/dependency_editor.h

@@ -119,6 +119,7 @@ class DependencyRemoveDialog : public ConfirmationDialog {
 
 	void _find_files_in_removed_folder(EditorFileSystemDirectory *efsd, const String &p_folder);
 	void _find_all_removed_dependencies(EditorFileSystemDirectory *efsd, Vector<RemovedDependency> &p_removed);
+	void _find_localization_remaps_of_removed_files(Vector<RemovedDependency> &p_removed);
 	void _build_removed_dependency_tree(const Vector<RemovedDependency> &p_removed);
 
 	void ok_pressed() override;

+ 1 - 0
editor/editor_node.cpp

@@ -6875,6 +6875,7 @@ EditorNode::EditorNode() {
 	filesystem_dock->connect("inherit", callable_mp(this, &EditorNode::_inherit_request));
 	filesystem_dock->connect("instance", callable_mp(this, &EditorNode::_instantiate_request));
 	filesystem_dock->connect("display_mode_changed", callable_mp(this, &EditorNode::_save_docks));
+	get_project_settings()->connect_filesystem_dock_signals(filesystem_dock);
 
 	// Scene: Top left.
 	dock_slot[DOCK_SLOT_LEFT_UR]->add_child(SceneTreeDock::get_singleton());

+ 103 - 0
editor/localization_editor.cpp

@@ -36,6 +36,7 @@
 #include "editor/editor_node.h"
 #include "editor/editor_scale.h"
 #include "editor/editor_translation_parser.h"
+#include "editor/filesystem_dock.h"
 #include "editor/pot_generator.h"
 #include "scene/gui/control.h"
 
@@ -379,6 +380,95 @@ void LocalizationEditor::_update_pot_file_extensions() {
 	}
 }
 
+void LocalizationEditor::connect_filesystem_dock_signals(FileSystemDock *p_fs_dock) {
+	p_fs_dock->connect("files_moved", callable_mp(this, &LocalizationEditor::_filesystem_files_moved));
+	p_fs_dock->connect("file_removed", callable_mp(this, &LocalizationEditor::_filesystem_file_removed));
+}
+
+void LocalizationEditor::_filesystem_files_moved(const String &p_old_file, const String &p_new_file) {
+	// Update remaps if the moved file is a part of them.
+	Dictionary remaps;
+	bool remaps_changed = false;
+
+	if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
+		remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
+	}
+
+	// Check for the keys.
+	if (remaps.has(p_old_file)) {
+		PackedStringArray remapped_files = remaps[p_old_file];
+		remaps.erase(p_old_file);
+		remaps[p_new_file] = remapped_files;
+		remaps_changed = true;
+		print_verbose(vformat("Changed remap key \"%s\" to \"%s\" due to a moved file.", p_old_file, p_new_file));
+	}
+
+	// Check for the Array elements of the values.
+	Array remap_keys = remaps.keys();
+	for (int i = 0; i < remap_keys.size(); i++) {
+		PackedStringArray remapped_files = remaps[remap_keys[i]];
+		bool remapped_files_updated = false;
+
+		for (int j = 0; j < remapped_files.size(); j++) {
+			int splitter_pos = remapped_files[j].rfind(":");
+			String res_path = remapped_files[j].substr(0, splitter_pos);
+
+			if (res_path == p_old_file) {
+				String locale_name = remapped_files[j].substr(splitter_pos + 1);
+				// Replace the element at that index.
+				remapped_files.insert(j, p_new_file + ":" + locale_name);
+				remapped_files.remove_at(j + 1);
+				remaps_changed = true;
+				remapped_files_updated = true;
+				print_verbose(vformat("Changed remap value \"%s\" to \"%s\" of key \"%s\" due to a moved file.", res_path + ":" + locale_name, remapped_files[j], remap_keys[i]));
+			}
+		}
+
+		if (remapped_files_updated) {
+			remaps[remap_keys[i]] = remapped_files;
+		}
+	}
+
+	if (remaps_changed) {
+		ProjectSettings::get_singleton()->set_setting("internationalization/locale/translation_remaps", remaps);
+		update_translations();
+		emit_signal("localization_changed");
+	}
+}
+
+void LocalizationEditor::_filesystem_file_removed(const String &p_file) {
+	// Check if the remaps are affected.
+	Dictionary remaps;
+
+	if (ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
+		remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
+	}
+
+	bool remaps_changed = remaps.has(p_file);
+
+	if (!remaps_changed) {
+		Array remap_keys = remaps.keys();
+		for (int i = 0; i < remap_keys.size() && !remaps_changed; i++) {
+			PackedStringArray remapped_files = remaps[remap_keys[i]];
+			for (int j = 0; j < remapped_files.size() && !remaps_changed; j++) {
+				int splitter_pos = remapped_files[j].rfind(":");
+				String res_path = remapped_files[j].substr(0, splitter_pos);
+				remaps_changed = p_file == res_path;
+				if (remaps_changed) {
+					print_verbose(vformat("Remap value \"%s\" of key \"%s\" has been removed from the file system.", remapped_files[j], remap_keys[i]));
+				}
+			}
+		}
+	} else {
+		print_verbose(vformat("Remap key \"%s\" has been removed from the file system.", p_file));
+	}
+
+	if (remaps_changed) {
+		update_translations();
+		emit_signal("localization_changed");
+	}
+}
+
 void LocalizationEditor::update_translations() {
 	if (updating_translations) {
 		return;
@@ -432,6 +522,13 @@ void LocalizationEditor::update_translations() {
 			t->set_tooltip(0, keys[i]);
 			t->set_metadata(0, keys[i]);
 			t->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), 0, false, TTR("Remove"));
+
+			// Display that it has been removed if this is the case.
+			if (!FileAccess::exists(keys[i])) {
+				t->set_text(0, t->get_text(0) + vformat(" (%s)", TTR("Removed")));
+				t->set_tooltip(0, vformat(TTR("%s cannot be found."), t->get_tooltip(0)));
+			}
+
 			if (keys[i] == remap_selected) {
 				t->select(0);
 				translation_res_option_add_button->set_disabled(false);
@@ -454,6 +551,12 @@ void LocalizationEditor::update_translations() {
 					t2->set_editable(1, true);
 					t2->set_metadata(1, path);
 					t2->set_tooltip(1, locale);
+
+					// Display that it has been removed if this is the case.
+					if (!FileAccess::exists(path)) {
+						t2->set_text(0, t2->get_text(0) + vformat(" (%s)", TTR("Removed")));
+						t2->set_tooltip(0, vformat(TTR("%s cannot be found."), t2->get_tooltip(0)));
+					}
 				}
 			}
 		}

+ 5 - 0
editor/localization_editor.h

@@ -36,6 +36,7 @@
 #include "scene/gui/tree.h"
 
 class EditorFileDialog;
+class FileSystemDock;
 
 class LocalizationEditor : public VBoxContainer {
 	GDCLASS(LocalizationEditor, VBoxContainer);
@@ -81,6 +82,9 @@ class LocalizationEditor : public VBoxContainer {
 	void _pot_generate(const String &p_file);
 	void _update_pot_file_extensions();
 
+	void _filesystem_files_moved(const String &p_old_file, const String &p_new_file);
+	void _filesystem_file_removed(const String &p_file);
+
 protected:
 	void _notification(int p_what);
 	static void _bind_methods();
@@ -88,6 +92,7 @@ protected:
 public:
 	void add_translation(const String &p_translation);
 	void update_translations();
+	void connect_filesystem_dock_signals(FileSystemDock *p_fs_dock);
 
 	LocalizationEditor();
 };

+ 4 - 0
editor/project_settings_editor.cpp

@@ -39,6 +39,10 @@
 
 ProjectSettingsEditor *ProjectSettingsEditor::singleton = nullptr;
 
+void ProjectSettingsEditor::connect_filesystem_dock_signals(FileSystemDock *p_fs_dock) {
+	localization_editor->connect_filesystem_dock_signals(p_fs_dock);
+}
+
 void ProjectSettingsEditor::popup_project_settings() {
 	// Restore valid window bounds or pop up at default size.
 	Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "project_settings", Rect2());

+ 3 - 0
editor/project_settings_editor.h

@@ -43,6 +43,8 @@
 #include "editor/shader_globals_editor.h"
 #include "scene/gui/tab_container.h"
 
+class FileSystemDock;
+
 class ProjectSettingsEditor : public AcceptDialog {
 	GDCLASS(ProjectSettingsEditor, AcceptDialog);
 
@@ -120,6 +122,7 @@ public:
 	TabContainer *get_tabs() { return tab_container; }
 
 	void queue_save();
+	void connect_filesystem_dock_signals(FileSystemDock *p_fs_dock);
 
 	ProjectSettingsEditor(EditorData *p_data);
 };