Skip to content

Commit a6956d3

Browse files
committed
misc: improved serialization
1 parent be716a5 commit a6956d3

File tree

11 files changed

+141
-52
lines changed

11 files changed

+141
-52
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Godot 4+ specific ignores
22
.godot/
3-
3+
# misc
44
export/
5-
6-
test/
5+
test/
6+
.vscode/
7+
addons/lua-gdextension/

addons/panku_console/common/config.gd

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ const OPTIONS = {
88
CUSTOM_DEFAULT_CONFIG = 'custom_default_config',
99
}
1010

11-
const INITIAL_DEFAULT_CONFIG_FILE_PATH = "res://addons/panku_console/default_panku_config.cfg"
12-
const USER_CONFIG_FILE_PATH = "user://panku_config.cfg"
11+
const INITIAL_DEFAULT_CONFIG_FILE_PATH = "res://addons/panku_console/default_panku_config.tres"
12+
const USER_CONFIG_FILE_PATH = "user://panku_config.tres"
13+
const SAVE_DELAY := 0.2
14+
15+
static var _cfg:Dictionary = {}
16+
static var _timer:SceneTreeTimer
1317

1418
# Get custom config file path from project settings
1519
static func get_custom_default_config_path() -> String:
@@ -26,35 +30,49 @@ static func panku_option(option: String) -> String:
2630

2731

2832
# load config from file, always return a dictionary
29-
static func _get_config(file_path:String) -> Dictionary:
33+
static func _load_config_file(file_path:String) -> Dictionary:
3034
if FileAccess.file_exists(file_path):
31-
var file = FileAccess.open(file_path, FileAccess.READ)
32-
var content := file.get_as_text()
33-
var config:Dictionary = str_to_var(content)
34-
if config: return config
35+
var res := load(file_path) as PankuConsoleSimpleRes
36+
if res == null: return {}
37+
return res.data
3538
return {}
3639

37-
# save user config to file
38-
static func set_config(config:Dictionary):
39-
var file = FileAccess.open(USER_CONFIG_FILE_PATH, FileAccess.WRITE)
40-
var content = var_to_str(config)
41-
file.store_string(content)
40+
# save user config to file with a time delay
41+
static func save_config_with_delay() -> void:
42+
if _timer: _timer.timeout.disconnect(save_config_immediately)
43+
_timer = (Engine.get_main_loop() as SceneTree).create_timer(SAVE_DELAY, true, false, true)
44+
_timer.timeout.connect(save_config_immediately)
45+
46+
static func save_config_immediately() -> void:
47+
var res := PankuConsoleSimpleRes.new()
48+
res.data = _cfg
49+
var status = ResourceSaver.save(res, USER_CONFIG_FILE_PATH)
50+
if status != OK: push_error("Failed to save panku config.")
51+
# print("Saved to ", USER_CONFIG_FILE_PATH)
4252

4353
# get config, if user config exists, return user config, otherwise return default config configured by plugin user
44-
static func get_config() -> Dictionary:
45-
var user_config:Dictionary = _get_config(USER_CONFIG_FILE_PATH)
54+
static func _get_config() -> Dictionary:
55+
if not _cfg.is_empty(): return _cfg
56+
var user_config:Dictionary = _load_config_file(USER_CONFIG_FILE_PATH)
4657
if not user_config.is_empty():
47-
return user_config
58+
_cfg = user_config
4859
# if no user config, return default config, which is read-only
49-
if is_custom_default_config_exists():
50-
return _get_config(get_custom_default_config_path())
51-
52-
return _get_config(INITIAL_DEFAULT_CONFIG_FILE_PATH)
60+
elif is_custom_default_config_exists():
61+
_cfg = _load_config_file(get_custom_default_config_path())
62+
else:
63+
_cfg = _load_config_file(INITIAL_DEFAULT_CONFIG_FILE_PATH)
64+
return _cfg
5365

54-
static func get_value(key:String, default:Variant) -> Variant:
55-
return get_config().get(key, default)
66+
static func _get_key(keys:Array[String]) -> String:
67+
return ".".join(keys)
68+
69+
static func get_value(keys:Array[String], default:Variant = null) -> Variant:
70+
var key := _get_key(keys)
71+
var cfg := _get_config()
72+
return cfg.get(key, default)
5673

