Page 1 of 1
OpenPreferences change + new command(s)
Posted: Mon Sep 24, 2018 2:21 pm
by BinoX
Can OpenPreferences have support for #PB_File_SharedWrite?
It would be nice to be able to edit the preferences file externally without having to close the program.
I realise this may cause issues with updates, however a couple of new command could change this:
e.g.
PreferenceFileHasChanged() (Which will return true if the file has been changed - maybe check the filesystem modified date)
and
ReloadPreferenceFile() Which could re-open the file with the new values (Assuming that the preferences are stored in memory and not re-read from the file each time)
so it could be:
Code: Select all
If PreferenceFileHasChanged()
ReloadPreferenceFile()
EndIf
Re: OpenPreferences change + new command(s)
Posted: Mon Sep 24, 2018 3:43 pm
by kenmo
I agree, these would be helpful.
I think the Preferences library is a bit outdated compared to the rest of PB. It could use some new features and improvements (first: ability to handle more than 1 prefs file at once! Use ID or #PB_Any like the File() commands)
In the meantime, you can simulate your requested behavior using temporary prefs files and copying to the "real" file.
Code: Select all
; 2018-09-24
;-
;- Globals
Global _PrefTempFile.s = GetTemporaryDirectory() + "tempPref.ini"
Global _PrefSaveFile.s
Global _PrefFlags.i
;-
;- Procedures
Procedure.i OpenPreferences_Ex(Filename.s, Flags.i = #Null)
_PrefSaveFile = Filename
_PrefFlags = Flags
CopyFile(_PrefSaveFile, _PrefTempFile)
ProcedureReturn (OpenPreferences(_PrefTempFile, Flags))
EndProcedure
Procedure.i CreatePreferences_Ex(Filename.s, Flags.i = #Null)
_PrefSaveFile = Filename
_PrefFlags = Flags
If (CreatePreferences(Filename, Flags))
ClosePreferences()
ProcedureReturn (CreatePreferences(_PrefTempFile, Flags))
EndIf
EndProcedure
Procedure ClosePreferences_Ex()
ClosePreferences()
CopyFile(_PrefTempFile, _PrefSaveFile)
EndProcedure
Procedure FlushPreferenceBuffers_Ex()
FlushPreferenceBuffers()
CopyFile(_PrefTempFile, _PrefSaveFile)
EndProcedure
Procedure.i PreferenceFileHasChanged_Ex()
ProcedureReturn (Bool(GetFileDate(_PrefSaveFile, #PB_Date_Modified) > GetFileDate(_PrefTempFile, #PB_Date_Modified)))
EndProcedure
Procedure.i ReloadPreferences_Ex()
ClosePreferences()
ProcedureReturn (OpenPreferences_Ex(_PrefSaveFile, _PrefFlags))
EndProcedure
;-
;- Demo Program
CompilerIf (#PB_Compiler_IsMainFile)
; Create a starting point for the user to edit externally...
CreatePreferences_Ex("test.ini")
WritePreferenceInteger("Number", 5)
FlushPreferenceBuffers_Ex()
Debug "Number = " + ReadPreferenceString("Number", "")
Debug "Try editing 'test.ini'"
OpenWindow(0, 0, 0, 220, 40, "Edit test.ini", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ButtonGadget(0, 5, 5, 160, 22, "Open test.ini")
Repeat
Select (WaitWindowEvent(100))
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
RunProgram("test.ini")
Case #Null
If (PreferenceFileHasChanged_Ex())
ReloadPreferences_Ex()
Debug ""
Debug "File updated! Number = " + ReadPreferenceString("Number", "")
EndIf
EndSelect
ForEver
;WritePreferenceInteger("Number", 555)
;WritePreferenceInteger("Quit", 1)
ClosePreferences_Ex()
CompilerEndIf
;-
Re: OpenPreferences change + new command(s)
Posted: Wed Sep 26, 2018 10:36 am
by BinoX
Huh..
To be honest, it didn't occur to me to copy the file... that really would have been the obvious solution.
Thanks!
Re: OpenPreferences change + new command(s)
Posted: Wed Sep 26, 2018 10:46 am
by PeDe
I have used this replacement for the preferences, and it works very well:
viewtopic.php?p=465077#p465077
Peter