surface_upgrade_tool.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**************************************************************************/
  2. /* surface_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 "surface_upgrade_tool.h"
  31. #include "editor/editor_file_system.h"
  32. #include "editor/editor_log.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/gui/editor_toaster.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "servers/rendering_server.h"
  38. SurfaceUpgradeTool *SurfaceUpgradeTool::singleton = nullptr;
  39. void SurfaceUpgradeTool::_add_files(EditorFileSystemDirectory *p_dir, Vector<String> &r_reimport_paths, Vector<String> &r_resave_paths) {
  40. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  41. _add_files(p_dir->get_subdir(i), r_reimport_paths, r_resave_paths);
  42. }
  43. for (int i = 0; i < p_dir->get_file_count(); i++) {
  44. if (p_dir->get_file_type(i) == "Mesh" ||
  45. p_dir->get_file_type(i) == "ArrayMesh" ||
  46. p_dir->get_file_type(i) == "PackedScene") {
  47. if (FileAccess::exists(p_dir->get_file_path(i) + ".import")) {
  48. r_reimport_paths.append(p_dir->get_file_path(i) + ".import");
  49. } else {
  50. r_resave_paths.append(p_dir->get_file_path(i));
  51. }
  52. }
  53. }
  54. }
  55. void SurfaceUpgradeTool::_try_show_popup() {
  56. if (singleton->show_requested || singleton->updating) {
  57. return;
  58. }
  59. singleton->show_requested = true;
  60. if (!EditorNode::get_singleton()->is_editor_ready()) {
  61. // EditorNode may not be ready yet. It will call this tool when it is.
  62. return;
  63. }
  64. if (EditorFileSystem::get_singleton()->is_importing()) {
  65. EditorFileSystem::get_singleton()->connect("resources_reimported", callable_mp(singleton, &SurfaceUpgradeTool::_show_popup), CONNECT_ONE_SHOT);
  66. } else {
  67. singleton->_show_popup();
  68. }
  69. RS::get_singleton()->set_warn_on_surface_upgrade(false);
  70. }
  71. void SurfaceUpgradeTool::_show_popup() {
  72. MutexLock lock(mutex);
  73. if (!show_requested) {
  74. return; // We only show the dialog if it was previously requested.
  75. }
  76. show_requested = false;
  77. // These messages are supposed to be translated as they are critical to users migrating their projects.
  78. const String confirmation_message = TTR("This project uses meshes with an outdated mesh format from previous Godot versions. The engine needs to update the format in order to use those meshes. Please use the 'Upgrade Mesh Surfaces' tool from the 'Project > Tools' menu. You can ignore this message and keep using outdated meshes, but keep in mind that this leads to increased load times every time you load the project.");
  79. EditorNode::get_log()->add_message(confirmation_message, EditorLog::MSG_TYPE_WARNING);
  80. const String toast_message = TTR("This project uses meshes with an outdated mesh format. Check the output log.");
  81. EditorToaster::get_singleton()->popup_str(toast_message, EditorToaster::SEVERITY_WARNING);
  82. }
  83. void SurfaceUpgradeTool::prepare_upgrade() {
  84. EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "run_on_restart", true);
  85. Vector<String> reimport_paths;
  86. Vector<String> resave_paths;
  87. _add_files(EditorFileSystem::get_singleton()->get_filesystem(), reimport_paths, resave_paths);
  88. EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "reimport_paths", reimport_paths);
  89. EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "resave_paths", resave_paths);
  90. // Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.
  91. callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor).call_deferred();
  92. }
  93. // Ensure that the warnings and popups are skipped.
  94. void SurfaceUpgradeTool::begin_upgrade() {
  95. updating = true;
  96. EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "run_on_restart", false);
  97. RS::get_singleton()->set_surface_upgrade_callback(nullptr);
  98. RS::get_singleton()->set_warn_on_surface_upgrade(false);
  99. }
  100. void SurfaceUpgradeTool::finish_upgrade() {
  101. EditorNode::get_singleton()->trigger_menu_option(EditorNode::FILE_CLOSE_ALL, true);
  102. // Update all meshes here.
  103. Vector<String> resave_paths = EditorSettings::get_singleton()->get_project_metadata("surface_upgrade_tool", "resave_paths", Vector<String>());
  104. Vector<String> reimport_paths = EditorSettings::get_singleton()->get_project_metadata("surface_upgrade_tool", "reimport_paths", Vector<String>());
  105. EditorProgress ep("surface_upgrade_resave", TTR("Upgrading All Meshes in Project"), resave_paths.size() + reimport_paths.size());
  106. int step = 0;
  107. for (const String &file_path : resave_paths) {
  108. Ref<Resource> res = ResourceLoader::load(file_path);
  109. ep.step(TTR("Attempting to re-save ") + file_path, step++);
  110. if (res.is_valid()) {
  111. // Ignore things that fail to load.
  112. ResourceSaver::save(res);
  113. }
  114. }
  115. EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "resave_paths", Vector<String>());
  116. // Remove the imported scenes/meshes from .import so they will be reimported automatically after this.
  117. for (const String &file_path : reimport_paths) {
  118. Ref<ConfigFile> config;
  119. config.instantiate();
  120. Error err = config->load(file_path);
  121. if (err != OK) {
  122. ERR_PRINT("Could not open " + file_path + " for upgrade.");
  123. continue;
  124. }
  125. String remap_path = config->get_value("remap", "path", "");
  126. if (remap_path.is_empty()) {
  127. continue;
  128. }
  129. ep.step(TTR("Attempting to remove ") + remap_path, step++);
  130. String path = OS::get_singleton()->get_resource_dir() + remap_path.replace_first("res://", "/");
  131. print_verbose("Moving to trash: " + path);
  132. err = OS::get_singleton()->move_to_trash(path);
  133. if (err != OK) {
  134. EditorNode::get_singleton()->add_io_error(TTR("Cannot remove:") + "\n" + remap_path + "\n");
  135. }
  136. }
  137. EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "reimport_paths", Vector<String>());
  138. emit_signal(SNAME("upgrade_finished"));
  139. }
  140. void SurfaceUpgradeTool::_bind_methods() {
  141. ADD_SIGNAL(MethodInfo("upgrade_finished"));
  142. }
  143. SurfaceUpgradeTool::SurfaceUpgradeTool() {
  144. singleton = this;
  145. RS::get_singleton()->set_surface_upgrade_callback(_try_show_popup);
  146. }
  147. SurfaceUpgradeTool::~SurfaceUpgradeTool() {
  148. singleton = nullptr;
  149. }
  150. void SurfaceUpgradeDialog::popup_on_demand() {
  151. const String confirmation_message = TTR("The mesh format has changed in Godot 4.2, which affects both imported meshes and meshes authored inside of Godot. The engine needs to update the format in order to use those meshes.\n\nIf your project predates Godot 4.2 and contains meshes, we recommend you run this one time conversion tool. This update will restart the editor and may take several minutes. Upgrading will make the meshes incompatible with previous versions of Godot.\n\nYou can still use your existing meshes as is. The engine will update each mesh in memory, but the update will not be saved. Choosing this option will lead to slower load times every time this project is loaded.");
  152. set_text(confirmation_message);
  153. get_ok_button()->set_text(TTR("Restart & Upgrade"));
  154. popup_centered(Size2(750 * EDSCALE, 0));
  155. }
  156. void SurfaceUpgradeDialog::_notification(int p_what) {
  157. switch (p_what) {
  158. case NOTIFICATION_READY:
  159. // Can't do it in the constructor because it doesn't know that the signal exists.
  160. connect(SceneStringName(confirmed), callable_mp(SurfaceUpgradeTool::get_singleton(), &SurfaceUpgradeTool::prepare_upgrade));
  161. break;
  162. }
  163. }
  164. SurfaceUpgradeDialog::SurfaceUpgradeDialog() {
  165. set_autowrap(true);
  166. get_label()->set_custom_minimum_size(Size2(750 * EDSCALE, 0));
  167. }