Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/project_upgrade/project_upgrade_tool.cpp
9898 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/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/gui/dialogs.h"
40
41
void ProjectUpgradeTool::_add_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_reimport_paths, Vector<String> &r_resave_scenes, Vector<String> &r_resave_resources) {
42
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
43
for (int i = 0; i < p_dir->get_file_count(); i++) {
44
const String path = p_dir->get_file_path(i);
45
const String ext = path.get_extension();
46
if (ext == "tscn" || ext == "scn") {
47
r_resave_scenes.append(path);
48
} else if (ext == "tres" || ext == "res") {
49
r_resave_resources.append(path);
50
} else if (da->file_exists(path + ".import")) {
51
r_reimport_paths.append(path);
52
}
53
}
54
55
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
56
_add_files(p_dir->get_subdir(i), r_reimport_paths, r_resave_scenes, r_resave_resources);
57
}
58
}
59
60
void ProjectUpgradeTool::_bind_methods() {
61
ADD_SIGNAL(MethodInfo("upgrade_finished"));
62
}
63
64
void ProjectUpgradeTool::popup_dialog() {
65
if (!upgrade_dialog) {
66
upgrade_dialog = memnew(ConfirmationDialog);
67
upgrade_dialog->set_autowrap(true);
68
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."));
69
upgrade_dialog->get_ok_button()->set_text(TTRC("Restart & Upgrade"));
70
upgrade_dialog->get_label()->set_custom_minimum_size(Size2(750 * EDSCALE, 0));
71
EditorNode::get_singleton()->get_gui_base()->add_child(upgrade_dialog);
72
upgrade_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ProjectUpgradeTool::prepare_upgrade));
73
}
74
upgrade_dialog->popup_centered();
75
}
76
77
void ProjectUpgradeTool::prepare_upgrade() {
78
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, true);
79
80
Vector<String> reimport_paths;
81
Vector<String> resave_scenes;
82
Vector<String> resave_resources;
83
_add_files(EditorFileSystem::get_singleton()->get_filesystem(), reimport_paths, resave_scenes, resave_resources);
84
85
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, reimport_paths);
86
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, resave_scenes);
87
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, resave_resources);
88
89
// Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.
90
callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor).call_deferred(false);
91
}
92
93
void ProjectUpgradeTool::begin_upgrade() {
94
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RUN_ON_RESTART, false);
95
DirAccess::remove_absolute("res://.godot/uid_cache.bin");
96
}
97
98
void ProjectUpgradeTool::finish_upgrade() {
99
EditorNode::get_singleton()->trigger_menu_option(EditorSceneTabs::SCENE_CLOSE_ALL, true);
100
101
Vector<String> paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Vector<String>());
102
EditorFileSystem::get_singleton()->reimport_files(paths);
103
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_REIMPORT_PATHS, Variant());
104
105
{
106
paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Vector<String>());
107
EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Scenes"), paths.size());
108
109
int step = 0;
110
for (const String &file_path : paths) {
111
ep.step(TTR("Re-saving scene:") + " " + file_path, step++);
112
EditorNode::get_singleton()->load_scene(file_path);
113
EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_SAVE_SCENE, true);
114
EditorNode::get_singleton()->trigger_menu_option(EditorNode::SCENE_CLOSE, true);
115
}
116
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_SCENES, Variant());
117
}
118
119
{
120
paths = EditorSettings::get_singleton()->get_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Vector<String>());
121
EditorProgress ep("uid_upgrade_resave", TTR("Updating Project Resources"), paths.size());
122
123
int step = 0;
124
for (const String &file_path : paths) {
125
ep.step(TTR("Re-saving resource:") + " " + file_path, step++);
126
Ref<Resource> res = ResourceLoader::load(file_path, "", ResourceFormatLoader::CACHE_MODE_REPLACE);
127
if (res.is_valid()) {
128
ResourceSaver::save(res);
129
}
130
}
131
EditorSettings::get_singleton()->set_project_metadata(META_PROJECT_UPGRADE_TOOL, META_RESAVE_RESOURCES, Variant());
132
}
133
134
emit_signal(UPGRADE_FINISHED);
135
}
136
137