Repository navigation
Expand file tree
/
Copy pathEekIntegration.cs
More file actions
126 lines (105 loc) · 3.91 KB
/
Copy pathEekIntegration.cs
File metadata and controls
126 lines (105 loc) · 3.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Microsoft.Win32;
namespace EekContextMenu;
public static class EekIntegration
{
public const string DefaultEekRoot = @"C:\EEK";
private const string SettingsKeyPath = @"Software\EekContextMenu";
private const string EekRootValue = "EekRoot";
private const string EnabledValue = "Enabled";
private const string CheckForUpdatesBeforeScanValue = "CheckForUpdatesBeforeScan";
private const string QuarantineDetectionsValue = "QuarantineDetections";
public sealed record ScanOptions(
string EekRoot,
bool CheckForUpdatesBeforeScan,
bool QuarantineDetections);
public static string GetEekRoot()
{
using var key = Registry.CurrentUser.OpenSubKey(SettingsKeyPath);
var value = key?.GetValue(EekRootValue) as string;
return string.IsNullOrWhiteSpace(value) ? DefaultEekRoot : value;
}
public static bool IsEnabled()
{
return GetBoolSetting(EnabledValue, false);
}
public static bool CheckForUpdatesBeforeScan()
{
return GetBoolSetting(CheckForUpdatesBeforeScanValue, true);
}
public static bool QuarantineDetections()
{
return GetBoolSetting(QuarantineDetectionsValue, true);
}
public static ScanOptions GetScanOptions()
{
return new ScanOptions(GetEekRoot(), CheckForUpdatesBeforeScan(), QuarantineDetections());
}
public static void Save(string eekRoot, bool enabled, bool checkForUpdatesBeforeScan, bool quarantineDetections)
{
var root = NormalizeEekRoot(eekRoot);
if (enabled)
{
var error = GetValidationError(root);
if (error is not null)
{
throw new InvalidOperationException(error);
}
}
using var key = Registry.CurrentUser.CreateSubKey(SettingsKeyPath, true)
?? throw new InvalidOperationException("Could not open the settings registry key.");
key.SetValue(EekRootValue, root, RegistryValueKind.String);
key.SetValue(EnabledValue, enabled ? 1 : 0, RegistryValueKind.DWord);
key.SetValue(CheckForUpdatesBeforeScanValue, checkForUpdatesBeforeScan ? 1 : 0, RegistryValueKind.DWord);
key.SetValue(QuarantineDetectionsValue, quarantineDetections ? 1 : 0, RegistryValueKind.DWord);
}
public static string? GetValidationError(string eekRoot)
{
string root;
try
{
root = NormalizeEekRoot(eekRoot);
}
catch
{
return "Choose a valid EEK folder.";
}
if (!Directory.Exists(root))
{
return "EEK folder was not found.";
}
if (!File.Exists(Path.Combine(root, "bin64", "a2cmd.exe")))
{
return @"bin64\a2cmd.exe was not found.";
}
return null;
}
public static string GetScannerPath(string eekRoot)
{
return Path.Combine(NormalizeEekRoot(eekRoot), "bin64", "a2cmd.exe");
}
public static string GetReportsFolder(string eekRoot)
{
return Path.Combine(NormalizeEekRoot(eekRoot), "Reports");
}
public static string GetQuarantineFolder(string eekRoot)
{
return Path.Combine(NormalizeEekRoot(eekRoot), "Quarantine");
}
private static bool GetBoolSetting(string name, bool fallback)
{
using var key = Registry.CurrentUser.OpenSubKey(SettingsKeyPath);
return key?.GetValue(name) is int value ? value != 0 : fallback;
}
private static string NormalizeEekRoot(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("Path is empty.", nameof(path));
}
var fullPath = Path.GetFullPath(path.Trim().Trim('"'));
var root = Path.GetPathRoot(fullPath);
return root is not null && fullPath.Length > root.Length
? fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
: fullPath;
}
}