Path: blob/master/editor/project_upgrade/project_upgrade_tool.cpp
9898 views
/**************************************************************************/1/* project_upgrade_tool.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 "project_upgrade_tool.h"3132#include "core/io/dir_access.h"33#include "editor/editor_node.h"34#include "editor/file_system/editor_file_system.h"35#include "editor/scene/editor_scene_tabs.h"36#include "editor/settings/editor_settings.h"37#include "editor/themes/editor_scale.h"38#include "scene/gui/dialogs.h"3940void ProjectUpgradeTool::_add_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_reimport_paths, Vector<String> &r_resave_scenes, Vector<String> &r_resave_resources) {41Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);42for (int i = 0; i < p_dir->get_file_count(); i++) {43const String path = p_dir->get_file_path(i);44const String ext = path.get_extension();45if (ext == "tscn" || ext == "scn") {46r_resave_scenes.append(path);47} else if (ext == "tres" || ext == "res") {48r_resave_resources.append(path);49} else if (da->file_exists(path + ".import")) {50r_reimport_paths.append(path);51}52}5354for (int i = 0; i < p_dir->get_subdir_count(); i++) {55_add_files(p_dir->get_subdir(i), r_reimport_paths, r_resave_scenes, r_resave_resources);56}57}5859void ProjectUpgradeTool::_bind_methods() {60ADD_SIGNAL(MethodInfo("upgrade_finished"));61}6263void ProjectUpgradeTool::popup_dialog() {64if (!upgrade_dialog) {65upgrade_dialog = memnew(ConfirmationDialog);66upgrade_dialog->set_autowrap(true);67upgrade_dialog->set_text(TTRC("Different engine version may have minor differences in various Resources, like additional fields or removed properties. When upgrading the project to a new version, such changes can cause diffs when saving scenes or resources, or reimporting.\n\nThis tool ensures that such changes are performed all at once. It will:\n- Regenerate UID cache\n- Load and re-save every text/binary Resource\n- Reimport every importable Resource\n\nFull upgrade will take considerable amount of time, but afterwards saving/reimporting any scene/resource should not cause unintended changes."));68upgrade_dialog->get_ok_button()->set_text(TTRC("Restart & Upgrade"));69upgrade_dialog->get_label()->set_custom_minimum_size(Size2(750 * EDSCALE, 0));70EditorNode::get_singleton()->get_gui_base()->add_child(upgrade_dialog);71upgrade_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ProjectUpgradeTool::prepare_upgrade));72}73upgrade_dialog->popup_centered();74}7576void ProjectUpgradeTool::prepare_upgrade() {77EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, true);7879Vector<String> reimport_paths;80Vector<String> resave_scenes;81Vector<String> resave_resources;82_add_files(EditorFileSystem::get_singleton()->get_filesystem(), reimport_paths, resave_scenes, resave_resources);8384EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, reimport_paths);85EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, resave_scenes);86EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, resave_resources);8788// Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.89callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor).call_deferred(false);90}9192void ProjectUpgradeTool::begin_upgrade() {93EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, false);94DirAccess::remove_absolute("res://.godot/uid_cache.bin");95}9697void ProjectUpgradeTool::finish_upgrade() {98EditorNode::get_singleton()->trigger_menu_option(EditorSceneTabs::SCENE_CLOSE_ALL, true);99100Vector<String> paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Vector<String>());101EditorFileSystem::get_singleton()->reimport_files(paths);102EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Variant());103104{105paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Vector<String>());106EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Scenes"), paths.size());107108int step = 0;109for (const String &file_path : paths) {110ep.step(TTR("Re-saving scene:") + " " + file_path, step++);111EditorNode::get_singleton()->load_scene(file_path);112EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_SAVE_SCENE, true);113EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_CLOSE, true);114}115EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Variant());116}117118{119paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Vector<String>());120EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Resources"), paths.size());121122int step = 0;123for (const String &file_path : paths) {124ep.step(TTR("Re-saving resource:") + " " + file_path, step++);125Ref<Resource> res = ResourceLoader::load(file_path, "", ResourceFormatLoader::CACHE_MODE_REPLACE);126if (res.is_valid()) {127ResourceSaver::save(res);128}129}130EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Variant());131}132133emit_signal(UPGRADE_FINISHED);134}135136137