resource_importer.h 8.6 KB

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