Skip to content

Commit cfb6dc4

Browse files
committed
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.
1 parent 71a9948 commit cfb6dc4

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

editor/file_system/editor_file_system.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,8 +3105,31 @@ Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) {
31053105
}
31063106
} else {
31073107
// Load the resource and save it again in the new location (this generates a new UID).
3108-
Error err;
3109-
Ref<Resource> res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
3108+
Error err = OK;
3109+
Ref<Resource> res = ResourceCache::get_ref(p_from);
3110+
if (res.is_null()) {
3111+
res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
3112+
} else {
3113+
bool edited = false;
3114+
List<Ref<Resource>> cached;
3115+
ResourceCache::get_cached_resources(&cached);
3116+
for (Ref<Resource> &resource : cached) {
3117+
if (!resource->is_edited()) {
3118+
continue;
3119+
}
3120+
if (!resource->get_path().begins_with(p_from)) {
3121+
continue;
3122+
}
3123+
// The resource or one of its built-in resources is edited.
3124+
edited = true;
3125+
resource->set_edited(false);
3126+
}
3127+
3128+
if (edited) {
3129+
// Save cached resources to prevent changes from being lost and to prevent discrepancies.
3130+
EditorNode::get_singleton()->save_resource(res);
3131+
}
3132+
}
31103133
if (err == OK && res.is_valid()) {
31113134
err = ResourceSaver::save(res, p_to, ResourceSaver::FLAG_COMPRESS);
31123135
if (err != OK) {

0 commit comments

Comments
 (0)