[Plugin system] Move writable media images to dependency injection.

This commit is contained in:
2023-10-06 00:40:44 +01:00
parent 02a207c320
commit 508b78719d
10 changed files with 31 additions and 56 deletions

View File

@@ -232,16 +232,16 @@ public class PluginRegisterGenerator : ISourceGenerator
if(writableImagePlugins?.Count > 0) if(writableImagePlugins?.Count > 0)
{ {
sb.AppendLine(" public List<Type> GetAllWritableImagePlugins() => new()"); sb.AppendLine(" public void RegisterWritableImagePlugins(IServiceCollection services)");
sb.AppendLine(" {"); sb.AppendLine(" {");
foreach(string plugin in writableImagePlugins) foreach(string plugin in writableImagePlugins)
sb.AppendLine($" typeof({plugin}),"); sb.AppendLine($" services.AddTransient<IBaseWritableImage, {plugin}>();");
sb.AppendLine(" };"); sb.AppendLine(" }");
} }
else else
sb.AppendLine(" public List<Type> GetAllWritableImagePlugins() => null;"); sb.AppendLine(" public void RegisterWritableImagePlugins(IServiceCollection services) {}");
sb.AppendLine(); sb.AppendLine();

View File

@@ -37,5 +37,5 @@ namespace Aaru.Gui.Models;
public sealed class ImagePluginModel public sealed class ImagePluginModel
{ {
public string Name => Plugin.Name; public string Name => Plugin.Name;
public IWritableImage Plugin { get; set; } public IBaseWritableImage Plugin { get; set; }
} }

View File

@@ -30,7 +30,6 @@
// Copyright © 2011-2023 Natalia Portillo // Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reactive; using System.Reactive;
using System.Reflection; using System.Reflection;
@@ -160,16 +159,16 @@ public sealed class PluginsViewModel : ViewModelBase
}); });
} }
foreach(Type baseWritableImageType in PluginRegister.Singleton.WritableImages.Values) foreach(IWritableImage writableImage in PluginRegister.Singleton.WritableImages.Values)
{ {
if(Activator.CreateInstance(baseWritableImageType) is not IWritableImage writableImage) if(writableImage is null)
continue; continue;
WritableImages.Add(new PluginModel WritableImages.Add(new PluginModel
{ {
Name = writableImage.Name, Name = writableImage.Name,
Uuid = writableImage.Id, Uuid = writableImage.Id,
Version = Assembly.GetAssembly(baseWritableImageType)?.GetName().Version?.ToString(), Version = Assembly.GetAssembly(writableImage.GetType())?.GetName().Version?.ToString(),
Author = writableImage.Author Author = writableImage.Author
}); });
} }

View File

@@ -175,9 +175,9 @@ public sealed class ImageConvertViewModel : ViewModelBase
PluginRegister plugins = PluginRegister.Singleton; PluginRegister plugins = PluginRegister.Singleton;
foreach(Type pluginType in plugins.WritableImages.Values) foreach(IBaseWritableImage plugin in plugins.WritableImages.Values)
{ {
if(Activator.CreateInstance(pluginType) is not IWritableImage plugin) if(plugin is null)
continue; continue;
if(plugin.SupportedMediaTypes.Contains(inputFormat.Info.MediaType)) if(plugin.SupportedMediaTypes.Contains(inputFormat.Info.MediaType))

View File

@@ -170,9 +170,9 @@ public sealed class MediaDumpViewModel : ViewModelBase
PluginRegister plugins = PluginRegister.Singleton; PluginRegister plugins = PluginRegister.Singleton;
foreach(Type pluginType in plugins.WritableImages.Values) foreach(IWritableImage plugin in plugins.WritableImages.Values)
{ {
if(Activator.CreateInstance(pluginType) is not IWritableImage plugin) if(plugin is null)
continue; continue;
if(plugin.SupportedMediaTypes.Contains(mediaType)) if(plugin.SupportedMediaTypes.Contains(mediaType))

View File

@@ -31,7 +31,6 @@
// ****************************************************************************/ // ****************************************************************************/
using System; using System;
using System.Collections.Generic;
using System.CommandLine; using System.CommandLine;
using System.CommandLine.NamingConventionBinder; using System.CommandLine.NamingConventionBinder;
using System.Linq; using System.Linq;
@@ -115,9 +114,8 @@ sealed class FormatsCommand : Command
table = new Table table = new Table
{ {
Title = new TableTitle(string.Format(UI.Read_only_media_image_formats_0, Title = new TableTitle(string.Format(UI.Read_only_media_image_formats_0,
plugins.MediaImages.Count(t => !t.Value.GetType(). plugins.MediaImages.Count(t => !plugins.WritableImages.
GetInterfaces(). ContainsKey(t.Key))))
Contains(typeof(IWritableImage)))))
}; };
if(verbose) if(verbose)
@@ -125,11 +123,8 @@ sealed class FormatsCommand : Command
table.AddColumn(UI.Title_Media_image_format); table.AddColumn(UI.Title_Media_image_format);
foreach(IMediaImage imagePlugin in plugins.MediaImages.Values. foreach(IMediaImage imagePlugin in
Where(t => !t.GetType(). plugins.MediaImages.Values.Where(t => !plugins.WritableImages.ContainsKey(t.Name)))
GetInterfaces().
Contains(typeof(IWritableImage))).
Where(t => t is not null))
{ {
if(verbose) if(verbose)
table.AddRow(imagePlugin.Id.ToString(), Markup.Escape(imagePlugin.Name)); table.AddRow(imagePlugin.Id.ToString(), Markup.Escape(imagePlugin.Name));
@@ -151,9 +146,9 @@ sealed class FormatsCommand : Command
table.AddColumn(UI.Title_Media_image_format); table.AddColumn(UI.Title_Media_image_format);
foreach(KeyValuePair<string, Type> kvp in plugins.WritableImages) foreach(IBaseWritableImage plugin in plugins.WritableImages.Values)
{ {
if(Activator.CreateInstance(kvp.Value) is not IBaseWritableImage plugin) if(plugin is null)
continue; continue;
if(verbose) if(verbose)

View File

@@ -536,9 +536,7 @@ sealed class ConvertImageCommand : Command
// Try extension // Try extension
if(string.IsNullOrEmpty(format)) if(string.IsNullOrEmpty(format))
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.KnownExtensions.Contains(Path.GetExtension(outputPath)) where plugin.KnownExtensions.Contains(Path.GetExtension(outputPath))
select plugin); select plugin);
@@ -547,9 +545,7 @@ sealed class ConvertImageCommand : Command
// Try Id // Try Id
else if(Guid.TryParse(format, out Guid outId)) else if(Guid.TryParse(format, out Guid outId))
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.Id.Equals(outId) where plugin.Id.Equals(outId)
select plugin); select plugin);
@@ -558,9 +554,7 @@ sealed class ConvertImageCommand : Command
// Try name // Try name
else else
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase) where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase)
select plugin); select plugin);

