code optimization

This commit is contained in:
2024-06-05 19:15:32 +02:00
parent d4be7d0566
commit 1b49c54822
151 changed files with 4124 additions and 4673 deletions

View File

@@ -1,87 +1,86 @@
using System;
namespace Pilz.Configuration
namespace Pilz.Configuration;
public class AutoSaveConfigurationManager : ConfigurationManager
{
public class AutoSaveConfigurationManager : ConfigurationManager
private bool addedHandler = false;
private bool enableAutoSave = false;
private string _ConfigFilePath = string.Empty;
private bool _AutoLoadConfigOnAccess = false;
public string ConfigFilePath
{
private bool addedHandler = false;
private bool enableAutoSave = false;
private string _ConfigFilePath = string.Empty;
private bool _AutoLoadConfigOnAccess = false;
public string ConfigFilePath
get => _ConfigFilePath;
set
{
get => _ConfigFilePath;
set
{
_ConfigFilePath = value;
if (AutoLoadConfigOnAccess)
Load();
}
}
public bool AutoLoadConfigOnAccess
{
get => _AutoLoadConfigOnAccess;
set
{
_AutoLoadConfigOnAccess = value;
if (value)
Load();
}
}
public bool AutoSaveConfigOnExit
{
get => enableAutoSave;
set
{
if (enableAutoSave != value)
{
enableAutoSave = value;
if (enableAutoSave)
AddAutoSaveHandler();
else
RemoveAutoSaveHandler();
}
}
}
private void AddAutoSaveHandler()
{
if (!addedHandler)
{
AppDomain.CurrentDomain.ProcessExit += AutoSaveSettingsOnExit;
addedHandler = true;
}
}
private void RemoveAutoSaveHandler()
{
AppDomain.CurrentDomain.ProcessExit -= AutoSaveSettingsOnExit;
addedHandler = false;
}
private void AutoSaveSettingsOnExit(object sender, EventArgs e)
{
Save();
}
private void Save()
{
if (!string.IsNullOrEmpty(ConfigFilePath) && Configuration is not null)
Configuration.WriteToFile(ConfigFilePath);
}
private void Load()
{
if (!string.IsNullOrEmpty(ConfigFilePath))
Configuration.ReadFromFile(ConfigFilePath);
}
~AutoSaveConfigurationManager()
{
RemoveAutoSaveHandler();
_ConfigFilePath = value;
if (AutoLoadConfigOnAccess)
Load();
}
}
public bool AutoLoadConfigOnAccess
{
get => _AutoLoadConfigOnAccess;
set
{
_AutoLoadConfigOnAccess = value;
if (value)
Load();
}
}
public bool AutoSaveConfigOnExit
{
get => enableAutoSave;
set
{
if (enableAutoSave != value)
{
enableAutoSave = value;
if (enableAutoSave)
AddAutoSaveHandler();
else
RemoveAutoSaveHandler();
}
}
}
private void AddAutoSaveHandler()
{
if (!addedHandler)
{
AppDomain.CurrentDomain.ProcessExit += AutoSaveSettingsOnExit;
addedHandler = true;
}
}
private void RemoveAutoSaveHandler()
{
AppDomain.CurrentDomain.ProcessExit -= AutoSaveSettingsOnExit;
addedHandler = false;
}
private void AutoSaveSettingsOnExit(object sender, EventArgs e)
{
Save();
}
private void Save()
{
if (!string.IsNullOrEmpty(ConfigFilePath) && Configuration is not null)
Configuration.WriteToFile(ConfigFilePath);
}
private void Load()
{
if (!string.IsNullOrEmpty(ConfigFilePath))
Configuration.ReadFromFile(ConfigFilePath);
}
~AutoSaveConfigurationManager()
{
RemoveAutoSaveHandler();
}
}