[Plugin system] Move read-only filesystems to dependency injection.

This commit is contained in:
2023-10-05 16:55:03 +01:00
parent bc7e02b24c
commit 696f3ffa9a
13 changed files with 36 additions and 62 deletions

View File

@@ -204,16 +204,14 @@ public class PluginRegisterGenerator : ISourceGenerator
if(readOnlyFileSystems?.Count > 0)
{
sb.AppendLine(" public List<Type> GetAllReadOnlyFilesystemPlugins() => new()");
sb.AppendLine(" public void RegisterReadOnlyFilesystemPlugins(IServiceCollection services)");
sb.AppendLine(" {");
foreach(string plugin in readOnlyFileSystems)
sb.AppendLine($" typeof({plugin}),");
sb.AppendLine(" };");
sb.AppendLine($" services.AddTransient<IReadOnlyFilesystem, {plugin}>();");
sb.AppendLine(" }");
}
else
sb.AppendLine(" public List<Type> GetAllReadOnlyFilesystemPlugins() => null;");
sb.AppendLine(" public void RegisterReadOnlyFilesystemPlugins(IServiceCollection services) {}");
sb.AppendLine();

View File

@@ -132,16 +132,16 @@ public sealed class PluginsViewModel : ViewModelBase
});
}
foreach(Type readOnlyFilesystem in PluginRegister.Singleton.ReadOnlyFilesystems.Values)
foreach(IReadOnlyFilesystem fs in PluginRegister.Singleton.ReadOnlyFilesystems.Values)
{
if(Activator.CreateInstance(readOnlyFilesystem) is not IReadOnlyFilesystem fs)
if(fs is null)
continue;
ReadOnlyFilesystems.Add(new PluginModel
{
Name = fs.Name,
Uuid = fs.Id,
Version = Assembly.GetAssembly(readOnlyFilesystem)?.GetName().Version?.ToString(),
Version = Assembly.GetAssembly(fs.GetType())?.GetName().Version?.ToString(),
Author = fs.Author
});
}

View File

@@ -282,13 +282,9 @@ public abstract class BlockMediaImageTest : BaseMediaImageTest
{
string pluginName = idPlugins[j];
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out Type pluginType))
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out IReadOnlyFilesystem fs))
continue;
Assert.IsNotNull(pluginType, Localization.Could_not_instantiate_filesystem_plugin);
var fs = Activator.CreateInstance(pluginType) as IReadOnlyFilesystem;
Assert.IsNotNull(fs, $"Could not instantiate filesystem {pluginName} in {testFile}");
ErrorNumber error = fs.Mount(image, partitions[i], null, null, null);

View File

@@ -202,15 +202,13 @@ public abstract class OpticalMediaImageTest : BaseMediaImageTest
for(var i = 0; i < track.FileSystems.Length; i++)
{
PluginRegister plugins = PluginRegister.Singleton;
bool found = plugins.Filesystems.TryGetValue(idPlugins[i], out Type pluginType);
bool found = plugins.Filesystems.TryGetValue(idPlugins[i], out IFilesystem fs);
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
// It is not the case, it changes
if(!found)
continue;
var fs = Activator.CreateInstance(pluginType) as IFilesystem;
Assert.NotNull(fs,
string.Format(Localization.Could_not_instantiate_filesystem_for_0,
testFile));
@@ -248,7 +246,7 @@ public abstract class OpticalMediaImageTest : BaseMediaImageTest
Assert.AreEqual(track.FileSystems[i].VolumeSerial, fsMetadata.VolumeSerial,
string.Format(Localization.Volume_serial_0, testFile));
if(Activator.CreateInstance(pluginType) is not IReadOnlyFilesystem rofs)
if(fs is not IReadOnlyFilesystem rofs)
{
if(track.FileSystems[i].Contents != null ||
track.FileSystems[i].ContentsJson != null ||

View File

@@ -124,13 +124,9 @@ public abstract class FsExtractHashIssueTest
{
string pluginName = idPlugins[j];
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out Type pluginType))
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out IReadOnlyFilesystem fs))
continue;
Assert.IsNotNull(pluginType, Localization.Could_not_instantiate_filesystem_plugin);
var fs = Activator.CreateInstance(pluginType) as IReadOnlyFilesystem;
Assert.IsNotNull(fs, string.Format(Localization.Could_not_instantiate_filesystem_0, pluginName));
filesystemFound = true;

View File

@@ -83,13 +83,9 @@ public abstract class FsExtractIssueTest
{
foreach(string pluginName in idPlugins)
{
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out pluginType))
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out IReadOnlyFilesystem fs))
continue;
Assert.IsNotNull(pluginType, Localization.Could_not_instantiate_filesystem_plugin);
var fs = Activator.CreateInstance(pluginType) as IReadOnlyFilesystem;
Assert.IsNotNull(fs, string.Format(Localization.Could_not_instantiate_filesystem_0, pluginName));
filesystemFound = true;
@@ -104,12 +100,7 @@ public abstract class FsExtractIssueTest
}
else
{
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out pluginType);
if(pluginType is null)
continue;
var fs = Activator.CreateInstance(pluginType) as IReadOnlyFilesystem;
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out IReadOnlyFilesystem fs);
Assert.IsNotNull(fs, string.Format(Localization.Could_not_instantiate_filesystem_0, fs?.Name));

