[Plugin system] Move byte addressable media images to dependency injection.

This commit is contained in:
2023-10-06 00:46:36 +01:00
parent 33da34f017
commit 8cc6d3f18b
2 changed files with 20 additions and 16 deletions

View File

@@ -36,8 +36,6 @@
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
@@ -107,7 +105,9 @@ public interface IPluginRegister
/// <param name="services">Service collection</param>
void RegisterArchivePlugins(IServiceCollection services);
/// <summary>Gets all byte addressable plugins</summary>
/// <returns>List of byte addressable plugins</returns>
List<Type> GetAllByteAddressablePlugins();
/// <summary>
/// Registers all byte addressable media image plugins in the provided service collection
/// </summary>
/// <param name="services">Service collection</param>
void RegisterByteAddressablePlugins(IServiceCollection services);
}

View File

@@ -33,7 +33,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Microsoft.Extensions.DependencyInjection;
@@ -44,13 +43,24 @@ public class PluginRegister
{
static PluginRegister _instance;
/// <summary>List of byte addressable image plugins</summary>
public readonly SortedDictionary<string, Type> ByteAddressableImages;
IServiceProvider _serviceProvider;
IServiceCollection _services;
PluginRegister() => ByteAddressableImages = new SortedDictionary<string, Type>();
PluginRegister() {}
/// <summary>List of byte addressable image plugins</summary>
public SortedDictionary<string, IByteAddressableImage> ByteAddressableImages
{
get
{
SortedDictionary<string, IByteAddressableImage> byteAddressableImages = new();
foreach(IByteAddressableImage plugin in _serviceProvider.GetServices<IByteAddressableImage>())
byteAddressableImages[plugin.Name.ToLower()] = plugin;
return byteAddressableImages;
}
}
/// <summary>List of writable media image plugins</summary>
public SortedDictionary<string, IBaseWritableImage> WritableImages
@@ -230,13 +240,7 @@ public class PluginRegister
pluginRegister.RegisterWritableFloppyImagePlugins(_services);
pluginRegister.RegisterWritableImagePlugins(_services);
pluginRegister.RegisterArchivePlugins(_services);
foreach(Type type in pluginRegister.GetAllByteAddressablePlugins() ?? Enumerable.Empty<Type>())
{
if(Activator.CreateInstance(type) is IByteAddressableImage plugin &&
!ByteAddressableImages.ContainsKey(plugin.Name.ToLower()))
ByteAddressableImages.Add(plugin.Name.ToLower(), type);
}
pluginRegister.RegisterByteAddressablePlugins(_services);
}
/// <summary>Gets the filter that allows to read the specified path</summary>