resource_importer.cpp 14 KB

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