Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading custom ProjectSettings instance #75048

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust
}
}
// Check for the existence of a csproj file.
if (_csproj_exists(get_resource_path())) {
if (_csproj_exists(p_path.get_base_dir())) {
Copy link
Member Author

@KoBeWi KoBeWi Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what was the intention here, but get_resource_path() can point to invalid directory and _csproj_exists() crash. If the previous code was correct, I could handle it in _csproj_exists() instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention was to get the path to the directory that contains project.godot (which is res://). Maybe we can just use the literal string "res://" here and it would be handled by DirAccess.

The _csproj_exists method is just meant to be FileAccess::exists("res://*.csproj") but FileAccess::exists doesn't support globbing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_resource_path() returns the loaded path. With my change, save_custom used to save to a directory different than res:// will look for .csproj inside that directory instead of the load path. Would that be correct? (I think it doesn't affect the editor, as save_custom() is normally always used with the current path)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we always look for the .csproj in the res:// directory so looking in a different directory wouldn't be correct. Are you saying the project.godot file can be in a directory other than res://? Doesn't the editor always assume this file to be in the root project directory?

In which cases is save_custom used to save to a directory different than res://? I see that this method is exposed to scripting, is this the case you are concerned about? If so, I don't think we care about this scenario. This detection is only used to add the "C#" feature tag to be used by the Project Manager, so any usage outside of that doesn't really matter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked how we use save_custom and it's used only for creating new projects and for saving project file for exported project. So I guess my concerns are irrelevant.

// If there is a csproj file, add the C# feature if it doesn't already exist.
if (!project_features.has("C#")) {
project_features.append("C#");
Expand Down Expand Up @@ -1568,6 +1568,14 @@ ProjectSettings::ProjectSettings() {
ProjectSettings::get_singleton()->add_hidden_prefix("input/");
}

ProjectSettings::ProjectSettings(const String &p_path) {
if (load_custom(p_path) == OK) {
project_loaded = true;
}
}

ProjectSettings::~ProjectSettings() {
singleton = nullptr;
if (singleton == this) {
singleton = nullptr;
}
}
1 change: 1 addition & 0 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class ProjectSettings : public Object {
#endif

ProjectSettings();
ProjectSettings(const String &p_path);
~ProjectSettings();
};

Expand Down
14 changes: 8 additions & 6 deletions editor/project_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,18 +802,20 @@ void ProjectManager::_apply_project_tags() {
}
}

ConfigFile cfg;
const String project_godot = project_list->get_selected_projects()[0].path.path_join("project.godot");
Error err = cfg.load(project_godot);
if (err != OK) {
tag_edit_error->set_text(vformat(TTR("Couldn't load project at '%s' (error %d). It may be missing or corrupted."), project_godot, err));
ProjectSettings *cfg = memnew(ProjectSettings(project_godot));
if (!cfg->is_project_loaded()) {
memdelete(cfg);
tag_edit_error->set_text(vformat(TTR("Couldn't load project at '%s'. It may be missing or corrupted."), project_godot));
tag_edit_error->show();
callable_mp((Window *)tag_manage_dialog, &Window::show).call_deferred(); // Make sure the dialog does not disappear.
return;
} else {
tags.sort();
cfg.set_value("application", "config/tags", tags);
err = cfg.save(project_godot);
cfg->set("application/config/tags", tags);
Error err = cfg->save_custom(project_godot);
memdelete(cfg);

if (err != OK) {
tag_edit_error->set_text(vformat(TTR("Couldn't save project at '%s' (error %d)."), project_godot, err));
tag_edit_error->show();
Expand Down
Loading