resource_importer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*************************************************************************/
  2. /* resource_importer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "resource_importer.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "core/variant/variant_parser.h"
  34. bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const {
  35. return p_a->get_importer_name() < p_b->get_importer_name();
  36. }
  37. Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid) const {
  38. Error err;
  39. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  40. if (!f) {
  41. if (r_valid) {
  42. *r_valid = false;
  43. }
  44. return err;
  45. }
  46. VariantParser::StreamFile stream;
  47. stream.f = f;
  48. String assign;
  49. Variant value;
  50. VariantParser::Tag next_tag;
  51. if (r_valid) {
  52. *r_valid = true;
  53. }
  54. int lines = 0;
  55. String error_text;
  56. bool path_found = false; //first match must have priority
  57. while (true) {
  58. assign = Variant();
  59. next_tag.fields.clear();
  60. next_tag.name = String();
  61. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  62. if (err == ERR_FILE_EOF) {
  63. memdelete(f);
  64. return OK;
  65. } else if (err != OK) {
  66. ERR_PRINT("ResourceFormatImporter::load - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  67. memdelete(f);
  68. return err;
  69. }
  70. if (assign != String()) {
  71. if (!path_found && assign.begins_with("path.") && r_path_and_type.path == String()) {
  72. String feature = assign.get_slicec('.', 1);
  73. if (OS::get_singleton()->has_feature(feature)) {
  74. r_path_and_type.path = value;
  75. path_found = true; //first match must have priority
  76. }
  77. } else if (!path_found && assign == "path") {
  78. r_path_and_type.path = value;
  79. path_found = true; //first match must have priority
  80. } else if (assign == "type") {
  81. r_path_and_type.type = ClassDB::get_compatibility_remapped_class(value);
  82. } else if (assign == "importer") {
  83. r_path_and_type.importer = value;
  84. } else if (assign == "group_file") {
  85. r_path_and_type.group_file = value;
  86. } else if (assign == "metadata") {
  87. r_path_and_type.metadata = value;
  88. } else if (assign == "valid") {
  89. if (r_valid) {
  90. *r_valid = value;
  91. }
  92. }
  93. } else if (next_tag.name != "remap") {
  94. break;
  95. }
  96. }
  97. memdelete(f);
  98. if (r_path_and_type.path == String() || r_path_and_type.type == String()) {
  99. return ERR_FILE_CORRUPT;
  100. }
  101. return OK;
  102. }
  103. RES 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) {
  104. PathAndType pat;
  105. Error err = _get_path_and_type(p_path, pat);
  106. if (err != OK) {
  107. if (r_error) {
  108. *r_error = err;
  109. }
  110. return RES();
  111. }
  112. RES res = ResourceLoader::_load(pat.path, p_path, pat.type, p_cache_mode, r_error, p_use_sub_threads, r_progress);
  113. #ifdef TOOLS_ENABLED
  114. if (res.is_valid()) {
  115. res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
  116. res->set_import_path(pat.path);
  117. }
  118. #endif
  119. return res;
  120. }
  121. void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const {
  122. Set<String> found;
  123. for (int i = 0; i < importers.size(); i++) {
  124. List<String> local_exts;
  125. importers[i]->get_recognized_extensions(&local_exts);
  126. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  127. if (!found.has(F->get())) {
  128. p_extensions->push_back(F->get());
  129. found.insert(F->get());
  130. }
  131. }
  132. }
  133. }
  134. void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  135. if (p_type == "") {
  136. get_recognized_extensions(p_extensions);
  137. return;
  138. }
  139. Set<String> found;
  140. for (int i = 0; i < importers.size(); i++) {
  141. String res_type = importers[i]->get_resource_type();
  142. if (res_type == String()) {
  143. continue;
  144. }
  145. if (!ClassDB::is_parent_class(res_type, p_type)) {
  146. continue;
  147. }
  148. List<String> local_exts;
  149. importers[i]->get_recognized_extensions(&local_exts);
  150. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  151. if (!found.has(F->get())) {
  152. p_extensions->push_back(F->get());
  153. found.insert(F->get());
  154. }
  155. }
  156. }
  157. }
  158. bool ResourceFormatImporter::exists(const String &p_path) const {
  159. return FileAccess::exists(p_path + ".import");
  160. }
  161. bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const {
  162. return FileAccess::exists(p_path + ".import");
  163. }
  164. int ResourceFormatImporter::get_import_order(const String &p_path) const {
  165. Ref<ResourceImporter> importer;
  166. if (FileAccess::exists(p_path + ".import")) {
  167. PathAndType pat;
  168. Error err = _get_path_and_type(p_path, pat);
  169. if (err == OK) {
  170. importer = get_importer_by_name(pat.importer);
  171. }
  172. } else {
  173. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  174. }
  175. if (importer.is_valid()) {
  176. return importer->get_import_order();
  177. }
  178. return 0;
  179. }
  180. bool ResourceFormatImporter::handles_type(const String &p_type) const {
  181. for (int i = 0; i < importers.size(); i++) {
  182. String res_type = importers[i]->get_resource_type();
  183. if (res_type == String()) {
  184. continue;
  185. }
  186. if (ClassDB::is_parent_class(res_type, p_type)) {
  187. return true;
  188. }
  189. }
  190. return true;
  191. }
  192. String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
  193. PathAndType pat;
  194. Error err = _get_path_and_type(p_path, pat);
  195. if (err != OK) {
  196. return String();
  197. }
  198. return pat.path;
  199. }
  200. void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
  201. Error err;
  202. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  203. if (!f) {
  204. return;
  205. }
  206. VariantParser::StreamFile stream;
  207. stream.f = f;
  208. String assign;
  209. Variant value;
  210. VariantParser::Tag next_tag;
  211. int lines = 0;
  212. String error_text;
  213. while (true) {
  214. assign = Variant();
  215. next_tag.fields.clear();
  216. next_tag.name = String();
  217. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  218. if (err == ERR_FILE_EOF) {
  219. memdelete(f);
  220. return;
  221. } else if (err != OK) {
  222. ERR_PRINT("ResourceFormatImporter::get_internal_resource_path_list - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  223. memdelete(f);
  224. return;
  225. }
  226. if (assign != String()) {
  227. if (assign.begins_with("path.")) {
  228. r_paths->push_back(value);
  229. } else if (assign == "path") {
  230. r_paths->push_back(value);
  231. }
  232. } else if (next_tag.name != "remap") {
  233. break;
  234. }
  235. }
  236. memdelete(f);
  237. }
  238. String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
  239. bool valid = true;
  240. PathAndType pat;
  241. _get_path_and_type(p_path, pat, &valid);
  242. return valid ? pat.group_file : String();
  243. }
  244. bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
  245. bool valid = true;
  246. PathAndType pat;
  247. _get_path_and_type(p_path, pat, &valid);
  248. return valid;
  249. }
  250. String ResourceFormatImporter::get_resource_type(const String &p_path) const {
  251. PathAndType pat;
  252. Error err = _get_path_and_type(p_path, pat);
  253. if (err != OK) {
  254. return "";
  255. }
  256. return pat.type;
  257. }
  258. Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
  259. PathAndType pat;
  260. Error err = _get_path_and_type(p_path, pat);
  261. if (err != OK) {
  262. return Variant();
  263. }
  264. return pat.metadata;
  265. }
  266. void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  267. PathAndType pat;
  268. Error err = _get_path_and_type(p_path, pat);
  269. if (err != OK) {
  270. return;
  271. }
  272. ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
  273. }
  274. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
  275. for (int i = 0; i < importers.size(); i++) {
  276. if (importers[i]->get_importer_name() == p_name) {
  277. return importers[i];
  278. }
  279. }
  280. return Ref<ResourceImporter>();
  281. }
  282. void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
  283. for (int i = 0; i < importers.size(); i++) {
  284. List<String> local_exts;
  285. importers[i]->get_recognized_extensions(&local_exts);
  286. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  287. if (p_extension.to_lower() == F->get()) {
  288. r_importers->push_back(importers[i]);
  289. }
  290. }
  291. }
  292. }
  293. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
  294. Ref<ResourceImporter> importer;
  295. float priority = 0;
  296. for (int i = 0; i < importers.size(); i++) {
  297. List<String> local_exts;
  298. importers[i]->get_recognized_extensions(&local_exts);
  299. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  300. if (p_extension.to_lower() == F->get() && importers[i]->get_priority() > priority) {
  301. importer = importers[i];
  302. priority = importers[i]->get_priority();
  303. }
  304. }
  305. }
  306. return importer;
  307. }
  308. String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
  309. return ProjectSettings::IMPORTED_FILES_PATH.plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text());
  310. }
  311. bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
  312. bool valid = true;
  313. PathAndType pat;
  314. _get_path_and_type(p_path, pat, &valid);
  315. if (!valid) {
  316. return false;
  317. }
  318. for (int i = 0; i < importers.size(); i++) {
  319. if (importers[i]->get_importer_name() == pat.importer) {
  320. if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
  321. return false;
  322. }
  323. }
  324. }
  325. return true;
  326. }
  327. String ResourceFormatImporter::get_import_settings_hash() const {
  328. Vector<Ref<ResourceImporter>> sorted_importers = importers;
  329. sorted_importers.sort_custom<SortImporterByName>();
  330. String hash;
  331. for (int i = 0; i < sorted_importers.size(); i++) {
  332. hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
  333. }
  334. return hash.md5_text();
  335. }
  336. ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
  337. ResourceFormatImporter::ResourceFormatImporter() {
  338. singleton = this;
  339. }