resource_importer.cpp 16 KB

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