Debian Codegraph @

core/io/resource_importer.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* resource_importer.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_importer.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/config_file.h"
35 #include "core/io/image.h"
36 #include "core/os/os.h"
37 #include "core/variant/variant_parser.h"
38
39 bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const {
40 return p_a->get_importer_name() < p_b->get_importer_name();
41 }
42
43 Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool p_load, bool *r_valid) const {
44 Error err;
45 Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
46
47 if (f.is_null()) {
48 if (r_valid) {
49 *r_valid = false;
50 }
51 return err;
52 }
53
54 VariantParser::StreamFile stream;
55 stream.f = f;
56
57 String assign;
58 Variant value;
59 VariantParser::Tag next_tag;
60
61 if (r_valid) {
62 *r_valid = true;
63 }
64
65 int lines = 0;
66 String error_text;
67 bool path_found = false; // First match must have priority.
68
69 String decomp_path;
70 bool decomp_path_found = false;
71 while (true) {
72 assign = Variant();
73 next_tag.fields.clear();
74 next_tag.name = String();
75
76 err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
77 if (err == ERR_FILE_EOF) {
78 if (p_load && !path_found && decomp_path_found) {
79 print_verbose(vformat("No natively supported texture format found for %s, using decompressable format %s.", p_path, decomp_path));
80 r_path_and_type.path = decomp_path;
81 }
82
83 return OK;
84 } else if (err != OK) {
85 ERR_PRINT(vformat("ResourceFormatImporter::load - %s.import:%d error: %s.", p_path, lines, error_text));
86 return err;
87 }
88
89 if (!assign.is_empty()) {
90 if (!path_found && assign.begins_with("path.") && r_path_and_type.path.is_empty()) {
91 String feature = assign.get_slicec('.', 1);
92 if (OS::get_singleton()->has_feature(feature)) {
93 r_path_and_type.path = value;
94 path_found = true; // First match must have priority.
95 } else if (p_load && Image::can_decompress(feature) && !decomp_path_found) { // When loading, check for decompressable formats and use first one found if nothing else is supported.
96 decomp_path = value;
97 decomp_path_found = true; // First match must have priority.
98 }
99
100 } else if (!path_found && assign == "path") {
101 r_path_and_type.path = value;
102 path_found = true; // First match must have priority.
103 } else if (assign == "type") {
104 r_path_and_type.type = ClassDB::get_compatibility_remapped_class(value);
105 } else if (assign == "importer") {
106 r_path_and_type.importer = value;
107 } else if (assign == "uid") {
108 r_path_and_type.uid = ResourceUID::get_singleton()->text_to_id(value);
109 } else if (assign == "group_file") {
110 r_path_and_type.group_file = value;
111 } else if (assign == "metadata") {
112 r_path_and_type.metadata = value;
113 } else if (assign == "valid") {
114 if (r_valid) {
115 *r_valid = value;
116 }
117 }
118
119 } else if (next_tag.name != "remap") {
120 break;
121 }
122 }
123
124 if (p_load && !path_found && decomp_path_found) {
125 print_verbose(vformat("No natively supported texture format found for %s, using decompressable format %s.", p_path, decomp_path));
126 r_path_and_type.path = decomp_path;
127 return OK;
128 }
129
130 #ifdef TOOLS_ENABLED
131 if (r_path_and_type.metadata && !r_path_and_type.path.is_empty()) {
132 Dictionary meta = r_path_and_type.metadata;
133 if (meta.has("has_editor_variant")) {
134 r_path_and_type.path = r_path_and_type.path.get_basename() + ".editor." + r_path_and_type.path.get_extension();
135 }
136 }
137 #endif
138
139 if (r_path_and_type.type.is_empty()) {
140 return ERR_FILE_CORRUPT;
141 }
142 if (r_path_and_type.path.is_empty()) {
143 // Some importers may not write files to the .godot folder, so the path can be empty.
144 if (r_path_and_type.importer.is_empty()) {
145 return ERR_FILE_CORRUPT;
146 }
147
148 // It's only invalid if the extension for the importer is not empty.
149 Ref<ResourceImporter> importer = get_importer_by_name(r_path_and_type.importer);
150 if (importer.is_null() || !importer->get_save_extension().is_empty()) {
151 return ERR_FILE_CORRUPT;
152 }
153 }
154 return OK;
155 }
156
157 Ref<Resource> ResourceFormatImporter::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
158 #ifdef TOOLS_ENABLED
159 // When loading a resource on startup, we use the load_on_startup callback,
160 // which executes the loading in the EditorFileSystem. It can reimport
161 // the resource and retry the load, allowing the resource to be loaded
162 // even if it is not yet imported.
163 if (ResourceImporter::load_on_startup != nullptr) {
164 return ResourceImporter::load_on_startup(this, p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode);
165 }
166 #endif
167
168 return load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, false);
169 }
170
171 Ref<Resource> ResourceFormatImporter::load_internal(const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode, bool p_silence_errors) {
172 PathAndType pat;
173 Error err = _get_path_and_type(p_path, pat, true);
174
175 if (err != OK) {
176 if (r_error) {
177 *r_error = err;
178 }
179
180 return Ref<Resource>();
181 }
182
183 if (p_silence_errors) {
184 // Note: Some importers do not create files in the .godot folder, so we need to check if the path is empty.
185 if (!pat.path.is_empty() && !FileAccess::exists(pat.path)) {
186 return Ref<Resource>();
187 }
188 }
189
190 Ref<Resource> res = ResourceLoader::_load(pat.path, p_path, pat.type, p_cache_mode, r_error, p_use_sub_threads, r_progress);
191
192 #ifdef TOOLS_ENABLED
193 if (res.is_valid()) {
194 res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
195 res->set_import_path(pat.path);
196 }
197 #endif
198
199 return res;
200 }
201
202 void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const {
203 HashSet<String> found;
204
205 for (int i = 0; i < importers.size(); i++) {
206 List<String> local_exts;
207 importers[i]->get_recognized_extensions(&local_exts);
208 for (const String &F : local_exts) {
209 if (!found.has(F)) {
210 p_extensions->push_back(F);
211 found.insert(F);
212 }
213 }
214 }
215 }
216
217 void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
218 if (p_type.is_empty()) {
219 get_recognized_extensions(p_extensions);
220 return;
221 }
222
223 HashSet<String> found;
224
225 for (int i = 0; i < importers.size(); i++) {
226 String res_type = importers[i]->get_resource_type();
227 if (res_type.is_empty()) {
228 continue;
229 }
230
231 if (!ClassDB::is_parent_class(res_type, p_type)) {
232 continue;
233 }
234
235 List<String> local_exts;
236 importers[i]->get_recognized_extensions(&local_exts);
237 for (const String &F : local_exts) {
238 if (!found.has(F)) {
239 p_extensions->push_back(F);
240 found.insert(F);
241 }
242 }
243 }
244 }
245
246 bool ResourceFormatImporter::exists(const String &p_path) const {
247 return FileAccess::exists(p_path + ".import");
248 }
249
250 bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const {
251 return FileAccess::exists(p_path + ".import");
252 }
253
254 Error ResourceFormatImporter::get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const {
255 r_order = 0;
256 r_importer = "";
257
258 r_can_threads = false;
259 Ref<ResourceImporter> importer;
260
261 if (FileAccess::exists(p_path + ".import")) {
262 PathAndType pat;
263 Error err = _get_path_and_type(p_path, pat, false);
264
265 if (err == OK) {
266 importer = get_importer_by_name(pat.importer);
267 }
268 } else {
269 importer = get_importer_by_file(p_path);
270 }
271
272 if (importer.is_valid()) {
273 r_order = importer->get_import_order();
274 r_importer = importer->get_importer_name();
275 r_can_threads = importer->can_import_threaded();
276 return OK;
277 } else {
278 return ERR_INVALID_PARAMETER;
279 }
280 }
281
282 int ResourceFormatImporter::get_import_order(const String &p_path) const {
283 Ref<ResourceImporter> importer;
284
285 if (FileAccess::exists(p_path + ".import")) {
286 PathAndType pat;
287 Error err = _get_path_and_type(p_path, pat, false);
288
289 if (err == OK) {
290 importer = get_importer_by_name(pat.importer);
291 }
292 } else {
293 importer = get_importer_by_file(p_path);
294 }
295
296 if (importer.is_valid()) {
297 return importer->get_import_order();
298 }
299
300 return 0;
301 }
302
303 bool ResourceFormatImporter::handles_type(const String &p_type) const {
304 for (int i = 0; i < importers.size(); i++) {
305 String res_type = importers[i]->get_resource_type();
306 if (res_type.is_empty()) {
307 continue;
308 }
309 if (ClassDB::is_parent_class(res_type, p_type)) {
310 return true;
311 }
312 }
313
314 return true;
315 }
316
317 String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
318 PathAndType pat;
319 Error err = _get_path_and_type(p_path, pat, false);
320
321 if (err != OK) {
322 return String();
323 }
324
325 return pat.path;
326 }
327
328 void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
329 Error err;
330 Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
331
332 if (f.is_null()) {
333 return;
334 }
335
336 VariantParser::StreamFile stream;
337 stream.f = f;
338
339 String assign;
340 Variant value;
341 VariantParser::Tag next_tag;
342
343 int lines = 0;
344 String error_text;
345 while (true) {
346 assign = Variant();
347 next_tag.fields.clear();
348 next_tag.name = String();
349
350 err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
351 if (err == ERR_FILE_EOF) {
352 return;
353 } else if (err != OK) {
354 ERR_PRINT(vformat("ResourceFormatImporter::get_internal_resource_path_list - %s.import:%d error: %s.", p_path, lines, error_text));
355 return;
356 }
357
358 if (!assign.is_empty()) {
359 if (assign.begins_with("path.")) {
360 r_paths->push_back(value);
361 } else if (assign == "path") {
362 r_paths->push_back(value);
363 }
364 } else if (next_tag.name != "remap") {
365 break;
366 }
367 }
368 }
369
370 String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
371 bool valid = true;
372 PathAndType pat;
373 _get_path_and_type(p_path, pat, false, &valid);
374 return valid ? pat.group_file : String();
375 }
376
377 bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
378 bool valid = true;
379 PathAndType pat;
380 _get_path_and_type(p_path, pat, false, &valid);
381 return valid;
382 }
383
384 String ResourceFormatImporter::get_resource_type(const String &p_path) const {
385 PathAndType pat;
386 Error err = _get_path_and_type(p_path, pat, false);
387
388 if (err != OK) {
389 return "";
390 }
391
392 return pat.type;
393 }
394
395 ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) const {
396 PathAndType pat;
397 Error err = _get_path_and_type(p_path, pat, false);
398
399 if (err != OK) {
400 return ResourceUID::INVALID_ID;
401 }
402
403 return pat.uid;
404 }
405
406 bool ResourceFormatImporter::has_custom_uid_support() const {
407 return true;
408 }
409
410 Error ResourceFormatImporter::get_resource_import_info(const String &p_path, StringName &r_type, ResourceUID::ID &r_uid, String &r_import_group_file) const {
411 PathAndType pat;
412 Error err = _get_path_and_type(p_path, pat, false);
413
414 if (err == OK) {
415 r_type = pat.type;
416 r_uid = pat.uid;
417 r_import_group_file = pat.group_file;
418 } else {
419 r_type = "";
420 r_uid = ResourceUID::INVALID_ID;
421 r_import_group_file = "";
422 }
423
424 return err;
425 }
426
427 Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
428 PathAndType pat;
429 Error err = _get_path_and_type(p_path, pat, false);
430
431 if (err != OK) {
432 return Variant();
433 }
434
435 return pat.metadata;
436 }
437
438 void ResourceFormatImporter::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
439 PathAndType pat;
440 Error err = _get_path_and_type(p_path, pat, false);
441
442 if (err != OK) {
443 return;
444 }
445
446 ResourceLoader::get_classes_used(pat.path, r_classes);
447 }
448
449 void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
450 PathAndType pat;
451 Error err = _get_path_and_type(p_path, pat, false);
452
453 if (err != OK) {
454 return;
455 }
456
457 ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
458 }
459
460 void ResourceFormatImporter::get_build_dependencies(const String &p_path, HashSet<String> *r_dependencies) {
461 if (!exists(p_path)) {
462 return;
463 }
464
465 List<Ref<ResourceImporter>> valid_importers;
466 get_importers_for_file(p_path, &valid_importers);
467 for (Ref<ResourceImporter> importer : valid_importers) {
468 importer->get_build_dependencies(p_path, r_dependencies);
469 }
470 }
471
472 Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
473 for (int i = 0; i < importers.size(); i++) {
474 if (importers[i]->get_importer_name() == p_name) {
475 return importers[i];
476 }
477 }
478
479 return Ref<ResourceImporter>();
480 }
481
482 void ResourceFormatImporter::add_importer(const Ref<ResourceImporter> &p_importer, bool p_first_priority) {
483 ERR_FAIL_COND(p_importer.is_null());
484 if (p_first_priority) {
485 importers.insert(0, p_importer);
486 } else {
487 importers.push_back(p_importer);
488 }
489 }
490
491 void ResourceFormatImporter::get_importers_for_file(const String &p_file, List<Ref<ResourceImporter>> *r_importers) {
492 for (int i = 0; i < importers.size(); i++) {
493 List<String> local_exts;
494 importers[i]->get_recognized_extensions(&local_exts);
495 for (const String &F : local_exts) {
496 if (p_file.right(F.length()).nocasecmp_to(F) == 0) {
497 r_importers->push_back(importers[i]);
498 break;
499 }
500 }
501 }
502 }
503
504 void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_importers) {
505 for (int i = 0; i < importers.size(); i++) {
506 r_importers->push_back(importers[i]);
507 }
508 }
509
510 Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_file(const String &p_file) const {
511 Ref<ResourceImporter> importer;
512 float priority = 0;
513
514 for (int i = 0; i < importers.size(); i++) {
515 List<String> local_exts;
516 importers[i]->get_recognized_extensions(&local_exts);
517 for (const String &F : local_exts) {
518 if (p_file.right(F.length()).nocasecmp_to(F) == 0 && importers[i]->get_priority() > priority) {
519 importer = importers[i];
520 priority = importers[i]->get_priority();
521 break;
522 }
523 }
524 }
525
526 return importer;
527 }
528
529 String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
530 return ProjectSettings::get_singleton()->get_imported_files_path().path_join(p_for_file.get_file() + "-" + p_for_file.md5_text());
531 }
532
533 bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
534 bool valid = true;
535 PathAndType pat;
536 _get_path_and_type(p_path, pat, false, &valid);
537
538 if (!valid) {
539 return false;
540 }
541
542 for (int i = 0; i < importers.size(); i++) {
543 if (importers[i]->get_importer_name() == pat.importer) {
544 if (!importers[i]->are_import_settings_valid(p_path, pat.metadata)) { //importer thinks this is not valid
545 return false;
546 }
547 }
548 }
549
550 return true;
551 }
552
553 String ResourceFormatImporter::get_import_settings_hash() const {
554 Vector<Ref<ResourceImporter>> sorted_importers = importers;
555
556 sorted_importers.sort_custom<SortImporterByName>();
557
558 String hash;
559 for (int i = 0; i < sorted_importers.size(); i++) {
560 hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
561 }
562 return hash.md5_text();
563 }
564
565 ResourceFormatImporter::ResourceFormatImporter() {
566 singleton = this;
567 }
568
569 //////////////
570
571 void ResourceImporter::get_build_dependencies(const String &p_path, HashSet<String> *r_dependencies) {
572 Vector<String> ret;
573 if (GDVIRTUAL_CALL(_get_build_dependencies, p_path, ret)) {
574 for (int i = 0; i < ret.size(); i++) {
575 r_dependencies->insert(ret[i]);
576 }
577 return;
578 }
579 }
580
581 void ResourceImporter::_bind_methods() {
582 BIND_ENUM_CONSTANT(IMPORT_ORDER_DEFAULT);
583 BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
584
585 GDVIRTUAL_BIND(_get_build_dependencies, "path");
586 }
587
588 /////
589
590 Error ResourceFormatImporterSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
591 Ref<ConfigFile> cf;
592 cf.instantiate();
593 Error err = cf->load(p_path + ".import");
594 if (err != OK) {
595 return err;
596 }
597 cf->set_value("remap", "uid", ResourceUID::get_singleton()->id_to_text(p_uid));
598 cf->save(p_path + ".import");
599
600 return OK;
601 }