resource_importer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**************************************************************************/
  2. /* resource_importer.h */
  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. #ifndef RESOURCE_IMPORTER_H
  31. #define RESOURCE_IMPORTER_H
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. class ResourceImporter;
  35. class ResourceFormatImporter : public ResourceFormatLoader {
  36. struct PathAndType {
  37. String path;
  38. String type;
  39. String importer;
  40. String group_file;
  41. Variant metadata;
  42. uint64_t uid = ResourceUID::INVALID_ID;
  43. };
  44. Error _get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid = nullptr) const;
  45. static ResourceFormatImporter *singleton;
  46. //need them to stay in order to compute the settings hash
  47. struct SortImporterByName {
  48. bool operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const;
  49. };
  50. Vector<Ref<ResourceImporter>> importers;
  51. public:
  52. static ResourceFormatImporter *get_singleton() { return singleton; }
  53. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  54. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  55. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override;
  56. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const override;
  57. virtual bool handles_type(const String &p_type) const override;
  58. virtual String get_resource_type(const String &p_path) const override;
  59. virtual ResourceUID::ID get_resource_uid(const String &p_path) const override;
  60. virtual Variant get_resource_metadata(const String &p_path) const;
  61. virtual bool is_import_valid(const String &p_path) const override;
  62. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
  63. virtual bool is_imported(const String &p_path) const override { return recognize_path(p_path); }
  64. virtual String get_import_group_file(const String &p_path) const override;
  65. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override;
  66. virtual bool exists(const String &p_path) const override;
  67. virtual int get_import_order(const String &p_path) const override;
  68. Error get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const;
  69. String get_internal_resource_path(const String &p_path) const;
  70. void get_internal_resource_path_list(const String &p_path, List<String> *r_paths);
  71. void add_importer(const Ref<ResourceImporter> &p_importer, bool p_first_priority = false);
  72. void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); }
  73. Ref<ResourceImporter> get_importer_by_name(const String &p_name) const;
  74. Ref<ResourceImporter> get_importer_by_extension(const String &p_extension) const;
  75. void get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers);
  76. void get_importers(List<Ref<ResourceImporter>> *r_importers);
  77. bool are_import_settings_valid(const String &p_path) const;
  78. String get_import_settings_hash() const;
  79. String get_import_base_path(const String &p_for_file) const;
  80. ResourceFormatImporter();
  81. };
  82. class ResourceImporter : public RefCounted {
  83. GDCLASS(ResourceImporter, RefCounted);
  84. protected:
  85. static void _bind_methods();
  86. public:
  87. virtual String get_importer_name() const = 0;
  88. virtual String get_visible_name() const = 0;
  89. virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
  90. virtual String get_save_extension() const = 0;
  91. virtual String get_resource_type() const = 0;
  92. virtual float get_priority() const { return 1.0; }
  93. virtual int get_import_order() const { return IMPORT_ORDER_DEFAULT; }
  94. virtual int get_format_version() const { return 0; }
  95. struct ImportOption {
  96. PropertyInfo option;
  97. Variant default_value;
  98. ImportOption(const PropertyInfo &p_info, const Variant &p_default) :
  99. option(p_info),
  100. default_value(p_default) {
  101. }
  102. ImportOption() {}
  103. };
  104. enum ImportOrder {
  105. IMPORT_ORDER_DEFAULT = 0,
  106. IMPORT_ORDER_SCENE = 100,
  107. };
  108. virtual bool has_advanced_options() const { return false; }
  109. virtual void show_advanced_options(const String &p_path) {}
  110. virtual int get_preset_count() const { return 0; }
  111. virtual String get_preset_name(int p_idx) const { return String(); }
  112. virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const = 0;
  113. virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const = 0;
  114. virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {}
  115. virtual String get_option_group_file() const { return String(); }
  116. virtual Error import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) = 0;
  117. virtual bool can_import_threaded() const { return true; }
  118. virtual void import_threaded_begin() {}
  119. virtual void import_threaded_end() {}
  120. virtual Error import_group_file(const String &p_group_file, const HashMap<String, HashMap<StringName, Variant>> &p_source_file_options, const HashMap<String, String> &p_base_paths) { return ERR_UNAVAILABLE; }
  121. virtual bool are_import_settings_valid(const String &p_path) const { return true; }
  122. virtual String get_import_settings_string() const { return String(); }
  123. };
  124. VARIANT_ENUM_CAST(ResourceImporter::ImportOrder);
  125. class ResourceFormatImporterSaver : public ResourceFormatSaver {
  126. GDCLASS(ResourceFormatImporterSaver, ResourceFormatSaver)
  127. public:
  128. virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override;
  129. };
  130. #endif // RESOURCE_IMPORTER_H