code optimization
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
namespace Pilz.Configuration
|
||||
{
|
||||
public abstract class ConfigurationManager
|
||||
{
|
||||
public SimpleConfiguration Configuration { get; private set; }
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
internal void SetConfiguration(SimpleConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
public abstract class ConfigurationManager
|
||||
{
|
||||
public SimpleConfiguration Configuration { get; private set; }
|
||||
|
||||
internal void SetConfiguration(SimpleConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
}
|
||||
@@ -1,88 +1,87 @@
|
||||
using System.Collections;
|
||||
using Pilz.GeneralEventArgs;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Pilz.GeneralEventArgs;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public class ConfigurationManagerList : IList<ConfigurationManager>
|
||||
{
|
||||
public class ConfigurationManagerList : IList<ConfigurationManager>
|
||||
public event GettingParentManagerEventHandler GettingParentManager;
|
||||
|
||||
public delegate void GettingParentManagerEventHandler(object sender, GetValueEventArgs<SimpleConfiguration> e);
|
||||
|
||||
private readonly List<ConfigurationManager> myList = new List<ConfigurationManager>();
|
||||
|
||||
private object GetParentManager()
|
||||
{
|
||||
public event GettingParentManagerEventHandler GettingParentManager;
|
||||
|
||||
public delegate void GettingParentManagerEventHandler(object sender, GetValueEventArgs<SimpleConfiguration> e);
|
||||
|
||||
private readonly List<ConfigurationManager> myList = new List<ConfigurationManager>();
|
||||
|
||||
private object GetParentManager()
|
||||
{
|
||||
var args = new GetValueEventArgs<SimpleConfiguration>();
|
||||
GettingParentManager?.Invoke(this, args);
|
||||
return args.Value;
|
||||
}
|
||||
|
||||
public ConfigurationManager this[int index]
|
||||
{
|
||||
get => myList[index];
|
||||
set => myList[index] = value;
|
||||
}
|
||||
|
||||
public int Count => myList.Count;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public void Insert(int index, ConfigurationManager item)
|
||||
{
|
||||
myList.Insert(index, item);
|
||||
item.SetConfiguration((SimpleConfiguration)GetParentManager());
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Add(ConfigurationManager item)
|
||||
{
|
||||
item.SetConfiguration((SimpleConfiguration)GetParentManager());
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (ConfigurationManager item in myList)
|
||||
item.SetConfiguration(null);
|
||||
myList.Clear();
|
||||
}
|
||||
|
||||
public void CopyTo(ConfigurationManager[] array, int arrayIndex)
|
||||
{
|
||||
myList.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public int IndexOf(ConfigurationManager item)
|
||||
{
|
||||
return myList.IndexOf(item);
|
||||
}
|
||||
|
||||
public bool Contains(ConfigurationManager item)
|
||||
{
|
||||
return myList.Contains(item);
|
||||
}
|
||||
|
||||
public bool Remove(ConfigurationManager item)
|
||||
{
|
||||
item.SetConfiguration(null);
|
||||
return myList.Remove(item);
|
||||
}
|
||||
|
||||
public IEnumerator<ConfigurationManager> GetEnumerator()
|
||||
{
|
||||
return (IEnumerator<ConfigurationManager>)IEnumerable_GetEnumerator();
|
||||
}
|
||||
|
||||
private IEnumerator IEnumerable_GetEnumerator()
|
||||
{
|
||||
return myList.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => IEnumerable_GetEnumerator();
|
||||
var args = new GetValueEventArgs<SimpleConfiguration>();
|
||||
GettingParentManager?.Invoke(this, args);
|
||||
return args.Value;
|
||||
}
|
||||
|
||||
public ConfigurationManager this[int index]
|
||||
{
|
||||
get => myList[index];
|
||||
set => myList[index] = value;
|
||||
}
|
||||
|
||||
public int Count => myList.Count;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public void Insert(int index, ConfigurationManager item)
|
||||
{
|
||||
myList.Insert(index, item);
|
||||
item.SetConfiguration((SimpleConfiguration)GetParentManager());
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Add(ConfigurationManager item)
|
||||
{
|
||||
item.SetConfiguration((SimpleConfiguration)GetParentManager());
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (ConfigurationManager item in myList)
|
||||
item.SetConfiguration(null);
|
||||
myList.Clear();
|
||||
}
|
||||
|
||||
public void CopyTo(ConfigurationManager[] array, int arrayIndex)
|
||||
{
|
||||
myList.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public int IndexOf(ConfigurationManager item)
|
||||
{
|
||||
return myList.IndexOf(item);
|
||||
}
|
||||
|
||||
public bool Contains(ConfigurationManager item)
|
||||
{
|
||||
return myList.Contains(item);
|
||||
}
|
||||
|
||||
public bool Remove(ConfigurationManager item)
|
||||
{
|
||||
item.SetConfiguration(null);
|
||||
return myList.Remove(item);
|
||||
}
|
||||
|
||||
public IEnumerator<ConfigurationManager> GetEnumerator()
|
||||
{
|
||||
return (IEnumerator<ConfigurationManager>)IEnumerable_GetEnumerator();
|
||||
}
|
||||
|
||||
private IEnumerator IEnumerable_GetEnumerator()
|
||||
{
|
||||
return myList.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => IEnumerable_GetEnumerator();
|
||||
}
|
||||
@@ -1,139 +1,138 @@
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.GeneralEventArgs;
|
||||
using System.IO;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public static class ConfigurationSerializer
|
||||
{
|
||||
public static class ConfigurationSerializer
|
||||
public static event GettingJsonSerializerEventHandler GettingJsonSerializer;
|
||||
|
||||
public delegate void GettingJsonSerializerEventHandler(object instance, GetValueEventArgs<JsonSerializer> e);
|
||||
|
||||
private static JsonSerializer GetJsonSerializer(SimpleConfiguration instance)
|
||||
{
|
||||
public static event GettingJsonSerializerEventHandler GettingJsonSerializer;
|
||||
var args = new GetValueEventArgs<JsonSerializer>(JsonSerializer.CreateDefault());
|
||||
GettingJsonSerializer?.Invoke(instance, args);
|
||||
return args.Value;
|
||||
}
|
||||
|
||||
public delegate void GettingJsonSerializerEventHandler(object instance, GetValueEventArgs<JsonSerializer> e);
|
||||
/// <summary>
|
||||
/// Writes the given instance to a string and return it.
|
||||
/// </summary>
|
||||
/// <param name="instance">The configuration instance that should be serialized.</param>
|
||||
/// <returns>The content of the configuration instance as string.</returns>
|
||||
public static string WriteToString(SimpleConfiguration instance)
|
||||
{
|
||||
var tw = new StringWriter();
|
||||
GetJsonSerializer(instance).Serialize(tw, instance);
|
||||
string txt = tw.ToString();
|
||||
tw.Close();
|
||||
return txt;
|
||||
}
|
||||
|
||||
private static JsonSerializer GetJsonSerializer(SimpleConfiguration instance)
|
||||
{
|
||||
var args = new GetValueEventArgs<JsonSerializer>(JsonSerializer.CreateDefault());
|
||||
GettingJsonSerializer?.Invoke(instance, args);
|
||||
return args.Value;
|
||||
}
|
||||
/// <summary>
|
||||
/// Write the given instance to a given stream.
|
||||
/// </summary>
|
||||
/// <param name="instance">The configuration instance that should be serialized.</param>
|
||||
/// <param name="stream">The stream where the content should be written to.</param>
|
||||
public static void WriteToStream(SimpleConfiguration instance, Stream stream)
|
||||
{
|
||||
var sr = new StreamWriter(stream);
|
||||
sr.Write(WriteToString(instance));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the given instance to a string and return it.
|
||||
/// </summary>
|
||||
/// <param name="instance">The configuration instance that should be serialized.</param>
|
||||
/// <returns>The content of the configuration instance as string.</returns>
|
||||
public static string WriteToString(SimpleConfiguration instance)
|
||||
{
|
||||
var tw = new StringWriter();
|
||||
GetJsonSerializer(instance).Serialize(tw, instance);
|
||||
string txt = tw.ToString();
|
||||
tw.Close();
|
||||
return txt;
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes the given instance to the given filePath as text file.
|
||||
/// </summary>
|
||||
/// <param name="instance">The configuration instance that should be serialized.</param>
|
||||
/// <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
|
||||
public static void WriteToFile(SimpleConfiguration instance, string filePath)
|
||||
{
|
||||
var fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
|
||||
WriteToStream(instance, fs);
|
||||
fs.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write the given instance to a given stream.
|
||||
/// </summary>
|
||||
/// <param name="instance">The configuration instance that should be serialized.</param>
|
||||
/// <param name="stream">The stream where the content should be written to.</param>
|
||||
public static void WriteToStream(SimpleConfiguration instance, Stream stream)
|
||||
{
|
||||
var sr = new StreamWriter(stream);
|
||||
sr.Write(WriteToString(instance));
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromString<T>(string content) where T : SimpleConfiguration
|
||||
{
|
||||
var sr = new StringReader(content);
|
||||
T instance = (T)GetJsonSerializer(null).Deserialize(sr, typeof(T));
|
||||
sr.Close();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the given instance to the given filePath as text file.
|
||||
/// </summary>
|
||||
/// <param name="instance">The configuration instance that should be serialized.</param>
|
||||
/// <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
|
||||
public static void WriteToFile(SimpleConfiguration instance, string filePath)
|
||||
{
|
||||
var fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
|
||||
WriteToStream(instance, fs);
|
||||
fs.Close();
|
||||
}
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to populate with the configuration.</param>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
public static void ReadFromString(SimpleConfiguration instance, string content)
|
||||
{
|
||||
var sr = new StringReader(content);
|
||||
GetJsonSerializer(null).Populate(sr, content);
|
||||
sr.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromString<T>(string content) where T : SimpleConfiguration
|
||||
{
|
||||
var sr = new StringReader(content);
|
||||
T instance = (T)GetJsonSerializer(null).Deserialize(sr, typeof(T));
|
||||
sr.Close();
|
||||
return instance;
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromStream<T>(Stream stream) where T : SimpleConfiguration
|
||||
{
|
||||
return ReadFromString<T>(GetContentOfStream(stream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to populate with the configuration.</param>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
public static void ReadFromString(SimpleConfiguration instance, string content)
|
||||
{
|
||||
var sr = new StringReader(content);
|
||||
GetJsonSerializer(null).Populate(sr, content);
|
||||
sr.Close();
|
||||
}
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to populate with the configuration.</param>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
public static void ReadFromStream(SimpleConfiguration instance, Stream stream)
|
||||
{
|
||||
ReadFromString(instance, GetContentOfStream(stream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromStream<T>(Stream stream) where T : SimpleConfiguration
|
||||
{
|
||||
return ReadFromString<T>(GetContentOfStream(stream));
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromFile<T>(string filePath) where T : SimpleConfiguration
|
||||
{
|
||||
return ReadFromString<T>(GetContentOfFile(filePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to populate with the configuration.</param>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
public static void ReadFromStream(SimpleConfiguration instance, Stream stream)
|
||||
{
|
||||
ReadFromString(instance, GetContentOfStream(stream));
|
||||
}
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to populate with the configuration.</param>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
public static void ReadFromFile(SimpleConfiguration instance, string filePath)
|
||||
{
|
||||
ReadFromString(instance, GetContentOfFile(filePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromFile<T>(string filePath) where T : SimpleConfiguration
|
||||
{
|
||||
return ReadFromString<T>(GetContentOfFile(filePath));
|
||||
}
|
||||
private static string GetContentOfStream(Stream stream)
|
||||
{
|
||||
var sr = new StreamReader(stream);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to populate with the configuration.</param>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
public static void ReadFromFile(SimpleConfiguration instance, string filePath)
|
||||
{
|
||||
ReadFromString(instance, GetContentOfFile(filePath));
|
||||
}
|
||||
|
||||
private static string GetContentOfStream(Stream stream)
|
||||
{
|
||||
var sr = new StreamReader(stream);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
|
||||
private static string GetContentOfFile(string filePath)
|
||||
{
|
||||
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||||
string content = GetContentOfStream(fs);
|
||||
fs.Close();
|
||||
return content;
|
||||
}
|
||||
private static string GetContentOfFile(string filePath)
|
||||
{
|
||||
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||||
string content = GetContentOfStream(fs);
|
||||
fs.Close();
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public interface IChildSettings
|
||||
{
|
||||
public interface IChildSettings
|
||||
{
|
||||
void Reset();
|
||||
}
|
||||
void Reset();
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public interface ISettings
|
||||
{
|
||||
public interface ISettings
|
||||
{
|
||||
IReadOnlyCollection<IChildSettings> Childs { get; }
|
||||
T Get<T>() where T : IChildSettings, ISettingsIdentifier;
|
||||
void Reset();
|
||||
}
|
||||
IReadOnlyCollection<IChildSettings> Childs { get; }
|
||||
T Get<T>() where T : IChildSettings, ISettingsIdentifier;
|
||||
void Reset();
|
||||
}
|
||||
@@ -1,13 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
public interface ISettingsIdentifier
|
||||
{
|
||||
public interface ISettingsIdentifier
|
||||
{
|
||||
static abstract string Identifier { get; }
|
||||
}
|
||||
static abstract string Identifier { get; }
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public interface ISettingsManager
|
||||
{
|
||||
public interface ISettingsManager
|
||||
{
|
||||
ISettings Instance { get; }
|
||||
void Save();
|
||||
void Load();
|
||||
void Reset();
|
||||
}
|
||||
ISettings Instance { get; }
|
||||
void Save();
|
||||
void Load();
|
||||
void Reset();
|
||||
}
|
||||
@@ -1,37 +1,35 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public class Settings : ISettings
|
||||
{
|
||||
public class Settings : ISettings
|
||||
[JsonProperty(nameof(Settings))]
|
||||
protected readonly Dictionary<string, IChildSettings> mySettings = [];
|
||||
|
||||
[JsonIgnore]
|
||||
public IReadOnlyCollection<IChildSettings> Childs => mySettings.Values;
|
||||
|
||||
public T Get<T>() where T : IChildSettings, ISettingsIdentifier
|
||||
{
|
||||
[JsonProperty(nameof(Settings))]
|
||||
protected readonly Dictionary<string, IChildSettings> mySettings = [];
|
||||
if (mySettings.TryGetValue(T.Identifier, out IChildSettings valueExisting) && valueExisting is T settingsExisting)
|
||||
return settingsExisting;
|
||||
|
||||
[JsonIgnore]
|
||||
public IReadOnlyCollection<IChildSettings> Childs => mySettings.Values;
|
||||
|
||||
public T Get<T>() where T : IChildSettings, ISettingsIdentifier
|
||||
if (Activator.CreateInstance<T>() is T settingsNew)
|
||||
{
|
||||
if (mySettings.TryGetValue(T.Identifier, out IChildSettings valueExisting) && valueExisting is T settingsExisting)
|
||||
return settingsExisting;
|
||||
|
||||
if (Activator.CreateInstance<T>() is T settingsNew)
|
||||
{
|
||||
settingsNew.Reset();
|
||||
mySettings.Add(T.Identifier, settingsNew);
|
||||
return settingsNew;
|
||||
}
|
||||
|
||||
return default;
|
||||
settingsNew.Reset();
|
||||
mySettings.Add(T.Identifier, settingsNew);
|
||||
return settingsNew;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
foreach (var s in mySettings.Values)
|
||||
s.Reset();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
foreach (var s in mySettings.Values)
|
||||
s.Reset();
|
||||
}
|
||||
}
|
||||
@@ -1,142 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public class SettingsManager : ISettingsManager
|
||||
{
|
||||
public class SettingsManager : ISettingsManager
|
||||
public event EventHandler AutoSavingSettings;
|
||||
public event EventHandler SavingSettings;
|
||||
public event EventHandler SavedSettings;
|
||||
public event EventHandler<ErrorEventArgs> OnSerializationError;
|
||||
|
||||
protected ISettings defaultInstance = null;
|
||||
protected bool enableAutoSave = false;
|
||||
protected bool addedHandler = false;
|
||||
|
||||
public string ConfigFilePath { get; set; }
|
||||
|
||||
public ISettings Instance
|
||||
{
|
||||
public event EventHandler AutoSavingSettings;
|
||||
public event EventHandler SavingSettings;
|
||||
public event EventHandler SavedSettings;
|
||||
public event EventHandler<ErrorEventArgs> OnSerializationError;
|
||||
|
||||
protected ISettings defaultInstance = null;
|
||||
protected bool enableAutoSave = false;
|
||||
protected bool addedHandler = false;
|
||||
|
||||
public string ConfigFilePath { get; set; }
|
||||
|
||||
public ISettings Instance
|
||||
get
|
||||
{
|
||||
get
|
||||
{
|
||||
if (defaultInstance is null)
|
||||
Load();
|
||||
return defaultInstance;
|
||||
}
|
||||
if (defaultInstance is null)
|
||||
Load();
|
||||
return defaultInstance;
|
||||
}
|
||||
}
|
||||
|
||||
public bool AutoSaveOnExit
|
||||
public bool AutoSaveOnExit
|
||||
{
|
||||
get => enableAutoSave;
|
||||
set
|
||||
{
|
||||
get => enableAutoSave;
|
||||
set
|
||||
if (enableAutoSave != value)
|
||||
{
|
||||
if (enableAutoSave != value)
|
||||
enableAutoSave = value;
|
||||
if (enableAutoSave)
|
||||
{
|
||||
enableAutoSave = value;
|
||||
if (enableAutoSave)
|
||||
{
|
||||
if (!addedHandler)
|
||||
AddAutoSaveHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (addedHandler)
|
||||
RemoveAutoSaveHandler();
|
||||
}
|
||||
if (!addedHandler)
|
||||
AddAutoSaveHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (addedHandler)
|
||||
RemoveAutoSaveHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SettingsManager()
|
||||
public SettingsManager()
|
||||
{
|
||||
}
|
||||
|
||||
public SettingsManager(string fileName, bool autoSaveOnExit) : this()
|
||||
{
|
||||
ConfigFilePath = fileName;
|
||||
AutoSaveOnExit = autoSaveOnExit;
|
||||
}
|
||||
|
||||
protected void AddAutoSaveHandler()
|
||||
{
|
||||
AppDomain.CurrentDomain.ProcessExit += AutoSaveSettingsOnExit;
|
||||
addedHandler = true;
|
||||
}
|
||||
|
||||
protected void RemoveAutoSaveHandler()
|
||||
{
|
||||
AppDomain.CurrentDomain.ProcessExit -= AutoSaveSettingsOnExit;
|
||||
addedHandler = false;
|
||||
}
|
||||
|
||||
private void AutoSaveSettingsOnExit(object sender, EventArgs e)
|
||||
{
|
||||
AutoSavingSettings?.Invoke(this, new EventArgs());
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ConfigFilePath) && defaultInstance is not null)
|
||||
SaveInternal();
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ConfigFilePath) && File.Exists(ConfigFilePath))
|
||||
LoadInternal();
|
||||
else
|
||||
CreateNewInstance();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Instance.Reset();
|
||||
}
|
||||
|
||||
protected virtual void CreateNewInstance()
|
||||
{
|
||||
defaultInstance = new Settings();
|
||||
defaultInstance.Reset();
|
||||
}
|
||||
|
||||
protected virtual void LoadInternal()
|
||||
{
|
||||
defaultInstance = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(ConfigFilePath), CreateJsonSerializerSettings());
|
||||
}
|
||||
|
||||
protected virtual void SaveInternal()
|
||||
{
|
||||
SavingSettings?.Invoke(this, EventArgs.Empty);
|
||||
File.WriteAllText(ConfigFilePath, JsonConvert.SerializeObject(defaultInstance, CreateJsonSerializerSettings()));
|
||||
SavedSettings?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
protected virtual JsonSerializerSettings CreateJsonSerializerSettings()
|
||||
{
|
||||
return new JsonSerializerSettings()
|
||||
{
|
||||
}
|
||||
Formatting = Formatting.Indented,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
Error = JsonSerializer_OnError,
|
||||
};
|
||||
}
|
||||
|
||||
public SettingsManager(string fileName, bool autoSaveOnExit) : this()
|
||||
{
|
||||
ConfigFilePath = fileName;
|
||||
AutoSaveOnExit = autoSaveOnExit;
|
||||
}
|
||||
protected virtual void JsonSerializer_OnError(object sender, ErrorEventArgs e)
|
||||
{
|
||||
const string errorResolvingType = "Error resolving type specified in JSON";
|
||||
|
||||
protected void AddAutoSaveHandler()
|
||||
{
|
||||
AppDomain.CurrentDomain.ProcessExit += AutoSaveSettingsOnExit;
|
||||
addedHandler = true;
|
||||
}
|
||||
// Invoke event
|
||||
OnSerializationError?.Invoke(sender, e);
|
||||
|
||||
protected void RemoveAutoSaveHandler()
|
||||
{
|
||||
AppDomain.CurrentDomain.ProcessExit -= AutoSaveSettingsOnExit;
|
||||
addedHandler = false;
|
||||
}
|
||||
|
||||
private void AutoSaveSettingsOnExit(object sender, EventArgs e)
|
||||
{
|
||||
AutoSavingSettings?.Invoke(this, new EventArgs());
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ConfigFilePath) && defaultInstance is not null)
|
||||
SaveInternal();
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ConfigFilePath) && File.Exists(ConfigFilePath))
|
||||
LoadInternal();
|
||||
else
|
||||
CreateNewInstance();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Instance.Reset();
|
||||
}
|
||||
|
||||
protected virtual void CreateNewInstance()
|
||||
{
|
||||
defaultInstance = new Settings();
|
||||
defaultInstance.Reset();
|
||||
}
|
||||
|
||||
protected virtual void LoadInternal()
|
||||
{
|
||||
defaultInstance = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(ConfigFilePath), CreateJsonSerializerSettings());
|
||||
}
|
||||
|
||||
protected virtual void SaveInternal()
|
||||
{
|
||||
SavingSettings?.Invoke(this, EventArgs.Empty);
|
||||
File.WriteAllText(ConfigFilePath, JsonConvert.SerializeObject(defaultInstance, CreateJsonSerializerSettings()));
|
||||
SavedSettings?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
protected virtual JsonSerializerSettings CreateJsonSerializerSettings()
|
||||
{
|
||||
return new JsonSerializerSettings()
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
Error = JsonSerializer_OnError,
|
||||
};
|
||||
}
|
||||
|
||||
protected virtual void JsonSerializer_OnError(object sender, ErrorEventArgs e)
|
||||
{
|
||||
const string errorResolvingType = "Error resolving type specified in JSON";
|
||||
|
||||
// Invoke event
|
||||
OnSerializationError?.Invoke(sender, e);
|
||||
|
||||
// Handle ourself
|
||||
if (!e.ErrorContext.Handled && e.ErrorContext.Error is JsonSerializationException serializationException && serializationException.Message.StartsWith(errorResolvingType))
|
||||
e.ErrorContext.Handled = true;
|
||||
}
|
||||
// Handle ourself
|
||||
if (!e.ErrorContext.Handled && e.ErrorContext.Error is JsonSerializationException serializationException && serializationException.Message.StartsWith(errorResolvingType))
|
||||
e.ErrorContext.Handled = true;
|
||||
}
|
||||
}
|
||||
@@ -1,110 +1,109 @@
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.GeneralEventArgs;
|
||||
using System.IO;
|
||||
|
||||
namespace Pilz.Configuration
|
||||
namespace Pilz.Configuration;
|
||||
|
||||
public class SimpleConfiguration
|
||||
{
|
||||
public class SimpleConfiguration
|
||||
[JsonIgnore]
|
||||
public readonly ConfigurationManagerList Managers = new ConfigurationManagerList();
|
||||
|
||||
public SimpleConfiguration()
|
||||
{
|
||||
[JsonIgnore]
|
||||
public readonly ConfigurationManagerList Managers = new ConfigurationManagerList();
|
||||
Managers.GettingParentManager += Managers_GettingParentManager;
|
||||
}
|
||||
|
||||
public SimpleConfiguration()
|
||||
{
|
||||
Managers.GettingParentManager += Managers_GettingParentManager;
|
||||
}
|
||||
private void Managers_GettingParentManager(object sender, GetValueEventArgs<SimpleConfiguration> e)
|
||||
{
|
||||
if (ReferenceEquals(sender, Managers))
|
||||
e.Value = this;
|
||||
}
|
||||
|
||||
private void Managers_GettingParentManager(object sender, GetValueEventArgs<SimpleConfiguration> e)
|
||||
{
|
||||
if (ReferenceEquals(sender, Managers))
|
||||
e.Value = this;
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes this instance to a string and return it.
|
||||
/// </summary>
|
||||
/// <returns>The content of the configuration instance as string.</returns>
|
||||
public string WriteToString()
|
||||
{
|
||||
return ConfigurationSerializer.WriteToString(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes this instance to a string and return it.
|
||||
/// </summary>
|
||||
/// <returns>The content of the configuration instance as string.</returns>
|
||||
public string WriteToString()
|
||||
{
|
||||
return ConfigurationSerializer.WriteToString(this);
|
||||
}
|
||||
/// <summary>
|
||||
/// Write this instance to a given stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream where the content should be written to.</param>
|
||||
public void WriteToStream(Stream stream)
|
||||
{
|
||||
ConfigurationSerializer.WriteToStream(this, stream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write this instance to a given stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream where the content should be written to.</param>
|
||||
public void WriteToStream(Stream stream)
|
||||
{
|
||||
ConfigurationSerializer.WriteToStream(this, stream);
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes this instance to the given filePath as text file.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
|
||||
public void WriteToFile(string filePath)
|
||||
{
|
||||
ConfigurationSerializer.WriteToFile(this, filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes this instance to the given filePath as text file.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file path where the content should be written to. The file will be created or overwritten.</param>
|
||||
public void WriteToFile(string filePath)
|
||||
{
|
||||
ConfigurationSerializer.WriteToFile(this, filePath);
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromString<T>(string content) where T : SimpleConfiguration
|
||||
{
|
||||
return ConfigurationSerializer.ReadFromString<T>(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromString<T>(string content) where T : SimpleConfiguration
|
||||
{
|
||||
return ConfigurationSerializer.ReadFromString<T>(content);
|
||||
}
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to this instance.
|
||||
/// </summary>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
public void ReadFromString(string content)
|
||||
{
|
||||
ConfigurationSerializer.ReadFromString(this, content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to this instance.
|
||||
/// </summary>
|
||||
/// <param name="content">The content of the configuration as string.</param>
|
||||
public void ReadFromString(string content)
|
||||
{
|
||||
ConfigurationSerializer.ReadFromString(this, content);
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromStream<T>(Stream stream) where T : SimpleConfiguration
|
||||
{
|
||||
return ConfigurationSerializer.ReadFromStream<T>(stream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromStream<T>(Stream stream) where T : SimpleConfiguration
|
||||
{
|
||||
return ConfigurationSerializer.ReadFromStream<T>(stream);
|
||||
}
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to this instance.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
public void ReadFromStream(Stream stream)
|
||||
{
|
||||
ConfigurationSerializer.ReadFromStream(this, stream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to this instance.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream with the content of the configuration.</param>
|
||||
public void ReadFromStream(Stream stream)
|
||||
{
|
||||
ConfigurationSerializer.ReadFromStream(this, stream);
|
||||
}
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromFile<T>(string filePath) where T : SimpleConfiguration
|
||||
{
|
||||
return ConfigurationSerializer.ReadFromFile<T>(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a configuratin from the given string and returns an instance of it.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the configuration class to instance.</typeparam>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
/// <returns></returns>
|
||||
public static T ReadFromFile<T>(string filePath) where T : SimpleConfiguration
|
||||
{
|
||||
return ConfigurationSerializer.ReadFromFile<T>(filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to this instance.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
public void ReadFromFile(string filePath)
|
||||
{
|
||||
ConfigurationSerializer.ReadFromFile(this, filePath);
|
||||
}
|
||||
/// <summary>
|
||||
/// Read a configuration from the given string and put them to this instance.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The path to the file with the content of the configuration.</param>
|
||||
public void ReadFromFile(string filePath)
|
||||
{
|
||||
ConfigurationSerializer.ReadFromFile(this, filePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user