resource_importer.cpp 12 KB

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