aboutsummaryrefslogtreecommitdiff
path: root/game/src/LocaleButton.gd
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-02-26 17:12:07 +0100
committer GitHub <noreply@github.com>2023-02-26 17:12:07 +0100
commit93c6b207c11fdaba484410eb53fc11b35fbbb3bd (patch)
tree911753c663adb3955ab15cc8b5ac37498c2fda6e /game/src/LocaleButton.gd
parenta37c5085c3b7ee515789d681c8c18d71071fb771 (diff)
Add Setting Sanatization (#21)
Diffstat (limited to 'game/src/LocaleButton.gd')
-rw-r--r--game/src/LocaleButton.gd44
1 files changed, 32 insertions, 12 deletions
diff --git a/game/src/LocaleButton.gd b/game/src/LocaleButton.gd
index 32807d0..9f499b8 100644
--- a/game/src/LocaleButton.gd
+++ b/game/src/LocaleButton.gd
@@ -1,5 +1,8 @@
extends OptionButton
+const section_name : String = "Localization"
+const setting_name : String = "Locale"
+
var _locales_country_rename : Dictionary
var _locales_list : Array[String]
@@ -21,15 +24,32 @@ func _ready():
Events.Options.load_settings.connect(load_setting)
Events.Options.save_settings.connect(save_setting)
-
-func load_setting(file : ConfigFile):
- var locale_index := _locales_list.find(file.get_value("Localization", "Locale", "") as String)
- if locale_index != -1:
- selected = locale_index
-
-func save_setting(file : ConfigFile):
- file.set_value("Localization", "Locale", _locales_list[selected])
-
-func _on_item_selected(index):
- TranslationServer.set_locale(_locales_list[index])
- Events.Options.save_settings_from_file.call_deferred()
+func _valid_index(index : int) -> bool:
+ return 0 <= index and index < _locales_list.size()
+
+func load_setting(file : ConfigFile) -> void:
+ if file == null: return
+ var load_value = file.get_value(section_name, setting_name, TranslationServer.get_locale())
+ match typeof(load_value):
+ TYPE_STRING, TYPE_STRING_NAME:
+ var locale_index := _locales_list.find(load_value as String)
+ if locale_index != -1:
+ selected = locale_index
+ return
+ push_error("Setting value '%s' invalid for setting [%s] %s" % [load_value, section_name, setting_name])
+ reset_setting()
+
+func save_setting(file : ConfigFile) -> void:
+ if file == null: return
+ file.set_value(section_name, setting_name, _locales_list[selected])
+
+func reset_setting() -> void:
+ selected = _locales_list.find(TranslationServer.get_locale())
+
+func _on_item_selected(index : int) -> void:
+ if _valid_index(index):
+ TranslationServer.set_locale(_locales_list[index])
+ Events.Options.save_settings_to_file.call_deferred()
+ else:
+ push_error("Invalid LocaleButton index: %d" % index)
+ reset_setting()