2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_import.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
2019-01-03 10:41:35 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-01-26 00:55:59 +00:00
|
|
|
#include "resource_import.h"
|
2017-03-05 14:47:28 +00:00
|
|
|
|
2017-02-06 03:38:39 +00:00
|
|
|
#include "os/os.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "variant_parser.h"
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-08-29 22:50:58 +00:00
|
|
|
Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid) const {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
Error err;
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-08-29 22:50:58 +00:00
|
|
|
if (!f) {
|
|
|
|
if (r_valid) {
|
|
|
|
*r_valid = false;
|
|
|
|
}
|
2017-01-26 00:55:59 +00:00
|
|
|
return err;
|
2017-08-29 22:50:58 +00:00
|
|
|
}
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
VariantParser::StreamFile stream;
|
2017-03-05 15:44:50 +00:00
|
|
|
stream.f = f;
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
String assign;
|
|
|
|
Variant value;
|
|
|
|
VariantParser::Tag next_tag;
|
|
|
|
|
2017-08-29 22:50:58 +00:00
|
|
|
if (r_valid) {
|
|
|
|
*r_valid = true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int lines = 0;
|
2017-01-26 00:55:59 +00:00
|
|
|
String error_text;
|
2017-07-19 20:00:46 +00:00
|
|
|
bool path_found = false; //first match must have priority
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
assign = Variant();
|
2017-01-26 00:55:59 +00:00
|
|
|
next_tag.fields.clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
next_tag.name = String();
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
|
|
|
|
if (err == ERR_FILE_EOF) {
|
2017-01-26 00:55:59 +00:00
|
|
|
memdelete(f);
|
|
|
|
return OK;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (err != OK) {
|
|
|
|
ERR_PRINTS("ResourceFormatImporter::load - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
|
2017-01-26 00:55:59 +00:00
|
|
|
memdelete(f);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (assign != String()) {
|
2017-07-19 20:00:46 +00:00
|
|
|
if (!path_found && assign.begins_with("path.") && r_path_and_type.path == String()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
String feature = assign.get_slicec('.', 1);
|
2017-10-02 19:38:39 +00:00
|
|
|
if (OS::get_singleton()->has_feature(feature)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_path_and_type.path = value;
|
2017-07-19 20:00:46 +00:00
|
|
|
path_found = true; //first match must have priority
|
2017-02-06 03:38:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
} else if (!path_found && assign == "path") {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_path_and_type.path = value;
|
2017-07-19 20:00:46 +00:00
|
|
|
path_found = true; //first match must have priority
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (assign == "type") {
|
|
|
|
r_path_and_type.type = value;
|
2017-09-20 23:59:19 +00:00
|
|
|
} else if (assign == "importer") {
|
|
|
|
r_path_and_type.importer = value;
|
2017-08-29 22:50:58 +00:00
|
|
|
} else if (assign == "valid") {
|
|
|
|
if (r_valid) {
|
|
|
|
*r_valid = value;
|
|
|
|
}
|
2017-01-26 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (next_tag.name != "remap") {
|
2017-01-26 00:55:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(f);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (r_path_and_type.path == String() || r_path_and_type.type == String()) {
|
2017-01-26 00:55:59 +00:00
|
|
|
return ERR_FILE_CORRUPT;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
RES ResourceFormatImporter::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
PathAndType pat;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = _get_path_and_type(p_path, pat);
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (err != OK) {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = err;
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
return RES();
|
|
|
|
}
|
|
|
|
|
2017-08-31 21:57:03 +00:00
|
|
|
RES res = ResourceLoader::_load(pat.path, p_path, pat.type, false, r_error);
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2017-02-15 11:29:46 +00:00
|
|
|
if (res.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
|
2017-02-15 11:29:46 +00:00
|
|
|
res->set_import_path(pat.path);
|
|
|
|
}
|
2017-02-01 12:45:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return res;
|
2017-01-26 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
Set<String> found;
|
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
for (int i = 0; i < importers.size(); i++) {
|
2017-01-26 00:55:59 +00:00
|
|
|
List<String> local_exts;
|
2018-01-06 19:36:49 +00:00
|
|
|
importers[i]->get_recognized_extensions(&local_exts);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
|
2017-01-26 00:55:59 +00:00
|
|
|
if (!found.has(F->get())) {
|
|
|
|
p_extensions->push_back(F->get());
|
|
|
|
found.insert(F->get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_type == "") {
|
2017-02-01 12:45:45 +00:00
|
|
|
return get_recognized_extensions(p_extensions);
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:55:59 +00:00
|
|
|
Set<String> found;
|
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
for (int i = 0; i < importers.size(); i++) {
|
|
|
|
String res_type = importers[i]->get_resource_type();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (res_type == String())
|
2017-02-01 23:41:05 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!ClassDB::is_parent_class(res_type, p_type))
|
2017-01-26 00:55:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
List<String> local_exts;
|
2018-01-06 19:36:49 +00:00
|
|
|
importers[i]->get_recognized_extensions(&local_exts);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
|
2017-01-26 00:55:59 +00:00
|
|
|
if (!found.has(F->get())) {
|
|
|
|
p_extensions->push_back(F->get());
|
|
|
|
found.insert(F->get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return FileAccess::exists(p_path + ".import");
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceFormatImporter::can_be_imported(const String &p_path) const {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
return ResourceFormatLoader::recognize_path(p_path);
|
2017-01-26 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 23:59:19 +00:00
|
|
|
int ResourceFormatImporter::get_import_order(const String &p_path) const {
|
|
|
|
|
|
|
|
Ref<ResourceImporter> importer;
|
|
|
|
|
|
|
|
if (FileAccess::exists(p_path + ".import")) {
|
|
|
|
|
|
|
|
PathAndType pat;
|
|
|
|
Error err = _get_path_and_type(p_path, pat);
|
|
|
|
|
|
|
|
if (err == OK) {
|
|
|
|
importer = get_importer_by_name(pat.importer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
importer = get_importer_by_extension(p_path.get_extension().to_lower());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (importer.is_valid())
|
|
|
|
return importer->get_import_order();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceFormatImporter::handles_type(const String &p_type) const {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
for (int i = 0; i < importers.size(); i++) {
|
2017-02-01 23:41:05 +00:00
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
String res_type = importers[i]->get_resource_type();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (res_type == String())
|
2017-02-01 23:41:05 +00:00
|
|
|
continue;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ClassDB::is_parent_class(res_type, p_type))
|
2017-01-26 00:55:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
|
2017-02-06 03:38:39 +00:00
|
|
|
|
|
|
|
PathAndType pat;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = _get_path_and_type(p_path, pat);
|
2017-02-06 03:38:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (err != OK) {
|
2017-02-06 03:38:39 +00:00
|
|
|
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pat.path;
|
|
|
|
}
|
|
|
|
|
2017-08-14 23:13:14 +00:00
|
|
|
void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
|
|
|
|
|
|
|
|
Error err;
|
|
|
|
FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
|
|
|
|
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VariantParser::StreamFile stream;
|
|
|
|
stream.f = f;
|
|
|
|
|
|
|
|
String assign;
|
|
|
|
Variant value;
|
|
|
|
VariantParser::Tag next_tag;
|
|
|
|
|
|
|
|
int lines = 0;
|
|
|
|
String error_text;
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
assign = Variant();
|
|
|
|
next_tag.fields.clear();
|
|
|
|
next_tag.name = String();
|
|
|
|
|
|
|
|
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
|
|
|
|
if (err == ERR_FILE_EOF) {
|
|
|
|
memdelete(f);
|
|
|
|
return;
|
|
|
|
} else if (err != OK) {
|
|
|
|
ERR_PRINTS("ResourceFormatImporter::get_internal_resource_path_list - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
|
|
|
|
memdelete(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assign != String()) {
|
|
|
|
if (assign.begins_with("path.")) {
|
|
|
|
r_paths->push_back(value);
|
|
|
|
} else if (assign == "path") {
|
|
|
|
r_paths->push_back(value);
|
|
|
|
}
|
|
|
|
} else if (next_tag.name != "remap") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memdelete(f);
|
|
|
|
}
|
|
|
|
|
2017-08-29 22:50:58 +00:00
|
|
|
bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
|
|
|
|
|
|
|
|
bool valid = true;
|
|
|
|
PathAndType pat;
|
|
|
|
_get_path_and_type(p_path, pat, &valid);
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:55:59 +00:00
|
|
|
String ResourceFormatImporter::get_resource_type(const String &p_path) const {
|
|
|
|
|
|
|
|
PathAndType pat;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = _get_path_and_type(p_path, pat);
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (err != OK) {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return pat.type;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
PathAndType pat;
|
2017-03-05 15:44:50 +00:00
|
|
|
Error err = _get_path_and_type(p_path, pat);
|
2017-01-26 00:55:59 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (err != OK) {
|
2017-01-26 00:55:59 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
|
2017-01-26 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 23:59:19 +00:00
|
|
|
Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
for (int i = 0; i < importers.size(); i++) {
|
|
|
|
if (importers[i]->get_importer_name() == p_name) {
|
|
|
|
return importers[i];
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ref<ResourceImporter>();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter> > *r_importers) {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
for (int i = 0; i < importers.size(); i++) {
|
2017-02-01 12:45:45 +00:00
|
|
|
List<String> local_exts;
|
2018-01-06 19:36:49 +00:00
|
|
|
importers[i]->get_recognized_extensions(&local_exts);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
|
|
|
|
if (p_extension.to_lower() == F->get()) {
|
2018-01-06 19:36:49 +00:00
|
|
|
r_importers->push_back(importers[i]);
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 23:59:19 +00:00
|
|
|
Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
Ref<ResourceImporter> importer;
|
2017-03-05 15:44:50 +00:00
|
|
|
float priority = 0;
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2018-01-06 19:36:49 +00:00
|
|
|
for (int i = 0; i < importers.size(); i++) {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
List<String> local_exts;
|
2018-01-06 19:36:49 +00:00
|
|
|
importers[i]->get_recognized_extensions(&local_exts);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
|
2018-01-06 19:36:49 +00:00
|
|
|
if (p_extension.to_lower() == F->get() && importers[i]->get_priority() > priority) {
|
|
|
|
importer = importers[i];
|
|
|
|
priority = importers[i]->get_priority();
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return importer;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
|
2017-02-01 12:45:45 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return "res://.import/" + p_for_file.get_file() + "-" + p_for_file.md5_text();
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ResourceFormatImporter *ResourceFormatImporter::singleton = NULL;
|
2017-02-01 12:45:45 +00:00
|
|
|
|
|
|
|
ResourceFormatImporter::ResourceFormatImporter() {
|
2017-03-05 15:44:50 +00:00
|
|
|
singleton = this;
|
2017-02-01 12:45:45 +00:00
|
|
|
}
|