| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/resource_loader.h" |
| 34 |
#include "core/io/resource_saver.h" |
| 35 |
|
| 36 |
class ResourceImporter; |
| 37 |
class ResourceFormatImporter; |
| 38 |
|
| 39 |
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); |
| 40 |
|
| 41 |
class ResourceFormatImporter : public ResourceFormatLoader { |
| 42 |
GDSOFTCLASS(ResourceFormatImporter, ResourceFormatLoader); |
| 43 |
|
| 44 |
struct PathAndType { |
| 45 |
String path; |
| 46 |
String type; |
| 47 |
String importer; |
| 48 |
String group_file; |
| 49 |
Variant metadata; |
| 50 |
ResourceUID::ID uid = ResourceUID::INVALID_ID; |
| 51 |
}; |
| 52 |
|
| 53 |
Error _get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool p_load, bool *r_valid = nullptr) const; |
| 54 |
|
| 55 |
static inline ResourceFormatImporter *singleton = nullptr; |
| 56 |
|
| 57 |
|
| 58 |
struct SortImporterByName { |
| 59 |
bool operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const; |
| 60 |
}; |
| 61 |
|
| 62 |
Vector<Ref<ResourceImporter>> importers; |
| 63 |
|
| 64 |
public: |
| 65 |
static ResourceFormatImporter *get_singleton() { return singleton; } |
| 66 |
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; |
| 67 |
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); |
| 68 |
virtual void get_recognized_extensions(List<String> *p_extensions) const override; |
| 69 |
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override; |
| 70 |
virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const override; |
| 71 |
virtual bool handles_type(const String &p_type) const override; |
| 72 |
virtual String get_resource_type(const String &p_path) const override; |
| 73 |
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override; |
| 74 |
virtual bool has_custom_uid_support() const override; |
| 75 |
virtual Variant get_resource_metadata(const String &p_path) const; |
| 76 |
virtual bool is_import_valid(const String &p_path) const override; |
| 77 |
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override; |
| 78 |
virtual bool is_imported(const String &p_path) const override { return recognize_path(p_path); } |
| 79 |
virtual String get_import_group_file(const String &p_path) const override; |
| 80 |
virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override; |
| 81 |
virtual bool exists(const String &p_path) const override; |
| 82 |
|
| 83 |
void get_build_dependencies(const String &p_path, HashSet<String> *r_dependencies); |
| 84 |
|
| 85 |
virtual int get_import_order(const String &p_path) const override; |
| 86 |
|
| 87 |
Error get_import_order_threads_and_importer(const String &p_path, int &r_order, bool &r_can_threads, String &r_importer) const; |
| 88 |
|
| 89 |
String get_internal_resource_path(const String &p_path) const; |
| 90 |
void get_internal_resource_path_list(const String &p_path, List<String> *r_paths); |
| 91 |
|
| 92 |
void add_importer(const Ref<ResourceImporter> &p_importer, bool p_first_priority = false); |
| 93 |
|
| 94 |
void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); } |
| 95 |
Ref<ResourceImporter> get_importer_by_name(const String &p_name) const; |
| 96 |
Ref<ResourceImporter> get_importer_by_file(const String &p_file) const; |
| 97 |
void get_importers_for_file(const String &p_file, List<Ref<ResourceImporter>> *r_importers); |
| 98 |
void get_importers(List<Ref<ResourceImporter>> *r_importers); |
| 99 |
|
| 100 |
bool are_import_settings_valid(const String &p_path) const; |
| 101 |
String get_import_settings_hash() const; |
| 102 |
|
| 103 |
String get_import_base_path(const String &p_for_file) const; |
| 104 |
Error get_resource_import_info(const String &p_path, StringName &r_type, ResourceUID::ID &r_uid, String &r_import_group_file) const; |
| 105 |
|
| 106 |
ResourceFormatImporter(); |
| 107 |
}; |
| 108 |
|
| 109 |
class ResourceImporter : public RefCounted { |
| 110 |
GDCLASS(ResourceImporter, RefCounted); |
| 111 |
|
| 112 |
protected: |
| 113 |
GDVIRTUAL1RC(Vector<String>, _get_build_dependencies, String) |
| 114 |
|
| 115 |
static void _bind_methods(); |
| 116 |
|
| 117 |
public: |
| 118 |
static inline ResourceFormatImporterLoadOnStartup load_on_startup = nullptr; |
| 119 |
|
| 120 |
virtual String get_importer_name() const = 0; |
| 121 |
virtual String get_visible_name() const = 0; |
| 122 |
virtual void get_recognized_extensions(List<String> *p_extensions) const = 0; |
| 123 |
virtual String get_save_extension() const = 0; |
| 124 |
virtual String get_resource_type() const = 0; |
| 125 |
virtual float get_priority() const { return 1.0; } |
| 126 |
virtual int get_import_order() const { return IMPORT_ORDER_DEFAULT; } |
| 127 |
virtual int get_format_version() const { return 0; } |
| 128 |
|
| 129 |
struct ImportOption { |
| 130 |
PropertyInfo option; |
| 131 |
Variant default_value; |
| 132 |
|
| 133 |
ImportOption(const PropertyInfo &p_info, const Variant &p_default) : |
| 134 |
option(p_info), |
| 135 |
default_value(p_default) { |
| 136 |
} |
| 137 |
ImportOption() {} |
| 138 |
}; |
| 139 |
|
| 140 |
enum ImportOrder { |
| 141 |
IMPORT_ORDER_DEFAULT = 0, |
| 142 |
IMPORT_ORDER_SCENE = 100, |
| 143 |
}; |
| 144 |
|
| 145 |
virtual bool has_advanced_options() const { return false; } |
| 146 |
virtual void show_advanced_options(const String &p_path) {} |
| 147 |
|
| 148 |
virtual int get_preset_count() const { return 0; } |
| 149 |
virtual String get_preset_name(int p_idx) const { return String(); } |
| 150 |
|
| 151 |
virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const = 0; |
| 152 |
virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const = 0; |
| 153 |
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {} |
| 154 |
virtual String get_option_group_file() const { return String(); } |
| 155 |
|
| 156 |
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; |
| 157 |
virtual bool can_import_threaded() const { return false; } |
| 158 |
virtual void import_threaded_begin() {} |
| 159 |
virtual void import_threaded_end() {} |
| 160 |
|
| 161 |
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; } |
| 162 |
virtual bool are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const { return true; } |
| 163 |
virtual String get_import_settings_string() const { return String(); } |
| 164 |
|
| 165 |
virtual void get_build_dependencies(const String &p_path, HashSet<String> *r_build_dependencies); |
| 166 |
}; |
| 167 |
|
| 168 |
VARIANT_ENUM_CAST(ResourceImporter::ImportOrder); |
| 169 |
|
| 170 |
class ResourceFormatImporterSaver : public ResourceFormatSaver { |
| 171 |
GDCLASS(ResourceFormatImporterSaver, ResourceFormatSaver) |
| 172 |
|
| 173 |
public: |
| 174 |
virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override; |
| 175 |
}; |