config in client suite

This commit is contained in:
Jonathan Jenne
2019-02-13 16:23:02 +01:00
parent 9ee59ac400
commit 914ba9dc90
11 changed files with 64 additions and 103 deletions

View File

@@ -12,7 +12,7 @@ Public Class ConfigManager(Of T)
Private ReadOnly _File As Filesystem.File
Private ReadOnly _UserPath As String
Private ReadOnly _ComputerPath As String
Private ReadOnly _CurrentDataPath As String
Private _CurrentDataPath As String
Private ReadOnly _Schema As T
Private ReadOnly _Serializer As XmlSerializer
@@ -53,25 +53,15 @@ Public Class ConfigManager(Of T)
Throw New DirectoryNotFoundException($"Path {ComputerConfigPath} does not exist!")
End If
If Not Filesystem.File Then Then
Throw New DirectoryNotFoundException()
If Not _File.TestPathIsDirectory(UserConfigPath) Then
Throw New ArgumentException($"Path {UserConfigPath} is not a directory!")
End If
If IO.File.Exists(_UserPath) Then
_Logger.Debug("Loading config from UserPath: {0}", _UserPath)
_CurrentDataPath = _UserPath
_Config = ReadFromFile(_UserPath)
ElseIf IO.File.Exists(_ComputerPath) Then
_Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath)
_CurrentDataPath = _ComputerPath
_Config = ReadFromFile(_ComputerPath)
Else
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
_CurrentDataPath = _UserPath
_Config = Activator.CreateInstance(_Schema.GetType)
WriteToFile(_Config, _UserPath)
If Not _File.TestPathIsDirectory(ComputerConfigPath) Then
Throw New ArgumentException($"Path {ComputerConfigPath} is not a directory!")
End If
_Config = LoadConfig()
End Sub
''' <summary>
@@ -81,6 +71,34 @@ Public Class ConfigManager(Of T)
WriteToFile(_Config, _UserPath)
End Sub
''' <summary>
''' First check if a user config exists and if it does, load it.
''' If not, check if a systemwide config exists and and if it does, load it.
''' Otherwise, create a user config using the default values from the supplied config class `T`
''' </summary>
''' <returns></returns>
Private Function LoadConfig() As T
Dim oConfig As T
If IO.File.Exists(_UserPath) Then
_Logger.Debug("Loading config from UserPath: {0}", _UserPath)
_CurrentDataPath = _UserPath
oConfig = ReadFromFile(_UserPath)
ElseIf IO.File.Exists(_ComputerPath) Then
_Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath)
_CurrentDataPath = _ComputerPath
oConfig = ReadFromFile(_ComputerPath)
Else
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
_CurrentDataPath = _UserPath
oConfig = Activator.CreateInstance(_Schema.GetType)
WriteToFile(_Config, _UserPath)
End If
Return oConfig
End Function
''' <summary>
''' Serialize a config object to byte array
''' </summary>