View File

@@ -31,7 +31,6 @@
// ****************************************************************************/ // ****************************************************************************/
using System; using System;
using System.Collections.Generic;
using System.CommandLine; using System.CommandLine;
using System.CommandLine.NamingConventionBinder; using System.CommandLine.NamingConventionBinder;
using System.Linq; using System.Linq;
@@ -92,9 +91,9 @@ sealed class ListOptionsCommand : Command
AaruConsole.WriteLine(UI.Read_Write_media_images_options); AaruConsole.WriteLine(UI.Read_Write_media_images_options);
foreach(KeyValuePair<string, Type> kvp in plugins.WritableImages) foreach(IBaseWritableImage plugin in plugins.WritableImages.Values)
{ {
if(Activator.CreateInstance(kvp.Value) is not IBaseWritableImage plugin) if(plugin is null)
continue; continue;
var options = plugin.SupportedOptions.ToList(); var options = plugin.SupportedOptions.ToList();

View File

@@ -377,9 +377,7 @@ sealed class DumpMediaCommand : Command
// Try extension // Try extension
if(string.IsNullOrEmpty(format)) if(string.IsNullOrEmpty(format))
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.KnownExtensions.Contains(extension) where plugin.KnownExtensions.Contains(extension)
select plugin); select plugin);
@@ -388,9 +386,7 @@ sealed class DumpMediaCommand : Command
// Try Id // Try Id
else if(Guid.TryParse(format, out Guid outId)) else if(Guid.TryParse(format, out Guid outId))
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.Id.Equals(outId) where plugin.Id.Equals(outId)
select plugin); select plugin);
@@ -399,9 +395,7 @@ sealed class DumpMediaCommand : Command
// Try name // Try name
else else
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase) where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase)
select plugin); select plugin);
@@ -645,9 +639,7 @@ sealed class DumpMediaCommand : Command
// Try extension // Try extension
if(string.IsNullOrEmpty(format)) if(string.IsNullOrEmpty(format))
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.KnownExtensions.Contains(Path.GetExtension(outputPath)) where plugin.KnownExtensions.Contains(Path.GetExtension(outputPath))
select plugin); select plugin);
@@ -656,9 +648,7 @@ sealed class DumpMediaCommand : Command
// Try Id // Try Id
else if(Guid.TryParse(format, out Guid outId)) else if(Guid.TryParse(format, out Guid outId))
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.Id.Equals(outId) where plugin.Id.Equals(outId)
select plugin); select plugin);
@@ -667,9 +657,7 @@ sealed class DumpMediaCommand : Command
// Try name // Try name
else else
{ {
candidates.AddRange(from pluginType in plugins.WritableImages.Values candidates.AddRange(from plugin in plugins.WritableImages.Values
select Activator.CreateInstance(pluginType) as IBaseWritableImage
into plugin
where plugin is not null where plugin is not null
where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase) where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase)
select plugin); select plugin);