Path: blob/master/editor/import/editor_import_plugin.cpp
20909 views
/**************************************************************************/1/* editor_import_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_import_plugin.h"3132#include "core/object/script_language.h"33#include "editor/file_system/editor_file_system.h"3435String EditorImportPlugin::get_importer_name() const {36String ret;37GDVIRTUAL_CALL(_get_importer_name, ret);38return ret;39}4041String EditorImportPlugin::get_visible_name() const {42String ret;43GDVIRTUAL_CALL(_get_visible_name, ret);44return ret;45}4647void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {48Vector<String> extensions;49GDVIRTUAL_CALL(_get_recognized_extensions, extensions);50for (int i = 0; i < extensions.size(); i++) {51p_extensions->push_back(extensions[i]);52}53}5455String EditorImportPlugin::get_preset_name(int p_idx) const {56String ret;57GDVIRTUAL_CALL(_get_preset_name, p_idx, ret);58return ret;59}6061int EditorImportPlugin::get_preset_count() const {62int ret;63if (GDVIRTUAL_CALL(_get_preset_count, ret)) {64return ret;65}66return 0;67}6869String EditorImportPlugin::get_save_extension() const {70String ret;71GDVIRTUAL_CALL(_get_save_extension, ret);72return ret;73}7475String EditorImportPlugin::get_resource_type() const {76String ret;77GDVIRTUAL_CALL(_get_resource_type, ret);78return ret;79}8081float EditorImportPlugin::get_priority() const {82float ret;83if (GDVIRTUAL_CALL(_get_priority, ret)) {84return ret;85}86return 1.0;87}8889int EditorImportPlugin::get_import_order() const {90int ret;91if (GDVIRTUAL_CALL(_get_import_order, ret)) {92return ret;93}94return IMPORT_ORDER_DEFAULT;95}9697int EditorImportPlugin::get_format_version() const {98int ret = 0;99if (GDVIRTUAL_CALL(_get_format_version, ret)) {100return ret;101}102return 0;103}104105void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {106Array needed = { "name", "default_value" };107TypedArray<Dictionary> options;108GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options);109for (int i = 0; i < options.size(); i++) {110Dictionary d = options[i];111ERR_FAIL_COND(!d.has_all(needed));112String name = d["name"];113Variant default_value = d["default_value"];114115PropertyHint hint = PROPERTY_HINT_NONE;116if (d.has("property_hint")) {117hint = (PropertyHint)d["property_hint"].operator int64_t();118}119120String hint_string;121if (d.has("hint_string")) {122hint_string = d["hint_string"];123}124125uint32_t usage = PROPERTY_USAGE_DEFAULT;126if (d.has("usage")) {127usage = d["usage"];128}129130ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);131r_options->push_back(option);132}133}134135bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {136Dictionary d;137HashMap<StringName, Variant>::ConstIterator E = p_options.begin();138while (E) {139d[E->key] = E->value;140++E;141}142bool visible = false;143if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {144return visible;145}146return true;147}148149Error 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) {150Dictionary options;151TypedArray<String> platform_variants, gen_files;152153HashMap<StringName, Variant>::ConstIterator E = p_options.begin();154while (E) {155options[E->key] = E->value;156++E;157}158159Error err = OK;160GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err);161for (int i = 0; i < platform_variants.size(); i++) {162r_platform_variants->push_back(platform_variants[i]);163}164for (int i = 0; i < gen_files.size(); i++) {165r_gen_files->push_back(gen_files[i]);166}167return err;168}169170bool EditorImportPlugin::can_import_threaded() const {171bool ret = false;172if (GDVIRTUAL_CALL(_can_import_threaded, ret)) {173return ret;174} else {175return ResourceImporter::can_import_threaded();176}177}178179Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {180HashMap<StringName, Variant> options;181for (const KeyValue<Variant, Variant> &kv : p_custom_options) {182options.insert(kv.key, kv.value);183}184return append_import_external_resource(p_file, options, p_custom_importer, p_generator_parameters);185}186187Error 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) {188ERR_FAIL_COND_V_MSG(!EditorFileSystem::get_singleton()->is_importing(), ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");189return EditorFileSystem::get_singleton()->reimport_append(p_file, p_custom_options, p_custom_importer, p_generator_parameters);190}191192void EditorImportPlugin::_bind_methods() {193GDVIRTUAL_BIND(_get_importer_name)194GDVIRTUAL_BIND(_get_visible_name)195GDVIRTUAL_BIND(_get_preset_count)196GDVIRTUAL_BIND(_get_preset_name, "preset_index")197GDVIRTUAL_BIND(_get_recognized_extensions)198GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")199GDVIRTUAL_BIND(_get_save_extension)200GDVIRTUAL_BIND(_get_resource_type)201GDVIRTUAL_BIND(_get_priority)202GDVIRTUAL_BIND(_get_import_order)203GDVIRTUAL_BIND(_get_format_version)204GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")205GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");206GDVIRTUAL_BIND(_can_import_threaded);207ClassDB::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()));208}209210211