1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-29 16:16:38 +00:00

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.
This commit is contained in:
风青山
2025-07-21 11:33:44 +08:00
parent 71a9948157
commit cfb6dc4dc0

View File

@@ -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<Resource> res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
Error err = OK;
Ref<Resource> 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<Ref<Resource>> cached;
ResourceCache::get_cached_resources(&cached);
for (Ref<Resource> &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) {