using MareSynchronosShared.Utils; using MareSynchronosShared.Utils.Configuration; using Microsoft.Extensions.Options; using System.Collections; using System.Text; namespace MareSynchronosShared.Services; public class MareConfigurationServiceServer : IConfigurationService where T : class, IMareConfiguration { private readonly IOptionsMonitor _config; public bool IsMain => true; public MareConfigurationServiceServer(IOptionsMonitor config) { _config = config; } public T1 GetValueOrDefault(string key, T1 defaultValue) { return _config.CurrentValue.GetValueOrDefault(key, defaultValue); } public T1 GetValue(string key) { return _config.CurrentValue.GetValue(key); } public override string ToString() { var props = _config.CurrentValue.GetType().GetProperties(); StringBuilder sb = new(); foreach (var prop in props) { var isRemote = prop.GetCustomAttributes(typeof(RemoteConfigurationAttribute), true).Any(); var getValueMethod = GetType().GetMethod(nameof(GetValue)).MakeGenericMethod(prop.PropertyType); var value = isRemote ? getValueMethod.Invoke(this, new[] { prop.Name }) : prop.GetValue(_config.CurrentValue); if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && !typeof(string).IsAssignableFrom(prop.PropertyType)) { var enumVal = (IEnumerable)value; value = string.Empty; foreach (var listVal in enumVal) { value += listVal.ToString() + ", "; } } sb.AppendLine($"{prop.Name} (IsRemote: {isRemote}) => {value}"); } return sb.ToString(); } }