From cfb6dc4dc08a9cfb91bcda396af7d43c0aea7972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E9=9D=92=E5=B1=B1?= Date: Mon, 21 Jul 2025 11:33:44 +0800 Subject: [PATCH] Save the changes before duplicating resource files First synchronize the in-memory data to the file, and then copy the file. This can avoid differences between the copied file and the source file. --- editor/file_system/editor_file_system.cpp | 27 +++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/editor/file_system/editor_file_system.cpp b/editor/file_system/editor_file_system.cpp index 18c3aba700..4e54285929 100644 --- a/editor/file_system/editor_file_system.cpp +++ b/editor/file_system/editor_file_system.cpp @@ -3105,8 +3105,31 @@ Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) { } } else { // Load the resource and save it again in the new location (this generates a new UID). - Error err; - Ref res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err); + Error err = OK; + Ref res = ResourceCache::get_ref(p_from); + if (res.is_null()) { + res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err); + } else { + bool edited = false; + List> cached; + ResourceCache::get_cached_resources(&cached); + for (Ref &resource : cached) { + if (!resource->is_edited()) { + continue; + } + if (!resource->get_path().begins_with(p_from)) { + continue; + } + // The resource or one of its built-in resources is edited. + edited = true; + resource->set_edited(false); + } + + if (edited) { + // Save cached resources to prevent changes from being lost and to prevent discrepancies. + EditorNode::get_singleton()->save_resource(res); + } + } if (err == OK && res.is_valid()) { err = ResourceSaver::save(res, p_to, ResourceSaver::FLAG_COMPRESS); if (err != OK) {