-
Notifications
You must be signed in to change notification settings - Fork 47
misc: improved serialization #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # Godot 4+ specific ignores | ||
| .godot/ | ||
|
|
||
| # misc | ||
| export/ | ||
|
|
||
| test/ | ||
| test/ | ||
| .vscode/ | ||
| addons/lua-gdextension/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,12 @@ const OPTIONS = { | |
| CUSTOM_DEFAULT_CONFIG = 'custom_default_config', | ||
| } | ||
|
|
||
| const INITIAL_DEFAULT_CONFIG_FILE_PATH = "res://addons/panku_console/default_panku_config.cfg" | ||
| const USER_CONFIG_FILE_PATH = "user://panku_config.cfg" | ||
| const INITIAL_DEFAULT_CONFIG_FILE_PATH = "res://addons/panku_console/default_panku_config.tres" | ||
| const USER_CONFIG_FILE_PATH = "user://panku_config.tres" | ||
| const SAVE_DELAY := 0.2 | ||
|
|
||
| static var _cfg:Dictionary = {} | ||
| static var _timer:SceneTreeTimer | ||
|
|
||
| # Get custom config file path from project settings | ||
| static func get_custom_default_config_path() -> String: | ||
|
|
@@ -26,35 +30,49 @@ static func panku_option(option: String) -> String: | |
|
|
||
|
|
||
| # load config from file, always return a dictionary | ||
| static func _get_config(file_path:String) -> Dictionary: | ||
| static func _load_config_file(file_path:String) -> Dictionary: | ||
| if FileAccess.file_exists(file_path): | ||
| var file = FileAccess.open(file_path, FileAccess.READ) | ||
| var content := file.get_as_text() | ||
| var config:Dictionary = str_to_var(content) | ||
| if config: return config | ||
| var res := load(file_path) as PankuConsoleSimpleRes | ||
| if res == null: return {} | ||
| return res.data | ||
| return {} | ||
|
|
||
| # save user config to file | ||
| static func set_config(config:Dictionary): | ||
| var file = FileAccess.open(USER_CONFIG_FILE_PATH, FileAccess.WRITE) | ||
| var content = var_to_str(config) | ||
| file.store_string(content) | ||
| # save user config to file with a time delay | ||
| static func save_config_with_delay() -> void: | ||
| if _timer: _timer.timeout.disconnect(save_config_immediately) | ||
| _timer = (Engine.get_main_loop() as SceneTree).create_timer(SAVE_DELAY, true, false, true) | ||
| _timer.timeout.connect(save_config_immediately) | ||
|
|
||
| static func save_config_immediately() -> void: | ||
| var res := PankuConsoleSimpleRes.new() | ||
| res.data = _cfg | ||
| var status = ResourceSaver.save(res, USER_CONFIG_FILE_PATH) | ||
| if status != OK: push_error("Failed to save panku config.") | ||
| # print("Saved to ", USER_CONFIG_FILE_PATH) | ||
|
|
||
| # get config, if user config exists, return user config, otherwise return default config configured by plugin user | ||
| static func get_config() -> Dictionary: | ||
| var user_config:Dictionary = _get_config(USER_CONFIG_FILE_PATH) | ||
| static func _get_config() -> Dictionary: | ||
| if not _cfg.is_empty(): return _cfg | ||
| var user_config:Dictionary = _load_config_file(USER_CONFIG_FILE_PATH) | ||
| if not user_config.is_empty(): | ||
| return user_config | ||
| _cfg = user_config | ||
| # if no user config, return default config, which is read-only | ||
| if is_custom_default_config_exists(): | ||
| return _get_config(get_custom_default_config_path()) | ||
|
|
||
| return _get_config(INITIAL_DEFAULT_CONFIG_FILE_PATH) | ||
| elif is_custom_default_config_exists(): | ||
| _cfg = _load_config_file(get_custom_default_config_path()) | ||
| else: | ||
| _cfg = _load_config_file(INITIAL_DEFAULT_CONFIG_FILE_PATH) | ||
| return _cfg | ||
|
|
||
| static func get_value(key:String, default:Variant) -> Variant: | ||
| return get_config().get(key, default) | ||
| static func _get_key(keys:Array[String]) -> String: | ||
| return ".".join(keys) | ||
|
|
||
| static func get_value(keys:Array[String], default:Variant = null) -> Variant: | ||
| var key := _get_key(keys) | ||
| var cfg := _get_config() | ||
| return cfg.get(key, default) | ||
|
|
||
| static func set_value(key:String, val:Variant) -> void: | ||
| var config = _get_config(USER_CONFIG_FILE_PATH) | ||
| config[key] = val | ||
| set_config(config) | ||
| static func set_value(keys:Array[String], val:Variant) -> void: | ||
| var key := _get_key(keys) | ||
|
Comment on lines
+66
to
+75
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cause many errors for me, since every Seems it works fine with key-value args static func get_value(key: String, default:Variant = null) -> Variant:
var cfg := _get_config()
return cfg.get(key, default)
static func set_value(key: String, val:Variant) -> void:
var cfg := _get_config()
cfg[key] = val
save_config_with_delay() |
||
| var cfg := _get_config() | ||
| cfg[key] = val | ||
| save_config_with_delay() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| [gd_resource type="Resource" script_class="PankuConsoleSimpleRes" load_steps=2 format=3 uid="uid://carjqi7cx353a"] | ||
|
|
||
| [ext_resource type="Script" path="res://addons/panku_console/dict_res.gd" id="1_8qd6x"] | ||
|
|
||
| [resource] | ||
| script = ExtResource("1_8qd6x") | ||
| data = { | ||
| "enable_os_window": false, | ||
| "engine_tools": { | ||
| "time_scale": "1" | ||
| }, | ||
| "engine_tools.time_scale": "1", | ||
|
Comment on lines
+9
to
+12
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is not it some kind of duplication? Which dictionary structure was originally planned: nested or plain with joined keys? |
||
| "exp_history": [[false, false, "print('Panku Console Loaded!')"]], | ||
| "expression_monitor.monitor_data": [{ | ||
| "expressions": [], | ||
| "group_name": "default group" | ||
| }], | ||
| "expression_monitor.window_position": Vector2(433.5, 415), | ||
| "expression_monitor.window_size": Vector2(413, 305), | ||
| "expression_monitor.window_visibility": false, | ||
| "general_settings": { | ||
| "window_blur_effect": true, | ||
| "window_position": Vector2(213.215, 75.1716), | ||
| "window_size": Vector2(709.046, 563.371), | ||
| "window_visibility": true | ||
| }, | ||
| "general_settings.global_font_size": 14, | ||
| "general_settings.window_blur_effect": true, | ||
| "general_settings.window_position": Vector2(584.001, 161.779), | ||
| "general_settings.window_size": Vector2(555.042, 397.87), | ||
| "general_settings.window_visibility": true, | ||
| "history_manager.window_position": Vector2(867, 415), | ||
| "history_manager.window_size": Vector2(413, 305), | ||
| "history_manager.window_visibility": false, | ||
| "interactive_shell": { | ||
| "pause_if_popup": true, | ||
| "show_side_menu": true, | ||
| "unified_visibility": false | ||
| }, | ||
| "interactive_shell.gui_mode": 0, | ||
| "interactive_shell.histories": [], | ||
| "interactive_shell.pause_if_popup": true, | ||
| "interactive_shell.show_side_menu": true, | ||
| "interactive_shell.unified_visibility": true, | ||
| "interactive_shell.window_position": Vector2(255.736, 71.0109), | ||
| "interactive_shell.window_size": Vector2(615.07, 443.081), | ||
| "interactive_shell.window_visibility": true, | ||
| "keyboard_shortcuts": { | ||
| "window_position": Vector2(617.943, 228.155), | ||
| "window_size": Vector2(412.999, 305) | ||
| }, | ||
| "keyboard_shortcuts.key_mapper": [], | ||
| "keyboard_shortcuts.window_position": Vector2(867, 0), | ||
| "keyboard_shortcuts.window_size": Vector2(413, 305), | ||
| "keyboard_shortcuts.window_visibility": false, | ||
| "native_logger": { | ||
| "screen_overlay": 0, | ||
| "screen_overlay_alpha": 1.0, | ||
| "screen_overlay_font_shadow": true, | ||
| "screen_overlay_override_font_size": 14, | ||
| "show_timestamp": true | ||
| }, | ||
| "native_logger.logger_tags": ["[error]", "[warning]"], | ||
| "native_logger.screen_overlay_alpha": 0.13, | ||
| "native_logger.screen_overlay_font_shadow": true, | ||
| "native_logger.screen_overlay_override_font_size": 9, | ||
| "native_logger.show_timestamp": true, | ||
| "native_logger.window_position": Vector2(314.725, 167.815), | ||
| "native_logger.window_size": Vector2(481.089, 317.56), | ||
| "native_logger.window_visibility": false, | ||
| "os_window_bg_color": Color(0.129412, 0.14902, 0.180392, 1), | ||
| "variable_tracker": { | ||
| "tracking_delay": 0.5, | ||
| "use_last_as_current": true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class_name PankuConsoleSimpleRes | ||
| extends Resource | ||
| @export var data := {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we need to change file extension hint here too.
Also, do we need to keep
default_panku_config.cfgfile in project or may be we can delete it within this pull request?