2021-01-14 20:35:30 +10:30
# A slightly redundant proxy to ProjectSettings
# This is mostly used so that signals can be used to respond to settings changes
extends Node
2024-05-29 22:11:01 +09:30
signal config_loaded
2021-01-14 20:35:30 +10:30
signal subsampling_changed ( xy )
2024-11-04 01:10:35 +10:30
# Newer Android versions require storage access permissions to be requested at runtime.
# There is no way around this, so this code needs to be updated to work.
var android_permissions_granted : = false
const REQUIRED_ANDROID_PERMISSIONS : = PoolStringArray ( [ ' MANAGE_EXTERNAL_FILES ' , ' READ_EXTERNAL_FILES ' , ' WRITE_EXTERNAL_FILES ' ] )
func check_android_permissions ( ) - > bool :
if self . android_permissions_granted :
return true
var permissions_granted : = OS . get_granted_permissions ( )
for perm in REQUIRED_ANDROID_PERMISSIONS :
if not ( perm in permissions_granted ) :
return false
self . android_permissions_granted = true
return true
func update_android_permissions ( ) - > void :
if not OS . has_feature ( ' Android ' ) :
return
if self . check_android_permissions ( ) :
return
OS . request_permissions ( )
func _on_request_permissions_result ( permission : String , granted : bool ) - > void :
print ( ' _on_request_permissions_result: %s = %s ' % [ permission , granted ] )
# if granted and (permission in REQUIRED_ANDROID_PERMISSIONS):
# self.call_deferred('update_view')
2024-05-29 22:11:01 +09:30
const ANDROID_USERDIR : = ' /storage/emulated/0/RhythmGame/ '
# The following would probably work. One huge caveat is that permission needs to be manually granted by the user in app settings as we can't use OS.request_permission('WRITE_EXTERNAL_STORAGE')
# '/storage/emulated/0/Android/data/au.ufeff.rhythmgame/'
# '/sdcard/Android/data/au.ufeff.rhythmgame/'
var user_data_dir : = OS . get_user_data_dir ( ) . rstrip ( ' / ' ) + ' / ' if OS . get_name ( ) != ' Android ' else ANDROID_USERDIR
var SETTINGS_FILENAME = user_data_dir + ' settings.cfg '
var config : ConfigFile
2021-01-14 20:35:30 +10:30
var subsampling : Vector2 setget SSXY_set , SSXY_get
var subsampling_x : float setget SSX_set , SSX_get
var subsampling_y : float setget SSY_set , SSY_get
func SSX_set ( x : float ) :
ProjectSettings . set_setting ( ' rendering/quality/subsampling/x ' , x )
emit_signal ( ' subsampling_changed ' , self . subsampling )
func SSY_set ( y : float ) :
ProjectSettings . set_setting ( ' rendering/quality/subsampling/y ' , y )
emit_signal ( ' subsampling_changed ' , self . subsampling )
func SSXY_set ( xy : Vector2 ) :
ProjectSettings . set_setting ( ' rendering/quality/subsampling/x ' , xy . x )
ProjectSettings . set_setting ( ' rendering/quality/subsampling/y ' , xy . y )
emit_signal ( ' subsampling_changed ' , self . subsampling )
func SSX_get ( ) - > float :
return ProjectSettings . get_setting ( ' rendering/quality/subsampling/x ' )
func SSY_get ( ) - > float :
return ProjectSettings . get_setting ( ' rendering/quality/subsampling/y ' )
func SSXY_get ( ) - > Vector2 :
return Vector2 ( self . subsampling_x , self . subsampling_y )
2021-02-07 00:17:27 +10:30
2024-05-29 22:11:01 +09:30
func get_library_paths ( ) - > PoolStringArray :
var paths = [ user_data_dir ]
var additional_paths = config . get_value ( ' libraries ' , ' additional_paths ' , [ ] )
# Ensure paths are valid and have trailing slash
for p in additional_paths :
if p is String :
paths . append ( p . rstrip ( ' / ' ) + ' / ' )
return PoolStringArray ( paths )
2021-02-07 00:17:27 +10:30
2024-11-04 01:10:35 +10:30
func set_additional_library_paths ( entries ) :
config . set_value ( ' libraries ' , ' additional_paths ' , PoolStringArray ( entries ) )
save_settings ( )
2021-02-07 00:17:27 +10:30
func load_settings ( ) :
2024-05-29 22:11:01 +09:30
config = ConfigFile . new ( )
config . set_value ( ' libraries ' , ' additional_paths ' , [ ] )
2021-02-07 00:17:27 +10:30
match config . load ( SETTINGS_FILENAME ) :
OK :
2024-05-30 01:04:35 +09:30
self . subsampling = Vector2 (
config . get_value ( ' rendering ' , ' subsampling_x ' , self . subsampling_x ) ,
config . get_value ( ' rendering ' , ' subsampling_y ' , self . subsampling_y )
)
2024-05-29 22:11:01 +09:30
emit_signal ( ' config_loaded ' )
2021-02-07 00:17:27 +10:30
ERR_FILE_NOT_FOUND :
save_settings ( )
2024-05-29 22:11:01 +09:30
print ( ' Loaded settings from ' + SETTINGS_FILENAME )
2021-02-07 00:17:27 +10:30
func save_settings ( ) :
2024-05-29 22:11:01 +09:30
config . save ( SETTINGS_FILENAME )
print ( ' Saved settings to ' + SETTINGS_FILENAME )
func _ready ( ) :
load_settings ( )
2024-11-04 01:10:35 +10:30
get_tree ( ) . connect ( ' on_request_permissions_result ' , self , ' _on_request_permissions_result ' )
self . update_android_permissions ( )