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