RaspberryIO_26/Swan.Lite/Configuration/SettingsProvider.cs

191 lines
6.1 KiB
C#
Raw Normal View History

2019-12-04 18:57:18 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Swan.Formatters;
using Swan.Reflection;
namespace Swan.Configuration
{
/// <summary>
/// Represents a provider to save and load settings using a plain JSON file.
/// </summary>
/// <example>
/// The following example shows how to save and load settings.
/// <code>
/// using Swan.Configuration;
///
/// public class Example
/// {
/// public static void Main()
/// {
/// // get user from settings
/// var user = SettingsProvider&lt;Settings&gt;.Instance.Global.User;
///
/// // modify the port
/// SettingsProvider&lt;Settings&gt;.Instance.Global.Port = 20;
///
/// // if we want these settings to persist
/// SettingsProvider&lt;Settings&gt;.Instance.PersistGlobalSettings();
/// }
///
/// public class Settings
/// {
/// public int Port { get; set; } = 9696;
///
/// public string User { get; set; } = "User";
/// }
/// }
/// </code>
/// </example>
/// <typeparam name="T">The type of settings model.</typeparam>
2019-12-08 19:54:52 +01:00
public sealed class SettingsProvider<T> : SingletonBase<SettingsProvider<T>>
2019-12-04 18:57:18 +01:00
{
2019-12-08 19:54:52 +01:00
private readonly Object _syncRoot = new Object();
2019-12-04 18:57:18 +01:00
private T _global;
/// <summary>
/// Gets or sets the configuration file path. By default the entry assembly directory is used
/// and the filename is 'appsettings.json'.
/// </summary>
/// <value>
/// The configuration file path.
/// </value>
2019-12-08 19:54:52 +01:00
public String ConfigurationFilePath { get; set; } = Path.Combine(SwanRuntime.EntryAssemblyDirectory, "appsettings.json");
2019-12-04 18:57:18 +01:00
/// <summary>
/// Gets the global settings object.
/// </summary>
/// <value>
/// The global settings object.
/// </value>
public T Global
{
get
{
2019-12-08 19:54:52 +01:00
lock (this._syncRoot)
2019-12-04 18:57:18 +01:00
{
2019-12-08 19:54:52 +01:00
if (Equals(this._global, default(T)!)) {
this.ReloadGlobalSettings();
}
return this._global;
2019-12-04 18:57:18 +01:00
}
}
}
/// <summary>
/// Reloads the global settings.
/// </summary>
public void ReloadGlobalSettings()
{
2019-12-08 19:54:52 +01:00
if (File.Exists(this.ConfigurationFilePath) == false || File.ReadAllText(this.ConfigurationFilePath).Length == 0)
{
this.ResetGlobalSettings();
2019-12-04 18:57:18 +01:00
return;
}
2019-12-08 19:54:52 +01:00
lock (this._syncRoot) {
this._global = Json.Deserialize<T>(File.ReadAllText(this.ConfigurationFilePath));
}
}
2019-12-04 18:57:18 +01:00
/// <summary>
/// Persists the global settings.
/// </summary>
2019-12-08 19:54:52 +01:00
public void PersistGlobalSettings() => File.WriteAllText(this.ConfigurationFilePath, Json.Serialize(this.Global, true));
2019-12-04 18:57:18 +01:00
/// <summary>
/// Updates settings from list.
/// </summary>
/// <param name="propertyList">The list.</param>
/// <returns>
/// A list of settings of type ref="ExtendedPropertyInfo".
/// </returns>
/// <exception cref="ArgumentNullException">propertyList.</exception>
2019-12-08 19:54:52 +01:00
public List<String> RefreshFromList(List<ExtendedPropertyInfo<T>> propertyList)
2019-12-04 18:57:18 +01:00
{
2019-12-08 19:54:52 +01:00
if (propertyList == null) {
throw new ArgumentNullException(nameof(propertyList));
}
List<String> changedSettings = new List<global::System.String>();
IEnumerable<PropertyInfo> globalProps = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties<T>();
foreach (ExtendedPropertyInfo<T> property in propertyList)
{
PropertyInfo propertyInfo = globalProps.FirstOrDefault(x => x.Name == property.Property);
if (propertyInfo == null) {
continue;
}
Object originalValue = propertyInfo.GetValue(this.Global);
Boolean isChanged = propertyInfo.PropertyType.IsArray
? property.Value is IEnumerable enumerable && propertyInfo.TrySetArray(enumerable.Cast<Object>(), this.Global)
: this.SetValue(property.Value, originalValue, propertyInfo);
if (!isChanged) {
continue;
}
changedSettings.Add(property.Property);
this.PersistGlobalSettings();
2019-12-04 18:57:18 +01:00
}
return changedSettings;
}
/// <summary>
/// Gets the list.
/// </summary>
/// <returns>A List of ExtendedPropertyInfo of the type T.</returns>
2019-12-08 19:54:52 +01:00
public List<ExtendedPropertyInfo<T>> GetList()
{
Dictionary<String, Object> jsonData = Json.Deserialize(Json.Serialize(this.Global)) as Dictionary<global::System.String, global::System.Object>;
2019-12-04 18:57:18 +01:00
return jsonData?.Keys
.Select(p => new ExtendedPropertyInfo<T>(p) { Value = jsonData[p] })
.ToList();
}
/// <summary>
/// Resets the global settings.
/// </summary>
public void ResetGlobalSettings()
{
2019-12-08 19:54:52 +01:00
lock (this._syncRoot) {
this._global = Activator.CreateInstance<T>();
}
this.PersistGlobalSettings();
2019-12-04 18:57:18 +01:00
}
2019-12-08 19:54:52 +01:00
private Boolean SetValue(Object property, Object originalValue, PropertyInfo propertyInfo)
2019-12-04 18:57:18 +01:00
{
switch (property)
{
case null when originalValue == null:
break;
case null:
2019-12-08 19:54:52 +01:00
propertyInfo.SetValue(this.Global, null);
2019-12-04 18:57:18 +01:00
return true;
default:
2019-12-08 19:54:52 +01:00
if (propertyInfo.PropertyType.TryParseBasicType(property, out Object propertyValue) &&
2019-12-04 18:57:18 +01:00
!propertyValue.Equals(originalValue))
{
2019-12-08 19:54:52 +01:00
propertyInfo.SetValue(this.Global, propertyValue);
2019-12-04 18:57:18 +01:00
return true;
}
break;
}
return false;
}
}
}