//
// Copyright (c) slskd Team. All rights reserved.
//
// 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/.
//
namespace slskd.Configuration
{
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.Configuration;
///
/// Extension methods for adding .
///
public static class RuntimePatchConfigurationExtensions
{
///
/// Adds a runtime patch configuration source to .
///
/// The to which to add.
/// Configures the source.
/// The updated .
public static IConfigurationBuilder AddRuntimePatch(
this IConfigurationBuilder builder,
Action configureSource)
=> builder.Add(configureSource);
}
///
/// A runtime patch .
///
public class RuntimePatchConfigurationProvider : ConfigurationProvider
{
///
/// Initializes a new instance of the class.
///
/// The source settings.
public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source)
{
Source = source;
source.Provider = this;
}
private RuntimePatchConfigurationSource Source { get; }
///
/// Loads patch values from the current and maps them to the corresponding keys.
///
public override void Load()
{
var data = new Dictionary(StringComparer.OrdinalIgnoreCase);
if (Source.Patch != null)
{
Flatten(Source.Patch, "slskd", data);
}
Data = data;
}
///
/// Applies the specified and triggers a configuration reload.
///
/// The patch to apply.
public void ApplyPatch(OptionsPatch patch)
{
Source.Patch = patch;
Load();
OnReload();
}
private void Flatten(object obj, string path, Dictionary data)
{
if (obj == null)
{
return;
}
var type = obj.GetType();
var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in props)
{
var value = property.GetValue(obj);
if (value == null)
{
continue;
}
var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant());
if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
{
Flatten(value, key, data);
}
else
{
data[key] = value.ToString();
}
}
}
}
///
/// Represents a runtime patch as an .
///
public class RuntimePatchConfigurationSource : IConfigurationSource
{
///
/// Gets or sets the provider instance.
///
internal RuntimePatchConfigurationProvider Provider { get; set; }
///
/// Gets or sets the current patch.
///
internal OptionsPatch Patch { get; set; }
///
/// Builds the for this source.
///
/// The .
/// A .
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new RuntimePatchConfigurationProvider(this);
}
///
/// Applies the specified and triggers a configuration reload.
///
/// The patch to apply.
public void ApplyPatch(OptionsPatch patch)
{
Provider?.ApplyPatch(patch);
}
}
}