From 33da34f017ccb082c5ab639267d9c7b0b5b67009 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Fri, 6 Oct 2023 00:40:37 +0100 Subject: [PATCH] [Plugin system] Move writable media images to dependency injection. --- Interfaces/IPluginRegister.cs | 8 +++++--- PluginRegister.cs | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Interfaces/IPluginRegister.cs b/Interfaces/IPluginRegister.cs index df6ca34..7e35cb1 100644 --- a/Interfaces/IPluginRegister.cs +++ b/Interfaces/IPluginRegister.cs @@ -95,9 +95,11 @@ public interface IPluginRegister /// Service collection void RegisterWritableFloppyImagePlugins(IServiceCollection services); - /// Gets all writable media image plugins - /// List of writable media image plugins - List GetAllWritableImagePlugins(); + /// + /// Registers all writable media image plugins in the provided service collection + /// + /// Service collection + void RegisterWritableImagePlugins(IServiceCollection services); /// /// Registers all archive plugins in the provided service collection diff --git a/PluginRegister.cs b/PluginRegister.cs index 6e2f10c..e561a3d 100644 --- a/PluginRegister.cs +++ b/PluginRegister.cs @@ -47,15 +47,22 @@ public class PluginRegister /// List of byte addressable image plugins public readonly SortedDictionary ByteAddressableImages; - /// List of writable media image plugins - public readonly SortedDictionary WritableImages; IServiceProvider _serviceProvider; IServiceCollection _services; - PluginRegister() + PluginRegister() => ByteAddressableImages = new SortedDictionary(); + + /// List of writable media image plugins + public SortedDictionary WritableImages { - WritableImages = new SortedDictionary(); - ByteAddressableImages = new SortedDictionary(); + get + { + SortedDictionary mediaImages = new(); + foreach(IBaseWritableImage plugin in _serviceProvider.GetServices()) + mediaImages[plugin.Name.ToLower()] = plugin; + + return mediaImages; + } } /// List of writable floppy image plugins @@ -221,14 +228,7 @@ public class PluginRegister pluginRegister.RegisterMediaImagePlugins(_services); pluginRegister.RegisterPartitionPlugins(_services); pluginRegister.RegisterWritableFloppyImagePlugins(_services); - - foreach(Type type in pluginRegister.GetAllWritableImagePlugins() ?? Enumerable.Empty()) - { - if(Activator.CreateInstance(type) is IBaseWritableImage plugin && - !WritableImages.ContainsKey(plugin.Name.ToLower())) - WritableImages.Add(plugin.Name.ToLower(), type); - } - + pluginRegister.RegisterWritableImagePlugins(_services); pluginRegister.RegisterArchivePlugins(_services); foreach(Type type in pluginRegister.GetAllByteAddressablePlugins() ?? Enumerable.Empty())