Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/import/editor_import_plugin.cpp
9902 views
1
/**************************************************************************/
2
/* editor_import_plugin.cpp */
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
#include "editor_import_plugin.h"
32
33
#include "core/object/script_language.h"
34
#include "editor/file_system/editor_file_system.h"
35
36
String EditorImportPlugin::get_importer_name() const {
37
String ret;
38
if (GDVIRTUAL_CALL(_get_importer_name, ret)) {
39
return ret;
40
}
41
ERR_FAIL_V_MSG(String(), "Unimplemented _get_importer_name in add-on.");
42
}
43
44
String EditorImportPlugin::get_visible_name() const {
45
String ret;
46
if (GDVIRTUAL_CALL(_get_visible_name, ret)) {
47
return ret;
48
}
49
ERR_FAIL_V_MSG(String(), "Unimplemented _get_visible_name in add-on.");
50
}
51
52
void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {
53
Vector<String> extensions;
54
55
if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {
56
for (int i = 0; i < extensions.size(); i++) {
57
p_extensions->push_back(extensions[i]);
58
}
59
return;
60
}
61
ERR_FAIL_MSG("Unimplemented _get_recognized_extensions in add-on.");
62
}
63
64
String EditorImportPlugin::get_preset_name(int p_idx) const {
65
String ret;
66
if (GDVIRTUAL_CALL(_get_preset_name, p_idx, ret)) {
67
return ret;
68
}
69
ERR_FAIL_V_MSG(itos(p_idx), "Unimplemented _get_preset_name in add-on.");
70
}
71
72
int EditorImportPlugin::get_preset_count() const {
73
int ret;
74
if (GDVIRTUAL_CALL(_get_preset_count, ret)) {
75
return ret;
76
}
77
return 0;
78
}
79
80
String EditorImportPlugin::get_save_extension() const {
81
String ret;
82
if (GDVIRTUAL_CALL(_get_save_extension, ret)) {
83
return ret;
84
}
85
ERR_FAIL_V_MSG(String(), "Unimplemented _get_save_extension in add-on.");
86
}
87
88
String EditorImportPlugin::get_resource_type() const {
89
String ret;
90
if (GDVIRTUAL_CALL(_get_resource_type, ret)) {
91
return ret;
92
}
93
ERR_FAIL_V_MSG(String(), "Unimplemented _get_resource_type in add-on.");
94
}
95
96
float EditorImportPlugin::get_priority() const {
97
float ret;
98
if (GDVIRTUAL_CALL(_get_priority, ret)) {
99
return ret;
100
}
101
return 1.0;
102
}
103
104
int EditorImportPlugin::get_import_order() const {
105
int ret;
106
if (GDVIRTUAL_CALL(_get_import_order, ret)) {
107
return ret;
108
}
109
return IMPORT_ORDER_DEFAULT;
110
}
111
112
int EditorImportPlugin::get_format_version() const {
113
int ret = 0;
114
if (GDVIRTUAL_CALL(_get_format_version, ret)) {
115
return ret;
116
}
117
return 0;
118
}
119
120
void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {
121
Array needed = { "name", "default_value" };
122
TypedArray<Dictionary> options;
123
if (GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options)) {
124
for (int i = 0; i < options.size(); i++) {
125
Dictionary d = options[i];
126
ERR_FAIL_COND(!d.has_all(needed));
127
String name = d["name"];
128
Variant default_value = d["default_value"];
129
130
PropertyHint hint = PROPERTY_HINT_NONE;
131
if (d.has("property_hint")) {
132
hint = (PropertyHint)d["property_hint"].operator int64_t();
133
}
134
135
String hint_string;
136
if (d.has("hint_string")) {
137
hint_string = d["hint_string"];
138
}
139
140
uint32_t usage = PROPERTY_USAGE_DEFAULT;
141
if (d.has("usage")) {
142
usage = d["usage"];
143
}
144
145
ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);
146
r_options->push_back(option);
147
}
148
return;
149
}
150
151
ERR_FAIL_MSG("Unimplemented _get_import_options in add-on.");
152
}
153
154
bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
155
Dictionary d;
156
HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
157
while (E) {
158
d[E->key] = E->value;
159
++E;
160
}
161
bool visible = false;
162
if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {
163
return visible;
164
}
165
return true;
166
}
167
168
Error EditorImportPlugin::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, Variant *r_metadata) {
169
Dictionary options;
170
TypedArray<String> platform_variants, gen_files;
171
172
HashMap<StringName, Variant>::ConstIterator E = p_options.begin();
173
while (E) {
174
options[E->key] = E->value;
175
++E;
176
}
177
178
Error err = OK;
179
if (GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err)) {
180
for (int i = 0; i < platform_variants.size(); i++) {
181
r_platform_variants->push_back(platform_variants[i]);
182
}
183
for (int i = 0; i < gen_files.size(); i++) {
184
r_gen_files->push_back(gen_files[i]);
185
}
186
return err;
187
}
188
ERR_FAIL_V_MSG(ERR_METHOD_NOT_FOUND, "Unimplemented _import in add-on.");
189
}
190
191
bool EditorImportPlugin::can_import_threaded() const {
192
bool ret = false;
193
if (GDVIRTUAL_CALL(_can_import_threaded, ret)) {
194
return ret;
195
} else {
196
return ResourceImporter::can_import_threaded();
197
}
198
}
199
200
Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
201
HashMap<StringName, Variant> options;
202
for (const KeyValue<Variant, Variant> &kv : p_custom_options) {
203
options.insert(kv.key, kv.value);
204
}
205
return append_import_external_resource(p_file, options, p_custom_importer, p_generator_parameters);
206
}
207
208
Error EditorImportPlugin::append_import_external_resource(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
209
ERR_FAIL_COND_V_MSG(!EditorFileSystem::get_singleton()->is_importing(), ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");
210
return EditorFileSystem::get_singleton()->reimport_append(p_file, p_custom_options, p_custom_importer, p_generator_parameters);
211
}
212
213
void EditorImportPlugin::_bind_methods() {
214
GDVIRTUAL_BIND(_get_importer_name)
215
GDVIRTUAL_BIND(_get_visible_name)
216
GDVIRTUAL_BIND(_get_preset_count)
217
GDVIRTUAL_BIND(_get_preset_name, "preset_index")
218
GDVIRTUAL_BIND(_get_recognized_extensions)
219
GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")
220
GDVIRTUAL_BIND(_get_save_extension)
221
GDVIRTUAL_BIND(_get_resource_type)
222
GDVIRTUAL_BIND(_get_priority)
223
GDVIRTUAL_BIND(_get_import_order)
224
GDVIRTUAL_BIND(_get_format_version)
225
GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")
226
GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");
227
GDVIRTUAL_BIND(_can_import_threaded);
228
ClassDB::bind_method(D_METHOD("append_import_external_resource", "path", "custom_options", "custom_importer", "generator_parameters"), &EditorImportPlugin::_append_import_external_resource, DEFVAL(Dictionary()), DEFVAL(String()), DEFVAL(Variant()));
229
}
230
231