From d4e6eff2de2b5abd576292e3f64fae336bbd531a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Thu, 5 Mar 2020 10:41:31 +0100 Subject: [PATCH] ConfigFile: Improve error messages and complete docs (cherry picked from commit f7c87ed3dd29884445a760cbe57404e139c76407) --- core/io/config_file.cpp | 18 +++++++++--------- core/io/config_file.h | 2 -- doc/classes/ConfigFile.xml | 25 +++++++++++++++++-------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 74e85d0cc0..64ce481c1d 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -86,7 +86,8 @@ void ConfigFile::set_value(const String &p_section, const String &p_key, const V Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const { if (!values.has(p_section) || !values[p_section].has(p_key)) { - ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, p_default, "Couldn't find the given section/key and no default was given."); + ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, Variant(), + vformat("Couldn't find the given section \"%s\" and key \"%s\", and no default was given.", p_section, p_key)); return p_default; } return values[p_section][p_key]; @@ -111,7 +112,7 @@ void ConfigFile::get_sections(List *r_sections) const { } void ConfigFile::get_section_keys(const String &p_section, List *r_keys) const { - ERR_FAIL_COND_MSG(!values.has(p_section), "Cannont get keys from nonexistent section '" + p_section + "'."); + ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section)); for (OrderedHashMap::ConstElement E = values[p_section].front(); E; E = E.next()) { r_keys->push_back(E.key()); @@ -120,12 +121,14 @@ void ConfigFile::get_section_keys(const String &p_section, List *r_keys) void ConfigFile::erase_section(const String &p_section) { + ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase nonexistent section \"%s\".", p_section)); values.erase(p_section); } void ConfigFile::erase_section_key(const String &p_section, const String &p_key) { - ERR_FAIL_COND_MSG(!values.has(p_section), "Cannot erase key from nonexistent section '" + p_section + "'."); + ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase key \"%s\" from nonexistent section \"%s\".", p_key, p_section)); + 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); } @@ -290,7 +293,7 @@ Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) if (err == ERR_FILE_EOF) { return OK; } else if (err != OK) { - ERR_PRINT("ConfgFile - " + p_path + ":" + itos(lines) + " error: " + error_text + "."); + ERR_PRINT(vformat("ConfigFile parse error at %s:%d: %s.", p_path, lines, error_text)); return err; } @@ -323,11 +326,8 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); - ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "pass"), &ConfigFile::load_encrypted_pass); + ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "password"), &ConfigFile::load_encrypted_pass); ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted); - ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "pass"), &ConfigFile::save_encrypted_pass); -} - -ConfigFile::ConfigFile() { + ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass); } diff --git a/core/io/config_file.h b/core/io/config_file.h index 2d61ef6afe..9f06d9aa4e 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -74,8 +74,6 @@ public: Error save_encrypted(const String &p_path, const Vector &p_key); Error save_encrypted_pass(const String &p_path, const String &p_pass); - - ConfigFile(); }; #endif // CONFIG_FILE_H diff --git a/doc/classes/ConfigFile.xml b/doc/classes/ConfigFile.xml index 0805b81e25..b11557ef75 100644 --- a/doc/classes/ConfigFile.xml +++ b/doc/classes/ConfigFile.xml @@ -37,7 +37,7 @@ - Deletes the specified section along with all the key-value pairs inside. + Deletes the specified section along with all the key-value pairs inside. Raises an error if the section does not exist. @@ -48,6 +48,7 @@ + Deletes the specified key in a section. Raises an error if either the section or the key do not exist. @@ -56,7 +57,7 @@ - Returns an array of all defined key identifiers in the specified section. + Returns an array of all defined key identifiers in the specified section. Raises an error and returns an empty array if the section does not exist. @@ -76,7 +77,7 @@ - Returns the current value for the specified section and key. If the section and/or the key do not exist, the method returns the value of the optional [code]default[/code] argument, or [code]null[/code] if it is omitted. + Returns the current value for the specified section and key. If either the section or the key do not exist, the method returns the fallback [code]default[/code] value. If [code]default[/code] is not specified or set to [code]null[/code], an error is also raised. @@ -105,7 +106,7 @@ - Loads the config file specified as a parameter. The file's contents are parsed and loaded in the ConfigFile object which the method was called on. + Loads the config file specified as a parameter. The file's contents are parsed and loaded in the [ConfigFile] object which the method was called on. Returns one of the [enum Error] code constants ([code]OK[/code] on success). @@ -117,6 +118,8 @@ + Loads the encrypted config file specified as a parameter, using the provided [code]key[/code] to decrypt it. The file's contents are parsed and loaded in the [ConfigFile] object which the method was called on. + Returns one of the [enum Error] code constants ([code]OK[/code] on success). @@ -124,9 +127,11 @@ - + + Loads the encrypted config file specified as a parameter, using the provided [code]password[/code] to decrypt it. The file's contents are parsed and loaded in the [ConfigFile] object which the method was called on. + Returns one of the [enum Error] code constants ([code]OK[/code] on success). @@ -145,7 +150,7 @@ - Saves the contents of the ConfigFile object to the file specified as a parameter. The output file uses an INI-style structure. + Saves the contents of the [ConfigFile] object to the file specified as a parameter. The output file uses an INI-style structure. Returns one of the [enum Error] code constants ([code]OK[/code] on success). @@ -157,6 +162,8 @@ + Saves the contents of the [ConfigFile] object to the AES-256 encrypted file specified as a parameter, using the provided [code]key[/code] to encrypt it. The output file uses an INI-style structure. + Returns one of the [enum Error] code constants ([code]OK[/code] on success). @@ -164,9 +171,11 @@ - + + Saves the contents of the [ConfigFile] object to the AES-256 encrypted file specified as a parameter, using the provided [code]password[/code] to encrypt it. The output file uses an INI-style structure. + Returns one of the [enum Error] code constants ([code]OK[/code] on success). @@ -179,7 +188,7 @@ - Assigns a value to the specified key of the specified section. If the section and/or the key do not exist, they are created. Passing a [code]null[/code] value deletes the specified key if it exists, and deletes the section if it ends up empty once the key has been removed. + Assigns a value to the specified key of the specified section. If either the section or the key do not exist, they are created. Passing a [code]null[/code] value deletes the specified key if it exists, and deletes the section if it ends up empty once the key has been removed.