Skip to content

Commit fda25ae

Browse files
author
BRUNER Patrick
committed
review fixes
1 parent 2e498ec commit fda25ae

2 files changed

Lines changed: 67 additions & 43 deletions

File tree

src/LogExpert.UI/Dialogs/PluginTrustDialog.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Globalization;
22
using System.Runtime.Versioning;
3+
using System.Security;
34

45
using LogExpert.PluginRegistry;
56

@@ -51,7 +52,7 @@ private void ApplyResources ()
5152
Text = Resources.PluginTrustDialog_UI_Title;
5253

5354
// Labels
54-
pluginCountLabel.Text = string.Format(Resources.PluginTrustDialog_UI_Label_TotalPlugins, 0);
55+
pluginCountLabel.Text = string.Format(CultureInfo.InvariantCulture, Resources.PluginTrustDialog_UI_Label_TotalPlugins, 0);
5556
groupBoxPlugins.Text = Resources.PluginTrustDialog_UI_GroupBox_TrustedPlugins;
5657

5758
// Buttons
@@ -88,7 +89,16 @@ private void LoadConfiguration ()
8889
// If user makes changes, they'll be saved then
8990
}
9091
}
91-
catch (Exception ex)
92+
catch (Exception ex) when (ex is ArgumentException or
93+
ArgumentNullException or
94+
PathTooLongException or
95+
DirectoryNotFoundException or
96+
IOException or
97+
UnauthorizedAccessException or
98+
FileNotFoundException or
99+
NotSupportedException or
100+
SecurityException or
101+
JsonException)
92102
{
93103
_ = MessageBox.Show(
94104
string.Format(CultureInfo.InvariantCulture, Resources.PluginTrustDialog_UI_Message_LoadError, ex.Message),
@@ -162,7 +172,9 @@ private void UpdateButtonStates ()
162172
private bool HasHashForSelection ()
163173
{
164174
if (pluginListView.SelectedItems.Count == 0)
175+
{
165176
return false;
177+
}
166178

167179
var pluginName = pluginListView.SelectedItems[0].Text;
168180
return _config.PluginHashes.ContainsKey(pluginName);
@@ -293,7 +305,16 @@ private void SaveButton_Click (object sender, EventArgs e)
293305
DialogResult = DialogResult.OK;
294306
Close();
295307
}
296-
catch (Exception ex)
308+
catch (Exception ex) when (ex is ArgumentException or
309+
ArgumentNullException or
310+
PathTooLongException or
311+
DirectoryNotFoundException or
312+
IOException or
313+
UnauthorizedAccessException or
314+
FileNotFoundException or
315+
NotSupportedException or
316+
SecurityException or
317+
JsonException)
297318
{
298319
_ = MessageBox.Show(
299320
string.Format(CultureInfo.InvariantCulture, Resources.PluginTrustDialog_UI_Message_SaveError, ex.Message),
@@ -329,4 +350,4 @@ private void PluginListView_SelectedIndexChanged (object sender, EventArgs e)
329350
}
330351

331352
#endregion
332-
}
353+
}

src/PluginHashGenerator.Tool/Program.cs

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Globalization;
4-
using System.IO;
5-
using System.Linq;
62
using System.Text;
3+
74
using LogExpert.PluginRegistry;
85

96
namespace PluginHashGenerator.Tool;
@@ -14,7 +11,9 @@ namespace PluginHashGenerator.Tool;
1411
/// </summary>
1512
internal class Program
1613
{
17-
private static int Main(string[] args)
14+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Tool for Hash Generation does not need localization")]
15+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Intentionally continue on error to process other plugins")]
16+
private static int Main (string[] args)
1817
{
1918
try
2019
{
@@ -37,7 +36,7 @@ private static int Main(string[] args)
3736

3837
// Find all plugin DLLs
3938
var pluginPaths = new List<string>();
40-
39+
4140
// Check plugins folder
4241
var pluginsDir = Path.Join(outputPath, "plugins");
4342
if (Directory.Exists(pluginsDir))
@@ -82,7 +81,7 @@ private static int Main(string[] args)
8281
foreach (var pluginPath in pluginPaths.OrderBy(p => p))
8382
{
8483
var fileName = Path.GetFileName(pluginPath);
85-
84+
8685
// Skip dependencies
8786
if (knownDependencies.Contains(fileName))
8887
{
@@ -93,14 +92,14 @@ private static int Main(string[] args)
9392
try
9493
{
9594
var hash = PluginHashCalculator.CalculateHash(pluginPath);
96-
95+
9796
// For x86 plugins, add suffix to distinguish them
9897
var key = fileName;
9998
if (pluginPath.Contains("pluginsx86", StringComparison.OrdinalIgnoreCase))
10099
{
101100
key = $"{Path.GetFileNameWithoutExtension(fileName)}.dll (x86)";
102101
}
103-
102+
104103
// Handle duplicate keys (same plugin in both folders)
105104
if (!pluginHashes.ContainsKey(key))
106105
{
@@ -110,6 +109,7 @@ private static int Main(string[] args)
110109
}
111110
catch (Exception ex)
112111
{
112+
//Intentionally continue on error to process other plugins
113113
Console.Error.WriteLine($" ✗ ERROR calculating hash for {fileName}: {ex.Message}");
114114
}
115115
}
@@ -121,7 +121,7 @@ private static int Main(string[] args)
121121
var targetDir = Path.GetDirectoryName(targetFile);
122122
if (!string.IsNullOrEmpty(targetDir) && !Directory.Exists(targetDir))
123123
{
124-
Directory.CreateDirectory(targetDir);
124+
_ = Directory.CreateDirectory(targetDir);
125125
}
126126

127127
// Write the file
@@ -140,43 +140,46 @@ private static int Main(string[] args)
140140
}
141141
}
142142

143-
private static string GenerateSourceCode(Dictionary<string, string> pluginHashes, string configuration)
143+
private static string GenerateSourceCode (Dictionary<string, string> pluginHashes, string configuration)
144144
{
145145
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
146-
146+
147147
var sb = new StringBuilder();
148-
sb.AppendLine("// <auto-generated/>");
149-
sb.AppendLine("// This file is auto-generated during build. Do not edit manually.");
150-
sb.AppendLine("// To regenerate, rebuild the project or run the GeneratePluginHashes MSBuild target.");
151-
sb.AppendLine();
152-
sb.AppendLine("using System.Collections.Generic;");
153-
sb.AppendLine();
154-
sb.AppendLine("namespace LogExpert.PluginRegistry;");
155-
sb.AppendLine();
156-
sb.AppendLine("public static partial class PluginValidator");
157-
sb.AppendLine("{");
158-
sb.AppendLine(" /// <summary>");
159-
sb.AppendLine(" /// Gets pre-calculated SHA256 hashes for built-in plugins.");
160-
sb.AppendLine($" /// Generated: {timestamp} UTC");
161-
sb.AppendLine($" /// Configuration: {configuration}");
162-
sb.AppendLine($" /// Plugin count: {pluginHashes.Count}");
163-
sb.AppendLine(" /// </summary>");
164-
sb.AppendLine(" public static Dictionary<string, string> GetBuiltInPluginHashes()");
165-
sb.AppendLine(" {");
166-
sb.AppendLine(" return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)");
167-
sb.AppendLine(" {");
168148

169149
foreach (var kvp in pluginHashes.OrderBy(kvp => kvp.Key))
170150
{
171151
// Properly escape the key for C# string literal
172-
var escapedKey = kvp.Key.Replace("\\", "\\\\").Replace("\"", "\\\"");
173-
sb.AppendLine($" [\"{escapedKey}\"] = \"{kvp.Value}\",");
152+
var escapedKey = kvp.Key.Replace("\\", "\\\\", StringComparison.OrdinalIgnoreCase).Replace("\"", "\\\"", StringComparison.OrdinalIgnoreCase);
153+
_ = sb.AppendLine(CultureInfo.InvariantCulture, $" [\"{escapedKey}\"] = \"{kvp.Value}\",");
174154
}
175155

176-
sb.AppendLine(" };");
177-
sb.AppendLine(" }");
178-
sb.AppendLine("}");
156+
string sourceCode = $$"""
157+
// <auto-generated/>
158+
// This file is auto-generated during build. Do not edit manually.
159+
// To regenerate, rebuild the project or run the GeneratePluginHashes MSBuild target.
160+
161+
using System.Collections.Generic;
162+
163+
namespace LogExpert.PluginRegistry;
164+
165+
public static partial class PluginValidator
166+
{
167+
/// <summary>
168+
/// Gets pre-calculated SHA256 hashes for built-in plugins.
169+
/// Generated: {{timestamp}} UTC
170+
/// Configuration: {{configuration}}
171+
/// Plugin count: {{pluginHashes.Count}}
172+
/// </summary>
173+
public static Dictionary<string, string> GetBuiltInPluginHashes()
174+
{
175+
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
176+
{
177+
{{sb}}
178+
};
179+
}
180+
}
181+
""";
179182

180-
return sb.ToString();
183+
return sourceCode;
181184
}
182-
}
185+
}

0 commit comments

Comments
 (0)