From 3dc47b0b847e4c42aeb0fd5e2f569c543b4f97ab Mon Sep 17 00:00:00 2001 From: kobewi Date: Sat, 22 Jul 2023 13:36:51 +0200 Subject: [PATCH] Load project metadata file only when needed --- editor/editor_settings.cpp | 39 ++++++++++++++++++++++++-------------- editor/editor_settings.h | 2 ++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 4ae0aa7e4a8..9830a091bf1 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -873,6 +873,10 @@ bool EditorSettings::_is_default_text_editor_theme(String p_theme_name) { return p_theme_name == "default" || p_theme_name == "godot 2" || p_theme_name == "custom"; } +const String EditorSettings::_get_project_metadata_path() const { + return EditorPaths::get_singleton()->get_project_settings_dir().path_join("project_metadata.cfg"); +} + // PUBLIC METHODS EditorSettings *EditorSettings::get_singleton() { @@ -1160,24 +1164,31 @@ void EditorSettings::add_property_hint(const PropertyInfo &p_hint) { // Metadata void EditorSettings::set_project_metadata(const String &p_section, const String &p_key, Variant p_data) { - Ref cf = memnew(ConfigFile); - String path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("project_metadata.cfg"); - Error err; - err = cf->load(path); - ERR_FAIL_COND_MSG(err != OK && err != ERR_FILE_NOT_FOUND, "Cannot load editor settings from file '" + path + "'."); - cf->set_value(p_section, p_key, p_data); - err = cf->save(path); - ERR_FAIL_COND_MSG(err != OK, "Cannot save editor settings to file '" + path + "'."); + const String path = _get_project_metadata_path(); + + if (project_metadata.is_null()) { + project_metadata.instantiate(); + + Error err = project_metadata->load(path); + if (err != OK && err != ERR_FILE_NOT_FOUND) { + ERR_PRINT("Cannot load project metadata from file '" + path + "'."); + } + } + project_metadata->set_value(p_section, p_key, p_data); + + Error err = project_metadata->save(path); + ERR_FAIL_COND_MSG(err != OK, "Cannot save project metadata to file '" + path + "'."); } Variant EditorSettings::get_project_metadata(const String &p_section, const String &p_key, Variant p_default) const { - Ref cf = memnew(ConfigFile); - String path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("project_metadata.cfg"); - Error err = cf->load(path); - if (err != OK) { - return p_default; + if (project_metadata.is_null()) { + project_metadata.instantiate(); + + const String path = _get_project_metadata_path(); + Error err = project_metadata->load(path); + ERR_FAIL_COND_V_MSG(err != OK && err != ERR_FILE_NOT_FOUND, p_default, "Cannot load project metadata from file '" + path + "'."); } - return cf->get_value(p_section, p_key, p_default); + return project_metadata->get_value(p_section, p_key, p_default); } void EditorSettings::set_favorites(const Vector &p_favorites) { diff --git a/editor/editor_settings.h b/editor/editor_settings.h index 660a9501a27..c3ce790e0e7 100644 --- a/editor/editor_settings.h +++ b/editor/editor_settings.h @@ -79,6 +79,7 @@ private: HashSet changed_settings; + mutable Ref project_metadata; HashMap hints; HashMap props; int last_order; @@ -106,6 +107,7 @@ private: void _load_godot2_text_editor_theme(); bool _save_text_editor_theme(String p_file); bool _is_default_text_editor_theme(String p_theme_name); + const String _get_project_metadata_path() const; protected: static void _bind_methods();