[Plugin system] Move filters to dependency injection.

This commit is contained in:
2023-10-05 16:00:59 +01:00
parent d626697942
commit 87c8242363
3 changed files with 61 additions and 127 deletions

View File

@@ -1,116 +0,0 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Filters.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Filters.
//
// --[ Description ] ----------------------------------------------------------
//
// Enumerates all filters and instantiates the correct one.
//
// --[ License ] --------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
namespace Aaru.CommonTypes;
/// <summary>Manages the known filters</summary>
public sealed class FiltersList
{
/// <summary>List of known filters</summary>
public readonly SortedDictionary<string, IFilter> Filters;
/// <summary>Fills the list of all known filters</summary>
public FiltersList()
{
var assembly = Assembly.Load("Aaru.Filters");
Filters = new SortedDictionary<string, IFilter>();
foreach(Type type in assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IFilter))))
{
try
{
var filter = (IFilter)type.GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty<object>());
if(filter != null && !Filters.ContainsKey(filter.Name.ToLower()))
Filters.Add(filter.Name.ToLower(), filter);
}
catch(Exception exception)
{
AaruConsole.ErrorWriteLine(Localization.Exception_0, exception);
}
}
}
/// <summary>Gets the filter that allows to read the specified path</summary>
/// <param name="path">Path</param>
/// <returns>The filter that allows reading the specified path</returns>
public IFilter GetFilter(string path)
{
IFilter noFilter = null;
foreach(IFilter filter in Filters.Values)
{
try
{
if(filter.Id != new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
{
if(!filter.Identify(path))
continue;
var foundFilter =
(IFilter)filter.GetType().GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty<object>());
if(foundFilter?.Open(path) == ErrorNumber.NoError)
return foundFilter;
}
else
noFilter = filter;
}
catch(IOException)
{
// Ignore and continue
}
}
if(!noFilter?.Identify(path) == true)
return null;
noFilter?.Open(path);
return noFilter;
}
}

View File

@@ -57,9 +57,11 @@ public interface IPluginRegister
/// <returns>List of filesystem plugins</returns>
List<Type> GetAllFilesystemPlugins();
/// <summary>Gets all filter plugins</summary>
/// <returns>List of filter plugins</returns>
List<Type> GetAllFilterPlugins();
/// <summary>
/// Registers all filter plugins in the provided service collection
/// </summary>
/// <param name="services">Service collection</param>
void RegisterFilterPlugins(IServiceCollection services);
/// <summary>Gets all floppy image plugins</summary>
/// <returns>List of floppy image plugins</returns>

View File

@@ -32,7 +32,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Microsoft.Extensions.DependencyInjection;
@@ -47,8 +49,7 @@ public class PluginRegister
public readonly SortedDictionary<string, Type> ByteAddressableImages;
/// <summary>List of all filesystem plugins</summary>
public readonly SortedDictionary<string, Type> Filesystems;
/// <summary>List of filter plugins</summary>
public readonly SortedDictionary<string, Type> Filters;
/// <summary>List of floppy image plugins</summary>
public readonly SortedDictionary<string, Type> FloppyImages;
/// <summary>List of all media image plugins</summary>
@@ -66,7 +67,6 @@ public class PluginRegister
PluginRegister()
{
Filters = new SortedDictionary<string, Type>();
Filesystems = new SortedDictionary<string, Type>();
ReadOnlyFilesystems = new SortedDictionary<string, Type>();
Partitions = new SortedDictionary<string, Type>();
@@ -78,6 +78,19 @@ public class PluginRegister
ByteAddressableImages = new SortedDictionary<string, Type>();
}
/// <summary>List of filter plugins</summary>
public SortedDictionary<string, IFilter> Filters
{
get
{
SortedDictionary<string, IFilter> filters = new();
foreach(IFilter plugin in _serviceProvider.GetServices<IFilter>())
filters.Add(plugin.Name.ToLower(), plugin);
return filters;
}
}
/// <summary>List of checksum plugins</summary>
public SortedDictionary<string, IChecksum> Checksums
{
@@ -137,11 +150,7 @@ public class PluginRegister
Filesystems.Add(plugin.Name.ToLower(), type);
}
foreach(Type type in pluginRegister.GetAllFilterPlugins() ?? Enumerable.Empty<Type>())
{
if(Activator.CreateInstance(type) is IFilter plugin && !Filters.ContainsKey(plugin.Name.ToLower()))
Filters.Add(plugin.Name.ToLower(), type);
}
pluginRegister.RegisterFilterPlugins(_services);
foreach(Type type in pluginRegister.GetAllFloppyImagePlugins() ?? Enumerable.Empty<Type>())
{
@@ -196,4 +205,43 @@ public class PluginRegister
ByteAddressableImages.Add(plugin.Name.ToLower(), type);
}
}
/// <summary>Gets the filter that allows to read the specified path</summary>
/// <param name="path">Path</param>
/// <returns>The filter that allows reading the specified path</returns>
public IFilter GetFilter(string path)
{
IFilter noFilter = null;
foreach(IFilter filter in Filters.Values)
{
try
{
if(filter.Id != new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
{
if(!filter.Identify(path))
continue;
var foundFilter =
(IFilter)filter.GetType().GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty<object>());
if(foundFilter?.Open(path) == ErrorNumber.NoError)
return foundFilter;
}
else
noFilter = filter;
}
catch(IOException)
{
// Ignore and continue
}
}
if(!noFilter?.Identify(path) == true)
return null;
noFilter?.Open(path);
return noFilter;
}
}