resource_import.cpp 9.4 KB

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