Skip to content
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

feat: better serialization #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions addons/panku_console/common/config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
3 changes: 3 additions & 0 deletions addons/panku_console/common/dict_res.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class_name PankuConsoleSimpleRes
extends Resource
@export var data := {}
16 changes: 4 additions & 12 deletions addons/panku_console/common/lynx_window2/lynx_windows_manager_2.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
20 changes: 6 additions & 14 deletions addons/panku_console/common/panku_module.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 2 additions & 0 deletions addons/panku_console/console.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
80 changes: 0 additions & 80 deletions addons/panku_console/default_panku_config.cfg

This file was deleted.

32 changes: 32 additions & 0 deletions addons/panku_console/default_panku_config.tres
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 2 additions & 10 deletions addons/panku_console/modules/history_manager/exp_history.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion addons/panku_console/modules/history_manager/module.gd
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
2 changes: 1 addition & 1 deletion addons/panku_console/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

Expand All @@ -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