57-
static func set_value(key:String, val:Variant) -> void:
58-
var config = _get_config(USER_CONFIG_FILE_PATH)
59-
config[key] = val
60-
set_config(config)
74+
static func set_value(keys:Array[String], val:Variant) -> void:
75+
var key := _get_key(keys)
76+
var cfg := _get_config()
77+
cfg[key] = val
78+
save_config_with_delay()

addons/panku_console/common/lynx_window2/lynx_windows_manager_2.gd

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ func get_os_window_bg_color() -> Color:
6464
return os_window_bg_color
6565

6666
func save_data():
67-
var cfg = PankuConfig.get_config()
68-
cfg[CFG_ENABLE_OS_WINDOW] = os_popup_btn_enabled
69-
cfg[CFG_OS_WINDOW_BGCOLOR] = os_window_bg_color
70-
PankuConfig.set_config(cfg)
67+
PankuConfig.set_value(CFG_ENABLE_OS_WINDOW, os_popup_btn_enabled)
68+
PankuConfig.set_value(CFG_OS_WINDOW_BGCOLOR, os_window_bg_color)
7169

7270
func load_data():
73-
var cfg = PankuConfig.get_config()
74-
enable_os_popup_btns(cfg.get(CFG_ENABLE_OS_WINDOW, false))
75-
set_os_window_bg_color(cfg.get(CFG_OS_WINDOW_BGCOLOR, Color("#2b2e32")))
71+
enable_os_popup_btns(PankuConfig.get_value(CFG_ENABLE_OS_WINDOW, false))
72+
set_os_window_bg_color(PankuConfig.get_value(CFG_OS_WINDOW_BGCOLOR, Color("#2b2e32")))
7673

7774
func _notification(what):
7875
#quit event

addons/panku_console/common/panku_module.gd

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,19 @@ func update_module(delta:float):
2424
pass
2525

2626
func save_module_data(key:String, value:Variant):
27-
var cfg:Dictionary = PankuConfig.get_config()
2827
var module_name:String = get_module_name()
29-
if !cfg.has(module_name):
30-
cfg[module_name] = {}
31-
cfg[module_name][key] = value
32-
PankuConfig.set_config(cfg)
28+
var module_data:Dictionary = PankuConfig.get_value(module_name, {})
29+
module_data[key] = value
30+
PankuConfig.set_value(module_name, module_data)
3331

3432
func load_module_data(key:String, default_value:Variant = null) -> Variant:
35-
var cfg:Dictionary = PankuConfig.get_config()
3633
var module_name:String = get_module_name()
37-
var module_data = cfg.get(module_name, {})
34+
var module_data = PankuConfig.get_value(module_name, {})
3835
return module_data.get(key, default_value)
3936

4037
func has_module_data(key:String) -> bool:
41-
var cfg:Dictionary = PankuConfig.get_config()
4238
var module_name:String = get_module_name()
43-
var module_data = cfg.get(module_name, {})
39+
var module_data = PankuConfig.get_value(module_name, {})
4440
return module_data.has(key)
4541

4642
func load_window_data(window:PankuLynxWindow):

