Debian Codegraph @

editor/import/editor_import_plugin.cpp

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