From 5f460e5daefc21dbdf11e5f558eb46e34ebaa212 Mon Sep 17 00:00:00 2001 From: Feo Wu Date: Wed, 11 Sep 2024 11:14:39 +0800 Subject: [PATCH] feat: better serialization --- addons/panku_console/common/config.gd | 61 ++++++++------ addons/panku_console/common/dict_res.gd | 3 + .../lynx_window2/lynx_windows_manager_2.gd | 16 +--- addons/panku_console/common/panku_module.gd | 20 ++--- addons/panku_console/console.gd | 2 + addons/panku_console/default_panku_config.cfg | 80 ------------------- .../panku_console/default_panku_config.tres | 32 ++++++++ .../data_controller/exporter/exporter_2.gd | 1 + .../modules/history_manager/exp_history.gd | 12 +-- .../modules/history_manager/module.gd | 4 +- addons/panku_console/plugin.gd | 2 +- project.godot | 6 +- 12 files changed, 95 insertions(+), 144 deletions(-) create mode 100644 addons/panku_console/common/dict_res.gd delete mode 100644 addons/panku_console/default_panku_config.cfg create mode 100644 addons/panku_console/default_panku_config.tres diff --git a/addons/panku_console/common/config.gd b/addons/panku_console/common/config.gd index da77124..80fb404 100644 --- a/addons/panku_console/common/config.gd +++ b/addons/panku_console/common/config.gd @@ -8,8 +8,11 @@ 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" + +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 +29,45 @@ 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(delay := 0.2) -> void: + if _timer: _timer.timeout.disconnect(save_config_immediately) + _timer = (Engine.get_main_loop() as SceneTree).create_timer(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_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 config = _get_config(USER_CONFIG_FILE_PATH) - config[key] = val - set_config(config) + var cfg := _get_config() + cfg[key] = val + # print("set_value: ", key, val) + save_config_with_delay() diff --git a/addons/panku_console/common/dict_res.gd b/addons/panku_console/common/dict_res.gd new file mode 100644 index 0000000..20d5150 --- /dev/null +++ b/addons/panku_console/common/dict_res.gd @@ -0,0 +1,3 @@ +class_name PankuConsoleSimpleRes +extends Resource +@export var data := {} diff --git a/addons/panku_console/common/lynx_window2/lynx_windows_manager_2.gd b/addons/panku_console/common/lynx_window2/lynx_windows_manager_2.gd index 3298c43..9c803ea 100644 --- a/addons/panku_console/common/lynx_window2/lynx_windows_manager_2.gd +++ b/addons/panku_console/common/lynx_window2/lynx_windows_manager_2.gd @@ -64,17 +64,9 @@ func get_os_window_bg_color() -> Color: return os_window_bg_color func save_data(): - var cfg = PankuConfig.get_config() - cfg[CFG_ENABLE_OS_WINDOW] = os_popup_btn_enabled - cfg[CFG_OS_WINDOW_BGCOLOR] = os_window_bg_color - PankuConfig.set_config(cfg) + PankuConfig.set_value(CFG_ENABLE_OS_WINDOW, os_popup_btn_enabled) + PankuConfig.set_value(CFG_OS_WINDOW_BGCOLOR, os_window_bg_color) func load_data(): - var cfg = PankuConfig.get_config() - enable_os_popup_btns(cfg.get(CFG_ENABLE_OS_WINDOW, false)) - set_os_window_bg_color(cfg.get(CFG_OS_WINDOW_BGCOLOR, Color("#2b2e32"))) - -func _notification(what): - #quit event - if what == NOTIFICATION_WM_CLOSE_REQUEST: - save_data() + enable_os_popup_btns(PankuConfig.get_value(CFG_ENABLE_OS_WINDOW, false)) + set_os_window_bg_color(PankuConfig.get_value(CFG_OS_WINDOW_BGCOLOR, Color("#2b2e32"))) diff --git a/addons/panku_console/common/panku_module.gd b/addons/panku_console/common/panku_module.gd index 4ef3188..4d64bf0 100644 --- a/addons/panku_console/common/panku_module.gd +++ b/addons/panku_console/common/panku_module.gd @@ -23,25 +23,17 @@ func quit_module(): func update_module(delta:float): pass +func _get_module_key(key:String) -> String: + return ".".join([get_module_name(), key]) + func save_module_data(key:String, value:Variant): - var cfg:Dictionary = PankuConfig.get_config() - var module_name:String = get_module_name() - if !cfg.has(module_name): - cfg[module_name] = {} - cfg[module_name][key] = value - PankuConfig.set_config(cfg) + PankuConfig.set_value(_get_module_key(key), value) func load_module_data(key:String, default_value:Variant = null) -> Variant: - var cfg:Dictionary = PankuConfig.get_config() - var module_name:String = get_module_name() - var module_data = cfg.get(module_name, {}) - return module_data.get(key, default_value) + return PankuConfig.get_value(_get_module_key(key), default_value) func has_module_data(key:String) -> bool: - var cfg:Dictionary = PankuConfig.get_config() - var module_name:String = get_module_name() - var module_data = cfg.get(module_name, {}) - return module_data.has(key) + return PankuConfig.get_value(_get_module_key(key)) == null func load_window_data(window:PankuLynxWindow): window.position = load_module_data("window_position", window.get_layout_position([ diff --git a/addons/panku_console/console.gd b/addons/panku_console/console.gd index cf4ce19..da9ecdd 100644 --- a/addons/panku_console/console.gd +++ b/addons/panku_console/console.gd @@ -76,3 +76,5 @@ func _notification(what): # quit event if what == NOTIFICATION_WM_CLOSE_REQUEST: module_manager.quit_modules() + windows_manager.save_data() + PankuConfig.save_config_immediately() \ No newline at end of file diff --git a/addons/panku_console/default_panku_config.cfg b/addons/panku_console/default_panku_config.cfg deleted file mode 100644 index e5deb54..0000000 --- a/addons/panku_console/default_panku_config.cfg +++ /dev/null @@ -1,80 +0,0 @@ -{ -"enable_os_window": false, -"engine_tools": { -"time_scale": 1.0 -}, -"exp_history": [], -"expression_monitor": { -"monitor_data": [{ -"expressions": ["engine_tools.get_performance_info()"], -"group_name": "default group" -}], -"window_position": Vector2(0, 49), -"window_size": Vector2(85, 74), -"window_visibility": false -}, -"general_settings": { -"enable_os_window": false, -"lynx_window_base_color": Color(0, 0.0470588, 0.0941176, 0.501961), -"lynx_window_blur_effect": true, -"lynx_window_enable_os_window": false, -"lynx_window_os_window_bg_color": Color(0, 0, 0, 0.658824), -"os_window_bg_color": Color(0, 0, 0, 0.992157), -"window_blur_effect": true, -"window_position": Vector2(429.546, 94.1911), -"window_size": Vector2(512.568, 478.128), -"window_visibility": true -}, -"history_manager": { -"window_position": Vector2(317.728, 138.82), -"window_size": Vector2(411.987, 339.537), -"window_visibility": false -}, -"interactive_shell": { -"gui_mode": 0, -"histories": [], -"init_expr": "", -"output_font_size": 14.0, -"pause_if_input": false, -"pause_if_popup": false, -"show_side_menu": true, -"unified_visibility": false, -"unified_window_visibility": false, -"window_position": Vector2(427.419, 75.3913), -"window_size": Vector2(510.736, 410.437), -"window_visibility": true -}, -"keyboard_shortcuts": { -"key_mapper": [], -"window_position": Vector2(0, 49), -"window_size": Vector2(85, 74), -"window_visibility": false -}, -"native_logger": { -"font_size": 17.0, -"logger_tags": ["[error]", "[warning]", "[info]"], -"screen_overlay": 0, -"screen_overlay_alpha": 0.44, -"screen_overlay_font_shadow": true, -"screen_overlay_font_size": 20.0, -"show_timestamp": true, -"window_position": Vector2(284.123, 124.547), -"window_size": Vector2(483.998, 379.028), -"window_visibility": false -}, -"os_window_bg_color": Color(0, 0, 0, 0.992157), -"os_window_bgcolor": Color(0, 0, 0, 0.658824), -"snake": { -"leader_board": [{ -"score": 40, -"timestamp": "2024-01-29T16:00:00" -}, { -"score": 20, -"timestamp": "2024-01-29T16:01:14" -}] -}, -"variable_tracker": { -"tracking_delay": 0.5, -"use_last_as_current": true -} -} diff --git a/addons/panku_console/default_panku_config.tres b/addons/panku_console/default_panku_config.tres new file mode 100644 index 0000000..9092767 --- /dev/null +++ b/addons/panku_console/default_panku_config.tres @@ -0,0 +1,32 @@ +[gd_resource type="Resource" script_class="PankuConsoleSimpleRes" load_steps=2 format=3] + +[ext_resource type="Script" path="res://addons/panku_console/common/dict_res.gd" id="1_xmc1q"] + +[resource] +script = ExtResource("1_xmc1q") +data = { +"enable_os_window": false, +"exp_history": [[false, false, "print('Panku Console Loaded!')"]], +"expression_monitor.monitor_data": [{ +"expressions": [], +"group_name": "default group" +}], +"general_settings.global_font_size": 12, +"general_settings.os_window_bg_color": Color(0, 0, 0, 1), +"general_settings.window_base_color": Color(0, 0.0470588, 0.0941176, 0.678431), +"general_settings.window_blur_effect": true, +"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, +"keyboard_shortcuts.key_mapper": [], +"native_logger.logger_tags": ["[error]", "[warning]"], +"native_logger.screen_overlay_alpha": 0.5, +"native_logger.screen_overlay_font_shadow": true, +"native_logger.screen_overlay_override_font_size": 0, +"native_logger.show_timestamp": true, +"os_window_bg_color": Color(0, 0, 0, 1), +"variable_tracker.tracking_delay": 0.5, +"variable_tracker.use_last_as_current": true +} diff --git a/addons/panku_console/modules/data_controller/exporter/exporter_2.gd b/addons/panku_console/modules/data_controller/exporter/exporter_2.gd index 05106cd..46102f3 100644 --- a/addons/panku_console/modules/data_controller/exporter/exporter_2.gd +++ b/addons/panku_console/modules/data_controller/exporter/exporter_2.gd @@ -138,6 +138,7 @@ func init_data(): if !is_instance_valid(obj): return if prop_name in obj: + # used to auto save settings panel data if obj.has_method("update_setting"): obj.update_setting(prop_name, val) else: diff --git a/addons/panku_console/modules/history_manager/exp_history.gd b/addons/panku_console/modules/history_manager/exp_history.gd index 8d4b469..5d67b53 100644 --- a/addons/panku_console/modules/history_manager/exp_history.gd +++ b/addons/panku_console/modules/history_manager/exp_history.gd @@ -185,18 +185,10 @@ func remove_selected(): reload() func load_data(): - #get saved data from cfg - var cfg = PankuConfig.get_config() - item_data = cfg.get(CFG_EXP_HISTORY, []) + item_data = PankuConfig.get_value(CFG_EXP_HISTORY, []) func save_data(): - var cfg = PankuConfig.get_config() - cfg[CFG_EXP_HISTORY] = item_data - PankuConfig.set_config(cfg) - -func _notification(what): - if what == NOTIFICATION_WM_CLOSE_REQUEST: - save_data() + PankuConfig.set_value(CFG_EXP_HISTORY, item_data) func add_history(exp:String): #ignore consecutive same diff --git a/addons/panku_console/modules/history_manager/module.gd b/addons/panku_console/modules/history_manager/module.gd index 24a7607..0e3ebc7 100644 --- a/addons/panku_console/modules/history_manager/module.gd +++ b/addons/panku_console/modules/history_manager/module.gd @@ -1,10 +1,11 @@ class_name PankuModuleHistoryManager extends PankuModule var window:PankuLynxWindow +var ui:Control func init_module(): # setup ui - var ui = preload("./exp_history.tscn").instantiate() + ui = preload("./exp_history.tscn").instantiate() ui.console = core core.new_expression_entered.connect( func(expression, result): @@ -21,6 +22,7 @@ func init_module(): func quit_module(): super.quit_module() save_window_data(window) + ui.save_data() func open_window(): window.show_window() diff --git a/addons/panku_console/plugin.gd b/addons/panku_console/plugin.gd index 35f71ea..601c5ed 100644 --- a/addons/panku_console/plugin.gd +++ b/addons/panku_console/plugin.gd @@ -81,7 +81,7 @@ func create_setting() -> void: PankuConfig.INITIAL_DEFAULT_CONFIG_FILE_PATH, TYPE_STRING, PROPERTY_HINT_FILE, - "*.cfg" + "*.tres" ) var error:int = ProjectSettings.save() diff --git a/project.godot b/project.godot index 3d5c53d..7967f5a 100644 --- a/project.godot +++ b/project.godot @@ -15,7 +15,7 @@ config/description="**Panku Console is a feature-packed real-time debugging tool Panku Console is designed to be modular and extensible, and it is easy to add and maintain features. It is also designed to be as unobtrusive as possible, so you can use it in your project without worrying about the impact on the final product." run/main_scene="res://example/main.tscn" -config/features=PackedStringArray("4.2", "Forward Plus") +config/features=PackedStringArray("4.3", "Forward Plus") boot_splash/bg_color=Color(1, 1, 1, 1) boot_splash/image="res://example/assets/bootsplash.png" boot_splash/fullsize=false @@ -31,6 +31,7 @@ Global="*res://example/global.tscn" window/size/viewport_width=1280 window/size/viewport_height=720 window/stretch/mode="canvas_items" +window/vsync/vsync_mode=0 [editor_plugins] @@ -42,5 +43,6 @@ import/blender/enabled=false [rendering] +renderer/rendering_method="gl_compatibility" +renderer/rendering_method.mobile="gl_compatibility" environment/defaults/default_clear_color=Color(1, 1, 1, 1) -anti_aliasing/quality/msaa_2d=3