namespace Unosquare.Swan.Abstractions
{
using Formatters;
using Reflection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
///
/// Represents a provider to save and load settings using a plain JSON file.
///
///
/// The following example shows how to save and load settings.
///
/// using Unosquare.Swan.Abstractions;
///
/// public class Example
/// {
/// public static void Main()
/// {
/// // get user from settings
/// var user = SettingsProvider<Settings>.Instance.Global.User;
///
/// // modify the port
/// SettingsProvider<Settings>.Instance.Global.Port = 20;
///
/// // if we want these settings to persist
/// SettingsProvider<Settings>.Instance.PersistGlobalSettings();
/// }
///
/// public class Settings
/// {
/// public int Port { get; set; } = 9696;
///
/// public string User { get; set; } = "User";
/// }
/// }
///
///
/// The type of settings model.
public sealed class SettingsProvider
: SingletonBase>
{
private readonly object _syncRoot = new object();
private T _global;
///
/// Gets or sets the configuration file path. By default the entry assembly directory is used
/// and the filename is 'appsettings.json'.
///
///
/// The configuration file path.
///
public string ConfigurationFilePath { get; set; } =
#if NETSTANDARD1_3
Path.Combine(Runtime.LocalStoragePath, "appsettings.json");
#else
Path.Combine(Runtime.EntryAssemblyDirectory, "appsettings.json");
#endif
///
/// Gets the global settings object.
///
///
/// The global settings object.
///
public T Global
{
get
{
lock (_syncRoot)
{
if (Equals(_global, default(T)))
ReloadGlobalSettings();
return _global;
}
}
}
///
/// Reloads the global settings.
///
public void ReloadGlobalSettings()
{
if (File.Exists(ConfigurationFilePath) == false || File.ReadAllText(ConfigurationFilePath).Length == 0)
{
ResetGlobalSettings();
return;
}
lock (_syncRoot)
_global = Json.Deserialize(File.ReadAllText(ConfigurationFilePath));
}
///
/// Persists the global settings.
///
public void PersistGlobalSettings() => File.WriteAllText(ConfigurationFilePath, Json.Serialize(Global, true));
///
/// Updates settings from list.
///
/// The list.
///
/// A list of settings of type ref="ExtendedPropertyInfo".
///
/// propertyList.
public List RefreshFromList(List> propertyList)
{
if (propertyList == null)
throw new ArgumentNullException(nameof(propertyList));
var changedSettings = new List();
var globalProps = Runtime.PropertyTypeCache.RetrieveAllProperties();
foreach (var property in propertyList)
{
var propertyInfo = globalProps.FirstOrDefault(x => x.Name == property.Property);
if (propertyInfo == null) continue;
var originalValue = propertyInfo.GetValue(Global);
var isChanged = propertyInfo.PropertyType.IsArray
? property.Value is IEnumerable enumerable && propertyInfo.TrySetArray(enumerable.Cast