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