resource_importer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "core/variant/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 = ClassDB::get_compatibility_remapped_class(value);
  82. } else if (assign == "importer") {
  83. r_path_and_type.importer = value;
  84. } else if (assign == "uid") {
  85. r_path_and_type.uid = ResourceUID::get_singleton()->text_to_id(value);
  86. } else if (assign == "group_file") {
  87. r_path_and_type.group_file = value;
  88. } else if (assign == "metadata") {
  89. r_path_and_type.metadata = value;
  90. } else if (assign == "valid") {
  91. if (r_valid) {
  92. *r_valid = value;
  93. }
  94. }
  95. } else if (next_tag.name != "remap") {
  96. break;
  97. }
  98. }
  99. memdelete(f);
  100. if (r_path_and_type.path == String() || r_path_and_type.type == String()) {
  101. return ERR_FILE_CORRUPT;
  102. }
  103. return OK;
  104. }
  105. RES ResourceFormatImporter::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  106. PathAndType pat;
  107. Error err = _get_path_and_type(p_path, pat);
  108. if (err != OK) {
  109. if (r_error) {
  110. *r_error = err;
  111. }
  112. return RES();
  113. }
  114. RES res = ResourceLoader::_load(pat.path, p_path, pat.type, p_cache_mode, r_error, p_use_sub_threads, r_progress);
  115. #ifdef TOOLS_ENABLED
  116. if (res.is_valid()) {
  117. res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
  118. res->set_import_path(pat.path);
  119. }
  120. #endif
  121. return res;
  122. }
  123. void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const {
  124. Set<String> found;
  125. for (int i = 0; i < importers.size(); i++) {
  126. List<String> local_exts;
  127. importers[i]->get_recognized_extensions(&local_exts);
  128. for (const String &F : local_exts) {
  129. if (!found.has(F)) {
  130. p_extensions->push_back(F);
  131. found.insert(F);
  132. }
  133. }
  134. }
  135. }
  136. void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  137. if (p_type == "") {
  138. get_recognized_extensions(p_extensions);
  139. return;
  140. }
  141. Set<String> found;
  142. for (int i = 0; i < importers.size(); i++) {
  143. String res_type = importers[i]->get_resource_type();
  144. if (res_type == String()) {
  145. continue;
  146. }
  147. if (!ClassDB::is_parent_class(res_type, p_type)) {
  148. continue;
  149. }
  150. List<String> local_exts;
  151. importers[i]->get_recognized_extensions(&local_exts);
  152. for (const String &F : local_exts) {
  153. if (!found.has(F)) {
  154. p_extensions->push_back(F);
  155. found.insert(F);
  156. }
  157. }
  158. }
  159. }
  160. bool ResourceFormatImporter::exists(const String &p_path) const {
  161. return FileAccess::exists(p_path + ".import");
  162. }
  163. bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const {
  164. return FileAccess::exists(p_path + ".import");
  165. }
  166. Error ResourceFormatImporter::get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const {
  167. r_order = 0;
  168. r_importer = "";
  169. r_can_threads = false;
  170. Ref<ResourceImporter> importer;
  171. if (FileAccess::exists(p_path + ".import")) {
  172. PathAndType pat;
  173. Error err = _get_path_and_type(p_path, pat);
  174. if (err == OK) {
  175. importer = get_importer_by_name(pat.importer);
  176. }
  177. } else {
  178. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  179. }
  180. if (importer.is_valid()) {
  181. r_order = importer->get_import_order();
  182. r_importer = importer->get_importer_name();
  183. r_can_threads = importer->can_import_threaded();
  184. return OK;
  185. } else {
  186. return ERR_INVALID_PARAMETER;
  187. }
  188. }
  189. int ResourceFormatImporter::get_import_order(const String &p_path) const {
  190. Ref<ResourceImporter> importer;
  191. if (FileAccess::exists(p_path + ".import")) {
  192. PathAndType pat;
  193. Error err = _get_path_and_type(p_path, pat);
  194. if (err == OK) {
  195. importer = get_importer_by_name(pat.importer);
  196. }
  197. } else {
  198. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  199. }
  200. if (importer.is_valid()) {
  201. return importer->get_import_order();
  202. }
  203. return 0;
  204. }
  205. bool ResourceFormatImporter::handles_type(const String &p_type) const {
  206. for (int i = 0; i < importers.size(); i++) {
  207. String res_type = importers[i]->get_resource_type();
  208. if (res_type == String()) {
  209. continue;
  210. }
  211. if (ClassDB::is_parent_class(res_type, p_type)) {
  212. return true;
  213. }
  214. }
  215. return true;
  216. }
  217. String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
  218. PathAndType pat;
  219. Error err = _get_path_and_type(p_path, pat);
  220. if (err != OK) {
  221. return String();
  222. }
  223. return pat.path;
  224. }
  225. void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
  226. Error err;
  227. FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  228. if (!f) {
  229. return;
  230. }
  231. VariantParser::StreamFile stream;
  232. stream.f = f;
  233. String assign;
  234. Variant value;
  235. VariantParser::Tag next_tag;
  236. int lines = 0;
  237. String error_text;
  238. while (true) {
  239. assign = Variant();
  240. next_tag.fields.clear();
  241. next_tag.name = String();
  242. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  243. if (err == ERR_FILE_EOF) {
  244. memdelete(f);
  245. return;
  246. } else if (err != OK) {
  247. ERR_PRINT("ResourceFormatImporter::get_internal_resource_path_list - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  248. memdelete(f);
  249. return;
  250. }
  251. if (assign != String()) {
  252. if (assign.begins_with("path.")) {
  253. r_paths->push_back(value);
  254. } else if (assign == "path") {
  255. r_paths->push_back(value);
  256. }
  257. } else if (next_tag.name != "remap") {
  258. break;
  259. }
  260. }
  261. memdelete(f);
  262. }
  263. String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
  264. bool valid = true;
  265. PathAndType pat;
  266. _get_path_and_type(p_path, pat, &valid);
  267. return valid ? pat.group_file : String();
  268. }
  269. bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
  270. bool valid = true;
  271. PathAndType pat;
  272. _get_path_and_type(p_path, pat, &valid);
  273. return valid;
  274. }
  275. String ResourceFormatImporter::get_resource_type(const String &p_path) const {
  276. PathAndType pat;
  277. Error err = _get_path_and_type(p_path, pat);
  278. if (err != OK) {
  279. return "";
  280. }
  281. return pat.type;
  282. }
  283. ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) const {
  284. PathAndType pat;
  285. Error err = _get_path_and_type(p_path, pat);
  286. if (err != OK) {
  287. return ResourceUID::INVALID_ID;
  288. }
  289. return pat.uid;
  290. }
  291. Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
  292. PathAndType pat;
  293. Error err = _get_path_and_type(p_path, pat);
  294. if (err != OK) {
  295. return Variant();
  296. }
  297. return pat.metadata;
  298. }
  299. void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  300. PathAndType pat;
  301. Error err = _get_path_and_type(p_path, pat);
  302. if (err != OK) {
  303. return;
  304. }
  305. ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
  306. }
  307. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
  308. for (int i = 0; i < importers.size(); i++) {
  309. if (importers[i]->get_importer_name() == p_name) {
  310. return importers[i];
  311. }
  312. }
  313. return Ref<ResourceImporter>();
  314. }
  315. void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
  316. for (int i = 0; i < importers.size(); i++) {
  317. List<String> local_exts;
  318. importers[i]->get_recognized_extensions(&local_exts);
  319. for (const String &F : local_exts) {
  320. if (p_extension.to_lower() == F) {
  321. r_importers->push_back(importers[i]);
  322. }
  323. }
  324. }
  325. }
  326. void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_importers) {
  327. for (int i = 0; i < importers.size(); i++) {
  328. r_importers->push_back(importers[i]);
  329. }
  330. }
  331. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
  332. Ref<ResourceImporter> importer;
  333. float priority = 0;
  334. for (int i = 0; i < importers.size(); i++) {
  335. List<String> local_exts;
  336. importers[i]->get_recognized_extensions(&local_exts);
  337. for (const String &F : local_exts) {
  338. if (p_extension.to_lower() == F && importers[i]->get_priority() > priority) {
  339. importer = importers[i];
  340. priority = importers[i]->get_priority();
  341. }
  342. }
  343. }
  344. return importer;
  345. }
  346. String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
  347. return ProjectSettings::get_singleton()->get_imported_files_path().plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text());
  348. }
  349. bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
  350. bool valid = true;
  351. PathAndType pat;
  352. _get_path_and_type(p_path, pat, &valid);
  353. if (!valid) {
  354. return false;
  355. }
  356. for (int i = 0; i < importers.size(); i++) {
  357. if (importers[i]->get_importer_name() == pat.importer) {
  358. if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
  359. return false;
  360. }
  361. }
  362. }
  363. return true;
  364. }
  365. String ResourceFormatImporter::get_import_settings_hash() const {
  366. Vector<Ref<ResourceImporter>> sorted_importers = importers;
  367. sorted_importers.sort_custom<SortImporterByName>();
  368. String hash;
  369. for (int i = 0; i < sorted_importers.size(); i++) {
  370. hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
  371. }
  372. return hash.md5_text();
  373. }
  374. ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
  375. ResourceFormatImporter::ResourceFormatImporter() {
  376. singleton = this;
  377. }
  378. void ResourceImporter::_bind_methods() {
  379. BIND_ENUM_CONSTANT(IMPORT_ORDER_DEFAULT);
  380. BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
  381. }