resource_importer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. class ResourceImporter;
  34. class ResourceFormatImporter : public ResourceFormatLoader {
  35. struct PathAndType {
  36. String path;
  37. String type;
  38. String importer;
  39. String group_file;
  40. Variant metadata;
  41. };
  42. Error _get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid = nullptr) const;
  43. static ResourceFormatImporter *singleton;
  44. //need them to stay in order to compute the settings hash
  45. struct SortImporterByName {
  46. bool operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const;
  47. };
  48. Vector<Ref<ResourceImporter>> importers;
  49. public:
  50. static ResourceFormatImporter *get_singleton() { return singleton; }
  51. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_no_subresource_cache = false);
  52. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  53. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  54. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
  55. virtual bool handles_type(const String &p_type) const;
  56. virtual String get_resource_type(const String &p_path) const;
  57. virtual Variant get_resource_metadata(const String &p_path) const;
  58. virtual bool is_import_valid(const String &p_path) const;
  59. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  60. virtual bool is_imported(const String &p_path) const { return recognize_path(p_path); }
  61. virtual String get_import_group_file(const String &p_path) const;
  62. virtual bool exists(const String &p_path) const;
  63. virtual bool can_be_imported(const String &p_path) const;
  64. virtual int get_import_order(const String &p_path) const;
  65. String get_internal_resource_path(const String &p_path) const;
  66. void get_internal_resource_path_list(const String &p_path, List<String> *r_paths);
  67. void add_importer(const Ref<ResourceImporter> &p_importer) {
  68. importers.push_back(p_importer);
  69. }
  70. void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); }
  71. Ref<ResourceImporter> get_importer_by_name(const String &p_name) const;
  72. Ref<ResourceImporter> get_importer_by_extension(const String &p_extension) const;
  73. void get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers);
  74. void get_importers(List<Ref<ResourceImporter>> *r_importers);
  75. bool are_import_settings_valid(const String &p_path) const;
  76. String get_import_settings_hash() const;
  77. String get_import_base_path(const String &p_for_file) const;
  78. ResourceFormatImporter();
  79. };
  80. class ResourceImporter : public Reference {
  81. GDCLASS(ResourceImporter, Reference);
  82. protected:
  83. static void _bind_methods();
  84. public:
  85. virtual String get_importer_name() const = 0;
  86. virtual String get_visible_name() const = 0;
  87. virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
  88. virtual String get_save_extension() const = 0;
  89. virtual String get_resource_type() const = 0;
  90. virtual float get_priority() const { return 1.0; }
  91. virtual int get_import_order() const { return IMPORT_ORDER_DEFAULT; }
  92. struct ImportOption {
  93. PropertyInfo option;
  94. Variant default_value;
  95. ImportOption(const PropertyInfo &p_info, const Variant &p_default) :
  96. option(p_info),
  97. default_value(p_default) {
  98. }
  99. ImportOption() {}
  100. };
  101. enum ImportOrder {
  102. IMPORT_ORDER_DEFAULT = 0,
  103. IMPORT_ORDER_SCENE = 100,
  104. };
  105. virtual int get_preset_count() const { return 0; }
  106. virtual String get_preset_name(int p_idx) const { return String(); }
  107. virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const = 0;
  108. virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const = 0;
  109. virtual String get_option_group_file() const { return String(); }
  110. virtual Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) = 0;
  111. virtual Error import_group_file(const String &p_group_file, const Map<String, Map<StringName, Variant>> &p_source_file_options, const Map<String, String> &p_base_paths) { return ERR_UNAVAILABLE; }
  112. virtual bool are_import_settings_valid(const String &p_path) const { return true; }
  113. virtual String get_import_settings_string() const { return String(); }
  114. };
  115. VARIANT_ENUM_CAST(ResourceImporter::ImportOrder);
  116. #endif // RESOURCE_IMPORTER_H