Skip to content

Commit 113f5f0

Browse files
Compression algorithms can be disabled, now
1 parent f60af31 commit 113f5f0

11 files changed

Lines changed: 234 additions & 107 deletions

CompressedFileViewer/FileHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void OpenFile(ScNotification notification)
4444
if (Preferences.DecompressAll && gzContentStream.Length > 0)
4545
{
4646
var subTree = logEntry.AppendMessage("Trying all supported compressions");
47-
foreach (var compression in Preferences.SupportedCompressionAlgorithms)
47+
foreach (var compression in Preferences.ActiveCompressionAlgorithms)
4848
{
4949
_ = gzContentStream.Seek(0, SeekOrigin.Begin);
5050
var enc = CompressionHelper.TryDecompress(gzContentStream, compression);

CompressedFileViewer/Plugin.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ private static void AddCommands()
7878
PluginBase.AddCommand("About", () => new Windows.AboutDialog().ShowDialog());
7979
PluginBase.AddCommand("Credits", () => new Windows.Credits().ShowDialog());
8080
PluginBase.AddMenuSeperator();
81-
foreach (var compr in Preferences.SupportedCompressionAlgorithms)
81+
82+
foreach (var compr in Preferences.EnabledCompressionAlgorithms)
8283
PluginBase.AddCommand(compr.AlgorithmName, () =>
8384
{
8485
IntPtr bufferId = NppGateway.GetCurrentBufferId();
85-
var compressor = Preferences.SupportedCompressionAlgorithms.FirstOrDefault(alg => alg.AlgorithmName == compr.AlgorithmName);
86+
var compressor = Preferences.EnabledCompressionAlgorithms.FirstOrDefault(alg => alg.AlgorithmName == compr.AlgorithmName);
8687

8788
compressor = FileTracker.GetCompressor(bufferId) == compressor ? null : compressor; // if already compressed (same compressor) disable compression
8889

@@ -135,15 +136,15 @@ private static void UpdateCommandChecked(IntPtr from)
135136
{
136137
NppGateway.SetMenuItemCheck(0, FileTracker.IsIncluded(from));
137138
var compr = FileTracker.GetCompressor(from);
138-
foreach (var posCompr in Preferences.SupportedCompressionAlgorithms)
139+
foreach (var posCompr in Preferences.EnabledCompressionAlgorithms)
139140
NppGateway.SetMenuItemCheck(posCompr.AlgorithmName, compr?.AlgorithmName == posCompr.AlgorithmName);
140141
}
141142

142143
private static void Compress()
143144
{
144145
Windows.CompressDialog compressForm = new()
145146
{
146-
CompressionSettings = Preferences.SupportedCompressionAlgorithms.ToArray(),
147+
CompressionSettings = Preferences.EnabledCompressionAlgorithms.ToArray(),
147148
};
148149
if (compressForm.ShowDialog() == true && compressForm.SelectedCompression != null)
149150
{
@@ -162,7 +163,7 @@ private static void Decompress()
162163
{
163164
Windows.DecompressDialog decompressForm = new()
164165
{
165-
CompressionSettings = Preferences.SupportedCompressionAlgorithms.ToArray(),
166+
CompressionSettings = Preferences.EnabledCompressionAlgorithms.ToArray(),
166167
};
167168
if (decompressForm.ShowDialog() == true && decompressForm.SelectedCompression != null)
168169
{
@@ -193,9 +194,14 @@ private static void LoadSettings()
193194
initFilePath = Path.Combine(initFilePath, PluginName + ".config");
194195
try
195196
{
196-
Preferences = Preferences.Deserialize(initFilePath); Logging.Log("Finished loading settings");
197+
Preferences = Preferences.Deserialize(initFilePath); Logging.Log("Finished loading settings");
198+
}
199+
catch (Exception ex)
200+
{
201+
Logging.Log(ex);
202+
Preferences = Preferences.Default;
197203
}
198-
catch (Exception ex) { Logging.Log(ex); Preferences = Preferences.Default; }
204+
foreach (var alg in Preferences.EnabledCompressionAlgorithms) alg.Initialize();
199205
}
200206
private static void SaveSettings()
201207
{

CompressedFileViewer/Preferences.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,28 @@ public class Preferences
3535
/// </summary>
3636
public bool UpdateStatusBar { get; set; } = true;
3737

38-
/// <summary>
39-
/// Gets or sets the list of enabled compression algorithms.
40-
/// </summary>
41-
public List<string> EnabledCompressionAlgorithms { get; set; } = new List<string>();
42-
4338
/// <summary>
4439
/// Gets the compression algorithms available.
4540
/// </summary>
4641
public IEnumerable<CompressionSettings> CompressionAlgorithms => GetType().GetProperties()
4742
.Where(m => m.PropertyType.IsSubclassOf(typeof(CompressionSettings)))
48-
.Select(m => (CompressionSettings)m.GetValue(this)!);
43+
.Select(m => (CompressionSettings)m.GetValue(this)!)
44+
.OrderBy((settings) => settings.SortOrder);
45+
46+
/// <summary>
47+
/// Gets the active compression algorithms that the plugin should switch through
48+
/// </summary>
49+
public IEnumerable<CompressionSettings> ActiveCompressionAlgorithms => SupportedCompressionAlgorithms.Where(comp => comp.IsActive);
50+
51+
/// <summary>
52+
/// Gets the enabled compression algorithms.
53+
/// </summary>
54+
public IEnumerable<CompressionSettings> EnabledCompressionAlgorithms => CompressionAlgorithms.Where(comp => comp.IsEnabled);
4955

5056
/// <summary>
5157
/// Gets the compression algorithms that are supported.
5258
/// </summary>
53-
public IEnumerable<CompressionSettings> SupportedCompressionAlgorithms => GetType().GetProperties()
54-
.Where(m => m.PropertyType.IsSubclassOf(typeof(CompressionSettings)))
55-
.Select(m => (CompressionSettings)m.GetValue(this)!).Where(alg => alg.IsSupported);
59+
public IEnumerable<CompressionSettings> SupportedCompressionAlgorithms => EnabledCompressionAlgorithms.Where(alg => alg.IsSupported);
5660
#endregion
5761

5862
#region Constructor
@@ -155,7 +159,7 @@ public static Preferences Deserialize(Stream from)
155159
/// <returns>The compression settings for the file.</returns>
156160
public CompressionSettings? GetCompressionBySuffix(string? path)
157161
{
158-
return SupportedCompressionAlgorithms.FirstOrDefault(comp => comp.Extensions.Any(ext => path?.EndsWith(ext, StringComparison.OrdinalIgnoreCase) ?? false));
162+
return EnabledCompressionAlgorithms.FirstOrDefault(comp => comp.Extensions.Any(ext => path?.EndsWith(ext, StringComparison.OrdinalIgnoreCase) ?? false));
159163
}
160164

161165
/// <summary>
@@ -172,10 +176,13 @@ public static Preferences Deserialize(Stream from)
172176
public GZipSettings GZipSettings { get; set; } = new();
173177
public ZstdSettings ZstdSettings { get; set; } = new();
174178
public XZSettings XZSettings { get; set; } = new();
175-
176179
public BrotliSettings BrotliSettings { get; set; } = new();
177180
#endregion
178181

182+
public CompressionSettings? GetCompressionByName(string name) => GetType().GetProperties()
183+
.Where(m => m.PropertyType.IsSubclassOf(typeof(CompressionSettings)))
184+
.Select(m => (CompressionSettings)m.GetValue(this)!).FirstOrDefault(s => s.AlgorithmName == name);
185+
179186
/// <summary>
180187
/// Gets the next enabled compression algoritm.
181188
/// </summary>
@@ -184,23 +191,18 @@ public static Preferences Deserialize(Stream from)
184191
/// <returns>The next compression algorithm</returns>
185192
public CompressionSettings? GetNextCompressor(string? compressionAlgorithm, CompressionSettings? compressionBySuffix)
186193
{
187-
string cAlg;
188194
if (string.IsNullOrWhiteSpace(compressionAlgorithm))
189-
cAlg = compressionBySuffix?.AlgorithmName ?? EnabledCompressionAlgorithms.FirstOrDefault(String.Empty);
195+
return compressionBySuffix ?? ActiveCompressionAlgorithms.FirstOrDefault();
190196
else
191-
cAlg = (compressionAlgorithm != compressionBySuffix?.AlgorithmName ?
197+
return (compressionAlgorithm != compressionBySuffix?.AlgorithmName ?
192198

193-
EnabledCompressionAlgorithms
194-
.SkipWhile(alg => alg != compressionAlgorithm)
199+
ActiveCompressionAlgorithms
200+
.SkipWhile(alg => alg.AlgorithmName != compressionAlgorithm)
195201
.Skip(1)
196202
:
197-
EnabledCompressionAlgorithms
203+
ActiveCompressionAlgorithms
198204
)
199-
.FirstOrDefault(algName => algName != compressionBySuffix?.AlgorithmName, String.Empty);
200-
201-
202-
return SupportedCompressionAlgorithms.FirstOrDefault(comp => comp.AlgorithmName == cAlg);
203-
205+
.FirstOrDefault(alg => alg != compressionBySuffix);
204206
}
205207

206208
/// <summary>
@@ -211,7 +213,6 @@ public static Preferences Default
211213
get
212214
{
213215
Preferences preferences = new(false);
214-
preferences.EnabledCompressionAlgorithms = preferences.SupportedCompressionAlgorithms.Select(c => c.AlgorithmName).ToList();
215216
preferences.GZipSettings.Extensions.AddRange([".gz", ".gzip"]);
216217
preferences.BZip2Settings.Extensions.AddRange([".bz2", ".bzip2"]);
217218
preferences.ZstdSettings.Extensions.Add(".zst");

CompressedFileViewer/Settings/BZip2Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ static BZip2Settings()
2525

2626
public override Stream GetDecompressionStream(Stream inStream) => new BZip2InputStream(inStream)
2727
{ IsStreamOwner = false };
28+
29+
public override void Initialize() { }
2830
}

CompressedFileViewer/Settings/BrotliSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public override Stream GetDecompressionStream(Stream inStream)
3232
BrotliStream brotliStream= new(inStream,CompressionMode.Decompress,true);
3333
return brotliStream;
3434
}
35+
36+
public override void Initialize() { }
3537
}

CompressedFileViewer/Settings/CompressionSettings.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public abstract class CompressionSettings
1616
/// <summary>
1717
/// List of file extensions associated with this compression algorithm
1818
/// </summary>
19-
public List<string> Extensions { get; set; } = new List<string>();
19+
public List<string> Extensions { get; set; } = [];
2020

2121
/// <summary>
2222
/// The name of the compression algorithm
@@ -64,6 +64,15 @@ public void Decompress(Stream inStream, Stream outStream)
6464
/// Whether the compression algorithm is supported or not.
6565
/// </summary>
6666
public abstract bool IsSupported { get; }
67+
68+
public bool IsEnabled { get; set; } = false;
69+
70+
public bool IsActive { get; set; } = true;
71+
72+
public int SortOrder { get; set; } = int.MaxValue;
73+
6774
public override string ToString() => AlgorithmName;
6875

76+
public abstract void Initialize();
77+
6978
}

CompressedFileViewer/Settings/GZipSettings.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,12 @@ namespace CompressedFileViewer.Settings;
1212
public class GZipSettings : CompressionSettings
1313
{
1414
public static readonly string ALGORITHM_NAME = "gzip";
15-
static GZipSettings()
16-
{
17-
try
18-
{
19-
using GZipOutputStream outStream = new(Stream.Null);
20-
using GZipInputStream inStream = new(Stream.Null);
21-
isSupported = true;
22-
}
23-
catch (Exception ex)
24-
{
25-
isSupported = false;
26-
Logging.Log(ex);
27-
}
28-
}
2915

3016
public int CompressionLevel { get; set; } = 9;
3117
public int BufferSize { get; set; } = 1024;
3218

3319
public override string AlgorithmName => ALGORITHM_NAME;
34-
private static readonly bool isSupported;
35-
public override bool IsSupported => isSupported;
20+
public override bool IsSupported => true;
3621

3722
public override Stream GetCompressionStream(Stream outStream)
3823
{
@@ -48,4 +33,6 @@ public override Stream GetCompressionStream(Stream outStream)
4833
{
4934
IsStreamOwner = false
5035
};
36+
37+
public override void Initialize() { }
5138
}

CompressedFileViewer/Settings/XZSettings.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,7 @@ namespace CompressedFileViewer.Settings;
1212
public class XZSettings : CompressionSettings
1313
{
1414
public static readonly string ALGORITHM_NAME = "xz";
15-
static bool isSupported = false;
16-
static XZSettings()
17-
{
18-
try
19-
{
20-
string currentDir = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)!;
21-
string libPath = "";
22-
/*switch (RuntimeInformation.ProcessArchitecture)
23-
{
24-
case Architecture.X86:
25-
libPath = System.IO.Path.Combine(currentDir, "x86", "liblzma.dll");
26-
break;
27-
case Architecture.X64:
28-
libPath = System.IO.Path.Combine(currentDir, "x64", "liblzma.dll");
29-
break;
30-
case Architecture.Arm64:
31-
libPath = System.IO.Path.Combine(currentDir, "arm64", "liblzma.dll");
32-
break;
33-
}*/
34-
libPath = System.IO.Path.Combine(currentDir, "liblzma.dll");
35-
XZInit.GlobalInit(libPath);
36-
Logging.Log("Finished loading liblzma.dll", $"Version: {XZInit.VersionString()}");
37-
isSupported = true;
38-
} catch (Exception ex)
39-
{
40-
isSupported = false;
41-
Logging.Log(ex);
42-
}
43-
}
15+
static bool isSupported = true;
4416

4517
public override string AlgorithmName => ALGORITHM_NAME;
4618

@@ -88,4 +60,34 @@ public override Stream GetDecompressionStream(Stream inStream)
8860
return new XZStream(inStream, DecompressOptions);
8961
return new XZStream(inStream, DecompressOptions, ThreadDecompressOptions);
9062
}
63+
64+
public override void Initialize()
65+
{
66+
try
67+
{
68+
string currentDir = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)!;
69+
string libPath = "";
70+
/*switch (RuntimeInformation.ProcessArchitecture)
71+
{
72+
case Architecture.X86:
73+
libPath = System.IO.Path.Combine(currentDir, "x86", "liblzma.dll");
74+
break;
75+
case Architecture.X64:
76+
libPath = System.IO.Path.Combine(currentDir, "x64", "liblzma.dll");
77+
break;
78+
case Architecture.Arm64:
79+
libPath = System.IO.Path.Combine(currentDir, "arm64", "liblzma.dll");
80+
break;
81+
}*/
82+
libPath = System.IO.Path.Combine(currentDir, "liblzma.dll");
83+
XZInit.GlobalInit(libPath);
84+
Logging.Log("Finished loading liblzma.dll", $"Version: {XZInit.VersionString()}");
85+
isSupported = true;
86+
}
87+
catch (Exception ex)
88+
{
89+
isSupported = false;
90+
Logging.Log(ex);
91+
}
92+
}
9193
}

CompressedFileViewer/Settings/ZstdSettings.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,7 @@ public class ZstdSettings : CompressionSettings
1111
{
1212
public static readonly string ALGORITHM_NAME = "zstd";
1313

14-
static bool isSupported;
15-
static ZstdSettings()
16-
{
17-
isSupported = true;
18-
try
19-
{
20-
using var compressor = new ZstdSharp.Compressor();
21-
using var decompressor = new ZstdSharp.Decompressor();
22-
}
23-
catch(Exception ex)
24-
{
25-
Logging.Log(ex);
26-
isSupported = false;
27-
}
28-
}
14+
static bool isSupported = true;
2915

3016
public int CompressionLevel { get; set; } = 11;
3117
public int BufferSize { get; set; } = 1024 * 1024;
@@ -44,4 +30,19 @@ public override Stream GetDecompressionStream(Stream inStream)
4430
ZstdSharp.DecompressionStream decompressionStream = new(inStream,BufferSize,true);
4531
return decompressionStream;
4632
}
33+
34+
public override void Initialize()
35+
{
36+
isSupported = true;
37+
try
38+
{
39+
using var compressor = new ZstdSharp.Compressor();
40+
using var decompressor = new ZstdSharp.Decompressor();
41+
}
42+
catch (Exception ex)
43+
{
44+
Logging.Log(ex);
45+
isSupported = false;
46+
}
47+
}
4748
}

0 commit comments

Comments
 (0)