mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 16:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
@@ -0,0 +1,27 @@
|
||||
namespace Lumina.Plugins.OpenLibrary.Core.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Keys of the settings of the Open Library metadata plugin, shared by the settings schema and the settings loader.
|
||||
/// </summary>
|
||||
internal static class OpenLibrarySettingsKeys
|
||||
{
|
||||
/// <summary>
|
||||
/// The key of the setting defining the contact email sent to the Open Library API.
|
||||
/// </summary>
|
||||
internal const string CONTACT_EMAIL = "ContactEmail";
|
||||
|
||||
/// <summary>
|
||||
/// The key of the setting defining the maximum number of results returned by a single search.
|
||||
/// </summary>
|
||||
internal const string SEARCH_RESULT_LIMIT = "SearchResultLimit";
|
||||
|
||||
/// <summary>
|
||||
/// The key of the setting defining the maximum number of editions fetched for a single work.
|
||||
/// </summary>
|
||||
internal const string WORK_EDITION_LIMIT = "WorkEditionLimit";
|
||||
|
||||
/// <summary>
|
||||
/// The key of the setting defining the minimum interval between consecutive requests to the Open Library API, in seconds.
|
||||
/// </summary>
|
||||
internal const string MINIMUM_REQUEST_INTERVAL_SECONDS = "MinimumRequestIntervalSeconds";
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#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;
|
||||
|
||||
/// <summary>
|
||||
/// Applies the settings persisted by the host onto the runtime settings of the Open Library metadata plugin.
|
||||
/// </summary>
|
||||
internal static class OpenLibrarySettingsLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// Overlays the persisted <paramref name="storedSettings"/> onto the provided <paramref name="settings"/>, keeping the default values of the settings that were not persisted.
|
||||
/// </summary>
|
||||
/// <param name="settings">The runtime settings onto which the persisted settings are applied.</param>
|
||||
/// <param name="storedSettings">The settings persisted by the host, keyed by setting key.</param>
|
||||
public static void Apply(OpenLibrarySettingsDto settings, IReadOnlyDictionary<string, string> 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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the persisted contact email only contains characters that are safe inside the request from header.
|
||||
/// </summary>
|
||||
/// <param name="contactEmail">The persisted contact email.</param>
|
||||
/// <returns><see langword="true"/> when the email is safe, otherwise <see langword="false"/>.</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Plugins.Contracts.Core.Plugins;
|
||||
using Lumina.Plugins.OpenLibrary.Common.Models.DTO.Settings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.OpenLibrary.Core.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Provides the runtime settings of the Open Library metadata plugin, overlaying the settings persisted by the host over the configured defaults.
|
||||
/// </summary>
|
||||
internal sealed class OpenLibrarySettingsProvider
|
||||
{
|
||||
private readonly IPluginSettingsStore? _settingsStore;
|
||||
private readonly Guid _pluginId;
|
||||
private readonly OpenLibrarySettingsDto _defaults;
|
||||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||||
private OpenLibrarySettingsDto? _runtimeSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="OpenLibrarySettingsProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="settingsStore">The store of the settings persisted by the host, or <see langword="null"/> when no store is available.</param>
|
||||
/// <param name="pluginId">The unique identifier of the plugin whose settings are read.</param>
|
||||
/// <param name="defaults">The runtime settings with the default values and the optional configuration callback already applied.</param>
|
||||
public OpenLibrarySettingsProvider(IPluginSettingsStore? settingsStore, Guid pluginId, OpenLibrarySettingsDto defaults)
|
||||
{
|
||||
_settingsStore = settingsStore;
|
||||
_pluginId = pluginId;
|
||||
_defaults = defaults;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the runtime settings of the plugin, reading and applying the settings persisted by the host on the first call.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>The runtime settings of the plugin.</returns>
|
||||
public async Task<OpenLibrarySettingsDto> GetAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
OpenLibrarySettingsDto? runtimeSettings = Volatile.Read(ref _runtimeSettings);
|
||||
if (runtimeSettings is not null)
|
||||
return runtimeSettings;
|
||||
|
||||
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
runtimeSettings = Volatile.Read(ref _runtimeSettings);
|
||||
if (runtimeSettings is null)
|
||||
{
|
||||
runtimeSettings = Clone(_defaults);
|
||||
if (_settingsStore is not null)
|
||||
{
|
||||
IReadOnlyDictionary<string, string>? storedSettings = await _settingsStore.GetSettingsAsync(_pluginId, cancellationToken).ConfigureAwait(false);
|
||||
if (storedSettings is not null)
|
||||
OpenLibrarySettingsLoader.Apply(runtimeSettings, storedSettings);
|
||||
}
|
||||
|
||||
Volatile.Write(ref _runtimeSettings, runtimeSettings);
|
||||
}
|
||||
|
||||
return runtimeSettings;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy of the given runtime settings.
|
||||
/// </summary>
|
||||
/// <param name="source">The runtime settings to copy.</param>
|
||||
/// <returns>A copy of the given runtime settings.</returns>
|
||||
private static OpenLibrarySettingsDto Clone(OpenLibrarySettingsDto source)
|
||||
{
|
||||
return new OpenLibrarySettingsDto
|
||||
{
|
||||
UserAgent = source.UserAgent,
|
||||
ContactEmail = source.ContactEmail,
|
||||
SearchResultLimit = source.SearchResultLimit,
|
||||
WorkEditionLimit = source.WorkEditionLimit,
|
||||
MinimumRequestInterval = source.MinimumRequestInterval
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user