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.
This commit is contained in:
Rémi Verschelde 2022-11-29 15:20:15 +01:00
parent 6fdbf79046
commit f21f75eb6f
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 19 additions and 17 deletions

View file

@ -1200,6 +1200,7 @@ ProjectSettings::ProjectSettings() {
// Initialization of engine variables should be done in the setup() method, // Initialization of engine variables should be done in the setup() method,
// so that the values can be overridden from project.godot or project.binary. // 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; singleton = this;
GLOBAL_DEF_BASIC("application/config/name", ""); GLOBAL_DEF_BASIC("application/config/name", "");

View file

@ -436,17 +436,17 @@ private:
return; return;
} }
ProjectSettings *current = memnew(ProjectSettings); // Load project.godot as ConfigFile to set the new name.
ConfigFile cfg;
int err = current->setup(dir2, ""); String project_godot = dir2.path_join("project.godot");
Error err = cfg.load(project_godot);
if (err != OK) { 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 { } else {
ProjectSettings::CustomMap edited_settings; cfg.set_value("application", "config/name", project_name->get_text().strip_edges());
edited_settings["application/config/name"] = project_name->get_text().strip_edges(); err = cfg.save(project_godot);
if (err != OK) {
if (current->save_custom(dir2.path_join("project.godot"), edited_settings, Vector<String>(), true) != OK) { set_message(vformat(TTR("Couldn't save project at '%s' (error %d)."), project_godot, err), MESSAGE_ERROR);
set_message(TTR("Couldn't edit project.godot in project path."), MESSAGE_ERROR);
} }
} }
@ -694,18 +694,19 @@ public:
default_files_container->hide(); default_files_container->hide();
get_ok_button()->set_disabled(false); get_ok_button()->set_disabled(false);
ProjectSettings *current = memnew(ProjectSettings); // Fetch current name from project.godot to prefill the text input.
ConfigFile cfg;
int err = current->setup(project_path->get_text(), ""); String project_godot = project_path->get_text().path_join("project.godot");
Error err = cfg.load(project_godot);
if (err != OK) { 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(); status_rect->show();
msg->show(); msg->show();
get_ok_button()->set_disabled(true); get_ok_button()->set_disabled(true);
} else if (current->has_setting("application/config/name")) { } else {
String proj = current->get("application/config/name"); String cur_name = cfg.get_value("application", "config/name", "");
project_name->set_text(proj); project_name->set_text(cur_name);
_text_changed(proj); _text_changed(cur_name);
} }
project_name->call_deferred(SNAME("grab_focus")); project_name->call_deferred(SNAME("grab_focus"));