Path: blob/master/editor/project_upgrade/project_upgrade_tool.cpp
20937 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/config/project_settings.h"33#include "core/io/dir_access.h"34#include "editor/editor_node.h"35#include "editor/file_system/editor_file_system.h"36#include "editor/scene/editor_scene_tabs.h"37#include "editor/settings/editor_settings.h"38#include "editor/themes/editor_scale.h"39#include "scene/3d/mesh_instance_3d.h"40#include "scene/gui/dialogs.h"4142void ProjectUpgradeTool::_add_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_reimport_paths, Vector<String> &r_resave_scenes, Vector<String> &r_resave_resources) {43Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);44for (int i = 0; i < p_dir->get_file_count(); i++) {45const String path = p_dir->get_file_path(i);46const String ext = path.get_extension();47if (ext == "tscn" || ext == "scn") {48r_resave_scenes.append(path);49} else if (ext == "tres" || ext == "res") {50r_resave_resources.append(path);51} else if (da->file_exists(path + ".import")) {52r_reimport_paths.append(path);53}54}5556for (int i = 0; i < p_dir->get_subdir_count(); i++) {57_add_files(p_dir->get_subdir(i), r_reimport_paths, r_resave_scenes, r_resave_resources);58}59}6061void ProjectUpgradeTool::_bind_methods() {62ADD_SIGNAL(MethodInfo("upgrade_finished"));63}6465void ProjectUpgradeTool::popup_dialog() {66if (!upgrade_dialog) {67upgrade_dialog = memnew(ConfirmationDialog);68upgrade_dialog->set_autowrap(true);69upgrade_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."));70upgrade_dialog->get_ok_button()->set_text(TTRC("Restart & Upgrade"));71upgrade_dialog->get_label()->set_custom_minimum_size(Size2(750 * EDSCALE, 0));72EditorNode::get_singleton()->get_gui_base()->add_child(upgrade_dialog);73upgrade_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ProjectUpgradeTool::prepare_upgrade));74}75upgrade_dialog->popup_centered();76}7778void ProjectUpgradeTool::prepare_upgrade() {79EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, true);8081#ifndef DISABLE_DEPRECATED82ProjectSettings::get_singleton()->set_setting("animation/compatibility/default_parent_skeleton_in_mesh_instance_3d", false);83ProjectSettings::get_singleton()->save();84#endif8586Vector<String> reimport_paths;87Vector<String> resave_scenes;88Vector<String> resave_resources;89_add_files(EditorFileSystem::get_singleton()->get_filesystem(), reimport_paths, resave_scenes, resave_resources);9091EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, reimport_paths);92EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, resave_scenes);93EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, resave_resources);9495// Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.96callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor).call_deferred(false);97}9899void ProjectUpgradeTool::begin_upgrade() {100EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, false);101DirAccess::remove_absolute("res://.godot/uid_cache.bin");102}103104void ProjectUpgradeTool::finish_upgrade() {105EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_CLOSE_ALL, true);106107Vector<String> paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Vector<String>());108EditorFileSystem::get_singleton()->reimport_files(paths);109EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Variant());110111#ifndef DISABLE_DEPRECATED112MeshInstance3D::upgrading_skeleton_compat = true;113#endif114115{116paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Vector<String>());117EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Scenes"), paths.size());118119int step = 0;120for (const String &file_path : paths) {121ep.step(TTR("Re-saving scene:") + " " + file_path, step++);122EditorNode::get_singleton()->load_scene(file_path);123EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_SAVE_SCENE, true);124EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_CLOSE, true);125}126EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Variant());127}128129#ifndef DISABLE_DEPRECATED130MeshInstance3D::upgrading_skeleton_compat = false;131#endif132133{134paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Vector<String>());135EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Resources"), paths.size());136137int step = 0;138for (const String &file_path : paths) {139ep.step(TTR("Re-saving resource:") + " " + file_path, step++);140Ref<Resource> res = ResourceLoader::load(file_path, "", ResourceFormatLoader::CACHE_MODE_REPLACE);141if (res.is_valid()) {142ResourceSaver::save(res);143}144}145EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Variant());146}147148emit_signal(UPGRADE_FINISHED);149}150151152