resource_importer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/config/project_settings.h"
  32. #include "core/io/config_file.h"
  33. #include "core/io/image.h"
  34. #include "core/os/os.h"
  35. #include "core/variant/variant_parser.h"
  36. ResourceFormatImporterLoadOnStartup ResourceImporter::load_on_startup = nullptr;
  37. bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const {
  38. return p_a->get_importer_name() < p_b->get_importer_name();
  39. }
  40. Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool p_load, bool *r_valid) const {
  41. Error err;
  42. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  43. if (f.is_null()) {
  44. if (r_valid) {
  45. *r_valid = false;
  46. }
  47. return err;
  48. }
  49. VariantParser::StreamFile stream;
  50. stream.f = f;
  51. String assign;
  52. Variant value;
  53. VariantParser::Tag next_tag;
  54. if (r_valid) {
  55. *r_valid = true;
  56. }
  57. int lines = 0;
  58. String error_text;
  59. bool path_found = false; // First match must have priority.
  60. String decomp_path;
  61. bool decomp_path_found = false;
  62. while (true) {
  63. assign = Variant();
  64. next_tag.fields.clear();
  65. next_tag.name = String();
  66. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  67. if (err == ERR_FILE_EOF) {
  68. if (p_load && !path_found && decomp_path_found) {
  69. print_verbose(vformat("No natively supported texture format found for %s, using decompressable format %s.", p_path, decomp_path));
  70. r_path_and_type.path = decomp_path;
  71. }
  72. return OK;
  73. } else if (err != OK) {
  74. ERR_PRINT(vformat("ResourceFormatImporter::load - %s.import:%d error: %s.", p_path, lines, error_text));
  75. return err;
  76. }
  77. if (!assign.is_empty()) {
  78. if (!path_found && assign.begins_with("path.") && r_path_and_type.path.is_empty()) {
  79. String feature = assign.get_slicec('.', 1);
  80. if (OS::get_singleton()->has_feature(feature)) {
  81. r_path_and_type.path = value;
  82. path_found = true; // First match must have priority.
  83. } else if (p_load && Image::can_decompress(feature) && !decomp_path_found) { // When loading, check for decompressable formats and use first one found if nothing else is supported.
  84. decomp_path = value;
  85. decomp_path_found = true; // First match must have priority.
  86. }
  87. } else if (!path_found && assign == "path") {
  88. r_path_and_type.path = value;
  89. path_found = true; // First match must have priority.
  90. } else if (assign == "type") {
  91. r_path_and_type.type = ClassDB::get_compatibility_remapped_class(value);
  92. } else if (assign == "importer") {
  93. r_path_and_type.importer = value;
  94. } else if (assign == "uid") {
  95. r_path_and_type.uid = ResourceUID::get_singleton()->text_to_id(value);
  96. } else if (assign == "group_file") {
  97. r_path_and_type.group_file = value;
  98. } else if (assign == "metadata") {
  99. r_path_and_type.metadata = value;
  100. } else if (assign == "valid") {
  101. if (r_valid) {
  102. *r_valid = value;
  103. }
  104. }
  105. } else if (next_tag.name != "remap") {
  106. break;
  107. }
  108. }
  109. #ifdef TOOLS_ENABLED
  110. if (r_path_and_type.metadata && !r_path_and_type.path.is_empty()) {
  111. Dictionary meta = r_path_and_type.metadata;
  112. if (meta.has("has_editor_variant")) {
  113. r_path_and_type.path = r_path_and_type.path.get_basename() + ".editor." + r_path_and_type.path.get_extension();
  114. }
  115. }
  116. #endif
  117. if (r_path_and_type.type.is_empty()) {
  118. return ERR_FILE_CORRUPT;
  119. }
  120. if (r_path_and_type.path.is_empty()) {
  121. // Some importers may not write files to the .godot folder, so the path can be empty.
  122. if (r_path_and_type.importer.is_empty()) {
  123. return ERR_FILE_CORRUPT;
  124. }
  125. // It's only invalid if the extension for the importer is not empty.
  126. Ref<ResourceImporter> importer = get_importer_by_name(r_path_and_type.importer);
  127. if (importer.is_null() || !importer->get_save_extension().is_empty()) {
  128. return ERR_FILE_CORRUPT;
  129. }
  130. }
  131. return OK;
  132. }
  133. 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) {
  134. #ifdef TOOLS_ENABLED
  135. // When loading a resource on startup, we use the load_on_startup callback,
  136. // which executes the loading in the EditorFileSystem. It can reimport
  137. // the resource and retry the load, allowing the resource to be loaded
  138. // even if it is not yet imported.
  139. if (ResourceImporter::load_on_startup != nullptr) {
  140. return ResourceImporter::load_on_startup(this, p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode);
  141. }
  142. #endif
  143. return load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, false);
  144. }
  145. Ref<Resource> ResourceFormatImporter::load_internal(const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode, bool p_silence_errors) {
  146. PathAndType pat;
  147. Error err = _get_path_and_type(p_path, pat, true);
  148. if (err != OK) {
  149. if (r_error) {
  150. *r_error = err;
  151. }
  152. return Ref<Resource>();
  153. }
  154. if (p_silence_errors) {
  155. // Note: Some importers do not create files in the .godot folder, so we need to check if the path is empty.
  156. if (!pat.path.is_empty() && !FileAccess::exists(pat.path)) {
  157. return Ref<Resource>();
  158. }
  159. }
  160. Ref<Resource> res = ResourceLoader::_load(pat.path, p_path, pat.type, p_cache_mode, r_error, p_use_sub_threads, r_progress);
  161. #ifdef TOOLS_ENABLED
  162. if (res.is_valid()) {
  163. res->set_import_last_modified_time(res->get_last_modified_time()); //pass this, if used
  164. res->set_import_path(pat.path);
  165. }
  166. #endif
  167. return res;
  168. }
  169. void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const {
  170. HashSet<String> found;
  171. for (int i = 0; i < importers.size(); i++) {
  172. List<String> local_exts;
  173. importers[i]->get_recognized_extensions(&local_exts);
  174. for (const String &F : local_exts) {
  175. if (!found.has(F)) {
  176. p_extensions->push_back(F);
  177. found.insert(F);
  178. }
  179. }
  180. }
  181. }
  182. void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  183. if (p_type.is_empty()) {
  184. get_recognized_extensions(p_extensions);
  185. return;
  186. }
  187. HashSet<String> found;
  188. for (int i = 0; i < importers.size(); i++) {
  189. String res_type = importers[i]->get_resource_type();
  190. if (res_type.is_empty()) {
  191. continue;
  192. }
  193. if (!ClassDB::is_parent_class(res_type, p_type)) {
  194. continue;
  195. }
  196. List<String> local_exts;
  197. importers[i]->get_recognized_extensions(&local_exts);
  198. for (const String &F : local_exts) {
  199. if (!found.has(F)) {
  200. p_extensions->push_back(F);
  201. found.insert(F);
  202. }
  203. }
  204. }
  205. }
  206. bool ResourceFormatImporter::exists(const String &p_path) const {
  207. return FileAccess::exists(p_path + ".import");
  208. }
  209. bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const {
  210. return FileAccess::exists(p_path + ".import");
  211. }
  212. Error ResourceFormatImporter::get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const {
  213. r_order = 0;
  214. r_importer = "";
  215. r_can_threads = false;
  216. Ref<ResourceImporter> importer;
  217. if (FileAccess::exists(p_path + ".import")) {
  218. PathAndType pat;
  219. Error err = _get_path_and_type(p_path, pat, false);
  220. if (err == OK) {
  221. importer = get_importer_by_name(pat.importer);
  222. }
  223. } else {
  224. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  225. }
  226. if (importer.is_valid()) {
  227. r_order = importer->get_import_order();
  228. r_importer = importer->get_importer_name();
  229. r_can_threads = importer->can_import_threaded();
  230. return OK;
  231. } else {
  232. return ERR_INVALID_PARAMETER;
  233. }
  234. }
  235. int ResourceFormatImporter::get_import_order(const String &p_path) const {
  236. Ref<ResourceImporter> importer;
  237. if (FileAccess::exists(p_path + ".import")) {
  238. PathAndType pat;
  239. Error err = _get_path_and_type(p_path, pat, false);
  240. if (err == OK) {
  241. importer = get_importer_by_name(pat.importer);
  242. }
  243. } else {
  244. importer = get_importer_by_extension(p_path.get_extension().to_lower());
  245. }
  246. if (importer.is_valid()) {
  247. return importer->get_import_order();
  248. }
  249. return 0;
  250. }
  251. bool ResourceFormatImporter::handles_type(const String &p_type) const {
  252. for (int i = 0; i < importers.size(); i++) {
  253. String res_type = importers[i]->get_resource_type();
  254. if (res_type.is_empty()) {
  255. continue;
  256. }
  257. if (ClassDB::is_parent_class(res_type, p_type)) {
  258. return true;
  259. }
  260. }
  261. return true;
  262. }
  263. String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const {
  264. PathAndType pat;
  265. Error err = _get_path_and_type(p_path, pat, false);
  266. if (err != OK) {
  267. return String();
  268. }
  269. return pat.path;
  270. }
  271. void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) {
  272. Error err;
  273. Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
  274. if (f.is_null()) {
  275. return;
  276. }
  277. VariantParser::StreamFile stream;
  278. stream.f = f;
  279. String assign;
  280. Variant value;
  281. VariantParser::Tag next_tag;
  282. int lines = 0;
  283. String error_text;
  284. while (true) {
  285. assign = Variant();
  286. next_tag.fields.clear();
  287. next_tag.name = String();
  288. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  289. if (err == ERR_FILE_EOF) {
  290. return;
  291. } else if (err != OK) {
  292. ERR_PRINT(vformat("ResourceFormatImporter::get_internal_resource_path_list - %s.import:%d error: %s.", p_path, lines, error_text));
  293. return;
  294. }
  295. if (!assign.is_empty()) {
  296. if (assign.begins_with("path.")) {
  297. r_paths->push_back(value);
  298. } else if (assign == "path") {
  299. r_paths->push_back(value);
  300. }
  301. } else if (next_tag.name != "remap") {
  302. break;
  303. }
  304. }
  305. }
  306. String ResourceFormatImporter::get_import_group_file(const String &p_path) const {
  307. bool valid = true;
  308. PathAndType pat;
  309. _get_path_and_type(p_path, pat, false, &valid);
  310. return valid ? pat.group_file : String();
  311. }
  312. bool ResourceFormatImporter::is_import_valid(const String &p_path) const {
  313. bool valid = true;
  314. PathAndType pat;
  315. _get_path_and_type(p_path, pat, false, &valid);
  316. return valid;
  317. }
  318. String ResourceFormatImporter::get_resource_type(const String &p_path) const {
  319. PathAndType pat;
  320. Error err = _get_path_and_type(p_path, pat, false);
  321. if (err != OK) {
  322. return "";
  323. }
  324. return pat.type;
  325. }
  326. ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) const {
  327. PathAndType pat;
  328. Error err = _get_path_and_type(p_path, pat, false);
  329. if (err != OK) {
  330. return ResourceUID::INVALID_ID;
  331. }
  332. return pat.uid;
  333. }
  334. bool ResourceFormatImporter::has_custom_uid_support() const {
  335. return true;
  336. }
  337. Error ResourceFormatImporter::get_resource_import_info(const String &p_path, StringName &r_type, ResourceUID::ID &r_uid, String &r_import_group_file) const {
  338. PathAndType pat;
  339. Error err = _get_path_and_type(p_path, pat, false);
  340. if (err == OK) {
  341. r_type = pat.type;
  342. r_uid = pat.uid;
  343. r_import_group_file = pat.group_file;
  344. } else {
  345. r_type = "";
  346. r_uid = ResourceUID::INVALID_ID;
  347. r_import_group_file = "";
  348. }
  349. return err;
  350. }
  351. Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
  352. PathAndType pat;
  353. Error err = _get_path_and_type(p_path, pat, false);
  354. if (err != OK) {
  355. return Variant();
  356. }
  357. return pat.metadata;
  358. }
  359. void ResourceFormatImporter::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  360. PathAndType pat;
  361. Error err = _get_path_and_type(p_path, pat, false);
  362. if (err != OK) {
  363. return;
  364. }
  365. ResourceLoader::get_classes_used(pat.path, r_classes);
  366. }
  367. void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  368. PathAndType pat;
  369. Error err = _get_path_and_type(p_path, pat, false);
  370. if (err != OK) {
  371. return;
  372. }
  373. ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types);
  374. }
  375. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const {
  376. for (int i = 0; i < importers.size(); i++) {
  377. if (importers[i]->get_importer_name() == p_name) {
  378. return importers[i];
  379. }
  380. }
  381. return Ref<ResourceImporter>();
  382. }
  383. void ResourceFormatImporter::add_importer(const Ref<ResourceImporter> &p_importer, bool p_first_priority) {
  384. ERR_FAIL_COND(p_importer.is_null());
  385. if (p_first_priority) {
  386. importers.insert(0, p_importer);
  387. } else {
  388. importers.push_back(p_importer);
  389. }
  390. }
  391. void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) {
  392. for (int i = 0; i < importers.size(); i++) {
  393. List<String> local_exts;
  394. importers[i]->get_recognized_extensions(&local_exts);
  395. for (const String &F : local_exts) {
  396. if (p_extension.to_lower() == F) {
  397. r_importers->push_back(importers[i]);
  398. break;
  399. }
  400. }
  401. }
  402. }
  403. void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_importers) {
  404. for (int i = 0; i < importers.size(); i++) {
  405. r_importers->push_back(importers[i]);
  406. }
  407. }
  408. Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const {
  409. Ref<ResourceImporter> importer;
  410. float priority = 0;
  411. for (int i = 0; i < importers.size(); i++) {
  412. List<String> local_exts;
  413. importers[i]->get_recognized_extensions(&local_exts);
  414. for (const String &F : local_exts) {
  415. if (p_extension.to_lower() == F && importers[i]->get_priority() > priority) {
  416. importer = importers[i];
  417. priority = importers[i]->get_priority();
  418. }
  419. }
  420. }
  421. return importer;
  422. }
  423. String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
  424. return ProjectSettings::get_singleton()->get_imported_files_path().path_join(p_for_file.get_file() + "-" + p_for_file.md5_text());
  425. }
  426. bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
  427. bool valid = true;
  428. PathAndType pat;
  429. _get_path_and_type(p_path, pat, false, &valid);
  430. if (!valid) {
  431. return false;
  432. }
  433. for (int i = 0; i < importers.size(); i++) {
  434. if (importers[i]->get_importer_name() == pat.importer) {
  435. if (!importers[i]->are_import_settings_valid(p_path, pat.metadata)) { //importer thinks this is not valid
  436. return false;
  437. }
  438. }
  439. }
  440. return true;
  441. }
  442. String ResourceFormatImporter::get_import_settings_hash() const {
  443. Vector<Ref<ResourceImporter>> sorted_importers = importers;
  444. sorted_importers.sort_custom<SortImporterByName>();
  445. String hash;
  446. for (int i = 0; i < sorted_importers.size(); i++) {
  447. hash += ":" + sorted_importers[i]->get_importer_name() + ":" + sorted_importers[i]->get_import_settings_string();
  448. }
  449. return hash.md5_text();
  450. }
  451. ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
  452. ResourceFormatImporter::ResourceFormatImporter() {
  453. singleton = this;
  454. }
  455. //////////////
  456. void ResourceImporter::_bind_methods() {
  457. BIND_ENUM_CONSTANT(IMPORT_ORDER_DEFAULT);
  458. BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
  459. }
  460. /////
  461. Error ResourceFormatImporterSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  462. Ref<ConfigFile> cf;
  463. cf.instantiate();
  464. Error err = cf->load(p_path + ".import");
  465. if (err != OK) {
  466. return err;
  467. }
  468. cf->set_value("remap", "uid", ResourceUID::get_singleton()->id_to_text(p_uid));
  469. cf->save(p_path + ".import");
  470. return OK;
  471. }