diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-06-07 20:53:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-07 20:53:38 +0200 |
commit | e136f4741c91c20fb18c44d31d4c6f88e9d1d171 (patch) | |
tree | 2443c7b9da219bdee1509f3fdbaef293a915f01c | |
parent | 96f41d6ada781b6ecfb0217c4847c11d3c1626ad (diff) | |
parent | 1ca227af1d76dd2b4ce756d822dc44bd4ce91f4a (diff) |
Merge pull request #41568 from dalexeev/config_file_empty_sect
Fix saving section-less keys in `ConfigFile`
-rw-r--r-- | core/io/config_file.cpp | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index dd0191f43f..ae421654ca 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -61,19 +61,19 @@ PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const { } void ConfigFile::set_value(const String &p_section, const String &p_key, const Variant &p_value) { - if (p_value.get_type() == Variant::NIL) { - //erase + if (p_value.get_type() == Variant::NIL) { // Erase key. if (!values.has(p_section)) { - return; // ? + return; } + values[p_section].erase(p_key); if (values[p_section].is_empty()) { values.erase(p_section); } - } else { if (!values.has(p_section)) { - values[p_section] = HashMap<String, Variant>(); + // Insert section-less keys at the beginning. + values.insert(p_section, HashMap<String, Variant>(), p_section.is_empty()); } values[p_section][p_key] = p_value; @@ -125,6 +125,9 @@ void ConfigFile::erase_section_key(const String &p_section, const String &p_key) ERR_FAIL_COND_MSG(!values[p_section].has(p_key), vformat("Cannot erase nonexistent key \"%s\" from section \"%s\".", p_key, p_section)); values[p_section].erase(p_key); + if (values[p_section].is_empty()) { + values.erase(p_section); + } } Error ConfigFile::save(const String &p_path) { |