1 /*************************************************************************/
2 /*  resource_saver.cpp                                                   */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "resource_saver.h"
32 #include "core/io/resource_loader.h"
33 #include "core/os/file_access.h"
34 #include "core/project_settings.h"
35 #include "core/script_language.h"
36 
37 Ref<ResourceFormatSaver> ResourceSaver::saver[MAX_SAVERS];
38 
39 int ResourceSaver::saver_count = 0;
40 bool ResourceSaver::timestamp_on_save = false;
41 ResourceSavedCallback ResourceSaver::save_callback = 0;
42 
save(const String & p_path,const RES & p_resource,uint32_t p_flags)43 Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
44 
45 	if (get_script_instance() && get_script_instance()->has_method("save")) {
46 		return (Error)get_script_instance()->call("save", p_path, p_resource, p_flags).operator int64_t();
47 	}
48 
49 	return ERR_METHOD_NOT_FOUND;
50 }
51 
recognize(const RES & p_resource) const52 bool ResourceFormatSaver::recognize(const RES &p_resource) const {
53 
54 	if (get_script_instance() && get_script_instance()->has_method("recognize")) {
55 		return get_script_instance()->call("recognize", p_resource);
56 	}
57 
58 	return false;
59 }
60 
get_recognized_extensions(const RES & p_resource,List<String> * p_extensions) const61 void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
62 
63 	if (get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")) {
64 		PoolStringArray exts = get_script_instance()->call("get_recognized_extensions", p_resource);
65 
66 		{
67 			PoolStringArray::Read r = exts.read();
68 			for (int i = 0; i < exts.size(); ++i) {
69 				p_extensions->push_back(r[i]);
70 			}
71 		}
72 	}
73 }
74 
_bind_methods()75 void ResourceFormatSaver::_bind_methods() {
76 
77 	{
78 		PropertyInfo arg0 = PropertyInfo(Variant::STRING, "path");
79 		PropertyInfo arg1 = PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
80 		PropertyInfo arg2 = PropertyInfo(Variant::INT, "flags");
81 		ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "save", arg0, arg1, arg2));
82 	}
83 
84 	ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::POOL_STRING_ARRAY, "get_recognized_extensions", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
85 	ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "recognize", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
86 }
87 
save(const String & p_path,const RES & p_resource,uint32_t p_flags)88 Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
89 
90 	String extension = p_path.get_extension();
91 	Error err = ERR_FILE_UNRECOGNIZED;
92 
93 	for (int i = 0; i < saver_count; i++) {
94 
95 		if (!saver[i]->recognize(p_resource))
96 			continue;
97 
98 		List<String> extensions;
99 		bool recognized = false;
100 		saver[i]->get_recognized_extensions(p_resource, &extensions);
101 
102 		for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
103 
104 			if (E->get().nocasecmp_to(extension) == 0)
105 				recognized = true;
106 		}
107 
108 		if (!recognized)
109 			continue;
110 
111 		String old_path = p_resource->get_path();
112 
113 		String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
114 
115 		RES rwcopy = p_resource;
116 		if (p_flags & FLAG_CHANGE_PATH)
117 			rwcopy->set_path(local_path);
118 
119 		err = saver[i]->save(p_path, p_resource, p_flags);
120 
121 		if (err == OK) {
122 
123 #ifdef TOOLS_ENABLED
124 
125 			((Resource *)p_resource.ptr())->set_edited(false);
126 			if (timestamp_on_save) {
127 				uint64_t mt = FileAccess::get_modified_time(p_path);
128 
129 				((Resource *)p_resource.ptr())->set_last_modified_time(mt);
130 			}
131 #endif
132 
133 			if (p_flags & FLAG_CHANGE_PATH)
134 				rwcopy->set_path(old_path);
135 
136 			if (save_callback && p_path.begins_with("res://"))
137 				save_callback(p_resource, p_path);
138 
139 			return OK;
140 		}
141 	}
142 
143 	return err;
144 }
145 
set_save_callback(ResourceSavedCallback p_callback)146 void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
147 
148 	save_callback = p_callback;
149 }
150 
get_recognized_extensions(const RES & p_resource,List<String> * p_extensions)151 void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) {
152 
153 	for (int i = 0; i < saver_count; i++) {
154 
155 		saver[i]->get_recognized_extensions(p_resource, p_extensions);
156 	}
157 }
158 
add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver,bool p_at_front)159 void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
160 
161 	ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
162 	ERR_FAIL_COND(saver_count >= MAX_SAVERS);
163 
164 	if (p_at_front) {
165 		for (int i = saver_count; i > 0; i--) {
166 			saver[i] = saver[i - 1];
167 		}
168 		saver[0] = p_format_saver;
169 		saver_count++;
170 	} else {
171 		saver[saver_count++] = p_format_saver;
172 	}
173 }
174 
remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver)175 void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
176 
177 	ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
178 
179 	// Find saver
180 	int i = 0;
181 	for (; i < saver_count; ++i) {
182 		if (saver[i] == p_format_saver)
183 			break;
184 	}
185 
186 	ERR_FAIL_COND(i >= saver_count); // Not found
187 
188 	// Shift next savers up
189 	for (; i < saver_count - 1; ++i) {
190 		saver[i] = saver[i + 1];
191 	}
192 	saver[saver_count - 1].unref();
193 	--saver_count;
194 }
195 
_find_custom_resource_format_saver(String path)196 Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(String path) {
197 	for (int i = 0; i < saver_count; ++i) {
198 		if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
199 			return saver[i];
200 		}
201 	}
202 	return Ref<ResourceFormatSaver>();
203 }
204 
add_custom_resource_format_saver(String script_path)205 bool ResourceSaver::add_custom_resource_format_saver(String script_path) {
206 
207 	if (_find_custom_resource_format_saver(script_path).is_valid())
208 		return false;
209 
210 	Ref<Resource> res = ResourceLoader::load(script_path);
211 	ERR_FAIL_COND_V(res.is_null(), false);
212 	ERR_FAIL_COND_V(!res->is_class("Script"), false);
213 
214 	Ref<Script> s = res;
215 	StringName ibt = s->get_instance_base_type();
216 	bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver");
217 	ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceSaver: " + script_path + ".");
218 
219 	Object *obj = ClassDB::instance(ibt);
220 
221 	ERR_FAIL_COND_V_MSG(obj == NULL, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + ".");
222 
223 	ResourceFormatSaver *crl = Object::cast_to<ResourceFormatSaver>(obj);
224 	crl->set_script(s.get_ref_ptr());
225 	ResourceSaver::add_resource_format_saver(crl);
226 
227 	return true;
228 }
229 
remove_custom_resource_format_saver(String script_path)230 void ResourceSaver::remove_custom_resource_format_saver(String script_path) {
231 
232 	Ref<ResourceFormatSaver> custom_saver = _find_custom_resource_format_saver(script_path);
233 	if (custom_saver.is_valid())
234 		remove_resource_format_saver(custom_saver);
235 }
236 
add_custom_savers()237 void ResourceSaver::add_custom_savers() {
238 	// Custom resource savers exploits global class names
239 
240 	String custom_saver_base_class = ResourceFormatSaver::get_class_static();
241 
242 	List<StringName> global_classes;
243 	ScriptServer::get_global_class_list(&global_classes);
244 
245 	for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
246 
247 		StringName class_name = E->get();
248 		StringName base_class = ScriptServer::get_global_class_native_base(class_name);
249 
250 		if (base_class == custom_saver_base_class) {
251 			String path = ScriptServer::get_global_class_path(class_name);
252 			add_custom_resource_format_saver(path);
253 		}
254 	}
255 }
256 
remove_custom_savers()257 void ResourceSaver::remove_custom_savers() {
258 
259 	Vector<Ref<ResourceFormatSaver> > custom_savers;
260 	for (int i = 0; i < saver_count; ++i) {
261 		if (saver[i]->get_script_instance()) {
262 			custom_savers.push_back(saver[i]);
263 		}
264 	}
265 
266 	for (int i = 0; i < custom_savers.size(); ++i) {
267 		remove_resource_format_saver(custom_savers[i]);
268 	}
269 }
270