resource_importer.h 8.5 KB

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