Skip to content

Commit 12e9f73

Browse files
authored
Merge pull request #217 from worron/plugin_tweaks
Refactor plugin script
2 parents 702d45c + 2efbf9c commit 12e9f73

File tree

3 files changed

+108
-62
lines changed

3 files changed

+108
-62
lines changed

addons/panku_console/common/config.gd

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,73 @@ const OPTIONS = {
1111
const INITIAL_DEFAULT_CONFIG_FILE_PATH = "res://addons/panku_console/default_panku_config.cfg"
1212
const USER_CONFIG_FILE_PATH = "user://panku_config.cfg"
1313

14+
15+
# Full option name in project settings.
16+
static func panku_option(option: String) -> String:
17+
return CONFIG_SECTION + "/" + option
18+
19+
20+
# A helper function to add custom project settings
21+
# See https://dfaction.net/handling-custom-project-settings-using-gdscript/
22+
static func add_custom_project_setting(
23+
name: String,
24+
default_value,
25+
type: int,
26+
hint: int = PROPERTY_HINT_NONE, hint_string: String = ""
27+
) -> void:
28+
if ProjectSettings.has_setting(name): return
29+
30+
var setting_info: Dictionary = {
31+
"name": name,
32+
"type": type,
33+
"hint": hint,
34+
"hint_string": hint_string
35+
}
36+
37+
ProjectSettings.set_setting(name, default_value)
38+
ProjectSettings.add_property_info(setting_info)
39+
ProjectSettings.set_initial_value(name, default_value)
40+
ProjectSettings.set_as_basic(name, true)
41+
42+
43+
static func init_all_project_settings() -> void:
44+
# Seems we can't add descriptions to custom settings now.
45+
46+
# Disable Panku Console in release builds
47+
add_custom_project_setting(
48+
panku_option(OPTIONS.DISABLE_ON_RELEASE),
49+
false,
50+
TYPE_BOOL
51+
)
52+
53+
# Path to the custom
54+
# `res://` path default config file, useful if you are going to keep panku console in release builds.
55+
add_custom_project_setting(
56+
panku_option(OPTIONS.CUSTOM_DEFAULT_CONFIG),
57+
INITIAL_DEFAULT_CONFIG_FILE_PATH,
58+
TYPE_STRING,
59+
PROPERTY_HINT_FILE,
60+
"*.cfg"
61+
)
62+
63+
# save_project_settings()
64+
65+
66+
static func clear_all_project_settings() -> void:
67+
for option in OPTIONS.values():
68+
var opt: String = panku_option(option)
69+
if ProjectSettings.has_setting(opt):
70+
ProjectSettings.clear(opt)
71+
72+
save_project_settings()
73+
74+
75+
static func save_project_settings() -> void:
76+
var error: int = ProjectSettings.save()
77+
if error != OK:
78+
push_error("Encountered error %d when saving project settings." % error)
79+
80+
1481
# Get custom config file path from project settings
1582
static func get_custom_default_config_path() -> String:
1683
return ProjectSettings.get_setting(panku_option(OPTIONS.CUSTOM_DEFAULT_CONFIG), INITIAL_DEFAULT_CONFIG_FILE_PATH)
@@ -20,11 +87,6 @@ static func is_custom_default_config_exists() -> bool:
2087
return FileAccess.file_exists(get_custom_default_config_path())
2188

2289

23-
# Full option name in project settings.
24-
static func panku_option(option: String) -> String:
25-
return CONFIG_SECTION + "/" + option
26-
27-
2890
# load config from file, always return a dictionary
2991
static func _get_config(file_path:String) -> Dictionary:
3092
if FileAccess.file_exists(file_path):
@@ -34,12 +96,14 @@ static func _get_config(file_path:String) -> Dictionary:
3496
if config: return config
3597
return {}
3698

99+
37100
# save user config to file
38101
static func set_config(config:Dictionary):
39102
var file = FileAccess.open(USER_CONFIG_FILE_PATH, FileAccess.WRITE)
40103
var content = var_to_str(config)
41104
file.store_string(content)
42105

106+
43107
# get config, if user config exists, return user config, otherwise return default config configured by plugin user
44108
static func get_config() -> Dictionary:
45109
var user_config:Dictionary = _get_config(USER_CONFIG_FILE_PATH)
@@ -51,9 +115,11 @@ static func get_config() -> Dictionary:
51115

52116
return _get_config(INITIAL_DEFAULT_CONFIG_FILE_PATH)
53117

118+
54119
static func get_value(key:String, default:Variant) -> Variant:
55120
return get_config().get(key, default)
56121

122+
57123
static func set_value(key:String, val:Variant) -> void:
58124
var config = _get_config(USER_CONFIG_FILE_PATH)
59125
config[key] = val

addons/panku_console/console.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class_name PankuConsole extends CanvasLayer
22
# `console.gd` is a global singleton that provides all modules with a common interface
33
# you can also use some of its members to interact with the console
4-
4+
55
signal interactive_shell_visibility_changed(visible:bool)
66
signal new_expression_entered(expression:String, result)
77
signal new_notification_created(bbcode:String, id:int)
@@ -36,6 +36,14 @@ func _input(event: InputEvent):
3636
func _ready():
3737
assert(get_tree().current_scene != self, "Do not run console.tscn as a scene!")
3838

39+
# Yep, seems like double check project settings in the main singleton
40+
# is the only "correct" way to work with custom project setting
41+
# https://github.com/godotengine/godot/issues/56598#issuecomment-1904100640
42+
PankuConfig.init_all_project_settings()
43+
44+
if not PankuConfig.is_custom_default_config_exists():
45+
push_warning("[Panku Console] Default config file not found. Using code-level default config.")
46+
3947
windows_manager = $LynxWindowsManager
4048
var base_instance = preload("./common/repl_base_instance.gd").new()
4149
base_instance._core = self

addons/panku_console/plugin.gd

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const SINGLETON_NAME = "Panku"
66
const SINGLETON_PATH = "res://addons/panku_console/console.tscn"
77
const SINGLETON_OPTION = "autoload/" + SINGLETON_NAME
88

9-
109
var exporter: PankuExporter
1110

11+
1212
# Custom export plugin to automatically disable console in release builds
1313
class PankuExporter extends EditorExportPlugin:
14-
const NAME = "Panku"
14+
const NAME = "PankuReleaseExporter"
1515
var owner: EditorPlugin
1616
var need_restore_singleton: bool
1717

@@ -37,22 +37,18 @@ class PankuExporter extends EditorExportPlugin:
3737
if need_restore_singleton:
3838
owner.safe_add_singleton()
3939

40-
# A helper function to add custom project settings
41-
# See https://dfaction.net/handling-custom-project-settings-using-gdscript/
42-
static func add_custom_project_setting(name: String, default_value, type: int, hint: int = PROPERTY_HINT_NONE, hint_string: String = "") -> void:
43-
if ProjectSettings.has_setting(name): return
4440

45-
var setting_info: Dictionary = {
46-
"name": name,
47-
"type": type,
48-
"hint": hint,
49-
"hint_string": hint_string
50-
}
41+
func _enable_exporter() -> void:
42+
if not exporter:
43+
# See https://github.com/godotengine/godot/issues/73525
44+
exporter = (PankuExporter as Variant).new()
45+
exporter.owner = self
46+
add_export_plugin(exporter)
47+
5148

52-
ProjectSettings.set_setting(name, default_value)
53-
ProjectSettings.add_property_info(setting_info)
54-
ProjectSettings.set_initial_value(name, default_value)
55-
ProjectSettings.set_as_basic(name, true)
49+
func _disable_exporter() -> void:
50+
if exporter:
51+
remove_export_plugin(exporter)
5652

5753

5854
# Adding singleton with preliminary check to avoid any conflicts.
@@ -66,52 +62,28 @@ func safe_remove_singleton() -> void:
6662
if ProjectSettings.has_setting(SINGLETON_OPTION):
6763
remove_autoload_singleton(SINGLETON_NAME)
6864

69-
func create_setting() -> void:
70-
# Seems we can't add descriptions to custom settings now.
71-
72-
# Disable Panku Console in release builds
73-
add_custom_project_setting(
74-
PankuConfig.panku_option(PankuConfig.OPTIONS.DISABLE_ON_RELEASE),
75-
false,
76-
TYPE_BOOL
77-
)
78-
# Path to the custom `res://` path default config file, useful if you are going to keep panku console in release builds.
79-
add_custom_project_setting(
80-
PankuConfig.panku_option(PankuConfig.OPTIONS.CUSTOM_DEFAULT_CONFIG),
81-
PankuConfig.INITIAL_DEFAULT_CONFIG_FILE_PATH,
82-
TYPE_STRING,
83-
PROPERTY_HINT_FILE,
84-
"*.cfg"
85-
)
86-
87-
var error:int = ProjectSettings.save()
88-
if error != OK:
89-
push_error("Encountered error %d when saving project settings." % error)
90-
91-
func _enter_tree() -> void:
92-
# See https://github.com/godotengine/godot/issues/73525
93-
exporter = (PankuExporter as Variant).new()
94-
exporter.owner = self
95-
add_export_plugin(exporter)
96-
create_setting()
9765

66+
func _enable_plugin() -> void:
9867
safe_add_singleton()
99-
print("[Panku Console] initialized! Project page: https://github.com/Ark2000/PankuConsole")
10068

101-
if not PankuConfig.is_custom_default_config_exists():
102-
push_warning("[Panku Console] Default config file not found. Using code-level default config.")
103-
104-
func _exit_tree() -> void:
105-
remove_export_plugin(exporter)
69+
print("[Panku Console] enabled.")
10670

10771

10872
func _disable_plugin() -> void:
10973
safe_remove_singleton()
110-
111-
for option in PankuConfig.OPTIONS.values():
112-
var opt: String = PankuConfig.panku_option(option)
113-
if ProjectSettings.has_setting(opt):
114-
ProjectSettings.clear(opt)
115-
ProjectSettings.save()
74+
PankuConfig.clear_all_project_settings()
11675

11776
print("[Panku Console] disabled.")
77+
78+
79+
func _enter_tree() -> void:
80+
PankuConfig.init_all_project_settings()
81+
_enable_exporter()
82+
83+
print("[Panku Console] initialized! Project page: https://github.com/Ark2000/PankuConsole")
84+
85+
86+
func _exit_tree() -> void:
87+
_disable_exporter()
88+
89+

0 commit comments

Comments
 (0)