From e653c79ef786cb00f739388df987b7105a7c55c8 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Mon, 4 Mar 2019 11:06:15 -0300 Subject: [PATCH] Better warnings when resources can't be saved. Fixes #26531 --- core/io/resource_importer.h | 1 + core/io/resource_loader.cpp | 25 +++++++++++++++++++++++++ core/io/resource_loader.h | 2 ++ editor/editor_node.cpp | 18 +++++++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/core/io/resource_importer.h b/core/io/resource_importer.h index 1d27d4dec34..ca40b47b853 100644 --- a/core/io/resource_importer.h +++ b/core/io/resource_importer.h @@ -68,6 +68,7 @@ public: virtual Variant get_resource_metadata(const String &p_path) const; virtual bool is_import_valid(const String &p_path) const; virtual void get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types = false); + virtual bool is_imported(const String &p_path) const { return recognize_path(p_path); } virtual bool can_be_imported(const String &p_path) const; virtual int get_import_order(const String &p_path) const; diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 98309048bb0..c917b9ba28d 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -633,6 +633,31 @@ bool ResourceLoader::is_import_valid(const String &p_path) { return false; //not found } +bool ResourceLoader::is_imported(const String &p_path) { + + String path = _path_remap(p_path); + + String local_path; + if (path.is_rel_path()) + local_path = "res://" + path; + else + local_path = ProjectSettings::get_singleton()->localize_path(path); + + for (int i = 0; i < loader_count; i++) { + + if (!loader[i]->recognize_path(local_path)) + continue; + /* + if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) + continue; + */ + + return loader[i]->is_imported(p_path); + } + + return false; //not found +} + void ResourceLoader::get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types) { String path = _path_remap(p_path); diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 70eb1a3de0e..ca7610a0d24 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -79,6 +79,7 @@ public: virtual void get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types = false); virtual Error rename_dependencies(const String &p_path, const Map &p_map); virtual bool is_import_valid(const String &p_path) const { return true; } + virtual bool is_imported(const String &p_path) const { return false; } virtual int get_import_order(const String &p_path) const { return 0; } virtual ~ResourceFormatLoader() {} @@ -154,6 +155,7 @@ public: static void get_dependencies(const String &p_path, List *p_dependencies, bool p_add_types = false); static Error rename_dependencies(const String &p_path, const Map &p_map); static bool is_import_valid(const String &p_path); + static bool is_imported(const String &p_path); static int get_import_order(const String &p_path); static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; } diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index afaf90a1587..1c9e07ae5b8 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -619,7 +619,11 @@ void EditorNode::save_resource_in_path(const Ref &p_resource, const St Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS); if (err != OK) { - show_accept(TTR("Error saving resource!"), TTR("OK")); + if (ResourceLoader::is_imported(p_resource->get_path())) { + show_accept(TTR("Imported resources can't be saved."), TTR("OK")); + } else { + show_accept(TTR("Error saving resource!"), TTR("OK")); + } return; } @@ -639,6 +643,18 @@ void EditorNode::save_resource(const Ref &p_resource) { void EditorNode::save_resource_as(const Ref &p_resource, const String &p_at_path) { + { + String path = p_resource->get_path(); + int srpos = path.find("::"); + if (srpos != -1) { + String base = path.substr(0, srpos); + if (!get_edited_scene() || get_edited_scene()->get_filename() != base) { + show_warning(TTR("This resource can't be saved because it does not belong to the edited scene. Make it unique first.")); + return; + } + } + } + file->set_mode(EditorFileDialog::MODE_SAVE_FILE); saving_resource = p_resource;