resource_importer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. Error ResourceFormatImporter::get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const {
  165. r_order = 0;
  166. r_importer = "";
  167. r_can_threads = false;
  168. Ref<ResourceImporter> importer;
  169. if (FileAccess::exists(p_path + ".import")) {
  170. PathAndType pat;
  171. Error err = _get_path_and_type(p_path, pat);
  172. if (err == OK) {
  173. importer = get_importer_by_name(pat.importer);
  174. }
  175. } else {
  176. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  177. }
  178. if (importer.is_valid()) {
  179. r_order = importer->get_import_order();
  180. r_importer = importer->get_importer_name();
  181. r_can_threads = importer->can_import_threaded();
  182. return OK;
  183. } else {
  184. return ERR_INVALID_PARAMETER;
  185. }
  186. }
  187. int ResourceFormatImporter::get_import_order(const String &p_path) const {
  188. Ref<ResourceImporter> importer;
  189. if (FileAccess::exists(p_path + ".import")) {
  190. PathAndType pat;
  191. Error err = _get_path_and_type(p_path, pat);
  192. if (err == OK) {
  193. importer = get_importer_by_name(pat.importer);
  194. }
  195. } else {
  196. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  197. }
  198. if (importer.is_valid()) {
  199. return importer->get_import_order();
  200. }
  201. return 0;
  202. }
  203. bool ResourceFormatImporter::handles_type(const String &p_type) const {
  204. for (int i = 0; i < importers.size(); i++) {
  205. String res_type = importers[i]->get_resource_type();
  206. if (res_type == String()) {
  207. continue;
  208. }
  209. if (ClassDB::is_parent_class(res_type, p_type)) {
  210. return true;
  211. }
  212. }
  213. return true;
  214. }
  215. String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
  216. PathAndType pat;
  217. Error err = _get_path_and_type(p_path, pat);
  218. if (err != OK) {
  219. return String();
  220. }
  221. return pat.path;
  222. }
  223. void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
  224. Error err;
  225. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  226. if (!f) {
  227. return;
  228. }
  229. VariantParser::StreamFile stream;
  230. stream.f = f;
  231. String assign;
  232. Variant value;
  233. VariantParser::Tag next_tag;
  234. int lines = 0;
  235. String error_text;
  236. while (true) {
  237. assign = Variant();
  238. next_tag.fields.clear();
  239. next_tag.name = String();
  240. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  241. if (err == ERR_FILE_EOF) {
  242. memdelete(f);
  243. return;
  244. } else if (err != OK) {
  245. ERR_PRINT("ResourceFormatImporter::get_internal_resource_path_list - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  246. memdelete(f);
  247. return;
  248. }
  249. if (assign != String()) {
  250. if (assign.begins_with("path.")) {
  251. r_paths->push_back(value);
  252. } else if (assign == "path") {
  253. r_paths->push_back(value);
  254. }
  255. } else if (next_tag.name != "remap") {
  256. break;
  257. }
  258. }
  259. memdelete(f);
  260. }
  261. String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
  262. bool valid = true;
  263. PathAndType pat;
  264. _get_path_and_type(p_path, pat, &valid);
  265. return valid ? pat.group_file : String();
  266. }
  267. bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
  268. bool valid = true;
  269. PathAndType pat;
  270. _get_path_and_type(p_path, pat, &valid);
  271. return valid;
  272. }
  273. String ResourceFormatImporter::get_resource_type(const String &p_path) const {
  274. PathAndType pat;
  275. Error err = _get_path_and_type(p_path, pat);
  276. if (err != OK) {
  277. return "";
  278. }
  279. return pat.type;
  280. }
  281. Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
  282. PathAndType pat;
  283. Error err = _get_path_and_type(p_path, pat);
  284. if (err != OK) {
  285. return Variant();
  286. }
  287. return pat.metadata;
  288. }
  289. void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  290. PathAndType pat;
  291. Error err = _get_path_and_type(p_path, pat);
  292. if (err != OK) {
  293. return;
  294. }
  295. ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
  296. }
  297. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
  298. for (int i = 0; i < importers.size(); i++) {
  299. if (importers[i]->get_importer_name() == p_name) {
  300. return importers[i];
  301. }
  302. }
  303. return Ref<ResourceImporter>();
  304. }
  305. void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
  306. for (int i = 0; i < importers.size(); i++) {
  307. List<String> local_exts;
  308. importers[i]->get_recognized_extensions(&local_exts);
  309. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  310. if (p_extension.to_lower() == F->get()) {
  311. r_importers->push_back(importers[i]);
  312. }
  313. }
  314. }
  315. }
  316. void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_importers) {
  317. for (int i = 0; i < importers.size(); i++) {
  318. r_importers->push_back(importers[i]);
  319. }
  320. }
  321. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
  322. Ref<ResourceImporter> importer;
  323. float priority = 0;
  324. for (int i = 0; i < importers.size(); i++) {
  325. List<String> local_exts;
  326. importers[i]->get_recognized_extensions(&local_exts);
  327. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  328. if (p_extension.to_lower() == F->get() && importers[i]->get_priority() > priority) {
  329. importer = importers[i];
  330. priority = importers[i]->get_priority();
  331. }
  332. }
  333. }
  334. return importer;
  335. }
  336. String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
  337. return ProjectSettings::IMPORTED_FILES_PATH.plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text());
  338. }
  339. bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
  340. bool valid = true;
  341. PathAndType pat;
  342. _get_path_and_type(p_path, pat, &valid);
  343. if (!valid) {
  344. return false;
  345. }
  346. for (int i = 0; i < importers.size(); i++) {
  347. if (importers[i]->get_importer_name() == pat.importer) {
  348. if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
  349. return false;
  350. }
  351. }
  352. }
  353. return true;
  354. }
  355. String ResourceFormatImporter::get_import_settings_hash() const {
  356. Vector<Ref<ResourceImporter>> sorted_importers = importers;
  357. sorted_importers.sort_custom<SortImporterByName>();
  358. String hash;
  359. for (int i = 0; i < sorted_importers.size(); i++) {
  360. hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
  361. }
  362. return hash.md5_text();
  363. }
  364. ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
  365. ResourceFormatImporter::ResourceFormatImporter() {
  366. singleton = this;
  367. }