resource_importer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*************************************************************************/
  2. /* resource_importer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  40. if (f.is_null()) {
  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. return OK;
  64. } else if (err != OK) {
  65. ERR_PRINT("ResourceFormatImporter::load - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  66. return err;
  67. }
  68. if (!assign.is_empty()) {
  69. if (!path_found && assign.begins_with("path.") && r_path_and_type.path.is_empty()) {
  70. String feature = assign.get_slicec('.', 1);
  71. if (OS::get_singleton()->has_feature(feature)) {
  72. r_path_and_type.path = value;
  73. path_found = true; //first match must have priority
  74. }
  75. } else if (!path_found && assign == "path") {
  76. r_path_and_type.path = value;
  77. path_found = true; //first match must have priority
  78. } else if (assign == "type") {
  79. r_path_and_type.type = ClassDB::get_compatibility_remapped_class(value);
  80. } else if (assign == "importer") {
  81. r_path_and_type.importer = value;
  82. } else if (assign == "uid") {
  83. r_path_and_type.uid = ResourceUID::get_singleton()->text_to_id(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. #ifdef TOOLS_ENABLED
  98. if (r_path_and_type.metadata && !r_path_and_type.path.is_empty()) {
  99. Dictionary meta = r_path_and_type.metadata;
  100. if (meta.has("has_editor_variant")) {
  101. r_path_and_type.path = r_path_and_type.path.get_basename() + ".editor." + r_path_and_type.path.get_extension();
  102. }
  103. }
  104. #endif
  105. if (r_path_and_type.path.is_empty() || r_path_and_type.type.is_empty()) {
  106. return ERR_FILE_CORRUPT;
  107. }
  108. return OK;
  109. }
  110. Ref<Resource> 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) {
  111. PathAndType pat;
  112. Error err = _get_path_and_type(p_path, pat);
  113. if (err != OK) {
  114. if (r_error) {
  115. *r_error = err;
  116. }
  117. return Ref<Resource>();
  118. }
  119. Ref<Resource> res = ResourceLoader::_load(pat.path, p_path, pat.type, p_cache_mode, r_error, p_use_sub_threads, r_progress);
  120. #ifdef TOOLS_ENABLED
  121. if (res.is_valid()) {
  122. res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
  123. res->set_import_path(pat.path);
  124. }
  125. #endif
  126. return res;
  127. }
  128. void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const {
  129. HashSet<String> found;
  130. for (int i = 0; i < importers.size(); i++) {
  131. List<String> local_exts;
  132. importers[i]->get_recognized_extensions(&local_exts);
  133. for (const String &F : local_exts) {
  134. if (!found.has(F)) {
  135. p_extensions->push_back(F);
  136. found.insert(F);
  137. }
  138. }
  139. }
  140. }
  141. void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  142. if (p_type.is_empty()) {
  143. get_recognized_extensions(p_extensions);
  144. return;
  145. }
  146. HashSet<String> found;
  147. for (int i = 0; i < importers.size(); i++) {
  148. String res_type = importers[i]->get_resource_type();
  149. if (res_type.is_empty()) {
  150. continue;
  151. }
  152. if (!ClassDB::is_parent_class(res_type, p_type)) {
  153. continue;
  154. }
  155. List<String> local_exts;
  156. importers[i]->get_recognized_extensions(&local_exts);
  157. for (const String &F : local_exts) {
  158. if (!found.has(F)) {
  159. p_extensions->push_back(F);
  160. found.insert(F);
  161. }
  162. }
  163. }
  164. }
  165. bool ResourceFormatImporter::exists(const String &p_path) const {
  166. return FileAccess::exists(p_path + ".import");
  167. }
  168. bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const {
  169. return FileAccess::exists(p_path + ".import");
  170. }
  171. Error ResourceFormatImporter::get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const {
  172. r_order = 0;
  173. r_importer = "";
  174. r_can_threads = false;
  175. Ref<ResourceImporter> importer;
  176. if (FileAccess::exists(p_path + ".import")) {
  177. PathAndType pat;
  178. Error err = _get_path_and_type(p_path, pat);
  179. if (err == OK) {
  180. importer = get_importer_by_name(pat.importer);
  181. }
  182. } else {
  183. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  184. }
  185. if (importer.is_valid()) {
  186. r_order = importer->get_import_order();
  187. r_importer = importer->get_importer_name();
  188. r_can_threads = importer->can_import_threaded();
  189. return OK;
  190. } else {
  191. return ERR_INVALID_PARAMETER;
  192. }
  193. }
  194. int ResourceFormatImporter::get_import_order(const String &p_path) const {
  195. Ref<ResourceImporter> importer;
  196. if (FileAccess::exists(p_path + ".import")) {
  197. PathAndType pat;
  198. Error err = _get_path_and_type(p_path, pat);
  199. if (err == OK) {
  200. importer = get_importer_by_name(pat.importer);
  201. }
  202. } else {
  203. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  204. }
  205. if (importer.is_valid()) {
  206. return importer->get_import_order();
  207. }
  208. return 0;
  209. }
  210. bool ResourceFormatImporter::handles_type(const String &p_type) const {
  211. for (int i = 0; i < importers.size(); i++) {
  212. String res_type = importers[i]->get_resource_type();
  213. if (res_type.is_empty()) {
  214. continue;
  215. }
  216. if (ClassDB::is_parent_class(res_type, p_type)) {
  217. return true;
  218. }
  219. }
  220. return true;
  221. }
  222. String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
  223. PathAndType pat;
  224. Error err = _get_path_and_type(p_path, pat);
  225. if (err != OK) {
  226. return String();
  227. }
  228. return pat.path;
  229. }
  230. void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
  231. Error err;
  232. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  233. if (f.is_null()) {
  234. return;
  235. }
  236. VariantParser::StreamFile stream;
  237. stream.f = f;
  238. String assign;
  239. Variant value;
  240. VariantParser::Tag next_tag;
  241. int lines = 0;
  242. String error_text;
  243. while (true) {
  244. assign = Variant();
  245. next_tag.fields.clear();
  246. next_tag.name = String();
  247. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  248. if (err == ERR_FILE_EOF) {
  249. return;
  250. } else if (err != OK) {
  251. ERR_PRINT("ResourceFormatImporter::get_internal_resource_path_list - " + p_path + ".import:" + itos(lines) + " error: " + error_text);
  252. return;
  253. }
  254. if (!assign.is_empty()) {
  255. if (assign.begins_with("path.")) {
  256. r_paths->push_back(value);
  257. } else if (assign == "path") {
  258. r_paths->push_back(value);
  259. }
  260. } else if (next_tag.name != "remap") {
  261. break;
  262. }
  263. }
  264. }
  265. String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
  266. bool valid = true;
  267. PathAndType pat;
  268. _get_path_and_type(p_path, pat, &valid);
  269. return valid ? pat.group_file : String();
  270. }
  271. bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
  272. bool valid = true;
  273. PathAndType pat;
  274. _get_path_and_type(p_path, pat, &valid);
  275. return valid;
  276. }
  277. String ResourceFormatImporter::get_resource_type(const String &p_path) const {
  278. PathAndType pat;
  279. Error err = _get_path_and_type(p_path, pat);
  280. if (err != OK) {
  281. return "";
  282. }
  283. return pat.type;
  284. }
  285. ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) const {
  286. PathAndType pat;
  287. Error err = _get_path_and_type(p_path, pat);
  288. if (err != OK) {
  289. return ResourceUID::INVALID_ID;
  290. }
  291. return pat.uid;
  292. }
  293. Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
  294. PathAndType pat;
  295. Error err = _get_path_and_type(p_path, pat);
  296. if (err != OK) {
  297. return Variant();
  298. }
  299. return pat.metadata;
  300. }
  301. void ResourceFormatImporter::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  302. PathAndType pat;
  303. Error err = _get_path_and_type(p_path, pat);
  304. if (err != OK) {
  305. return;
  306. }
  307. ResourceLoader::get_classes_used(pat.path, r_classes);
  308. }
  309. void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  310. PathAndType pat;
  311. Error err = _get_path_and_type(p_path, pat);
  312. if (err != OK) {
  313. return;
  314. }
  315. ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
  316. }
  317. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
  318. for (int i = 0; i < importers.size(); i++) {
  319. if (importers[i]->get_importer_name() == p_name) {
  320. return importers[i];
  321. }
  322. }
  323. return Ref<ResourceImporter>();
  324. }
  325. void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
  326. for (int i = 0; i < importers.size(); i++) {
  327. List<String> local_exts;
  328. importers[i]->get_recognized_extensions(&local_exts);
  329. for (const String &F : local_exts) {
  330. if (p_extension.to_lower() == F) {
  331. r_importers->push_back(importers[i]);
  332. }
  333. }
  334. }
  335. }
  336. void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_importers) {
  337. for (int i = 0; i < importers.size(); i++) {
  338. r_importers->push_back(importers[i]);
  339. }
  340. }
  341. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
  342. Ref<ResourceImporter> importer;
  343. float priority = 0;
  344. for (int i = 0; i < importers.size(); i++) {
  345. List<String> local_exts;
  346. importers[i]->get_recognized_extensions(&local_exts);
  347. for (const String &F : local_exts) {
  348. if (p_extension.to_lower() == F && importers[i]->get_priority() > priority) {
  349. importer = importers[i];
  350. priority = importers[i]->get_priority();
  351. }
  352. }
  353. }
  354. return importer;
  355. }
  356. String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
  357. return ProjectSettings::get_singleton()->get_imported_files_path().path_join(p_for_file.get_file() + "-" + p_for_file.md5_text());
  358. }
  359. bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
  360. bool valid = true;
  361. PathAndType pat;
  362. _get_path_and_type(p_path, pat, &valid);
  363. if (!valid) {
  364. return false;
  365. }
  366. for (int i = 0; i < importers.size(); i++) {
  367. if (importers[i]->get_importer_name() == pat.importer) {
  368. if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
  369. return false;
  370. }
  371. }
  372. }
  373. return true;
  374. }
  375. String ResourceFormatImporter::get_import_settings_hash() const {
  376. Vector<Ref<ResourceImporter>> sorted_importers = importers;
  377. sorted_importers.sort_custom<SortImporterByName>();
  378. String hash;
  379. for (int i = 0; i < sorted_importers.size(); i++) {
  380. hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
  381. }
  382. return hash.md5_text();
  383. }
  384. ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
  385. ResourceFormatImporter::ResourceFormatImporter() {
  386. singleton = this;
  387. }
  388. void ResourceImporter::_bind_methods() {
  389. BIND_ENUM_CONSTANT(IMPORT_ORDER_DEFAULT);
  390. BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
  391. }
  392. void ResourceFormatImporter::add_importer(const Ref<ResourceImporter> &p_importer, bool p_first_priority) {
  393. ERR_FAIL_COND(p_importer.is_null());
  394. if (p_first_priority) {
  395. importers.insert(0, p_importer);
  396. } else {
  397. importers.push_back(p_importer);
  398. }
  399. }