add basic config version checking, discarding outdated configs

This commit is contained in:
Crispy 2021-07-22 17:16:27 +02:00
parent 284cfa3c2f
commit 73d9d9b88b
4 changed files with 25 additions and 7 deletions

View file

@ -90,6 +90,7 @@ func _save_sub_setting(val, def):
func load_settings() -> void: func load_settings() -> void:
var file = File.new() var file = File.new()
var validator: Script = preload("res://settings_validator.gd")
if not file.file_exists(SETTINGS_PATH): if not file.file_exists(SETTINGS_PATH):
if DEBUG_SETTINGS: if DEBUG_SETTINGS:
@ -100,8 +101,11 @@ func load_settings() -> void:
var new_settings = parse_json(file.get_as_text()) var new_settings = parse_json(file.get_as_text())
file.close() file.close()
if validator.is_valid(new_settings):
for key in new_settings: for key in new_settings:
s[key] = _load_sub_setting(new_settings[key], SETTINGS_DEF[key]) s[key] = _load_sub_setting(new_settings[key], SETTINGS_DEF[key])
else:
print("Invalid or outdated config file, using defaults")
if DEBUG_SETTINGS: if DEBUG_SETTINGS:
print("Loaded settings from file") print("Loaded settings from file")

View file

@ -1,7 +1,14 @@
extends Node extends Node
const PATH = "user://overlay_data_DEV.json" const VERSION = 1
const PATH = "user://overlay_data.json"
const DEF = { const DEF = {
"version": {
"name": "Version of save file",
"type": "int",
"default": VERSION
},
"grab_mode": { "grab_mode": {
"name": "Grab mode", "name": "Grab mode",
"description": "Grab and drag around any overlay", "description": "Grab and drag around any overlay",
@ -28,12 +35,12 @@ const DEF = {
}, },
"width": { "width": {
"name": "Width (m)", "name": "Width (m)",
"type": "number", "type": "float",
"default": 0.4 "default": 0.4
}, },
"alpha": { "alpha": {
"name": "Alpha", "name": "Alpha",
"type": "number", "type": "float",
"default": 1.0 "default": 1.0
}, },
"target": { "target": {

View file

@ -0,0 +1,7 @@
extends Node
static func is_valid(to_check: Dictionary) -> bool:
if not to_check.has("version"):
return false
return to_check.version == preload("res://settings_definition.gd").VERSION

View file

@ -13,8 +13,8 @@ func _ready() -> void:
$BasicOptions/Label.text = overlay_name $BasicOptions/Label.text = overlay_name
name = overlay_name name = overlay_name
# overlay.connect("overlay_visible_changed", self, "_overlay_visible_changed") overlay.connect("overlay_visible_changed", self, "_overlay_visible_changed")
# overlay.connect("path_changed", self, "_update_warning") overlay.connect("path_changed", self, "_update_warning")
func _apply_loaded(): func _apply_loaded():