#region ========================================================================= USING ===================================================================================== using Lumina.Plugins.Contracts.Core.Metadata; using Lumina.Plugins.Contracts.Core.Plugins; using Lumina.Plugins.OpenLibrary.Common.Models.DTO.Settings; using Lumina.Plugins.OpenLibrary.Core; using Lumina.Plugins.OpenLibrary.Core.Api; using Lumina.Plugins.OpenLibrary.Core.Settings; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using System; using System.Collections.Generic; using System.Threading; #endregion namespace Lumina.Plugins.OpenLibrary.Common.DependencyInjection; /// /// Provides extension methods for registering the Open Library metadata provider services. /// internal static class OpenLibraryServices { /// /// Registers the Open Library book metadata provider and its dependencies, associated with the plugin that provides it. /// /// The service collection to register the services into. /// The unique identifier of the plugin that provides the metadata. /// Action used to configure the . /// The service collection, for chaining. internal static IServiceCollection AddOpenLibraryBookMetadataProvider(this IServiceCollection services, Guid pluginId, Action? settingsCallback = null) { ArgumentNullException.ThrowIfNull(services); // Build the runtime settings per scope, overlaying the settings persisted by the host over the defaults and the optional callback. services.TryAddScoped(serviceProvider => { OpenLibrarySettingsDto openLibrarySettings = new(); settingsCallback?.Invoke(openLibrarySettings); IPluginSettingsStore? settingsStore = serviceProvider.GetService(); if (settingsStore is not null) { IReadOnlyDictionary? storedSettings = settingsStore.GetSettingsAsync(pluginId, CancellationToken.None).GetAwaiter().GetResult(); if (storedSettings is not null) OpenLibrarySettingsLoader.Apply(openLibrarySettings, storedSettings); } return openLibrarySettings; }); services.AddHttpClient(); // Register the metadata provider as a keyed by pluginId transient service, so it can be resolved specifically among other IRemoteMetadataProvider implementations. services.AddKeyedTransient(pluginId); return services; } }