From e8e3a4aa80fbcca9cf8c841c94ff73396c662352 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 19:48:55 -0600 Subject: wip --- .../VolatileOverlayConfigurationSource.cs | 127 +++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs (limited to 'src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs new file mode 100644 index 00000000..30328292 --- /dev/null +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -0,0 +1,127 @@ +// +// 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.Linq; + using System.Reflection; + using System.Text.Json; + using Microsoft.Extensions.Configuration; + + /// + /// A runtime patch . + /// + public class VolatileOverlayConfigurationProvider : ConfigurationProvider + { + // /// + // /// Initializes a new instance of the class. + // /// + // /// The source settings. + // public VolatileOverlayConfigurationProvider(VolatileOverlayConfigurationSource source) + // { + // TargetType = source.TargetType; + // Namespace = TargetType.Namespace.Split('.').First(); + // } + public VolatileOverlayConfigurationProvider() + { + TargetType = typeof(T); + Namespace = TargetType.Namespace.Split('.').First(); + } + + private T Current { get; set; } + private string Namespace { get; set; } + private Type TargetType { get; set; } + + public override void Load() + { + if (Current is null) + { + return; + } + + void Map(Type type, string path) + { + 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); + } + 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(Current); + + 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(Current)); + } + } + } + } + + Map(TargetType, Namespace); + + Console.WriteLine("load called!"); + // Data["slskd:soulseek:listenport"] = Current.Soulseek.ListenPort.ToString(); + + // todo: take the current patch value and stuff the options into Data[key] + Console.WriteLine(Data.ToJson()); + } + + public void Apply(T overlay) + { + Current = overlay; + Load(); + OnReload(); + } + } + + public class VolatileOverlayConfigurationSource : IConfigurationSource + { + public VolatileOverlayConfigurationSource() + { + Provider = new VolatileOverlayConfigurationProvider(); + } + + private VolatileOverlayConfigurationProvider Provider { get; set; } + + public void Apply(T overlay) => Provider.Apply(overlay); + + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return Provider; + } + } +} -- cgit v1.2.3 From 95197676a8712df1e30edece4f20ec39726d1257 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 20:07:43 -0600 Subject: more wip --- .../VolatileOverlayConfigurationSource.cs | 25 +++++----------------- 1 file changed, 5 insertions(+), 20 deletions(-) (limited to 'src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 30328292..9ae9c9ef 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -28,15 +28,6 @@ namespace slskd.Configuration /// public class VolatileOverlayConfigurationProvider : ConfigurationProvider { - // /// - // /// Initializes a new instance of the class. - // /// - // /// The source settings. - // public VolatileOverlayConfigurationProvider(VolatileOverlayConfigurationSource source) - // { - // TargetType = source.TargetType; - // Namespace = TargetType.Namespace.Split('.').First(); - // } public VolatileOverlayConfigurationProvider() { TargetType = typeof(T); @@ -54,7 +45,7 @@ namespace slskd.Configuration return; } - void Map(Type type, string path) + void Map(Type type, string path, object instance) { var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -64,7 +55,7 @@ namespace slskd.Configuration if (property.PropertyType.Namespace.StartsWith(Namespace)) { - Map(property.PropertyType, key); + Map(property.PropertyType, key, property.GetValue(instance)); } else { @@ -72,7 +63,7 @@ namespace slskd.Configuration // and the default value from the class is "stuck", so adding them again results in duplicates. if (!property.PropertyType.IsArray) { - var value = property.GetValue(Current); + var value = property.GetValue(instance); if (value != null) { @@ -85,19 +76,13 @@ namespace slskd.Configuration // (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(Current)); + Data[key] = JsonSerializer.Serialize(property.GetValue(instance)); } } } } - Map(TargetType, Namespace); - - Console.WriteLine("load called!"); - // Data["slskd:soulseek:listenport"] = Current.Soulseek.ListenPort.ToString(); - - // todo: take the current patch value and stuff the options into Data[key] - Console.WriteLine(Data.ToJson()); + Map(TargetType, Namespace, Current); } public void Apply(T overlay) -- cgit v1.2.3 From 894458eaa695a44eed02e5c50190326428337cda Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 08:03:27 -0600 Subject: docs and stuff --- .../DefaultValueConfigurationSource.cs | 1 + .../VolatileOverlayConfigurationSource.cs | 56 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) (limited to 'src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 8cf62e91..f125b1fc 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -85,6 +85,7 @@ namespace slskd.Configuration /// public override void Load() { + // note: pretty much the same as VolatileOverlayConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path) { var defaults = Activator.CreateInstance(type); diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 9ae9c9ef..8707fe69 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -24,27 +24,44 @@ namespace slskd.Configuration using Microsoft.Extensions.Configuration; /// - /// A runtime patch . + /// 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(); } - private T Current { get; set; } + /// + /// 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 (Current is null) + 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) { var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -82,28 +99,57 @@ namespace slskd.Configuration } } - Map(TargetType, Namespace, Current); + Map(TargetType, Namespace, CurrentValue); } + /// + /// Applies the goven . + /// + /// An object containing the values to overlay. public void Apply(T overlay) { - Current = 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; -- cgit v1.2.3 From d70aa1b4427be1a4fed47409720f2035034bf41e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:45:06 -0600 Subject: Update src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 8707fe69..318742e7 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -103,7 +103,7 @@ namespace slskd.Configuration } /// - /// Applies the goven . + /// Applies the given . /// /// An object containing the values to overlay. public void Apply(T overlay) -- cgit v1.2.3 From bf6c8f8974239bf0fcf6d6b141ec674e66c137c6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 17:57:02 -0600 Subject: avoid null refs --- src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 318742e7..0d6debec 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -64,6 +64,11 @@ namespace slskd.Configuration // 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) -- cgit v1.2.3