From 24e1ba129837ef97ac706012e563e275c7125f5a Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Mon, 22 Mar 2021 16:41:47 -0300 Subject: [PATCH] Add a "keep" import mode to keep files as-is and export them. (cherry picked from commit 8d64f3bd76b251ee33a8b2b4b3b53e7e3c44eafb) --- core/io/config_file.cpp | 5 ++ core/io/config_file.h | 2 + editor/editor_export.cpp | 14 ++++ editor/editor_file_system.cpp | 25 +++++++ editor/import_dock.cpp | 120 ++++++++++++++++++++++------------ 5 files changed, 123 insertions(+), 43 deletions(-) diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index f4ec08d3d76..da6c0c8bfcb 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -307,6 +307,9 @@ Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) return OK; } +void ConfigFile::clear() { + values.clear(); +} void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value); @@ -330,4 +333,6 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted); ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass); + + ClassDB::bind_method(D_METHOD("clear"), &ConfigFile::clear); } diff --git a/core/io/config_file.h b/core/io/config_file.h index 3cc0ffd2b9e..7fb615a9a73 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -69,6 +69,8 @@ public: Error load(const String &p_path); Error parse(const String &p_data); + void clear(); + Error load_encrypted(const String &p_path, const Vector &p_key); Error load_encrypted_pass(const String &p_path, const String &p_pass); diff --git a/editor/editor_export.cpp b/editor/editor_export.cpp index be633849291..d437d79cd0b 100644 --- a/editor/editor_export.cpp +++ b/editor/editor_export.cpp @@ -790,6 +790,20 @@ Error EditorExportPlatform::export_project_files(const Ref & continue; } + String importer_type = config->get_value("remap", "importer"); + + if (importer_type == "keep") { + //just keep file as-is + Vector array = FileAccess::get_file_as_array(path); + err = p_func(p_udata, path, array, idx, total); + + if (err != OK) { + return err; + } + + continue; + } + List remaps; config->get_section_keys("remap", &remaps); diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 5b1729ee172..fffba63e99d 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -372,6 +372,7 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo List to_check; + String importer_name; String source_file = ""; String source_md5 = ""; Vector dest_files; @@ -400,6 +401,8 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo for (int i = 0; i < fa.size(); i++) { to_check.push_back(fa[i]); } + } else if (assign == "importer") { + importer_name = value; } else if (!p_only_imported_files) { if (assign == "source_file") { source_file = value; @@ -415,6 +418,10 @@ bool EditorFileSystem::_test_for_reimport(const String &p_path, bool p_only_impo memdelete(f); + if (importer_name == "keep") { + return false; //keep mode, do not reimport + } + // Read the md5's from a separate file (so the import parameters aren't dependent on the file version String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path); FileAccess *md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err); @@ -1570,6 +1577,10 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector source_file_options[p_files[i]] = Map(); importer_name = file_importer_name; + if (importer_name == "keep") { + continue; //do nothing + } + Ref importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_CORRUPT); List options; @@ -1594,6 +1605,10 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]); } + if (importer_name == "keep") { + return OK; // (do nothing) + } + ERR_FAIL_COND_V(importer_name == String(), ERR_UNCONFIGURED); Ref importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); @@ -1741,6 +1756,16 @@ void EditorFileSystem::_reimport_file(const String &p_file) { late_added_files.insert(p_file); //imported files do not call update_file(), but just in case.. } + if (importer_name == "keep") { + //keep files, do nothing. + fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file); + fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import"); + fs->files[cpos]->deps.clear(); + fs->files[cpos]->type = ""; + fs->files[cpos]->import_valid = false; + EditorResourcePreview::get_singleton()->check_for_invalidation(p_file); + return; + } Ref importer; bool load_default = false; //find the importer diff --git a/editor/import_dock.cpp b/editor/import_dock.cpp index e1c7dd6a426..e63d4a5dad6 100644 --- a/editor/import_dock.cpp +++ b/editor/import_dock.cpp @@ -101,11 +101,9 @@ void ImportDock::set_edit_path(const String &p_path) { return; } - params->importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(config->get_value("remap", "importer")); - if (params->importer.is_null()) { - clear(); - return; - } + String importer_name = config->get_value("remap", "importer"); + + params->importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); params->paths.clear(); params->paths.push_back(p_path); @@ -127,11 +125,18 @@ void ImportDock::set_edit_path(const String &p_path) { for (List >::Element *E = importer_names.front(); E; E = E->next()) { import_as->add_item(E->get().first); import_as->set_item_metadata(import_as->get_item_count() - 1, E->get().second); - if (E->get().second == params->importer->get_importer_name()) { + if (E->get().second == importer_name) { import_as->select(import_as->get_item_count() - 1); } } + import_as->add_separator(); + import_as->add_item(TTR("Keep File (No Import)")); + import_as->set_item_metadata(import_as->get_item_count() - 1, "keep"); + if (importer_name == "keep") { + import_as->select(import_as->get_item_count() - 1); + } + import->set_disabled(false); import_as->set_disabled(false); preset->set_disabled(false); @@ -142,7 +147,10 @@ void ImportDock::set_edit_path(const String &p_path) { void ImportDock::_update_options(const Ref &p_config) { List options; - params->importer->get_import_options(&options); + + if (params->importer.is_valid()) { + params->importer->get_import_options(&options); + } params->properties.clear(); params->values.clear(); @@ -273,6 +281,13 @@ void ImportDock::set_edit_multiple_paths(const Vector &p_paths) { void ImportDock::_update_preset_menu() { preset->get_popup()->clear(); + if (params->importer.is_null()) { + preset->get_popup()->add_item(TTR("Default")); + preset->hide(); + return; + } + preset->show(); + if (params->importer->get_preset_count() == 0) { preset->get_popup()->add_item(TTR("Default")); } else { @@ -292,20 +307,25 @@ void ImportDock::_update_preset_menu() { void ImportDock::_importer_selected(int i_idx) { String name = import_as->get_selected_metadata(); - Ref importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(name); - ERR_FAIL_COND(importer.is_null()); + if (name == "keep") { + params->importer.unref(); + _update_options(Ref()); + } else { + Ref importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(name); + ERR_FAIL_COND(importer.is_null()); - params->importer = importer; + params->importer = importer; - Ref config; - if (params->paths.size()) { - config.instance(); - Error err = config->load(params->paths[0] + ".import"); - if (err != OK) { - config.unref(); + Ref config; + if (params->paths.size()) { + config.instance(); + Error err = config->load(params->paths[0] + ".import"); + if (err != OK) { + config.unref(); + } } + _update_options(config); } - _update_options(config); } void ImportDock::_preset_selected(int p_idx) { @@ -407,6 +427,13 @@ void ImportDock::_reimport_attempt() { bool need_restart = false; bool used_in_resources = false; + + String importer_name; + if (params->importer.is_valid()) { + importer_name = params->importer->get_importer_name(); + } else { + importer_name = "keep"; + } for (int i = 0; i < params->paths.size(); i++) { Ref config; config.instance(); @@ -414,7 +441,7 @@ void ImportDock::_reimport_attempt() { ERR_CONTINUE(err != OK); String imported_with = config->get_value("remap", "importer"); - if (imported_with != params->importer->get_importer_name()) { + if (imported_with != importer_name) { need_restart = true; if (_find_owners(EditorFileSystem::get_singleton()->get_filesystem(), params->paths[i])) { used_in_resources = true; @@ -448,38 +475,45 @@ void ImportDock::_reimport() { Error err = config->load(params->paths[i] + ".import"); ERR_CONTINUE(err != OK); - String importer_name = params->importer->get_importer_name(); + if (params->importer.is_valid()) { + String importer_name = params->importer->get_importer_name(); - if (params->checking && config->get_value("remap", "importer") == params->importer->get_importer_name()) { - //update only what is edited (checkboxes) if the importer is the same - for (List::Element *E = params->properties.front(); E; E = E->next()) { - if (params->checked.has(E->get().name)) { + if (params->checking && config->get_value("remap", "importer") == params->importer->get_importer_name()) { + //update only what is edited (checkboxes) if the importer is the same + for (List::Element *E = params->properties.front(); E; E = E->next()) { + if (params->checked.has(E->get().name)) { + config->set_value("params", E->get().name, params->values[E->get().name]); + } + } + } else { + //override entirely + config->set_value("remap", "importer", importer_name); + if (config->has_section("params")) { + config->erase_section("params"); + } + + for (List::Element *E = params->properties.front(); E; E = E->next()) { config->set_value("params", E->get().name, params->values[E->get().name]); } } - } else { - //override entirely - config->set_value("remap", "importer", importer_name); - if (config->has_section("params")) { - config->erase_section("params"); + + //handle group file + Ref importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); + ERR_CONTINUE(!importer.is_valid()); + String group_file_property = importer->get_option_group_file(); + if (group_file_property != String()) { + //can import from a group (as in, atlas) + ERR_CONTINUE(!params->values.has(group_file_property)); + String group_file = params->values[group_file_property]; + config->set_value("remap", "group_file", group_file); + } else { + config->set_value("remap", "group_file", Variant()); //clear group file if unused } - for (List::Element *E = params->properties.front(); E; E = E->next()) { - config->set_value("params", E->get().name, params->values[E->get().name]); - } - } - - //handle group file - Ref importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name); - ERR_CONTINUE(!importer.is_valid()); - String group_file_property = importer->get_option_group_file(); - if (group_file_property != String()) { - //can import from a group (as in, atlas) - ERR_CONTINUE(!params->values.has(group_file_property)); - String group_file = params->values[group_file_property]; - config->set_value("remap", "group_file", group_file); } else { - config->set_value("remap", "group_file", Variant()); //clear group file if unused + //set to no import + config->clear(); + config->set_value("remap", "importer", "keep"); } config->save(params->paths[i] + ".import");