addons/panku_console/console.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ func _notification(what):
7676
# quit event
7777
if what == NOTIFICATION_WM_CLOSE_REQUEST:
7878
module_manager.quit_modules()
79+
PankuConfig.save_config_immediately()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[gd_resource type="Resource" script_class="PankuConsoleSimpleRes" load_steps=2 format=3 uid="uid://carjqi7cx353a"]
2+
3+
[ext_resource type="Script" path="res://addons/panku_console/dict_res.gd" id="1_8qd6x"]
4+
5+
[resource]
6+
script = ExtResource("1_8qd6x")
7+
data = {
8+
"enable_os_window": false,
9+
"engine_tools": {
10+
"time_scale": "1"
11+
},
12+
"engine_tools.time_scale": "1",
13+
"exp_history": [[false, false, "print('Panku Console Loaded!')"]],
14+
"expression_monitor.monitor_data": [{
15+
"expressions": [],
16+
"group_name": "default group"
17+
}],
18+
"expression_monitor.window_position": Vector2(433.5, 415),
19+
"expression_monitor.window_size": Vector2(413, 305),
20+
"expression_monitor.window_visibility": false,
21+
"general_settings": {
22+
"window_blur_effect": true,
23+
"window_position": Vector2(213.215, 75.1716),
24+
"window_size": Vector2(709.046, 563.371),
25+
"window_visibility": true
26+
},
27+
"general_settings.global_font_size": 14,
28+
"general_settings.window_blur_effect": true,
29+
"general_settings.window_position": Vector2(584.001, 161.779),
30+
"general_settings.window_size": Vector2(555.042, 397.87),
31+
"general_settings.window_visibility": true,
32+
"history_manager.window_position": Vector2(867, 415),
33+
"history_manager.window_size": Vector2(413, 305),
34+
"history_manager.window_visibility": false,
35+
"interactive_shell": {
36+
"pause_if_popup": true,
37+
"show_side_menu": true,
38+
"unified_visibility": false
39+
},
40+
"interactive_shell.gui_mode": 0,
41+
"interactive_shell.histories": [],
42+
"interactive_shell.pause_if_popup": true,
43+
"interactive_shell.show_side_menu": true,
44+
"interactive_shell.unified_visibility": true,
45+
"interactive_shell.window_position": Vector2(255.736, 71.0109),
46+
"interactive_shell.window_size": Vector2(615.07, 443.081),
47+
"interactive_shell.window_visibility": true,
48+
"keyboard_shortcuts": {
49+
"window_position": Vector2(617.943, 228.155),
50+
"window_size": Vector2(412.999, 305)
51+
},
52+
"keyboard_shortcuts.key_mapper": [],
53+
"keyboard_shortcuts.window_position": Vector2(867, 0),
54+
"keyboard_shortcuts.window_size": Vector2(413, 305),
55+
"keyboard_shortcuts.window_visibility": false,
56+
"native_logger": {
57+
"screen_overlay": 0,
58+
"screen_overlay_alpha": 1.0,
59+
"screen_overlay_font_shadow": true,
60+
"screen_overlay_override_font_size": 14,
61+
"show_timestamp": true
62+
},
63+
"native_logger.logger_tags": ["[error]", "[warning]"],
64+
"native_logger.screen_overlay_alpha": 0.13,
65+
"native_logger.screen_overlay_font_shadow": true,
66+
"native_logger.screen_overlay_override_font_size": 9,
67+
"native_logger.show_timestamp": true,
68+
"native_logger.window_position": Vector2(314.725, 167.815),
69+
"native_logger.window_size": Vector2(481.089, 317.56),
70+
"native_logger.window_visibility": false,
71+
"os_window_bg_color": Color(0.129412, 0.14902, 0.180392, 1),
72+
"variable_tracker": {
73+
"tracking_delay": 0.5,
74+
"use_last_as_current": true
75+
}
76+
}

addons/panku_console/dict_res.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class_name PankuConsoleSimpleRes
2+
extends Resource
3+
@export var data := {}

addons/panku_console/modules/data_controller/exporter/exporter_2.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func init_data():
138138
if !is_instance_valid(obj):
139139
return
140140
if prop_name in obj:
141+
# used to auto save settings panel data
141142
if obj.has_method("update_setting"):
142143
obj.update_setting(prop_name, val)
143144
else:

addons/panku_console/modules/engine_tools/opt.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func toggle_2d_debug_draw():
2727
func reload_current_scene():
2828
_module.reload_current_scene()
2929

30-
@export_range(0.1, 2.0) var time_scale := 1.0:
30+
@export_range(0.1, 10.0, 0.001) var time_scale := 1.0:
3131
set(v):
3232
time_scale = v
3333
_module.set_time_scale(time_scale)

addons/panku_console/modules/history_manager/exp_history.gd

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,10 @@ func remove_selected():
185185
reload()
186186

187187
func load_data():
188-
#get saved data from cfg
189-
var cfg = PankuConfig.get_config()
190-
item_data = cfg.get(CFG_EXP_HISTORY, [])
188+
item_data = PankuConfig.get_value(CFG_EXP_HISTORY, [])
191189

192190
func save_data():
193-
var cfg = PankuConfig.get_config()
194-
cfg[CFG_EXP_HISTORY] = item_data
195-
PankuConfig.set_config(cfg)
191+
PankuConfig.set_value(CFG_EXP_HISTORY, item_data)
196192

197193
func _notification(what):
198194
if what == NOTIFICATION_WM_CLOSE_REQUEST:

0 commit comments

Comments
 (0)