#Config file error handlin

1 messages · Page 1 of 1 (latest)

somber moat
#

hey i need some help with config files. I want to handle errors caused by changing a value in a config file to non expected data types. This is to prevent someone from changing stuff around and making it so that the game crashes on startup and it can always have a default value. This is what I currently have for checking save files.

func _check_corruption(value : int):
    
    ##Clears array that removes non expected keys from save files.
    
    keys_to_remove.clear()
    
    ##Checks for incorrect values and non specified keys and handles them.
    ##TODO Figure out how to handle errors, specifically non expected data types.
    
    for key in temp.get_section_keys("profile"):
        
        match key:
            "name":
                if not typeof(temp.get_value("profile", key)) == TYPE_STRING || temp.get_value("profile", key) == null :
                    _fix_corruption(value, key)
                    push_error("Save file " + str(value) + " name is corrupted!")
            "wins":
                if not typeof(temp.get_value("profile", key)) == TYPE_INT || temp.get_value("profile", key) == null :
                    _fix_corruption(value, key)
                    push_error("Save file " + str(value) + " wins is corrupted!")
            _:
                keys_to_remove.append(key)
    
    for key in keys_to_remove :
        temp.erase_section_key("profile", key)
        temp.save(CURRENT_SAVE)


func _fix_corruption(value, key):
    
    ##Fixes incorrect values.
    
    if key == "name" :
        temp.set_value("profile", "name", "Placeholder")
        push_warning("Save file " + str(value) + " name is fixed!")
    elif key == "wins":
        temp.set_value("profile", "wins", 0)
        push_warning("Save file " + str(value) + " wins is fixed!")
    print(temp.get_value("profile","wins"))
    
    temp.save(CURRENT_SAVE)
#

The error im trying to avoid is for example on save0 changing it to be like this:

[profile]

name="Placeholder"
wins=aba

Which gives the error :
E 0:00:00:0904 save_files.gd:47 @ _create_save(): ConfigFile parse error at user://save0.ini:3: Unexpected identifier: 'aba'..
<C++ Source> core/io/config_file.cpp:304 @ _parse()
<Stack Trace> save_files.gd:47 @ _create_save()
save_files.gd:17 @ _save_file_create()
settings_save.gd:33 @ _ready()

somber moat
somber moat
#

Config file error handlin

somber moat
#

Ok seems like i found a fix but i dont understand why it works.


var valid_keys = ["name", "wins"]

##In function _check_corruption i added this
    ##Checks if save file has all expected keys.
    
    for key in valid_keys:
        if not temp.has_section_key("profile", key) :
            _fix_corruption(value, key)