2023-10-05 13:47:51 +01:00
|
|
|
// /***************************************************************************
|
|
|
|
|
// Aaru Data Preservation Suite
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : PluginRegister.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Common types.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Gets lists of all known plugins.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Copyright © 2011-2023 Natalia Portillo
|
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-10-05 16:00:59 +01:00
|
|
|
using System.IO;
|
2023-10-05 13:47:51 +01:00
|
|
|
using System.Linq;
|
2023-10-05 16:00:59 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2023-10-05 13:47:51 +01:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Aaru.CommonTypes;
|
|
|
|
|
|
|
|
|
|
public class PluginRegister
|
|
|
|
|
{
|
|
|
|
|
static PluginRegister _instance;
|
2023-10-05 16:19:54 +01:00
|
|
|
|
2023-10-05 13:47:51 +01:00
|
|
|
/// <summary>List of byte addressable image plugins</summary>
|
|
|
|
|
public readonly SortedDictionary<string, Type> ByteAddressableImages;
|
2023-10-05 16:00:59 +01:00
|
|
|
|
2023-10-05 13:47:51 +01:00
|
|
|
/// <summary>List of floppy image plugins</summary>
|
|
|
|
|
public readonly SortedDictionary<string, Type> FloppyImages;
|
|
|
|
|
/// <summary>List of writable floppy image plugins</summary>
|
|
|
|
|
public readonly SortedDictionary<string, Type> WritableFloppyImages;
|
|
|
|
|
/// <summary>List of writable media image plugins</summary>
|
|
|
|
|
public readonly SortedDictionary<string, Type> WritableImages;
|
|
|
|
|
IServiceProvider _serviceProvider;
|
|
|
|
|
IServiceCollection _services;
|
|
|
|
|
|
|
|
|
|
PluginRegister()
|
|
|
|
|
{
|
|
|
|
|
WritableImages = new SortedDictionary<string, Type>();
|
|
|
|
|
FloppyImages = new SortedDictionary<string, Type>();
|
|
|
|
|
WritableFloppyImages = new SortedDictionary<string, Type>();
|
|
|
|
|
ByteAddressableImages = new SortedDictionary<string, Type>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 23:57:12 +01:00
|
|
|
/// <summary>List of all media image plugins</summary>
|
|
|
|
|
public SortedDictionary<string, IMediaImage> MediaImages
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IMediaImage> mediaImages = new();
|
|
|
|
|
foreach(IMediaImage plugin in _serviceProvider.GetServices<IMediaImage>())
|
|
|
|
|
mediaImages[plugin.Name.ToLower()] = plugin;
|
|
|
|
|
|
|
|
|
|
return mediaImages;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:54:55 +01:00
|
|
|
/// <summary>List of read-only filesystem plugins</summary>
|
|
|
|
|
public SortedDictionary<string, IReadOnlyFilesystem> ReadOnlyFilesystems
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IReadOnlyFilesystem> readOnlyFilesystems = new();
|
|
|
|
|
foreach(IReadOnlyFilesystem plugin in _serviceProvider.GetServices<IReadOnlyFilesystem>())
|
|
|
|
|
readOnlyFilesystems[plugin.Name.ToLower()] = plugin;
|
|
|
|
|
|
|
|
|
|
return readOnlyFilesystems;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:39:00 +01:00
|
|
|
/// <summary>List of all filesystem plugins</summary>
|
|
|
|
|
public SortedDictionary<string, IFilesystem> Filesystems
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IFilesystem> filesystems = new();
|
|
|
|
|
foreach(IFilesystem plugin in _serviceProvider.GetServices<IFilesystem>())
|
|
|
|
|
filesystems[plugin.Name.ToLower()] = plugin;
|
|
|
|
|
|
|
|
|
|
return filesystems;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:19:54 +01:00
|
|
|
/// <summary>List of all archive formats</summary>
|
|
|
|
|
public SortedDictionary<string, IArchive> Archives
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IArchive> archives = new();
|
|
|
|
|
foreach(IArchive plugin in _serviceProvider.GetServices<IArchive>())
|
2023-10-05 16:39:00 +01:00
|
|
|
archives[plugin.Name.ToLower()] = plugin;
|
2023-10-05 16:19:54 +01:00
|
|
|
|
|
|
|
|
return archives;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:11:05 +01:00
|
|
|
/// <summary>List of all partition plugins</summary>
|
|
|
|
|
public SortedDictionary<string, IPartition> Partitions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IPartition> partitions = new();
|
|
|
|
|
foreach(IPartition plugin in _serviceProvider.GetServices<IPartition>())
|
2023-10-05 16:39:00 +01:00
|
|
|
partitions[plugin.Name.ToLower()] = plugin;
|
2023-10-05 16:11:05 +01:00
|
|
|
|
|
|
|
|
return partitions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:00:59 +01:00
|
|
|
/// <summary>List of filter plugins</summary>
|
|
|
|
|
public SortedDictionary<string, IFilter> Filters
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IFilter> filters = new();
|
|
|
|
|
foreach(IFilter plugin in _serviceProvider.GetServices<IFilter>())
|
2023-10-05 16:39:00 +01:00
|
|
|
filters[plugin.Name.ToLower()] = plugin;
|
2023-10-05 16:00:59 +01:00
|
|
|
|
|
|
|
|
return filters;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 13:47:51 +01:00
|
|
|
/// <summary>List of checksum plugins</summary>
|
|
|
|
|
public SortedDictionary<string, IChecksum> Checksums
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SortedDictionary<string, IChecksum> checksums = new();
|
|
|
|
|
foreach(IChecksum plugin in _serviceProvider.GetServices<IChecksum>())
|
2023-10-05 16:39:00 +01:00
|
|
|
checksums[plugin.Name.ToLower()] = plugin;
|
2023-10-05 13:47:51 +01:00
|
|
|
|
|
|
|
|
return checksums;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets a singleton with all the known plugins</summary>
|
|
|
|
|
public static PluginRegister Singleton
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(_instance != null)
|
|
|
|
|
return _instance;
|
|
|
|
|
|
|
|
|
|
_instance = new PluginRegister
|
|
|
|
|
{
|
|
|
|
|
_services = new ServiceCollection()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_instance._serviceProvider = _instance._services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Replaces registered plugins list of this instance with the new ones provided by the providen registrators.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="registrators">List of plugin registrators as obtained from the assemblies that implement them.</param>
|
|
|
|
|
public void InitPlugins(IEnumerable<IPluginRegister> registrators)
|
|
|
|
|
{
|
|
|
|
|
_services = new ServiceCollection();
|
|
|
|
|
|
|
|
|
|
foreach(IPluginRegister registrator in registrators)
|
|
|
|
|
AddPlugins(registrator);
|
|
|
|
|
|
|
|
|
|
_instance._serviceProvider = _instance._services.BuildServiceProvider();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds plugins to the central plugin register</summary>
|
|
|
|
|
/// <param name="pluginRegister">Plugin register</param>
|
|
|
|
|
void AddPlugins(IPluginRegister pluginRegister)
|
|
|
|
|
{
|
|
|
|
|
pluginRegister.RegisterChecksumPlugins(_services);
|
2023-10-05 16:39:00 +01:00
|
|
|
pluginRegister.RegisterFilesystemPlugins(_services);
|
2023-10-05 16:00:59 +01:00
|
|
|
pluginRegister.RegisterFilterPlugins(_services);
|
2023-10-05 16:54:55 +01:00
|
|
|
pluginRegister.RegisterReadOnlyFilesystemPlugins(_services);
|
2023-10-05 13:47:51 +01:00
|
|
|
|
|
|
|
|
foreach(Type type in pluginRegister.GetAllFloppyImagePlugins() ?? Enumerable.Empty<Type>())
|
|
|
|
|
{
|
|
|
|
|
if(Activator.CreateInstance(type) is IFloppyImage plugin &&
|
|
|
|
|
!FloppyImages.ContainsKey(plugin.Name.ToLower()))
|
|
|
|
|
FloppyImages.Add(plugin.Name.ToLower(), type);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 23:57:12 +01:00
|
|
|
pluginRegister.RegisterMediaImagePlugins(_services);
|
2023-10-05 16:11:05 +01:00
|
|
|
pluginRegister.RegisterPartitionPlugins(_services);
|
2023-10-05 13:47:51 +01:00
|
|
|
|
|
|
|
|
foreach(Type type in pluginRegister.GetAllWritableFloppyImagePlugins() ?? Enumerable.Empty<Type>())
|
|
|
|
|
{
|
|
|
|
|
if(Activator.CreateInstance(type) is IWritableFloppyImage plugin &&
|
|
|
|
|
!WritableFloppyImages.ContainsKey(plugin.Name.ToLower()))
|
|
|
|
|
WritableFloppyImages.Add(plugin.Name.ToLower(), type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(Type type in pluginRegister.GetAllWritableImagePlugins() ?? Enumerable.Empty<Type>())
|
|
|
|
|
{
|
|
|
|
|
if(Activator.CreateInstance(type) is IBaseWritableImage plugin &&
|
|
|
|
|
!WritableImages.ContainsKey(plugin.Name.ToLower()))
|
|
|
|
|
WritableImages.Add(plugin.Name.ToLower(), type);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 16:19:54 +01:00
|
|
|
pluginRegister.RegisterArchivePlugins(_services);
|
2023-10-05 13:47:51 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-05 16:00:59 +01:00
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2023-10-05 13:47:51 +01:00
|
|
|
}
|