From 8e9c4e04be67f329e0b2e862566b5e1686fa5ccb Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 30 Dec 2024 13:00:37 +0100 Subject: [PATCH] Assign new UID when duplicating file externally --- editor/editor_file_system.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 89cba03d4eb..478a19d6412 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -843,11 +843,26 @@ bool EditorFileSystem::_update_scan_actions() { fs_changed = true; + const String new_file_path = ia.dir->get_file_path(idx); + const ResourceUID::ID existing_id = ResourceLoader::get_resource_uid(new_file_path); + if (existing_id != ResourceUID::INVALID_ID) { + const String old_path = ResourceUID::get_singleton()->get_id_path(existing_id); + if (old_path != new_file_path && FileAccess::exists(old_path)) { + const ResourceUID::ID new_id = ResourceUID::get_singleton()->create_id(); + ResourceUID::get_singleton()->add_id(new_id, new_file_path); + ResourceSaver::set_uid(new_file_path, new_id); + WARN_PRINT(vformat("Duplicate UID detected for Resource at \"%s\".\nOld Resource path: \"%s\". The new file UID was changed automatically.", new_file_path, old_path)); + } else { + // Re-assign the UID to file, just in case it was pulled from cache. + ResourceSaver::set_uid(new_file_path, existing_id); + } + } + if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) { - _queue_update_script_class(ia.dir->get_file_path(idx), ia.new_file->type, ia.new_file->script_class_name, ia.new_file->script_class_extends, ia.new_file->script_class_icon_path); + _queue_update_script_class(new_file_path, ia.new_file->type, ia.new_file->script_class_name, ia.new_file->script_class_extends, ia.new_file->script_class_icon_path); } if (ia.new_file->type == SNAME("PackedScene")) { - _queue_update_scene_groups(ia.dir->get_file_path(idx)); + _queue_update_scene_groups(new_file_path); } } break;