Debian Codegraph @

core/io/resource_saver.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* resource_saver.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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/config/project_settings.h"
33 #include "core/io/file_access.h"
34 #include "core/io/resource_loader.h"
35 #include "core/object/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 = nullptr;
42 ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr;
43
44 Error ResourceFormatSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
45 Error err = ERR_METHOD_NOT_FOUND;
46 GDVIRTUAL_CALL(_save, p_resource, p_path, p_flags, err);
47 return err;
48 }
49
50 Error ResourceFormatSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
51 Error err = ERR_FILE_UNRECOGNIZED;
52 GDVIRTUAL_CALL(_set_uid, p_path, p_uid, err);
53 return err;
54 }
55
56 bool ResourceFormatSaver::recognize(const Ref<Resource> &p_resource) const {
57 bool success = false;
58 GDVIRTUAL_CALL(_recognize, p_resource, success);
59 return success;
60 }
61
62 void ResourceFormatSaver::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
63 PackedStringArray exts;
64 if (GDVIRTUAL_CALL(_get_recognized_extensions, p_resource, exts)) {
65 const String *r = exts.ptr();
66 for (int i = 0; i < exts.size(); ++i) {
67 p_extensions->push_back(r[i]);
68 }
69 }
70 }
71
72 bool ResourceFormatSaver::recognize_path(const Ref<Resource> &p_resource, const String &p_path) const {
73 bool ret = false;
74 if (GDVIRTUAL_CALL(_recognize_path, p_resource, p_path, ret)) {
75 return ret;
76 }
77
78 String extension = p_path.get_extension();
79
80 List<String> extensions;
81 get_recognized_extensions(p_resource, &extensions);
82
83 for (const String &E : extensions) {
84 if (E.nocasecmp_to(extension) == 0) {
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 void ResourceFormatSaver::_bind_methods() {
93 GDVIRTUAL_BIND(_save, "resource", "path", "flags");
94 GDVIRTUAL_BIND(_set_uid, "path", "uid");
95 GDVIRTUAL_BIND(_recognize, "resource");
96 GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
97 GDVIRTUAL_BIND(_recognize_path, "resource", "path");
98 }
99
100 Error ResourceSaver::save(RequiredParam<Resource> rp_resource, const String &p_path, uint32_t p_flags) {
101 EXTRACT_PARAM_OR_FAIL_V_MSG(p_resource, rp_resource, ERR_INVALID_PARAMETER, vformat("Can't save empty resource to path '%s'.", p_path));
102 String path = p_path;
103 if (path.is_empty()) {
104 path = p_resource->get_path();
105 }
106 ERR_FAIL_COND_V_MSG(path.is_empty(), ERR_INVALID_PARAMETER, "Can't save resource to empty path. Provide non-empty path or a Resource with non-empty resource_path.");
107
108 Error err = ERR_FILE_UNRECOGNIZED;
109
110 for (int i = 0; i < saver_count; i++) {
111 if (!saver[i]->recognize(p_resource)) {
112 continue;
113 }
114
115 if (!saver[i]->recognize_path(p_resource, path)) {
116 continue;
117 }
118
119 String old_path = p_resource->get_path();
120
121 String local_path = ProjectSettings::get_singleton()->localize_path(path);
122
123 if (p_flags & FLAG_CHANGE_PATH) {
124 p_resource->set_path(local_path);
125 }
126
127 err = saver[i]->save(p_resource, path, p_flags);
128
129 if (err == OK) {
130 #ifdef TOOLS_ENABLED
131
132 ((Resource *)p_resource.ptr())->set_edited(false);
133 if (timestamp_on_save) {
134 uint64_t mt = FileAccess::get_modified_time(path);
135
136 ((Resource *)p_resource.ptr())->set_last_modified_time(mt);
137 }
138 #endif
139
140 if (p_flags & FLAG_CHANGE_PATH) {
141 p_resource->set_path(old_path);
142 }
143
144 if (save_callback && path.begins_with("res://")) {
145 save_callback(p_resource, path);
146 }
147
148 return OK;
149 }
150 }
151
152 return err;
153 }
154
155 Error ResourceSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
156 String path = p_path;
157
158 ERR_FAIL_COND_V_MSG(path.is_empty(), ERR_INVALID_PARAMETER, "Can't update UID to empty path. Provide non-empty path.");
159
160 Error err = ERR_FILE_UNRECOGNIZED;
161
162 for (int i = 0; i < saver_count; i++) {
163 err = saver[i]->set_uid(path, p_uid);
164 if (err == OK) {
165 break;
166 }
167 }
168
169 return err;
170 }
171
172 void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
173 save_callback = p_callback;
174 }
175
176 void ResourceSaver::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) {
177 ERR_FAIL_COND_MSG(p_resource.is_null(), "It's not a reference to a valid Resource object.");
178 for (int i = 0; i < saver_count; i++) {
179 saver[i]->get_recognized_extensions(p_resource, p_extensions);
180 }
181 }
182
183 void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
184 ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
185 ERR_FAIL_COND(saver_count >= MAX_SAVERS);
186
187 if (p_at_front) {
188 for (int i = saver_count; i > 0; i--) {
189 saver[i] = saver[i - 1];
190 }
191 saver[0] = p_format_saver;
192 saver_count++;
193 } else {
194 saver[saver_count++] = p_format_saver;
195 }
196 }
197
198 void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
199 ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
200
201 // Find saver
202 int i = 0;
203 for (; i < saver_count; ++i) {
204 if (saver[i] == p_format_saver) {
205 break;
206 }
207 }
208
209 ERR_FAIL_COND(i >= saver_count); // Not found
210
211 // Shift next savers up
212 for (; i < saver_count - 1; ++i) {
213 saver[i] = saver[i + 1];
214 }
215 saver[saver_count - 1].unref();
216 --saver_count;
217 }
218
219 Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(const String &path) {
220 for (int i = 0; i < saver_count; ++i) {
221 if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
222 return saver[i];
223 }
224 }
225 return Ref<ResourceFormatSaver>();
226 }
227
228 bool ResourceSaver::add_custom_resource_format_saver(const String &script_path) {
229 if (_find_custom_resource_format_saver(script_path).is_valid()) {
230 return false;
231 }
232
233 Ref<Resource> res = ResourceLoader::load(script_path);
234 ERR_FAIL_COND_V(res.is_null(), false);
235 ERR_FAIL_COND_V(!res->is_class("Script"), false);
236
237 Ref<Script> s = res;
238 StringName ibt = s->get_instance_base_type();
239 bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver");
240 ERR_FAIL_COND_V_MSG(!valid_type, false, vformat("Failed to add a custom resource saver, script '%s' does not inherit 'ResourceFormatSaver'.", script_path));
241
242 Object *obj = ClassDB::instantiate(ibt);
243 ERR_FAIL_NULL_V_MSG(obj, false, vformat("Failed to add a custom resource saver, cannot instantiate '%s'.", ibt));
244
245 Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj);
246 crl->set_script(s);
247 ResourceSaver::add_resource_format_saver(crl);
248
249 return true;
250 }
251
252 void ResourceSaver::add_custom_savers() {
253 // Custom resource savers exploits global class names
254
255 String custom_saver_base_class = ResourceFormatSaver::get_class_static();
256
257 LocalVector<StringName> global_classes;
258 ScriptServer::get_global_class_list(global_classes);
259
260 for (const StringName &class_name : global_classes) {
261 StringName base_class = ScriptServer::get_global_class_native_base(class_name);
262
263 if (base_class == custom_saver_base_class) {
264 String path = ScriptServer::get_global_class_path(class_name);
265 add_custom_resource_format_saver(path);
266 }
267 }
268 }
269
270 void ResourceSaver::remove_custom_savers() {
271 Vector<Ref<ResourceFormatSaver>> custom_savers;
272 for (int i = 0; i < saver_count; ++i) {
273 if (saver[i]->get_script_instance()) {
274 custom_savers.push_back(saver[i]);
275 }
276 }
277
278 for (int i = 0; i < custom_savers.size(); ++i) {
279 remove_resource_format_saver(custom_savers[i]);
280 }
281 }
282
283 ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
284 if (save_get_id_for_path) {
285 return save_get_id_for_path(p_path, p_generate);
286 }
287 return ResourceUID::INVALID_ID;
288 }
289
290 void ResourceSaver::set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback) {
291 save_get_id_for_path = p_callback;
292 }