Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/project_upgrade/project_upgrade_tool.cpp
20937 views
1
/**************************************************************************/
2
/* project_upgrade_tool.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "project_upgrade_tool.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/io/dir_access.h"
35
#include "editor/editor_node.h"
36
#include "editor/file_system/editor_file_system.h"
37
#include "editor/scene/editor_scene_tabs.h"
38
#include "editor/settings/editor_settings.h"
39
#include "editor/themes/editor_scale.h"
40
#include "scene/3d/mesh_instance_3d.h"
41
#include "scene/gui/dialogs.h"
42
43
void ProjectUpgradeTool::_add_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_reimport_paths, Vector<String> &r_resave_scenes, Vector<String> &r_resave_resources) {
44
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
45
for (int i = 0; i < p_dir->get_file_count(); i++) {
46
const String path = p_dir->get_file_path(i);
47
const String ext = path.get_extension();
48
if (ext == "tscn" || ext == "scn") {
49
r_resave_scenes.append(path);
50
} else if (ext == "tres" || ext == "res") {
51
r_resave_resources.append(path);
52
} else if (da->file_exists(path + ".import")) {
53
r_reimport_paths.append(path);
54
}
55
}
56
57
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
58
_add_files(p_dir->get_subdir(i), r_reimport_paths, r_resave_scenes, r_resave_resources);
59
}
60
}
61
62
void ProjectUpgradeTool::_bind_methods() {
63
ADD_SIGNAL(MethodInfo("upgrade_finished"));
64
}
65
66
void ProjectUpgradeTool::popup_dialog() {
67
if (!upgrade_dialog) {
68
upgrade_dialog = memnew(ConfirmationDialog);
69
upgrade_dialog->set_autowrap(true);
70
upgrade_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."));
71
upgrade_dialog->get_ok_button()->set_text(TTRC("Restart & Upgrade"));
72
upgrade_dialog->get_label()->set_custom_minimum_size(Size2(750 * EDSCALE, 0));
73
EditorNode::get_singleton()->get_gui_base()->add_child(upgrade_dialog);
74
upgrade_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ProjectUpgradeTool::prepare_upgrade));
75
}
76
upgrade_dialog->popup_centered();
77
}
78
79
void ProjectUpgradeTool::prepare_upgrade() {
80
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, true);
81
82
#ifndef DISABLE_DEPRECATED
83
ProjectSettings::get_singleton()->set_setting("animation/compatibility/default_parent_skeleton_in_mesh_instance_3d", false);
84
ProjectSettings::get_singleton()->save();
85
#endif
86
87
Vector<String> reimport_paths;
88
Vector<String> resave_scenes;
89
Vector<String> resave_resources;
90
_add_files(EditorFileSystem::get_singleton()->get_filesystem(), reimport_paths, resave_scenes, resave_resources);
91
92
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, reimport_paths);
93
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, resave_scenes);
94
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, resave_resources);
95
96
// Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.
97
callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor).call_deferred(false);
98
}
99
100
void ProjectUpgradeTool::begin_upgrade() {
101
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, false);
102
DirAccess::remove_absolute("res://.godot/uid_cache.bin");
103
}
104
105
void ProjectUpgradeTool::finish_upgrade() {
106
EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_CLOSE_ALL, true);
107
108
Vector<String> paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Vector<String>());
109
EditorFileSystem::get_singleton()->reimport_files(paths);
110
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Variant());
111
112
#ifndef DISABLE_DEPRECATED
113
MeshInstance3D::upgrading_skeleton_compat = true;
114
#endif
115
116
{
117
paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Vector<String>());
118
EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Scenes"), paths.size());
119
120
int step = 0;
121
for (const String &file_path : paths) {
122
ep.step(TTR("Re-saving scene:") + " " + file_path, step++);
123
EditorNode::get_singleton()->load_scene(file_path);
124
EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_SAVE_SCENE, true);
125
EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_CLOSE, true);
126
}
127
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Variant());
128
}
129
130
#ifndef DISABLE_DEPRECATED
131
MeshInstance3D::upgrading_skeleton_compat = false;
132
#endif
133
134
{
135
paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Vector<String>());
136
EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Resources"), paths.size());
137
138
int step = 0;
139
for (const String &file_path : paths) {
140
ep.step(TTR("Re-saving resource:") + " " + file_path, step++);
141
Ref<Resource> res = ResourceLoader::load(file_path, "", ResourceFormatLoader::CACHE_MODE_REPLACE);
142
if (res.is_valid()) {
143
ResourceSaver::save(res);
144
}
145
}
146
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Variant());
147
}
148
149
emit_signal(UPGRADE_FINISHED);
150
}
151
152