2
0

resource_importer.cpp 12 KB

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