View File

@@ -321,15 +321,15 @@ sealed class ExtractFilesCommand : Command
foreach(string pluginName in idPlugins)
{
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out pluginType))
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out IReadOnlyFilesystem fs))
continue;
AaruConsole.WriteLine($"[bold]{string.Format(UI.As_identified_by_0, pluginType.Name)
if(fs is null)
continue;
AaruConsole.WriteLine($"[bold]{string.Format(UI.As_identified_by_0, fs.Name)
}[/]");
if(Activator.CreateInstance(pluginType) is not IReadOnlyFilesystem fs)
continue;
Core.Spectre.ProgressSingleSpinner(ctx =>
{
ctx.AddTask(UI.Mounting_filesystem).IsIndeterminate();
@@ -353,12 +353,12 @@ sealed class ExtractFilesCommand : Command
}
else
{
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out pluginType);
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out IReadOnlyFilesystem fs);
if(pluginType == null || Activator.CreateInstance(pluginType) is not IReadOnlyFilesystem fs)
if(fs is null)
continue;
AaruConsole.WriteLine($"[bold]{string.Format(UI.Identified_by_0, pluginType.Name)}[/]");
AaruConsole.WriteLine($"[bold]{string.Format(UI.Identified_by_0, fs.Name)}[/]");
Core.Spectre.ProgressSingleSpinner(ctx =>
{

View File

@@ -294,9 +294,9 @@ sealed class LsCommand : Command
foreach(string pluginName in idPlugins)
{
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out pluginType))
if(!plugins.ReadOnlyFilesystems.TryGetValue(pluginName, out IReadOnlyFilesystem fs))
continue;
if(Activator.CreateInstance(pluginType) is not IReadOnlyFilesystem fs)
if(fs is null)
continue;
AaruConsole.WriteLine($"[bold]{string.Format(UI.As_identified_by_0, fs.Name)}[/]");
@@ -320,9 +320,9 @@ sealed class LsCommand : Command
}
else
{
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out pluginType);
plugins.ReadOnlyFilesystems.TryGetValue(idPlugins[0], out IReadOnlyFilesystem fs);
if(Activator.CreateInstance(pluginType) is not IReadOnlyFilesystem fs)
if(fs is null)
continue;
AaruConsole.WriteLine($"[bold]{string.Format(UI.Identified_by_0, fs.Name)}[/]");

View File

@@ -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_only_filesystems_options);
foreach(KeyValuePair<string, Type> kvp in plugins.ReadOnlyFilesystems)
foreach(IReadOnlyFilesystem fs in plugins.ReadOnlyFilesystems.Values)
{
if(Activator.CreateInstance(kvp.Value) is not IReadOnlyFilesystem fs)
if(fs is null)
continue;
var options = fs.SupportedOptions.ToList();

View File

@@ -181,7 +181,6 @@ sealed class FormatsCommand : Command
table.AddColumn(UI.Title_Filesystem);
foreach(IFilesystem fs in idOnlyFilesystems)
{
if(verbose)
@@ -205,9 +204,9 @@ sealed class FormatsCommand : Command
table.AddColumn(UI.Title_Filesystem);
foreach(KeyValuePair<string, Type> kvp in plugins.ReadOnlyFilesystems)
foreach(IReadOnlyFilesystem fs in plugins.ReadOnlyFilesystems.Values)
{
if(Activator.CreateInstance(kvp.Value) is not IReadOnlyFilesystem fs)
if(fs is null)
continue;
if(verbose)

View File

@@ -944,7 +944,7 @@ sealed class ConvertImageCommand : Command
if(plugins.ReadOnlyFilesystems.
TryGetValue("iso9660 filesystem",
out Type pluginType))
out IReadOnlyFilesystem rofs))
{
AaruConsole.DebugWriteLine(MODULE_NAME,
UI.Generating_decryption_keys);
@@ -952,7 +952,7 @@ sealed class ConvertImageCommand : Command
generatedTitleKeys =
CSS.GenerateTitleKeys(inputOptical, partitions,
trackSectors,
pluginType);
rofs);
}
}
@@ -999,7 +999,7 @@ sealed class ConvertImageCommand : Command
if(plugins.ReadOnlyFilesystems.
TryGetValue("iso9660 filesystem",
out Type pluginType))
out IReadOnlyFilesystem rofs))
{
AaruConsole.DebugWriteLine(MODULE_NAME,
UI.Generating_decryption_keys);
@@ -1007,7 +1007,7 @@ sealed class ConvertImageCommand : Command
generatedTitleKeys =
CSS.GenerateTitleKeys(inputOptical, partitions,
trackSectors,
pluginType);
rofs);
}
}

View File

@@ -89,12 +89,9 @@ sealed class ListNamespacesCommand : Command
PluginRegister plugins = PluginRegister.Singleton;
foreach(KeyValuePair<string, Type> kvp in plugins.ReadOnlyFilesystems)
foreach(IReadOnlyFilesystem fs in plugins.ReadOnlyFilesystems.Values)
{
if(Activator.CreateInstance(kvp.Value) is not IReadOnlyFilesystem fs)
continue;
if(fs.Namespaces is null)
if(fs?.Namespaces is null)
continue;
Table table = new()