diff options
author | Hop311 <Hop3114@gmail.com> | 2023-02-26 17:12:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-26 17:12:07 +0100 |
commit | 93c6b207c11fdaba484410eb53fc11b35fbbb3bd (patch) | |
tree | 911753c663adb3955ab15cc8b5ac37498c2fda6e /game/src/OptionMenu/SettingNodes/SettingOptionButton.gd | |
parent | a37c5085c3b7ee515789d681c8c18d71071fb771 (diff) |
Add Setting Sanatization (#21)
Diffstat (limited to 'game/src/OptionMenu/SettingNodes/SettingOptionButton.gd')
-rw-r--r-- | game/src/OptionMenu/SettingNodes/SettingOptionButton.gd | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/game/src/OptionMenu/SettingNodes/SettingOptionButton.gd b/game/src/OptionMenu/SettingNodes/SettingOptionButton.gd index 3a5c979..c7b760d 100644 --- a/game/src/OptionMenu/SettingNodes/SettingOptionButton.gd +++ b/game/src/OptionMenu/SettingNodes/SettingOptionButton.gd @@ -11,30 +11,39 @@ var setting_name : String = "SettingOptionMenu" var default_selected : int = -1: get: return default_selected set(v): - if v == -1: + if v < 0 or item_count == 0: default_selected = -1 return default_selected = v % item_count -@export -var add_nonstandard_value := false +func _valid_index(index : int) -> bool: + return 0 <= index and index < item_count func _get_value_for_file(select_value : int): - if select_value > -1: - return get_item_text(select_value) + if _valid_index(select_value): + return select_value else: return null func _set_value_from_file(load_value) -> void: - selected = -1 - for item_index in range(item_count): - if load_value == get_item_text(item_index): - selected = item_index - if selected == -1: - if add_nonstandard_value: - add_item(load_value) - selected = item_count - 1 - else: push_error("Setting value '%s' invalid for setting [%s] \"%s\"" % [load_value, section_name, setting_name]) + match typeof(load_value): + TYPE_INT: + if _valid_index(load_value): + selected = load_value + return + TYPE_STRING, TYPE_STRING_NAME: + var load_string := load_value as String + if load_string.is_valid_int(): + var load_int := load_string.to_int() + if _valid_index(load_int): + selected = load_int + return + for item_index in item_count: + if load_string == get_item_text(item_index): + selected = item_index + return + push_error("Setting value '%s' invalid for setting [%s] \"%s\"" % [load_value, section_name, setting_name]) + selected = default_selected func _setup_button() -> void: pass @@ -44,6 +53,9 @@ func _ready(): Events.Options.save_settings.connect(save_setting) Events.Options.reset_settings.connect(reset_setting) _setup_button() + if not _valid_index(default_selected) or selected == -1: + OS.alert("Failed to generate %s %s options." % [setting_name, section_name], "%s Options Error" % section_name) + get_tree().quit() func load_setting(file : ConfigFile) -> void: if file == null: return |