resource_importer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**************************************************************************/
  2. /* resource_importer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/project_settings.h"
  33. #include "core/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 = 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_no_subresource_cache) {
  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_no_subresource_cache, r_error);
  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. bool ResourceFormatImporter::can_be_imported(const String &p_path) const {
  165. return ResourceFormatLoader::recognize_path(p_path);
  166. }
  167. int ResourceFormatImporter::get_import_order(const String &p_path) const {
  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. return importer->get_import_order();
  180. }
  181. return 0;
  182. }
  183. bool ResourceFormatImporter::handles_type(const String &p_type) const {
  184. for (int i = 0; i < importers.size(); i++) {
  185. String res_type = importers[i]->get_resource_type();
  186. if (res_type == String()) {
  187. continue;
  188. }
  189. if (ClassDB::is_parent_class(res_type, p_type)) {
  190. return true;
  191. }
  192. }
  193. return true;
  194. }
  195. String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
  196. PathAndType pat;
  197. Error err = _get_path_and_type(p_path, pat);
  198. if (err != OK) {
  199. return String();
  200. }
  201. return pat.path;
  202. }
  203. void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
  204. Error err;
  205. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  206. if (!f) {
  207. return;
  208. }
  209. VariantParser::StreamFile stream;
  210. stream.f = f;
  211. String assign;
  212. Variant value;
  213. VariantParser::Tag next_tag;
  214. int lines = 0;
  215. String error_text;
  216. while (true) {
  217. assign = Variant();
  218. next_tag.fields.clear();
  219. next_tag.name = String();
  220. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  221. if (err == ERR_FILE_EOF) {
  222. memdelete(f);
  223. return;
  224. } else if (err != OK) {
  225. ERR_PRINT("ResourceFormatImporter::get_internal_resource_path_list - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  226. memdelete(f);
  227. return;
  228. }
  229. if (assign != String()) {
  230. if (assign.begins_with("path.")) {
  231. r_paths->push_back(value);
  232. } else if (assign == "path") {
  233. r_paths->push_back(value);
  234. }
  235. } else if (next_tag.name != "remap") {
  236. break;
  237. }
  238. }
  239. memdelete(f);
  240. }
  241. String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
  242. bool valid = true;
  243. PathAndType pat;
  244. _get_path_and_type(p_path, pat, &valid);
  245. return valid ? pat.group_file : String();
  246. }
  247. bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
  248. bool valid = true;
  249. PathAndType pat;
  250. _get_path_and_type(p_path, pat, &valid);
  251. return valid;
  252. }
  253. String ResourceFormatImporter::get_resource_type(const String &p_path) const {
  254. PathAndType pat;
  255. Error err = _get_path_and_type(p_path, pat);
  256. if (err != OK) {
  257. return "";
  258. }
  259. return pat.type;
  260. }
  261. Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
  262. PathAndType pat;
  263. Error err = _get_path_and_type(p_path, pat);
  264. if (err != OK) {
  265. return Variant();
  266. }
  267. return pat.metadata;
  268. }
  269. void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  270. PathAndType pat;
  271. Error err = _get_path_and_type(p_path, pat);
  272. if (err != OK) {
  273. return;
  274. }
  275. ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
  276. }
  277. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
  278. for (int i = 0; i < importers.size(); i++) {
  279. if (importers[i]->get_importer_name() == p_name) {
  280. return importers[i];
  281. }
  282. }
  283. return Ref<ResourceImporter>();
  284. }
  285. void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
  286. for (int i = 0; i < importers.size(); i++) {
  287. List<String> local_exts;
  288. importers[i]->get_recognized_extensions(&local_exts);
  289. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  290. if (p_extension.to_lower() == F->get()) {
  291. r_importers->push_back(importers[i]);
  292. }
  293. }
  294. }
  295. }
  296. void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_importers) {
  297. for (int i = 0; i < importers.size(); i++) {
  298. r_importers->push_back(importers[i]);
  299. }
  300. }
  301. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
  302. Ref<ResourceImporter> importer;
  303. float priority = 0;
  304. for (int i = 0; i < importers.size(); i++) {
  305. List<String> local_exts;
  306. importers[i]->get_recognized_extensions(&local_exts);
  307. for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
  308. if (p_extension.to_lower() == F->get() && importers[i]->get_priority() > priority) {
  309. importer = importers[i];
  310. priority = importers[i]->get_priority();
  311. }
  312. }
  313. }
  314. return importer;
  315. }
  316. String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
  317. return ProjectSettings::get_singleton()->get_project_data_path().plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text());
  318. }
  319. bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
  320. bool valid = true;
  321. PathAndType pat;
  322. _get_path_and_type(p_path, pat, &valid);
  323. if (!valid) {
  324. return false;
  325. }
  326. for (int i = 0; i < importers.size(); i++) {
  327. if (importers[i]->get_importer_name() == pat.importer) {
  328. if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
  329. return false;
  330. }
  331. }
  332. }
  333. return true;
  334. }
  335. String ResourceFormatImporter::get_import_settings_hash() const {
  336. Vector<Ref<ResourceImporter>> sorted_importers = importers;
  337. sorted_importers.sort_custom<SortImporterByName>();
  338. String hash;
  339. for (int i = 0; i < sorted_importers.size(); i++) {
  340. hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
  341. }
  342. return hash.md5_text();
  343. }
  344. ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
  345. ResourceFormatImporter::ResourceFormatImporter() {
  346. singleton = this;
  347. }
  348. void ResourceImporter::_bind_methods() {
  349. BIND_ENUM_CONSTANT(IMPORT_ORDER_DEFAULT);
  350. BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
  351. }