#region ========================================================================= USING ===================================================================================== using Lumina.Plugins.OpenLibrary.Common.Models.DTO.Settings; using System; using System.Collections.Generic; using System.Globalization; #endregion namespace Lumina.Plugins.OpenLibrary.Core.Settings; /// /// Applies the settings persisted by the host onto the runtime settings of the Open Library metadata plugin. /// internal static class OpenLibrarySettingsLoader { /// /// Overlays the persisted onto the provided , keeping the default values of the settings that were not persisted. /// /// The runtime settings onto which the persisted settings are applied. /// The settings persisted by the host, keyed by setting key. public static void Apply(OpenLibrarySettingsDto settings, IReadOnlyDictionary storedSettings) { if (storedSettings.TryGetValue(OpenLibrarySettingsKeys.CONTACT_EMAIL, out string? contactEmail)) { string? trimmedContactEmail = string.IsNullOrWhiteSpace(contactEmail) ? null : contactEmail.Trim(); settings.ContactEmail = trimmedContactEmail is not null && IsSafeContactEmail(trimmedContactEmail) ? trimmedContactEmail : null; } if (storedSettings.TryGetValue(OpenLibrarySettingsKeys.SEARCH_RESULT_LIMIT, out string? searchResultLimit) && int.TryParse(searchResultLimit, NumberStyles.Integer, CultureInfo.InvariantCulture, out int searchResultLimitValue)) settings.SearchResultLimit = Math.Max(1, searchResultLimitValue); if (storedSettings.TryGetValue(OpenLibrarySettingsKeys.WORK_EDITION_LIMIT, out string? workEditionLimit) && int.TryParse(workEditionLimit, NumberStyles.Integer, CultureInfo.InvariantCulture, out int workEditionLimitValue)) settings.WorkEditionLimit = Math.Max(1, workEditionLimitValue); if (storedSettings.TryGetValue(OpenLibrarySettingsKeys.MINIMUM_REQUEST_INTERVAL_SECONDS, out string? minimumRequestInterval) && double.TryParse(minimumRequestInterval, NumberStyles.Float, CultureInfo.InvariantCulture, out double minimumRequestIntervalValue)) settings.MinimumRequestInterval = TimeSpan.FromSeconds(Math.Max(0, minimumRequestIntervalValue)); } /// /// Determines whether the persisted contact email only contains characters that are safe inside the request from header. /// /// The persisted contact email. /// when the email is safe, otherwise . private static bool IsSafeContactEmail(string contactEmail) { foreach (char character in contactEmail) if (!char.IsAsciiLetterOrDigit(character) && character is not ('@' or '.' or '_' or '-' or '+')) return false; return true; } }