mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
[Plugin system] Move writable media images to dependency injection.
This commit is contained in:
Submodule Aaru.CommonTypes updated: a5cdad33c1...33da34f017
@@ -232,16 +232,16 @@ public class PluginRegisterGenerator : ISourceGenerator
|
||||
|
||||
if(writableImagePlugins?.Count > 0)
|
||||
{
|
||||
sb.AppendLine(" public List<Type> GetAllWritableImagePlugins() => new()");
|
||||
sb.AppendLine(" public void RegisterWritableImagePlugins(IServiceCollection services)");
|
||||
sb.AppendLine(" {");
|
||||
|
||||
foreach(string plugin in writableImagePlugins)
|
||||
sb.AppendLine($" typeof({plugin}),");
|
||||
sb.AppendLine($" services.AddTransient<IBaseWritableImage, {plugin}>();");
|
||||
|
||||
sb.AppendLine(" };");
|
||||
sb.AppendLine(" }");
|
||||
}
|
||||
else
|
||||
sb.AppendLine(" public List<Type> GetAllWritableImagePlugins() => null;");
|
||||
sb.AppendLine(" public void RegisterWritableImagePlugins(IServiceCollection services) {}");
|
||||
|
||||
sb.AppendLine();
|
||||
|
||||
|
||||
@@ -36,6 +36,6 @@ namespace Aaru.Gui.Models;
|
||||
|
||||
public sealed class ImagePluginModel
|
||||
{
|
||||
public string Name => Plugin.Name;
|
||||
public IWritableImage Plugin { get; set; }
|
||||
public string Name => Plugin.Name;
|
||||
public IBaseWritableImage Plugin { get; set; }
|
||||
}
|
||||
@@ -30,7 +30,6 @@
|
||||
// Copyright © 2011-2023 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reactive;
|
||||
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;
|
||||
|
||||
WritableImages.Add(new PluginModel
|
||||
{
|
||||
Name = writableImage.Name,
|
||||
Uuid = writableImage.Id,
|
||||
Version = Assembly.GetAssembly(baseWritableImageType)?.GetName().Version?.ToString(),
|
||||
Version = Assembly.GetAssembly(writableImage.GetType())?.GetName().Version?.ToString(),
|
||||
Author = writableImage.Author
|
||||
});
|
||||
}
|
||||
|
||||
@@ -175,9 +175,9 @@ public sealed class ImageConvertViewModel : ViewModelBase
|
||||
|
||||
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;
|
||||
|
||||
if(plugin.SupportedMediaTypes.Contains(inputFormat.Info.MediaType))
|
||||
|
||||
@@ -170,9 +170,9 @@ public sealed class MediaDumpViewModel : ViewModelBase
|
||||
|
||||
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;
|
||||
|
||||
if(plugin.SupportedMediaTypes.Contains(mediaType))
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
// ****************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.NamingConventionBinder;
|
||||
using System.Linq;
|
||||
@@ -115,9 +114,8 @@ sealed class FormatsCommand : Command
|
||||
table = new Table
|
||||
{
|
||||
Title = new TableTitle(string.Format(UI.Read_only_media_image_formats_0,
|
||||
plugins.MediaImages.Count(t => !t.Value.GetType().
|
||||
GetInterfaces().
|
||||
Contains(typeof(IWritableImage)))))
|
||||
plugins.MediaImages.Count(t => !plugins.WritableImages.
|
||||
ContainsKey(t.Key))))
|
||||
};
|
||||
|
||||
if(verbose)
|
||||
@@ -125,11 +123,8 @@ sealed class FormatsCommand : Command
|
||||
|
||||
table.AddColumn(UI.Title_Media_image_format);
|
||||
|
||||
foreach(IMediaImage imagePlugin in plugins.MediaImages.Values.
|
||||
Where(t => !t.GetType().
|
||||
GetInterfaces().
|
||||
Contains(typeof(IWritableImage))).
|
||||
Where(t => t is not null))
|
||||
foreach(IMediaImage imagePlugin in
|
||||
plugins.MediaImages.Values.Where(t => !plugins.WritableImages.ContainsKey(t.Name)))
|
||||
{
|
||||
if(verbose)
|
||||
table.AddRow(imagePlugin.Id.ToString(), Markup.Escape(imagePlugin.Name));
|
||||
@@ -151,9 +146,9 @@ sealed class FormatsCommand : Command
|
||||
|
||||
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;
|
||||
|
||||
if(verbose)
|
||||
|
||||
@@ -536,9 +536,7 @@ sealed class ConvertImageCommand : Command
|
||||
// Try extension
|
||||
if(string.IsNullOrEmpty(format))
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.KnownExtensions.Contains(Path.GetExtension(outputPath))
|
||||
select plugin);
|
||||
@@ -547,9 +545,7 @@ sealed class ConvertImageCommand : Command
|
||||
// Try Id
|
||||
else if(Guid.TryParse(format, out Guid outId))
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.Id.Equals(outId)
|
||||
select plugin);
|
||||
@@ -558,9 +554,7 @@ sealed class ConvertImageCommand : Command
|
||||
// Try name
|
||||
else
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase)
|
||||
select plugin);
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
// ****************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.NamingConventionBinder;
|
||||
using System.Linq;
|
||||
@@ -92,9 +91,9 @@ sealed class ListOptionsCommand : Command
|
||||
|
||||
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;
|
||||
|
||||
var options = plugin.SupportedOptions.ToList();
|
||||
|
||||
@@ -377,9 +377,7 @@ sealed class DumpMediaCommand : Command
|
||||
// Try extension
|
||||
if(string.IsNullOrEmpty(format))
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.KnownExtensions.Contains(extension)
|
||||
select plugin);
|
||||
@@ -388,9 +386,7 @@ sealed class DumpMediaCommand : Command
|
||||
// Try Id
|
||||
else if(Guid.TryParse(format, out Guid outId))
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.Id.Equals(outId)
|
||||
select plugin);
|
||||
@@ -399,9 +395,7 @@ sealed class DumpMediaCommand : Command
|
||||
// Try name
|
||||
else
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase)
|
||||
select plugin);
|
||||
@@ -645,9 +639,7 @@ sealed class DumpMediaCommand : Command
|
||||
// Try extension
|
||||
if(string.IsNullOrEmpty(format))
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.KnownExtensions.Contains(Path.GetExtension(outputPath))
|
||||
select plugin);
|
||||
@@ -656,9 +648,7 @@ sealed class DumpMediaCommand : Command
|
||||
// Try Id
|
||||
else if(Guid.TryParse(format, out Guid outId))
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.Id.Equals(outId)
|
||||
select plugin);
|
||||
@@ -667,9 +657,7 @@ sealed class DumpMediaCommand : Command
|
||||
// Try name
|
||||
else
|
||||
{
|
||||
candidates.AddRange(from pluginType in plugins.WritableImages.Values
|
||||
select Activator.CreateInstance(pluginType) as IBaseWritableImage
|
||||
into plugin
|
||||
candidates.AddRange(from plugin in plugins.WritableImages.Values
|
||||
where plugin is not null
|
||||
where plugin.Name.Equals(format, StringComparison.InvariantCultureIgnoreCase)
|
||||
select plugin);
|
||||
|
||||
Reference in New Issue
Block a user