resource_import.cpp 10 KB

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