resource_importer.cpp 12 KB

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