From b66ccf8ffa201c7acaaba5f4e512de3e1ddc694a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:53:00 -0600 Subject: stub a configuration source --- .../RuntimePatchConfigurationSource.cs | 152 +++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs (limited to 'src') diff --git a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs new file mode 100644 index 00000000..84dd8b35 --- /dev/null +++ b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs @@ -0,0 +1,152 @@ +// +// 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); + } + } +} -- cgit v1.2.3