Browse Source

Expose the logic to recognize a save path in ResourceSaver

Gilles Roudière 2 years ago
parent
commit
e23f82f3c1
3 changed files with 33 additions and 11 deletions
  1. 22 11
      core/io/resource_saver.cpp
  2. 2 0
      core/io/resource_saver.h
  3. 9 0
      doc/classes/ResourceFormatSaver.xml

+ 22 - 11
core/io/resource_saver.cpp

@@ -69,10 +69,31 @@ void ResourceFormatSaver::get_recognized_extensions(const Ref<Resource> &p_resou
 	}
 	}
 }
 }
 
 
+bool ResourceFormatSaver::recognize_path(const Ref<Resource> &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<String> 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() {
 void ResourceFormatSaver::_bind_methods() {
 	GDVIRTUAL_BIND(_save, "resource", "path", "flags");
 	GDVIRTUAL_BIND(_save, "resource", "path", "flags");
 	GDVIRTUAL_BIND(_recognize, "resource");
 	GDVIRTUAL_BIND(_recognize, "resource");
 	GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
 	GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
+	GDVIRTUAL_BIND(_recognize_path, "resource", "path");
 }
 }
 
 
 Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
 Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
@@ -90,17 +111,7 @@ Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path,
 			continue;
 			continue;
 		}
 		}
 
 
-		List<String> 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;
 			continue;
 		}
 		}
 
 

+ 2 - 0
core/io/resource_saver.h

@@ -44,11 +44,13 @@ protected:
 	GDVIRTUAL3R(int64_t, _save, Ref<Resource>, String, uint32_t)
 	GDVIRTUAL3R(int64_t, _save, Ref<Resource>, String, uint32_t)
 	GDVIRTUAL1RC(bool, _recognize, Ref<Resource>)
 	GDVIRTUAL1RC(bool, _recognize, Ref<Resource>)
 	GDVIRTUAL1RC(Vector<String>, _get_recognized_extensions, Ref<Resource>)
 	GDVIRTUAL1RC(Vector<String>, _get_recognized_extensions, Ref<Resource>)
+	GDVIRTUAL2RC(bool, _recognize_path, Ref<Resource>, String)
 
 
 public:
 public:
 	virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
 	virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
 	virtual bool recognize(const Ref<Resource> &p_resource) const;
 	virtual bool recognize(const Ref<Resource> &p_resource) const;
 	virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
 	virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
+	virtual bool recognize_path(const Ref<Resource> &p_resource, const String &p_path) const;
 
 
 	virtual ~ResourceFormatSaver() {}
 	virtual ~ResourceFormatSaver() {}
 };
 };

+ 9 - 0
doc/classes/ResourceFormatSaver.xml

@@ -24,6 +24,15 @@
 				Returns whether the given resource object can be saved by this saver.
 				Returns whether the given resource object can be saved by this saver.
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="_recognize_path" qualifiers="virtual const">
+			<return type="bool" />
+			<param index="0" name="resource" type="Resource" />
+			<param index="1" name="path" type="String" />
+			<description>
+				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].
+			</description>
+		</method>
 		<method name="_save" qualifiers="virtual">
 		<method name="_save" qualifiers="virtual">
 			<return type="int" />
 			<return type="int" />
 			<param index="0" name="resource" type="Resource" />
 			<param index="0" name="resource" type="Resource" />