Path: blob/master/editor/import/editor_import_plugin.cpp
9902 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;37if (GDVIRTUAL_CALL(_get_importer_name, ret)) {38return ret;39}40ERR_FAIL_V_MSG(String(), "Unimplemented _get_importer_name in add-on.");41}4243String EditorImportPlugin::get_visible_name() const {44String ret;45if (GDVIRTUAL_CALL(_get_visible_name, ret)) {46return ret;47}48ERR_FAIL_V_MSG(String(), "Unimplemented _get_visible_name in add-on.");49}5051void EditorImportPlugin::get_recognized_extensions(List<String> *p_extensions) const {52Vector<String> extensions;5354if (GDVIRTUAL_CALL(_get_recognized_extensions, extensions)) {55for (int i = 0; i < extensions.size(); i++) {56p_extensions->push_back(extensions[i]);57}58return;59}60ERR_FAIL_MSG("Unimplemented _get_recognized_extensions in add-on.");61}6263String EditorImportPlugin::get_preset_name(int p_idx) const {64String ret;65if (GDVIRTUAL_CALL(_get_preset_name, p_idx, ret)) {66return ret;67}68ERR_FAIL_V_MSG(itos(p_idx), "Unimplemented _get_preset_name in add-on.");69}7071int EditorImportPlugin::get_preset_count() const {72int ret;73if (GDVIRTUAL_CALL(_get_preset_count, ret)) {74return ret;75}76return 0;77}7879String EditorImportPlugin::get_save_extension() const {80String ret;81if (GDVIRTUAL_CALL(_get_save_extension, ret)) {82return ret;83}84ERR_FAIL_V_MSG(String(), "Unimplemented _get_save_extension in add-on.");85}8687String EditorImportPlugin::get_resource_type() const {88String ret;89if (GDVIRTUAL_CALL(_get_resource_type, ret)) {90return ret;91}92ERR_FAIL_V_MSG(String(), "Unimplemented _get_resource_type in add-on.");93}9495float EditorImportPlugin::get_priority() const {96float ret;97if (GDVIRTUAL_CALL(_get_priority, ret)) {98return ret;99}100return 1.0;101}102103int EditorImportPlugin::get_import_order() const {104int ret;105if (GDVIRTUAL_CALL(_get_import_order, ret)) {106return ret;107}108return IMPORT_ORDER_DEFAULT;109}110111int EditorImportPlugin::get_format_version() const {112int ret = 0;113if (GDVIRTUAL_CALL(_get_format_version, ret)) {114return ret;115}116return 0;117}118119void EditorImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options, int p_preset) const {120Array needed = { "name", "default_value" };121TypedArray<Dictionary> options;122if (GDVIRTUAL_CALL(_get_import_options, p_path, p_preset, options)) {123for (int i = 0; i < options.size(); i++) {124Dictionary d = options[i];125ERR_FAIL_COND(!d.has_all(needed));126String name = d["name"];127Variant default_value = d["default_value"];128129PropertyHint hint = PROPERTY_HINT_NONE;130if (d.has("property_hint")) {131hint = (PropertyHint)d["property_hint"].operator int64_t();132}133134String hint_string;135if (d.has("hint_string")) {136hint_string = d["hint_string"];137}138139uint32_t usage = PROPERTY_USAGE_DEFAULT;140if (d.has("usage")) {141usage = d["usage"];142}143144ImportOption option(PropertyInfo(default_value.get_type(), name, hint, hint_string, usage), default_value);145r_options->push_back(option);146}147return;148}149150ERR_FAIL_MSG("Unimplemented _get_import_options in add-on.");151}152153bool EditorImportPlugin::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {154Dictionary d;155HashMap<StringName, Variant>::ConstIterator E = p_options.begin();156while (E) {157d[E->key] = E->value;158++E;159}160bool visible = false;161if (GDVIRTUAL_CALL(_get_option_visibility, p_path, p_option, d, visible)) {162return visible;163}164return true;165}166167Error 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) {168Dictionary options;169TypedArray<String> platform_variants, gen_files;170171HashMap<StringName, Variant>::ConstIterator E = p_options.begin();172while (E) {173options[E->key] = E->value;174++E;175}176177Error err = OK;178if (GDVIRTUAL_CALL(_import, p_source_file, p_save_path, options, platform_variants, gen_files, err)) {179for (int i = 0; i < platform_variants.size(); i++) {180r_platform_variants->push_back(platform_variants[i]);181}182for (int i = 0; i < gen_files.size(); i++) {183r_gen_files->push_back(gen_files[i]);184}185return err;186}187ERR_FAIL_V_MSG(ERR_METHOD_NOT_FOUND, "Unimplemented _import in add-on.");188}189190bool EditorImportPlugin::can_import_threaded() const {191bool ret = false;192if (GDVIRTUAL_CALL(_can_import_threaded, ret)) {193return ret;194} else {195return ResourceImporter::can_import_threaded();196}197}198199Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {200HashMap<StringName, Variant> options;201for (const KeyValue<Variant, Variant> &kv : p_custom_options) {202options.insert(kv.key, kv.value);203}204return append_import_external_resource(p_file, options, p_custom_importer, p_generator_parameters);205}206207Error 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) {208ERR_FAIL_COND_V_MSG(!EditorFileSystem::get_singleton()->is_importing(), ERR_INVALID_PARAMETER, "Can only append files to import during a current reimport process.");209return EditorFileSystem::get_singleton()->reimport_append(p_file, p_custom_options, p_custom_importer, p_generator_parameters);210}211212void EditorImportPlugin::_bind_methods() {213GDVIRTUAL_BIND(_get_importer_name)214GDVIRTUAL_BIND(_get_visible_name)215GDVIRTUAL_BIND(_get_preset_count)216GDVIRTUAL_BIND(_get_preset_name, "preset_index")217GDVIRTUAL_BIND(_get_recognized_extensions)218GDVIRTUAL_BIND(_get_import_options, "path", "preset_index")219GDVIRTUAL_BIND(_get_save_extension)220GDVIRTUAL_BIND(_get_resource_type)221GDVIRTUAL_BIND(_get_priority)222GDVIRTUAL_BIND(_get_import_order)223GDVIRTUAL_BIND(_get_format_version)224GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options")225GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files");226GDVIRTUAL_BIND(_can_import_threaded);227ClassDB::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()));228}229230231