From f22efdea0af0023fbc3cfba93e964c1d96878603 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 7 Jun 2021 17:09:35 +0200 Subject: [PATCH] settings save at most every second. Checks if settings have changed every second. --- .../addons/openvr_overlay/overlay_instance.gd | 2 - .../addons/settings-manager/Settings.tscn | 7 +- ovr-utils/addons/settings-manager/plugin.cfg | 2 +- ovr-utils/addons/settings-manager/settings.gd | 132 ----------------- .../settings-manager/settings_manager.gd | 133 +++++++++++++++++- .../settings-manager/settings_plugin.gd | 10 ++ ovr-utils/overlay_manager.gd | 1 - ovr-utils/overlay_scripts/main_menu.gd | 2 +- 8 files changed, 143 insertions(+), 146 deletions(-) delete mode 100644 ovr-utils/addons/settings-manager/settings.gd create mode 100644 ovr-utils/addons/settings-manager/settings_plugin.gd diff --git a/ovr-utils/addons/openvr_overlay/overlay_instance.gd b/ovr-utils/addons/openvr_overlay/overlay_instance.gd index 7e5af57..2ec36ff 100644 --- a/ovr-utils/addons/openvr_overlay/overlay_instance.gd +++ b/ovr-utils/addons/openvr_overlay/overlay_instance.gd @@ -102,8 +102,6 @@ func save_settings(): for t_key in TARGETS: Settings.s.overlays[name].offsets[t_key] = _offsets[t_key] - Settings.save_settings() - func update_tracker_id(): _tracker_id = -1 diff --git a/ovr-utils/addons/settings-manager/Settings.tscn b/ovr-utils/addons/settings-manager/Settings.tscn index 5ad12ee..c6aada0 100644 --- a/ovr-utils/addons/settings-manager/Settings.tscn +++ b/ovr-utils/addons/settings-manager/Settings.tscn @@ -1,12 +1,11 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://addons/settings-manager/settings.gd" type="Script" id=1] +[ext_resource path="res://addons/settings-manager/settings_manager.gd" type="Script" id=1] [node name="Settings" type="Node"] script = ExtResource( 1 ) -[node name="AutosaveTimer" type="Timer" parent="."] -wait_time = 300.0 +[node name="SaveTimer" type="Timer" parent="."] autostart = true -[connection signal="timeout" from="AutosaveTimer" to="." method="_on_AutosaveTimer_timeout"] +[connection signal="timeout" from="SaveTimer" to="." method="_on_SaveTimer_timeout"] diff --git a/ovr-utils/addons/settings-manager/plugin.cfg b/ovr-utils/addons/settings-manager/plugin.cfg index 0a1f5cf..a2268b0 100644 --- a/ovr-utils/addons/settings-manager/plugin.cfg +++ b/ovr-utils/addons/settings-manager/plugin.cfg @@ -4,4 +4,4 @@ name="SettingsManager" description="Simple settings manager for godot" author="CrispyPin" version="" -script="settings_manager.gd" +script="settings_plugin.gd" diff --git a/ovr-utils/addons/settings-manager/settings.gd b/ovr-utils/addons/settings-manager/settings.gd deleted file mode 100644 index f256958..0000000 --- a/ovr-utils/addons/settings-manager/settings.gd +++ /dev/null @@ -1,132 +0,0 @@ -extends Node - -signal settings_saved -signal settings_loaded # emitted when settings are loaded from file, needs to be connected in _init() -signal settings_changed # has to be manually called by the modifying script - -var DEBUG_SETTINGS = false -var SETTINGS_PATH = "user://overlay_data.json" -const SETTINGS_DEF = preload("res://addons/settings-manager/settings_definition.gd").DEF - -var s: Dictionary = {} - - -func _ready() -> void: - _init_settings() - load_settings() - save_settings() - - -func _init_settings() -> void: - for key in SETTINGS_DEF: - s[key] = _init_sub_setting(SETTINGS_DEF[key]) - if DEBUG_SETTINGS: - print("Default settings: ", s) - - -func _init_sub_setting(def): - match def.type: - "dict": - if has_flag(def, "resize"): - return def.default - var _s = {} - for key in def.definition: - _s[key] = _init_sub_setting(def.definition[key]) - return _s - _: - return def.default - - -func save_settings(): - var to_save = {} - for key in s: - var val = _save_sub_setting(s[key], SETTINGS_DEF[key]) - if val != null: - to_save[key] = val - - var file = File.new() - file.open(SETTINGS_PATH, File.WRITE) - file.store_line(to_json(to_save)) - file.close() - emit_signal("settings_saved") - - if DEBUG_SETTINGS: - print("Settings saved to file") - - -func _save_sub_setting(val, def): - if has_flag(def, "no_save"): - return null - - match def.type: - "vector2": - return [val.x, val.y] - "vector3": - return [val.x, val.y, val.z] - "quat": - return [val.x, val.y, val.z, val.w] - "dict": - var resize = has_flag(def, "resize") - var _s = {} - for key in val: - var subdef = def.definition if resize else def.definition[key] - var v = _save_sub_setting(val[key], subdef) - if v != null: - _s[key] = v - return _s - _: - return val - - -func load_settings() -> void: - var file = File.new() - - if not file.file_exists(SETTINGS_PATH): - if DEBUG_SETTINGS: - print("No settings file exists, using defaults") - return - - file.open(SETTINGS_PATH, File.READ) - var new_settings = parse_json(file.get_as_text()) - file.close() - - for key in new_settings: - s[key] = _load_sub_setting(new_settings[key], SETTINGS_DEF[key]) - - emit_signal("settings_loaded") - if DEBUG_SETTINGS: - print("Loaded settings from file") -# print(s) - - -func _load_sub_setting(val, def): - match def.type: - "vector2": - return Vector2(val[0], val[1]) - "vector3": - return Vector3(val[0], val[1], val[2]) - "quat": - return Quat(val[0], val[1], val[2], val[3]) - "dict": - var _s = {} - var resize = has_flag(def, "resize") - for key in val: - var subdef = def.definition if resize else def.definition[key] - _s[key] = _load_sub_setting(val[key], subdef) - return _s - _: - return val - - -func has_flag(def, flag): - return def.has("flags") and flag in def.flags - - -func _exit_tree() -> void: - # save on quit - save_settings() - - -func _on_AutosaveTimer_timeout() -> void: - # auto saves every 5 minutes, saving should also be done manually - save_settings() diff --git a/ovr-utils/addons/settings-manager/settings_manager.gd b/ovr-utils/addons/settings-manager/settings_manager.gd index 9b2e63b..d1ad0e5 100644 --- a/ovr-utils/addons/settings-manager/settings_manager.gd +++ b/ovr-utils/addons/settings-manager/settings_manager.gd @@ -1,10 +1,133 @@ -tool -extends EditorPlugin +extends Node + +signal settings_saved +signal settings_loaded # emitted when settings are loaded from file, needs to be connected in _init() +signal settings_changed # has to be manually called by the modifying script + +var DEBUG_SETTINGS = false +var SETTINGS_PATH = "user://overlay_data.json" +const SETTINGS_DEF = preload("res://addons/settings-manager/settings_definition.gd").DEF + +var s: Dictionary = {} +var _saved_hash: int + +func _ready() -> void: + _init_settings() + load_settings() + force_save() -func _enter_tree() -> void: - add_autoload_singleton("Settings", "res://addons/settings-manager/Settings.tscn") +func _on_SaveTimer_timeout() -> void: + if s.hash() != _saved_hash: + force_save() + + +func _init_settings() -> void: + for key in SETTINGS_DEF: + s[key] = _init_sub_setting(SETTINGS_DEF[key]) + if DEBUG_SETTINGS: + print("Default settings: ", s) + + +func _init_sub_setting(def): + match def.type: + "dict": + if has_flag(def, "resize"): + return def.default + var _s = {} + for key in def.definition: + _s[key] = _init_sub_setting(def.definition[key]) + return _s + _: + return def.default + + +func force_save(): + var to_save = {} + for key in s: + var val = _save_sub_setting(s[key], SETTINGS_DEF[key]) + if val != null: + to_save[key] = val + + var file = File.new() + file.open(SETTINGS_PATH, File.WRITE) + file.store_line(to_json(to_save)) + file.close() + emit_signal("settings_saved") + + _saved_hash = s.hash() + + if DEBUG_SETTINGS: + print("Settings saved to file") + + +func _save_sub_setting(val, def): + if has_flag(def, "no_save"): + return null + + match def.type: + "vector2": + return [val.x, val.y] + "vector3": + return [val.x, val.y, val.z] + "quat": + return [val.x, val.y, val.z, val.w] + "dict": + var resize = has_flag(def, "resize") + var _s = {} + for key in val: + var subdef = def.definition if resize else def.definition[key] + var v = _save_sub_setting(val[key], subdef) + if v != null: + _s[key] = v + return _s + _: + return val + + +func load_settings() -> void: + var file = File.new() + + if not file.file_exists(SETTINGS_PATH): + if DEBUG_SETTINGS: + print("No settings file exists, using defaults") + return + + file.open(SETTINGS_PATH, File.READ) + var new_settings = parse_json(file.get_as_text()) + file.close() + + for key in new_settings: + s[key] = _load_sub_setting(new_settings[key], SETTINGS_DEF[key]) + + emit_signal("settings_loaded") + if DEBUG_SETTINGS: + print("Loaded settings from file") +# print(s) + + +func _load_sub_setting(val, def): + match def.type: + "vector2": + return Vector2(val[0], val[1]) + "vector3": + return Vector3(val[0], val[1], val[2]) + "quat": + return Quat(val[0], val[1], val[2], val[3]) + "dict": + var _s = {} + var resize = has_flag(def, "resize") + for key in val: + var subdef = def.definition if resize else def.definition[key] + _s[key] = _load_sub_setting(val[key], subdef) + return _s + _: + return val + + +func has_flag(def, flag): + return def.has("flags") and flag in def.flags func _exit_tree() -> void: - remove_autoload_singleton("Settings") + force_save() diff --git a/ovr-utils/addons/settings-manager/settings_plugin.gd b/ovr-utils/addons/settings-manager/settings_plugin.gd new file mode 100644 index 0000000..9b2e63b --- /dev/null +++ b/ovr-utils/addons/settings-manager/settings_plugin.gd @@ -0,0 +1,10 @@ +tool +extends EditorPlugin + + +func _enter_tree() -> void: + add_autoload_singleton("Settings", "res://addons/settings-manager/Settings.tscn") + + +func _exit_tree() -> void: + remove_autoload_singleton("Settings") diff --git a/ovr-utils/overlay_manager.gd b/ovr-utils/overlay_manager.gd index 46575bc..998dbe3 100644 --- a/ovr-utils/overlay_manager.gd +++ b/ovr-utils/overlay_manager.gd @@ -48,4 +48,3 @@ func remove_overlay(name): to_remove.queue_free() emit_signal("removed_overlay", name) Settings.s.overlays.erase(name) - Settings.save_settings() diff --git a/ovr-utils/overlay_scripts/main_menu.gd b/ovr-utils/overlay_scripts/main_menu.gd index d10b4dc..2a214f1 100644 --- a/ovr-utils/overlay_scripts/main_menu.gd +++ b/ovr-utils/overlay_scripts/main_menu.gd @@ -38,5 +38,5 @@ func _on_QuitToggle_toggled(state: bool) -> void: func _on_Quit_pressed() -> void: - Settings.save_settings() + Settings.force_save() get_tree().quit()