aboutsummaryrefslogtreecommitdiff
path: root/game/src/Autoload
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/Autoload
parenta37c5085c3b7ee515789d681c8c18d71071fb771 (diff)
Add Setting Sanatization (#21)
Diffstat (limited to 'game/src/Autoload')
-rw-r--r--game/src/Autoload/Events/Options.gd7
-rw-r--r--game/src/Autoload/Resolution.gd101
2 files changed, 69 insertions, 39 deletions
diff --git a/game/src/Autoload/Events/Options.gd b/game/src/Autoload/Events/Options.gd
index 0acaa63..8af620d 100644
--- a/game/src/Autoload/Events/Options.gd
+++ b/game/src/Autoload/Events/Options.gd
@@ -7,14 +7,17 @@ signal reset_settings()
func load_settings_from_file() -> void:
load_settings.emit(_settings_file)
-func save_settings_from_file() -> void:
+func save_settings_to_file() -> void:
save_settings.emit(_settings_file)
_settings_file.save(_settings_file_path)
func try_reset_settings() -> void:
reset_settings.emit()
-var _settings_file_path := ProjectSettings.get_setting("openvic2/settings/settings_file_path", "user://settings.cfg") as String
+const settings_file_path_setting : String = "openvic2/settings/settings_file_path"
+const settings_file_path_default : String = "user://settings.cfg"
+
+var _settings_file_path : String = ProjectSettings.get_setting(settings_file_path_setting, settings_file_path_default)
var _settings_file := ConfigFile.new()
func _init():
diff --git a/game/src/Autoload/Resolution.gd b/game/src/Autoload/Resolution.gd
index ac54c0d..e1e788b 100644
--- a/game/src/Autoload/Resolution.gd
+++ b/game/src/Autoload/Resolution.gd
@@ -1,36 +1,72 @@
extends Node
-var _resolutions : Array[Dictionary]= [
- { "name": &"", "value": Vector2i(3840,2160) },
- { "name": &"", "value": Vector2i(2560,1080) },
- { "name": &"", "value": Vector2i(1920,1080) },
- { "name": &"", "value": Vector2i(1366,768) },
- { "name": &"", "value": Vector2i(1536,864) },
- { "name": &"", "value": Vector2i(1280,720) },
- { "name": &"", "value": Vector2i(1440,900) },
- { "name": &"", "value": Vector2i(1600,900) },
- { "name": &"", "value": Vector2i(1024,600) },
- { "name": &"", "value": Vector2i(800,600) }
-]
+const error_resolution : Vector2i = Vector2i(-1,-1)
+
+@export
+var minimum_resolution : Vector2i = Vector2i(1,1)
+
+const _starting_resolutions : Dictionary = {
+ Vector2i(3840,2160): &"4K",
+ Vector2i(2560,1080): &"UW1080p",
+ Vector2i(1920,1080): &"1080p",
+ Vector2i(1366,768): &"",
+ Vector2i(1536,864): &"",
+ Vector2i(1280,720): &"720p",
+ Vector2i(1440,900): &"",
+ Vector2i(1600,900): &"",
+ Vector2i(1024,600): &"",
+ Vector2i(800,600): &""
+}
+
+var _resolutions : Dictionary
+
+const _regex_pattern : String = "(\\d+)\\s*[xX,]\\s*(\\d+)"
+var _regex : RegEx
func _ready():
- for resolution in _resolutions:
- resolution["tag"] = _get_name_of_resolution(resolution["name"], resolution["value"])
+ assert(minimum_resolution.x > 0 and minimum_resolution.y > 0, "Minimum resolution must be positive!")
+ for resolution_value in _starting_resolutions:
+ add_resolution(resolution_value, _starting_resolutions[resolution_value])
+ assert(not _resolutions.is_empty(), "No valid starting resolutions!")
+
+ _regex = RegEx.new()
+ var err := _regex.compile(_regex_pattern)
+ assert(err == OK, "Resolution RegEx failed to compile!")
-func has_resolution(resolution_name : StringName) -> bool:
- return resolution_name in _resolutions
-func get_resolution(resolution_name : StringName, default : Vector2i = Vector2i(1920, 1080)) -> Vector2i:
- var resolution := _get_resolution_by_name(resolution_name)
- if resolution.x < 0 and resolution.y < 0:
- return default
- return resolution
+func has_resolution(resolution_value : Vector2i) -> bool:
+ return resolution_value in _resolutions
-func get_resolution_name_list() -> Array[StringName]:
- var result : Array[StringName] = []
- for resolution in _resolutions:
- result.append(resolution["tag"])
- return result
+func add_resolution(resolution_value : Vector2i, resolution_name : StringName = &"") -> bool:
+ if has_resolution(resolution_value): return true
+ var res_dict := { value = resolution_value }
+ var display_name := "%sx%s" % [resolution_value.x, resolution_value.y]
+ if not resolution_name.is_empty():
+ res_dict.name = resolution_name
+ display_name = "%s (%s)" % [display_name, resolution_name]
+ res_dict.display_name = StringName(display_name)
+ if resolution_value.x < minimum_resolution.x or resolution_value.y < minimum_resolution.y:
+ push_error("Resolution %s is smaller than minimum (%sx%s)" % [res_dict.display_name, minimum_resolution.x, minimum_resolution.y])
+ return false
+ _resolutions[resolution_value] = res_dict
+ return true
+
+func get_resolution_value_list() -> Array:
+ var list := _resolutions.keys()
+ list.sort_custom(func(a, b): return a > b)
+ return list
+
+func get_resolution_display_name(resolution_value : Vector2i) -> StringName:
+ return _resolutions.get(resolution_value, { display_name = &"unknown resolution" }).display_name
+
+func get_resolution_value_from_string(resolution_string : String) -> Vector2i:
+ if not resolution_string.is_empty():
+ for resolution in _resolutions.values():
+ if resolution_string == resolution.get(name) or resolution_string == resolution.display_name:
+ return resolution.value
+ var result := _regex.search(resolution_string)
+ if result: return Vector2i(result.get_string(1).to_int(), result.get_string(2).to_int())
+ return error_resolution
func get_current_resolution() -> Vector2i:
var window := get_viewport().get_window()
@@ -41,6 +77,8 @@ func get_current_resolution() -> Vector2i:
return window.size
func set_resolution(resolution : Vector2i) -> void:
+ if not has_resolution(resolution):
+ push_warning("Setting resolution to non-standard value %sx%s" % [resolution.x, resolution.y])
var window := get_viewport().get_window()
match window.mode:
Window.MODE_EXCLUSIVE_FULLSCREEN, Window.MODE_FULLSCREEN:
@@ -51,14 +89,3 @@ func set_resolution(resolution : Vector2i) -> void:
func reset_resolution() -> void:
set_resolution(get_current_resolution())
-
-func _get_name_of_resolution(resolution_name : StringName, resolution_value : Vector2i) -> StringName:
- if resolution_name != null and not resolution_name.is_empty():
- return "%s (%sx%s)" % [resolution_name, resolution_value.x, resolution_value.y]
- return "%sx%s" % [resolution_value.x, resolution_value.y]
-
-func _get_resolution_by_name(resolution_name : StringName) -> Vector2i:
- for resolution in _resolutions:
- if resolution["name"] == resolution_name or resolution["tag"] == resolution_name:
- return resolution["value"]
- return Vector2i(-1, -1)