project_upgrade_tool.cpp 7.3 KB

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