From efdcf205d2b428580ee9b4a50572649bc06b4276 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Mon, 27 Jun 2016 13:17:20 -0300 Subject: [PATCH] Make most resources (save for packedscenes and scripts) reload if they change on disk. Closes #4059. --- core/resource.cpp | 28 +++++++++++++++++++++++++--- core/resource.h | 2 +- core/script_language.h | 1 + scene/resources/packed_scene.h | 1 + scene/resources/sample.cpp | 2 ++ scene/resources/sample.h | 1 + scene/resources/texture.cpp | 4 ---- scene/resources/texture.h | 1 - tools/editor/editor_node.cpp | 6 ++++-- 9 files changed, 35 insertions(+), 11 deletions(-) diff --git a/core/resource.cpp b/core/resource.cpp index 97dee3e1d70b..b80ec7012dcd 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -30,7 +30,7 @@ #include "core_string_names.h" #include #include "os/file_access.h" - +#include "io/resource_loader.h" void ResourceImportMetadata::set_editor(const String& p_editor) { @@ -218,14 +218,36 @@ String Resource::get_name() const { return name; } -bool Resource::can_reload_from_file() { +bool Resource::editor_can_reload_from_file() { - return false; + return true; //by default yes } void Resource::reload_from_file() { + String path=get_path(); + if (!path.is_resource_file()) + return; + + Ref s = ResourceLoader::load(path,get_type(),true); + + if (!s.is_valid()) + return; + + List pi; + s->get_property_list(&pi); + + for (List::Element *E=pi.front();E;E=E->next()) { + + if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) + continue; + if (E->get().name=="resource/path") + continue; //do not change path + + set(E->get().name,s->get(E->get().name)); + + } } diff --git a/core/resource.h b/core/resource.h index 958414f62bc5..0673a4e89d1e 100644 --- a/core/resource.h +++ b/core/resource.h @@ -121,7 +121,7 @@ friend class ResourceCache; void _take_over_path(const String& p_path); public: - virtual bool can_reload_from_file(); + virtual bool editor_can_reload_from_file(); virtual void reload_from_file(); void register_owner(Object *p_owner); diff --git a/core/script_language.h b/core/script_language.h index 51fb351fde40..6d75b83aaf4b 100644 --- a/core/script_language.h +++ b/core/script_language.h @@ -78,6 +78,7 @@ class Script : public Resource { protected: + virtual bool editor_can_reload_from_file() { return false; } // this is handled by editor better void _notification( int p_what); static void _bind_methods(); diff --git a/scene/resources/packed_scene.h b/scene/resources/packed_scene.h index 6bde508d7200..3b6c0898e0f6 100644 --- a/scene/resources/packed_scene.h +++ b/scene/resources/packed_scene.h @@ -196,6 +196,7 @@ class PackedScene : public Resource { protected: + virtual bool editor_can_reload_from_file() { return false; } // this is handled by editor better static void _bind_methods(); public: diff --git a/scene/resources/sample.cpp b/scene/resources/sample.cpp index 87fcfc425da8..aae4e85a2737 100644 --- a/scene/resources/sample.cpp +++ b/scene/resources/sample.cpp @@ -187,6 +187,8 @@ RID Sample::get_rid() const { return sample; } + + void Sample::_bind_methods(){ diff --git a/scene/resources/sample.h b/scene/resources/sample.h index 0a8816723355..18672e41c020 100644 --- a/scene/resources/sample.h +++ b/scene/resources/sample.h @@ -75,6 +75,7 @@ class Sample : public Resource { public: + void create(Format p_format, bool p_stereo, int p_length); Format get_format() const; diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index 2fa00c7da7c7..726b1938c46f 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -98,10 +98,6 @@ Texture::Texture() { -bool ImageTexture::can_reload_from_file() { - - return true; -} void ImageTexture::reload_from_file() { diff --git a/scene/resources/texture.h b/scene/resources/texture.h index 103b425cd820..05ea8339789b 100644 --- a/scene/resources/texture.h +++ b/scene/resources/texture.h @@ -103,7 +103,6 @@ class ImageTexture : public Texture { float lossy_storage_quality; protected: - virtual bool can_reload_from_file(); virtual void reload_from_file(); bool _set(const StringName& p_name, const Variant& p_value); diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index 3ba9f3b79943..35b41a8bb9f6 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -2804,10 +2804,12 @@ void EditorNode::_menu_option_confirm(int p_option,bool p_confirmed) { List > cached; ResourceCache::get_cached_resources(&cached); - + //this should probably be done in a thread.. for(List >::Element *E=cached.front();E;E=E->next()) { - if (!E->get()->can_reload_from_file()) + if (!E->get()->editor_can_reload_from_file()) + continue; + if (!E->get()->get_path().is_resource_file() && !E->get()->get_path().is_abs_path()) continue; if (!FileAccess::exists(E->get()->get_path())) continue;