-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArchiveFactory.cs
101 lines (61 loc) · 3.09 KB
/
ArchiveFactory.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
98
99
100
101
using Gsemac.Reflection.Plugins;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Gsemac.IO.Compression {
public class ArchiveFactory :
IArchiveFactory {
// Public members
public static ArchiveFactory Default => new ArchiveFactory();
public ArchiveFactory() :
this(null) {
}
public ArchiveFactory(IServiceProvider serviceProvider) {
pluginLoader = CreatePluginLoader(serviceProvider);
}
public IEnumerable<ICodecCapabilities> GetSupportedFileFormats() {
return GetSupportedArchiveFormats();
}
public IArchive Open(Stream stream, IFileFormat archiveFormat, IArchiveOptions archiveOptions) {
if (stream is null)
throw new ArgumentNullException(nameof(stream));
if (archiveFormat is null)
stream = FileFormatFactory.Default.FromStream(stream, out archiveFormat);
if (archiveOptions is null)
archiveOptions = ArchiveOptions.Default;
if (archiveFormat is null)
throw new FileFormatException(IO.Properties.ExceptionMessages.UnsupportedFileFormat);
List<Exception> exceptions = new List<Exception>();
// Get an archive factory capable of opening this archive according to the file access option.
var a = GetArchiveFactories();
var b = a.SelectMany(f => f.GetSupportedFileFormats());
IArchiveFactory archiveFactory = GetArchiveFactories()
.Where(decoder => decoder.GetSupportedFileFormats()
.Where(f => (archiveOptions.FileAccess == FileAccess.Write && f.CanWrite) || f.CanRead)
.Any(f => f.Format.Equals(archiveFormat)))
.FirstOrDefault();
if (archiveFactory is object) {
return archiveFactory.Open(stream, archiveFormat, archiveOptions);
}
else {
// If we reach this point, we have no factories capable of reading this file format.
throw new FileFormatException(archiveFormat is null ? IO.Properties.ExceptionMessages.UnsupportedFileFormat : string.Format(IO.Properties.ExceptionMessages.UnsupportedFileFormatWithFormat, archiveFormat));
}
}
// Private members
private readonly IPluginLoader pluginLoader;
private IEnumerable<IArchiveFactory> GetArchiveFactories() {
return pluginLoader.GetPlugins<IArchiveFactory>();
}
private IEnumerable<ICodecCapabilities> GetSupportedArchiveFormats() {
return CodecCapabilities.Flatten(GetArchiveFactories().SelectMany(decoder => decoder.GetSupportedFileFormats()))
.OrderBy(format => format.Format);
}
private IPluginLoader CreatePluginLoader(IServiceProvider serviceProvider) {
return new PluginLoader<IArchiveFactory>(serviceProvider, new PluginLoaderOptions() {
PluginSearchPattern = "Gsemac.IO.Compression.*.dll",
});
}
}
}