Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
#include "editor/settings/project_settings_editor.h"
#include "editor/shader/editor_native_shader_source_visualizer.h"
#include "editor/shader/visual_shader_editor_plugin.h"
#include "editor/svg_import_conversion_tool.h"
#include "editor/themes/editor_scale.h"
#include "editor/themes/editor_theme_manager.h"
#include "editor/translations/editor_translation_parser.h"
Expand Down Expand Up @@ -1076,6 +1077,10 @@ void EditorNode::_execute_upgrades() {
// Execute another scan to reimport the modified files.
project_upgrade_tool->connect(project_upgrade_tool->UPGRADE_FINISHED, callable_mp(EditorFileSystem::get_singleton(), &EditorFileSystem::scan), CONNECT_ONE_SHOT);
project_upgrade_tool->finish_upgrade();
} else if (run_svg_import_conversion_tool) {
run_svg_import_conversion_tool = false;
svg_import_conversion_tool->connect("conversion_finished", callable_mp(EditorFileSystem::get_singleton(), &EditorFileSystem::scan), CONNECT_ONE_SHOT);
svg_import_conversion_tool->finish_conversion();
}
}

Expand Down Expand Up @@ -3644,6 +3649,9 @@ void EditorNode::_tool_menu_option(int p_idx) {
case TOOLS_PROJECT_UPGRADE: {
project_upgrade_tool->popup_dialog();
} break;
case TOOLS_SVG_IMPORT_CONVERSION: {
svg_import_conversion_tool->popup_dialog();
} break;
case TOOLS_CUSTOM: {
if (tool_menu->get_item_submenu(p_idx) == "") {
Callable callback = tool_menu->get_item_metadata(p_idx);
Expand Down Expand Up @@ -7560,6 +7568,13 @@ EditorNode::EditorNode() {
project_upgrade_tool->begin_upgrade();
}

// Similar warm up for the svg import upgrade tool.
svg_import_conversion_tool = memnew(SvgImportConversionTool);
run_svg_import_conversion_tool = EditorSettings::get_singleton()->get_project_metadata(svg_import_conversion_tool->META_SVG_IMPORT_CONVERSION_TOOL, svg_import_conversion_tool->META_RUN_ON_RESTART, false);
if (run_svg_import_conversion_tool) {
svg_import_conversion_tool->begin_conversion();
}

{
bool agile_input_event_flushing = EDITOR_GET("input/buffering/agile_event_flushing");
bool use_accumulated_input = EDITOR_GET("input/buffering/use_accumulated_input");
Expand Down Expand Up @@ -8122,6 +8137,7 @@ EditorNode::EditorNode() {
tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/orphan_resource_explorer", TTRC("Orphan Resource Explorer...")), TOOLS_ORPHAN_RESOURCES);
tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/engine_compilation_configuration_editor", TTRC("Engine Compilation Configuration Editor...")), TOOLS_BUILD_PROFILE_MANAGER);
tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/upgrade_project", TTRC("Upgrade Project Files...")), TOOLS_PROJECT_UPGRADE);
tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/upgrade_svg_imports", TTRC("Convert SVG Import Files from Texture2D to SVGTexture...")), TOOLS_SVG_IMPORT_CONVERSION);

project_menu->add_separator();
project_menu->add_shortcut(ED_SHORTCUT("editor/reload_current_project", TTRC("Reload Current Project")), PROJECT_RELOAD_CURRENT_PROJECT);
Expand Down Expand Up @@ -8850,6 +8866,7 @@ EditorNode::~EditorNode() {
memdelete(editor_plugins_force_input_forwarding);
memdelete(progress_hb);
memdelete(project_upgrade_tool);
memdelete(svg_import_conversion_tool);
memdelete(editor_dock_manager);

EditorSettings::destroy();
Expand Down
4 changes: 4 additions & 0 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class ProjectExportDialog;
class ProjectSettingsEditor;
class SceneImportSettingsDialog;
class ProjectUpgradeTool;
class SvgImportConversionTool;

#ifdef ANDROID_ENABLED
class TouchActionsPanel;
Expand Down Expand Up @@ -173,6 +174,7 @@ class EditorNode : public Node {
TOOLS_ORPHAN_RESOURCES,
TOOLS_BUILD_PROFILE_MANAGER,
TOOLS_PROJECT_UPGRADE,
TOOLS_SVG_IMPORT_CONVERSION,
TOOLS_CUSTOM,

VCS_METADATA,
Expand Down Expand Up @@ -493,6 +495,8 @@ class EditorNode : public Node {

ProjectUpgradeTool *project_upgrade_tool = nullptr;
bool run_project_upgrade_tool = false;
SvgImportConversionTool *svg_import_conversion_tool = nullptr;
bool run_svg_import_conversion_tool = false;

bool was_window_windowed_last = false;

Expand Down
195 changes: 195 additions & 0 deletions editor/svg_import_conversion_tool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**************************************************************************/
/* svg_import_conversion_tool.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "svg_import_conversion_tool.h"

#include "core/io/config_file.h"
#include "core/io/dir_access.h"
#include "editor/editor_node.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/dialogs.h"

void SvgImportConversionTool::_find_svg_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_svg_paths) {
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
for (int i = 0; i < p_dir->get_file_count(); i++) {
const String path = p_dir->get_file_path(i);
const String ext = path.get_extension();
if (ext == "svg") {
if (da->file_exists(path + ".import")) {
if (_is_texture2d_import(path + ".import")) {
r_svg_paths.append(path);
}
}
}
}

for (int i = 0; i < p_dir->get_subdir_count(); i++) {
_find_svg_files(p_dir->get_subdir(i), r_svg_paths);
}
}

void SvgImportConversionTool::_bind_methods() {
ADD_SIGNAL(MethodInfo("conversion_finished"));
ClassDB::bind_method(D_METHOD("_on_dialog_confirmed"), &SvgImportConversionTool::_on_dialog_confirmed);
}

void SvgImportConversionTool::popup_dialog() {
if (!convert_dialog) {
convert_dialog = memnew(ConfirmationDialog);
convert_dialog->set_autowrap(true);
convert_dialog->set_text(TTRC("This tool will convert all SVG files that are currently imported as Texture2D to use the SVGTexture importer instead.\n\nThe following will be preserved:\n- SVG scale (renamed to base_scale)\n\nDo you want to continue?"));
convert_dialog->get_ok_button()->set_text(TTRC("Convert"));
convert_dialog->get_label()->set_custom_minimum_size(Size2(750 * EDSCALE, 0));
EditorNode::get_singleton()->get_gui_base()->add_child(convert_dialog);
convert_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SvgImportConversionTool::convert_svgs));
}
convert_dialog->popup_centered();
}

void SvgImportConversionTool::prepare_conversion() {
EditorSettings::get_singleton()->set_project_metadata(META_SVG_IMPORT_CONVERSION_TOOL, META_RUN_ON_RESTART, true);

Vector<String> reimport_svg_paths;
_find_svg_files(EditorFileSystem::get_singleton()->get_filesystem(), reimport_svg_paths);

EditorSettings::get_singleton()->set_project_metadata(META_SVG_IMPORT_CONVERSION_TOOL, META_REIMPORT_PATHS, reimport_svg_paths);

// Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.
callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor).call_deferred(false);
}

void SvgImportConversionTool::begin_conversion() {
EditorSettings::get_singleton()->set_project_metadata(META_SVG_IMPORT_CONVERSION_TOOL, META_RUN_ON_RESTART, false);
}

bool SvgImportConversionTool::_is_texture2d_import(const String &p_import_path) {
Ref<ConfigFile> cf;
cf.instantiate();
Error err = cf->load(p_import_path);
if (err != OK) {
return false;
}

String importer = cf->get_value("remap", "importer", "");
return importer == "texture";
}

void SvgImportConversionTool::_convert_svg_file(const String &p_path) {
String import_path = p_path + ".import";
float svg_scale = 1.0;
ResourceUID::ID uid = ResourceUID::INVALID_ID;

// Read old import settings
Ref<ConfigFile> cf;
cf.instantiate();
if (cf->load(import_path) == OK) {
if (cf->has_section_key("remap", "uid")) {
String uid_text = cf->get_value("remap", "uid", "");
uid = ResourceUID::get_singleton()->text_to_id(uid_text);
}
if (cf->has_section_key("params", "svg/scale")) {
svg_scale = cf->get_value("params", "svg/scale", 1.0);
}
}

String base_path = p_path.get_basename();
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (da.is_valid()) {
da->remove(base_path + ".ctex");

const char *variants[] = { "s3tc", "etc2", "bptc", "astc", nullptr };
int i = 0;
while (variants[i]) {
da->remove(base_path + "." + variants[i] + ".ctex");
i++;
}

da->remove(base_path + ".editor.ctex");
da->remove(base_path + ".editor.meta");
}

cf->clear();

cf->set_value("remap", "importer", "svg");
cf->set_value("remap", "type", "SVGTexture");
if (uid != ResourceUID::INVALID_ID) {
cf->set_value("remap", "uid", ResourceUID::get_singleton()->id_to_text(uid));
}

// The path will be set by the importer during reimport
cf->set_value("remap", "path", "");

cf->set_value("params", "base_scale", svg_scale);

cf->save(import_path);
}

void SvgImportConversionTool::_on_dialog_confirmed() {
convert_svgs();
}

void SvgImportConversionTool::convert_svgs() {
if (!EditorFileSystem::get_singleton() || !EditorFileSystem::get_singleton()->get_filesystem()) {
if (EditorNode::get_singleton()) {
EditorNode::get_singleton()->show_warning("Editor file system is not ready. Please try again.");
}
return;
}

Vector<String> svg_paths;
_find_svg_files(EditorFileSystem::get_singleton()->get_filesystem(), svg_paths);

if (svg_paths.is_empty()) {
if (EditorNode::get_singleton()) {
EditorNode::get_singleton()->show_warning("No SVG files found that are imported as Texture2D.");
}
return;
}

print_line(vformat("Found %d SVG files to convert.", svg_paths.size()));

for (int i = 0; i < svg_paths.size(); i++) {
print_line(vformat("Converting: %s", svg_paths[i]));
_convert_svg_file(svg_paths[i]);
}

print_line(vformat("Converted %d SVG files to use SVGTexture importer.", svg_paths.size()));
print_line(vformat("Recommend reloading the project to prevent (non-harmful) cache errors."));

if (!svg_paths.is_empty()) {
EditorFileSystem::get_singleton()->reimport_files(svg_paths);
}
}

void SvgImportConversionTool::finish_conversion() {
emit_signal(CONVERSION_FINISHED);
}
64 changes: 64 additions & 0 deletions editor/svg_import_conversion_tool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**************************************************************************/
/* svg_import_conversion_tool.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "core/object/class_db.h"

class ConfirmationDialog;
class EditorFileSystemDirectory;

class SvgImportConversionTool : public Object {
GDCLASS(SvgImportConversionTool, Object);

ConfirmationDialog *convert_dialog = nullptr;

void _find_svg_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_svg_paths);
bool _is_texture2d_import(const String &p_import_path);
void _convert_svg_file(const String &p_path);
void _on_dialog_confirmed();

const String META_REIMPORT_PATHS = "reimport_paths";

public:
const String META_SVG_IMPORT_CONVERSION_TOOL = "svg_import_conversion_tool";
const String META_RUN_ON_RESTART = "run_on_restart";
const StringName CONVERSION_FINISHED = "conversion_finished";

protected:
static void _bind_methods();

public:
void popup_dialog();
void prepare_conversion();
void begin_conversion();
void finish_conversion();
void convert_svgs();
};