From e23f82f3c1987f7d275ad638f43d735d4081f69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilles=20Roudi=C3=A8re?= Date: Wed, 19 Oct 2022 10:46:51 +0200 Subject: Expose the logic to recognize a save path in ResourceSaver --- core/io/resource_saver.cpp | 33 ++++++++++++++++++++++----------- core/io/resource_saver.h | 2 ++ doc/classes/ResourceFormatSaver.xml | 9 +++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index 2f863baaac..6cda840604 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -69,10 +69,31 @@ void ResourceFormatSaver::get_recognized_extensions(const Ref &p_resou } } +bool ResourceFormatSaver::recognize_path(const Ref &p_resource, const String &p_path) const { + bool ret = false; + if (GDVIRTUAL_CALL(_recognize_path, p_resource, p_path, ret)) { + return ret; + } + + String extension = p_path.get_extension(); + + List extensions; + get_recognized_extensions(p_resource, &extensions); + + for (const String &E : extensions) { + if (E.nocasecmp_to(extension) == 0) { + return true; + } + } + + return false; +} + void ResourceFormatSaver::_bind_methods() { GDVIRTUAL_BIND(_save, "resource", "path", "flags"); GDVIRTUAL_BIND(_recognize, "resource"); GDVIRTUAL_BIND(_get_recognized_extensions, "resource"); + GDVIRTUAL_BIND(_recognize_path, "resource", "path"); } Error ResourceSaver::save(const Ref &p_resource, const String &p_path, uint32_t p_flags) { @@ -90,17 +111,7 @@ Error ResourceSaver::save(const Ref &p_resource, const String &p_path, continue; } - List extensions; - bool recognized = false; - saver[i]->get_recognized_extensions(p_resource, &extensions); - - for (const String &E : extensions) { - if (E.nocasecmp_to(extension) == 0) { - recognized = true; - } - } - - if (!recognized) { + if (!saver[i]->recognize_path(p_resource, path)) { continue; } diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h index 4fee2bcfd1..5e48ce88c3 100644 --- a/core/io/resource_saver.h +++ b/core/io/resource_saver.h @@ -44,11 +44,13 @@ protected: GDVIRTUAL3R(int64_t, _save, Ref, String, uint32_t) GDVIRTUAL1RC(bool, _recognize, Ref) GDVIRTUAL1RC(Vector, _get_recognized_extensions, Ref) + GDVIRTUAL2RC(bool, _recognize_path, Ref, String) public: virtual Error save(const Ref &p_resource, const String &p_path, uint32_t p_flags = 0); virtual bool recognize(const Ref &p_resource) const; virtual void get_recognized_extensions(const Ref &p_resource, List *p_extensions) const; + virtual bool recognize_path(const Ref &p_resource, const String &p_path) const; virtual ~ResourceFormatSaver() {} }; diff --git a/doc/classes/ResourceFormatSaver.xml b/doc/classes/ResourceFormatSaver.xml index 05bfcf3446..1f2af6d157 100644 --- a/doc/classes/ResourceFormatSaver.xml +++ b/doc/classes/ResourceFormatSaver.xml @@ -24,6 +24,15 @@ Returns whether the given resource object can be saved by this saver. + + + + + + Returns [code]true[/code] if this saver handles a given save path and [code]false[/code] otherwise. + If this method is not implemented, the default behavior returns whether the path's extension is within the ones provided by [method _get_recognized_extensions]. + + -- cgit v1.2.3