From 4ee12fa80bf7d222cd34428e65a1813f11c97321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Tue, 29 Nov 2022 15:20:15 +0100 Subject: [PATCH] Project Manager: Fix hacky code for project rename Instantiating a new ProjectSettings is *not* the way to go. ConfigFile works just fine to read/change a single value. Fixes memory leaks as the instantiated ProjectSettings was never freed. Forbid doing this to prevent such problems. Fixes #25661. (cherry picked from commit f21f75eb6f7c7b5aaefd8e1c369fe09e1d91cc68) --- core/project_settings.cpp | 1 + editor/project_manager.cpp | 35 ++++++++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/core/project_settings.cpp b/core/project_settings.cpp index 3f839071286..95e57eb745c 100644 --- a/core/project_settings.cpp +++ b/core/project_settings.cpp @@ -1049,6 +1049,7 @@ ProjectSettings::ProjectSettings() { // Initialization of engine variables should be done in the setup() method, // so that the values can be overridden from project.godot or project.binary. + CRASH_COND_MSG(singleton != nullptr, "Instantiating a new ProjectSettings singleton is not supported."); singleton = this; last_order = NO_BUILTIN_ORDER_BASE; last_builtin_order = 0; diff --git a/editor/project_manager.cpp b/editor/project_manager.cpp index aac729dc0e9..21375cd978b 100644 --- a/editor/project_manager.cpp +++ b/editor/project_manager.cpp @@ -430,17 +430,17 @@ private: return; } - ProjectSettings *current = memnew(ProjectSettings); - - int err = current->setup(dir2, ""); + // Load project.godot as ConfigFile to set the new name. + ConfigFile cfg; + String project_godot = dir2.plus_file("project.godot"); + Error err = cfg.load(project_godot); if (err != OK) { - set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR); + set_message(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err), MESSAGE_ERROR); } else { - ProjectSettings::CustomMap edited_settings; - edited_settings["application/config/name"] = project_name->get_text().strip_edges(); - - if (current->save_custom(dir2.plus_file("project.godot"), edited_settings, Vector(), true) != OK) { - set_message(TTR("Couldn't edit project.godot in project path."), MESSAGE_ERROR); + cfg.set_value("application", "config/name", project_name->get_text().strip_edges()); + err = cfg.save(project_godot); + if (err != OK) { + set_message(vformat(TTR("Couldn't save project at '%s' (error %d)."), project_godot, err), MESSAGE_ERROR); } } @@ -688,18 +688,19 @@ public: rasterizer_container->hide(); get_ok()->set_disabled(false); - ProjectSettings *current = memnew(ProjectSettings); - - int err = current->setup(project_path->get_text(), ""); + // Fetch current name from project.godot to prefill the text input. + ConfigFile cfg; + String project_godot = project_path->get_text().plus_file("project.godot"); + Error err = cfg.load(project_godot); if (err != OK) { - set_message(vformat(TTR("Couldn't load project.godot in project path (error %d). It may be missing or corrupted."), err), MESSAGE_ERROR); + set_message(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err), MESSAGE_ERROR); status_rect->show(); msg->show(); get_ok()->set_disabled(true); - } else if (current->has_setting("application/config/name")) { - String proj = current->get("application/config/name"); - project_name->set_text(proj); - _text_changed(proj); + } else { + String cur_name = cfg.get_value("application", "config/name", ""); + project_name->set_text(cur_name); + _text_changed(cur_name); } project_name->call_deferred("grab_focus");