Don't rescan filesystem when duplicating

This commit is contained in:
kobewi
2024-09-16 20:48:51 +02:00
parent f032af7453
commit 7377ca8d7d
3 changed files with 171 additions and 82 deletions

View File

@@ -1491,76 +1491,22 @@ void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const Strin
EditorNode::get_singleton()->add_io_error(TTR("Cannot move a folder into itself.") + "\n" + old_path + "\n");
return;
}
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (p_item.is_file) {
print_verbose("Duplicating " + old_path + " -> " + new_path);
// Create the directory structure.
da->make_dir_recursive(new_path.get_base_dir());
EditorFileSystem::get_singleton()->make_dir_recursive(p_new_path.get_base_dir());
if (FileAccess::exists(old_path + ".import")) {
Error err = da->copy(old_path, new_path);
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ": " + error_names[err] + "\n");
return;
}
// Remove uid from .import file to avoid conflict.
Ref<ConfigFile> cfg;
cfg.instantiate();
cfg->load(old_path + ".import");
cfg->erase_section_key("remap", "uid");
err = cfg->save(new_path + ".import");
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ".import: " + error_names[err] + "\n");
return;
}
} else {
// Files which do not use an uid can just be copied.
if (ResourceLoader::get_resource_uid(old_path) == ResourceUID::INVALID_ID) {
Error err = da->copy(old_path, new_path);
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ": " + error_names[err] + "\n");
}
return;
}
// Load the resource and save it again in the new location (this generates a new UID).
Error err;
Ref<Resource> res = ResourceLoader::load(old_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
if (err == OK && res.is_valid()) {
err = ResourceSaver::save(res, new_path, ResourceSaver::FLAG_COMPRESS);
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + " " + vformat(TTR("Failed to save resource at %s: %s"), new_path, error_names[err]));
}
} else if (err != OK) {
// When loading files like text files the error is OK but the resource is still null.
// We can ignore such files.
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + " " + vformat(TTR("Failed to load resource at %s: %s"), new_path, error_names[err]));
}
Error err = EditorFileSystem::get_singleton()->copy_file(old_path, new_path);
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + ": " + error_names[err] + "\n");
}
} else {
da->make_dir(new_path);
// Recursively duplicate all files inside the folder.
Ref<DirAccess> old_dir = DirAccess::open(old_path);
ERR_FAIL_COND(old_dir.is_null());
Ref<FileAccess> file_access = FileAccess::create(FileAccess::ACCESS_RESOURCES);
old_dir->set_include_navigational(false);
old_dir->list_dir_begin();
for (String f = old_dir->_get_next(); !f.is_empty(); f = old_dir->_get_next()) {
if (f.get_extension() == "import") {
continue;
}
if (file_access->file_exists(old_path + f)) {
_try_duplicate_item(FileOrFolder(old_path + f, true), new_path + f);
} else if (da->dir_exists(old_path + f)) {
_try_duplicate_item(FileOrFolder(old_path + f, false), new_path + f);
}
Error err = EditorFileSystem::get_singleton()->copy_directory(old_path, new_path);
if (err != OK) {
EditorNode::get_singleton()->add_io_error(TTR("Error duplicating directory:") + "\n" + old_path + "\n");
}
old_dir->list_dir_end();
}
}
@@ -1866,21 +1812,15 @@ void FileSystemDock::_rename_operation_confirm() {
}
void FileSystemDock::_duplicate_operation_confirm(const String &p_path) {
String base_dir = p_path.trim_suffix("/").get_base_dir();
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (!da->dir_exists(base_dir)) {
Error err = da->make_dir_recursive(base_dir);
const String base_dir = p_path.trim_suffix("/").get_base_dir();
if (!DirAccess::dir_exists_absolute(base_dir)) {
Error err = EditorFileSystem::get_singleton()->make_dir_recursive(base_dir);
if (err != OK) {
EditorNode::get_singleton()->show_warning(vformat(TTR("Could not create base directory: %s"), error_names[err]));
return;
}
}
_try_duplicate_item(to_duplicate, p_path);
// Rescan everything.
print_verbose("FileSystem: calling rescan.");
_rescan();
}
void FileSystemDock::_overwrite_dialog_action(bool p_overwrite) {