-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleInspector.cs
97 lines (85 loc) · 2.8 KB
/
ConsoleInspector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using IS4.SFI.Analyzers;
using IS4.SFI.Application;
using IS4.SFI.Application.Plugins;
using IS4.SFI.Application.Tools;
using IS4.SFI.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace IS4.SFI.ConsoleApp
{
/// <summary>
/// The specific implementation of <see cref="Inspector"/> for the console application.
/// </summary>
class ConsoleInspector : ExtensibleInspector
{
string baseDirectory = Path.Combine(AppContext.BaseDirectory, "plugins");
#if !SFI_MINIMAL
#if WINDOWS || NETFRAMEWORK
/// <summary>
/// The default image analyzer.
/// </summary>
public ImageAnalyzer? ImageAnalyzer { get; private set; }
#endif
#endif
/// <inheritdoc/>
public ConsoleInspector()
{
}
public async override ValueTask AddDefault()
{
#if !SFI_MINIMAL
#if WINDOWS || NETFRAMEWORK
await LoadAssembly(Formats.All.Provider.Assembly);
await LoadAssembly(Hashes.All.Provider.Assembly);
ImageAnalyzer = Analyzers.OfType<ImageAnalyzer>().FirstOrDefault()!;
var algorithms = ImageAnalyzer.DataHashAlgorithms;
foreach(var algorithm in DataAnalyzer.HashAlgorithms)
{
var algType = algorithm.GetType();
if(!algorithms.Any(a => algType.Equals(a.GetType())))
{
if(algorithm is ICloneable cloneable)
{
algorithms.Add((IDataHashAlgorithm)cloneable.Clone());
}else{
algorithms.Add(algorithm);
}
}
}
#else
await LoadAssembly(Formats.AllPortable.Provider.Assembly);
await LoadAssembly(Hashes.AllPortable.Provider.Assembly);
#endif
#endif
Plugins.Clear();
foreach(var plugin in LoadPlugins())
{
Plugins.Add(plugin);
}
await base.AddDefault();
}
IEnumerable<Plugin> LoadPlugins()
{
if(Directory.Exists(baseDirectory))
{
foreach(var dir in Directory.EnumerateDirectories(baseDirectory))
{
yield return PluginResolvers.GetPluginFromDirectory(dir);
}
foreach(var zip in Directory.EnumerateFiles(baseDirectory, "*.zip"))
{
yield return PluginResolvers.GetPluginFromZip(zip);
}
}
}
/// <inheritdoc/>
protected override void AddLoadDirectories(PluginLoadContext context)
{
context.AddDirectory(baseDirectory);
context.AddDirectory(AppContext.BaseDirectory);
}
}
}