rework Settings in Pilz.Configuration

This commit is contained in:
2024-01-04 11:45:54 +01:00
parent ff2a221bfa
commit da5de9ecf7
7 changed files with 103 additions and 63 deletions

View File

@@ -1,20 +1,32 @@
Imports System.IO
Imports System.Reflection
Imports Newtonsoft.Json.Linq
Public NotInheritable Class SettingsManager(Of T As SettingsBase)
Imports Newtonsoft.Json
Public Class SettingsManager
Implements ISettingsManager
Public Shared Event InitializingManager As EventHandler
Public Event AutoSavingSettings As EventHandler
Public Event SavingSettings As EventHandler
Public Event SavedSettings As EventHandler
Private defaultInstance As T = Nothing
Private ReadOnly settingsProvider As New List(Of ISettingsProvider)
Private defaultInstance As ISettings = Nothing
Private enableAutoSave As Boolean = False
Private addedHandler As Boolean = False
Public Property ConfigFilePath As String Implements ISettingsManager.ConfigFilePath
Public ReadOnly Property Instance As IChildSettings
Get
If defaultInstance Is Nothing Then
Load()
End If
Return defaultInstance
End Get
End Property
Public Property AutoSaveOnExit As Boolean
Get
Return enableAutoSave
@@ -40,25 +52,18 @@ Public NotInheritable Class SettingsManager(Of T As SettingsBase)
AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf AutoSaveSettingsOnExit
addedHandler = True
End Sub
Private Sub RemoveAutoSaveHandler()
RemoveHandler AppDomain.CurrentDomain.ProcessExit, AddressOf AutoSaveSettingsOnExit
addedHandler = False
End Sub
Public ReadOnly Property Instance As T
Get
If defaultInstance Is Nothing Then
Load()
End If
Return defaultInstance
End Get
End Property
Public Sub New()
ConfigFilePath = ""
AutoSaveOnExit = False
RaiseEvent InitializingManager(Me, EventArgs.Empty)
End Sub
Public Sub New(fileName As String, autoSaveOnExit As Boolean)
Me.New
ConfigFilePath = fileName
Me.AutoSaveOnExit = autoSaveOnExit
End Sub
@@ -70,50 +75,44 @@ Public NotInheritable Class SettingsManager(Of T As SettingsBase)
Public Sub Save() Implements ISettingsManager.Save
If Not String.IsNullOrEmpty(ConfigFilePath) AndAlso defaultInstance IsNot Nothing Then
SavePrivate()
SaveInternal()
End If
End Sub
Public Sub Load() Implements ISettingsManager.Load
If Not String.IsNullOrEmpty(ConfigFilePath) AndAlso File.Exists(ConfigFilePath) Then
LoadPrivate()
LoadInternal()
Else
CreateNewInstance()
End If
If defaultInstance IsNot Nothing Then
defaultInstance._settingsManager = Me
End If
End Sub
Public Sub Reset() Implements ISettingsManager.Reset
Instance.ResetValues()
Instance.Reset()
End Sub
Private Sub CreateNewInstance()
Dim ctor As ConstructorInfo = GetType(T).GetConstructor({})
If ctor IsNot Nothing Then
defaultInstance = ctor.Invoke({})
defaultInstance.ResetValues()
Else
Throw New Exception("The base type has no constructor with no parameters.")
End If
Protected Overridable Sub CreateNewInstance()
defaultInstance = New Settings
defaultInstance.Initialize(settingsProvider)
defaultInstance.Reset()
End Sub
Private Sub LoadPrivate()
defaultInstance = JObject.Parse(File.ReadAllText(ConfigFilePath)).ToObject(Of T)
Protected Overridable Sub LoadInternal()
defaultInstance = JsonConvert.DeserializeObject(Of Settings)(File.ReadAllText(ConfigFilePath), CreateJsonSerializerSettings)
defaultInstance.Initialize(settingsProvider)
End Sub
Private Sub SavePrivate()
RaiseEvent SavingSettings(Me, New EventArgs)
Dim obj As JObject = JObject.FromObject(defaultInstance)
If obj IsNot Nothing Then
File.WriteAllText(ConfigFilePath, obj.ToString)
End If
RaiseEvent SavedSettings(Me, New EventArgs)
Protected Overridable Sub SaveInternal()
RaiseEvent SavingSettings(Me, EventArgs.Empty)
File.WriteAllText(ConfigFilePath, JsonConvert.SerializeObject(defaultInstance, CreateJsonSerializerSettings))
RaiseEvent SavedSettings(Me, EventArgs.Empty)
End Sub
Protected Overridable Function CreateJsonSerializerSettings() As JsonSerializerSettings
Return New JsonSerializerSettings With {
.Formatting = Formatting.Indented,
.TypeNameHandling = TypeNameHandling.Auto
}
End Function
End Class