@@ -11,6 +11,73 @@ const OPTIONS = {
1111const INITIAL_DEFAULT_CONFIG_FILE_PATH = "res://addons/panku_console/default_panku_config.cfg"
1212const 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
1582static 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
2991static 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
38101static 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
44108static 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+
54119static func get_value (key :String , default :Variant ) -> Variant :
55120 return get_config ().get (key , default )
56121
122+
57123static func set_value (key :String , val :Variant ) -> void :
58124 var config = _get_config (USER_CONFIG_FILE_PATH )
59125 config [key ] = val
0 commit comments