//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ https://slskd.org
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, either version 3 of the License, or
// │ (at your option) any later version.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to
// │ Section 7 of the GNU Affero General Public License. See the
// │ LICENSE file in the root directory of this project for the
// │ complete terms and conditions.
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
namespace slskd.Configuration
{
using System;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
///
/// A that provides a volatile, run-time overlay that supersedes other configuration
/// sources.
///
///
/// To use this provider as intended, the application must instantiate and store an instance of either
/// or , and then invoke with the desired overlay object.
///
/// The Type of the overlay.
public class VolatileOverlayConfigurationProvider : ConfigurationProvider
{
///
/// Initializes a new instance of the class.
///
public VolatileOverlayConfigurationProvider()
{
TargetType = typeof(T);
Namespace = TargetType.Namespace.Split('.').First();
}
///
/// Gets the current overlay value.
///
public T CurrentValue { get; private set; }
private string Namespace { get; set; }
private Type TargetType { get; set; }
///
/// Loads values from the overlay.
///
public override void Load()
{
if (CurrentValue is null)
{
return;
}
// note: pretty much the same as DefaultValueConfigurationProvider; the two should be updated in lockstep
void Map(Type type, string path, object instance)
{
if (instance is null)
{
return;
}
var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in props)
{
var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant());
if (property.PropertyType.Namespace.StartsWith(Namespace))
{
Map(property.PropertyType, key, property.GetValue(instance));
}
else
{
// don't add array values to the configuration; these are additive across providers
// and the default value from the class is "stuck", so adding them again results in duplicates.
if (!property.PropertyType.IsArray)
{
var value = property.GetValue(instance);
if (value != null)
{
Data[key] = value.ToString();
}
}
else
{
// serialize array defaults and stick them on the parent key
// (not indexed by array position). this value is "stuck", and
// we want to show that in the config debug view. this isn't really
// functional, just illustrative.
Data[key] = JsonSerializer.Serialize(property.GetValue(instance));
}
}
}
}
Map(TargetType, Namespace, CurrentValue);
}
///
/// Applies the given .
///
/// An object containing the values to overlay.
public void Apply(T overlay)
{
CurrentValue = overlay;
Load();
OnReload();
}
}
///
/// Represents values that are provided at run-time as an .
///
///
/// To use this provider as intended, the application must instantiate and store an instance of either
/// or , and then invoke with the desired overlay object.
///
/// The Type of the overlay.
public class VolatileOverlayConfigurationSource : IConfigurationSource
{
///
/// Initializes a new instance of the class.
///
public VolatileOverlayConfigurationSource()
{
Provider = new VolatileOverlayConfigurationProvider();
}
///
/// Gets the current overlay value.
///
public T CurrentValue => Provider.CurrentValue;
private VolatileOverlayConfigurationProvider Provider { get; set; }
///
/// Applies the given to the underlying .
///
/// An object containing the values to overlay.
public void Apply(T overlay) => Provider.Apply(overlay);
///
/// Builds the for this source.
///
/// The .
/// A .
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return Provider;
}
}
}