Skip to content

Commit

Permalink
Code Cleanup. (#266)
Browse files Browse the repository at this point in the history
Signed-off-by: AraHaan <[email protected]>
  • Loading branch information
AraHaan authored May 7, 2024
1 parent d7599c5 commit 4af6ee0
Show file tree
Hide file tree
Showing 97 changed files with 406 additions and 755 deletions.
3 changes: 3 additions & 0 deletions ref/PluginUpdateCheck/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ dotnet_diagnostic.CS1591.severity = suggestion

# S3903: Move '%s' into a named namespace.
dotnet_diagnostic.S3903.severity = none

# IDE0251: Make member 'readonly'
dotnet_diagnostic.IDE0251.severity = silent
3 changes: 3 additions & 0 deletions ref/ZipAssembly/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,6 @@ dotnet_diagnostic.S3903.severity = none

# S1133: Do not forget to remove this deprecated code someday.
dotnet_diagnostic.S1133.severity = suggestion

# AD0001: Analyzer Failure
dotnet_diagnostic.AD0001.severity = suggestion
7 changes: 7 additions & 0 deletions src/BlowFish/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ csharp_preferred_modifier_order = public,private,protected,internal,static,exter

# Code-block preferences
csharp_prefer_braces = true:warning
csharp_style_namespace_declarations = file_scoped

# Expression-level preferences
csharp_prefer_simple_default_expression = true:warning
Expand Down Expand Up @@ -149,3 +150,9 @@ dotnet_diagnostic.S3903.severity = none

# S1133: Do not forget to remove this deprecated code someday.
dotnet_diagnostic.S1133.severity = suggestion

# IDE0072: Add missing cases
dotnet_diagnostic.IDE0072.severity = suggestion

#Remove unnecessary usings/imports
dotnet_diagnostic.IDE0005.severity = suggestion
20 changes: 10 additions & 10 deletions src/BlowFish/BlowFish/BlowFish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public static void XorBlock(Stream block, byte[] iv)
[Obsolete("Using CipherMode.ECB in this method is deprecated.")]
public string? Encrypt(string ct, CipherMode mode)
=> mode switch
{
CipherMode.ECB => this.bfInternal.EncryptECB(ct),
CipherMode.CBC => this.bfInternal.EncryptCBC(ct),
_ => null,
};
{
CipherMode.ECB => this.bfInternal.EncryptECB(ct),
CipherMode.CBC => this.bfInternal.EncryptCBC(ct),
_ => null,
};

/// <summary>
/// Encrypts a byte array.
Expand Down Expand Up @@ -126,11 +126,11 @@ public static void XorBlock(Stream block, byte[] iv)
[Obsolete("Using CipherMode.ECB in this method is deprecated.")]
public string? Decrypt(string ct, CipherMode mode)
=> mode switch
{
CipherMode.ECB => this.bfInternal.DecryptECB(ct),
CipherMode.CBC => this.bfInternal.DecryptCBC(ct),
_ => null,
};
{
CipherMode.ECB => this.bfInternal.DecryptECB(ct),
CipherMode.CBC => this.bfInternal.DecryptCBC(ct),
_ => null,
};

/// <summary>
/// Decrypts a byte array.
Expand Down
32 changes: 16 additions & 16 deletions src/BlowFish/BlowFish/BlowFishInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal IEnumerable<byte> IV
get => this.initVector;
set
{
var array = value?.ToArray() ?? Array.Empty<byte>();
var array = value?.ToArray() ?? [];
ThrowHelpers.ThrowInvalidOperation(array!.Length != 8, Resources.BlowFish_Invalid_IV_Size!);
this.initVector = array;
this.iVSet = true;
Expand All @@ -70,16 +70,16 @@ internal IEnumerable<byte> IV
// SBLOCKS ARE THE HEX DIGITS OF PI.
// The amount of hex digits can be increased if you want to experiment with more rounds and longer key lengths
private static ReadOnlySpan<uint> SetupP
=> new uint[]
{
=>
[
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b,
};
];

private static ReadOnlySpan<uint> SetupS0
=> new uint[]
{
=>
[
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658,
Expand Down Expand Up @@ -123,11 +123,11 @@ private static ReadOnlySpan<uint> SetupS0
0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f,
0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a,
};
];

private static ReadOnlySpan<uint> SetupS1
=> new uint[]
{
=>
[
0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d,
0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65,
Expand Down Expand Up @@ -171,11 +171,11 @@ private static ReadOnlySpan<uint> SetupS1
0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd,
0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7,
};
];

private static ReadOnlySpan<uint> SetupS2
=> new uint[]
{
=>
[
0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7,
0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af,
Expand Down Expand Up @@ -219,11 +219,11 @@ private static ReadOnlySpan<uint> SetupS2
0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8,
0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0,
};
];

private static ReadOnlySpan<uint> SetupS3
=> new uint[]
{
=>
[
0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742,
0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79,
Expand Down Expand Up @@ -267,7 +267,7 @@ private static ReadOnlySpan<uint> SetupS3
0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c,
0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6,
};
];

internal string EncryptCBC(string pt)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Common/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ csharp_preferred_modifier_order = public,private,protected,internal,static,exter

# Code-block preferences
csharp_prefer_braces = true:warning
csharp_style_namespace_declarations = file_scoped

# Expression-level preferences
csharp_prefer_simple_default_expression = true:warning
Expand Down Expand Up @@ -149,3 +150,9 @@ dotnet_diagnostic.CA1307.severity = warning

# S3903: Move '%s' into a named namespace.
dotnet_diagnostic.S3903.severity = none

# IDE0072: Add missing cases
dotnet_diagnostic.IDE0072.severity = silent

#Remove unnecessary usings/imports
dotnet_diagnostic.IDE0005.severity = suggestion
24 changes: 7 additions & 17 deletions src/Common/Common/MessageEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,25 @@ namespace Elskom.Generic.Libs;
/// <summary>
/// Event that holds the message text and the caption.
/// </summary>
public class MessageEventArgs : EventArgs
/// <param name="text">The text for the message.</param>
/// <param name="caption">The title (caption) for the message.</param>
/// <param name="errorlevel">The error level for the message, or <see cref="Libs.ErrorLevel.None"/> for no error level information.</param>
public class MessageEventArgs(string text, string caption, ErrorLevel errorlevel) : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="MessageEventArgs"/> class.
/// </summary>
/// <param name="text">The text for the message.</param>
/// <param name="caption">The title (caption) for the message.</param>
/// <param name="errorlevel">The error level for the message, or <see cref="Libs.ErrorLevel.None"/> for no error level information.</param>
public MessageEventArgs(string text, string caption, ErrorLevel errorlevel)
{
this.Text = text;
this.Caption = caption;
this.ErrorLevel = errorlevel;
}

/// <summary>
/// Gets the text for the message.
/// </summary>
public string Text { get; }
public string Text { get; } = text;

/// <summary>
/// Gets the caption (title) for the message.
/// </summary>
public string Caption { get; }
public string Caption { get; } = caption;

/// <summary>
/// Gets the <see cref="Libs.ErrorLevel"/> of the message.
/// </summary>
public ErrorLevel ErrorLevel { get; }
public ErrorLevel ErrorLevel { get; } = errorlevel;

/// <summary>
/// Gets or sets the ExitCode for the application.
Expand Down
44 changes: 15 additions & 29 deletions src/Common/Common/NotificationEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,49 @@ namespace Elskom.Generic.Libs;
/// <summary>
/// Event that holds that data to call the apis for notifications.
/// </summary>
public class NotificationEventArgs : EventArgs
/// <param name="timeout">The notification timeout.</param>
/// <param name="title">The Notification or MessageBox title.</param>
/// <param name="text">The Notification or MessageBox text.</param>
/// <param name="icon">The notification icon.</param>
/// <param name="useNotifications">Indicates whether the event should call into the platform's Notifications API, or the MessageBox API.</param>
/// <param name="messageBoxButtons">The MessageBox Buttons.</param>
/// <param name="messageBoxIcon">The MessageBox Icon.</param>
public class NotificationEventArgs(int timeout, string title, string text, int icon, bool useNotifications, int messageBoxButtons, int messageBoxIcon) : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="NotificationEventArgs"/> class.
/// </summary>
/// <param name="timeout">The notification timeout.</param>
/// <param name="title">The Notification or MessageBox title.</param>
/// <param name="text">The Notification or MessageBox text.</param>
/// <param name="icon">The notification icon.</param>
/// <param name="useNotifications">Indicates whether the event should call into the platform's Notifications API, or the MessageBox API.</param>
/// <param name="messageBoxButtons">The MessageBox Buttons.</param>
/// <param name="messageBoxIcon">The MessageBox Icon.</param>
public NotificationEventArgs(int timeout, string title, string text, int icon, bool useNotifications, int messageBoxButtons, int messageBoxIcon)
{
this.TimeOut = timeout;
this.Title = title;
this.Text = text;
this.Icon = icon;
this.UseNotifications = useNotifications;
this.MessageBoxButtons = messageBoxButtons;
this.MessageBoxIcon = messageBoxIcon;
}

/// <summary>
/// Gets the timeout to use for the Notification.
/// </summary>
public int TimeOut { get; }
public int TimeOut { get; } = timeout;

/// <summary>
/// Gets the title of the Notification or MessageBox.
/// </summary>
public string Title { get; }
public string Title { get; } = title;

/// <summary>
/// Gets the text of the Notification or MessageBox.
/// </summary>
public string Text { get; }
public string Text { get; } = text;

/// <summary>
/// Gets the icon to use for the Notification.
/// </summary>
public int Icon { get; }
public int Icon { get; } = icon;

/// <summary>
/// Gets a value indicating whether gets whether to use platform's specific Notification APIs, or it's MessageBox APIs.
/// </summary>
public bool UseNotifications { get; }
public bool UseNotifications { get; } = useNotifications;

/// <summary>
/// Gets the Buttons to use for the platform's specific MessageBox APIs.
/// </summary>
public int MessageBoxButtons { get; }
public int MessageBoxButtons { get; } = messageBoxButtons;

/// <summary>
/// Gets the Icon to use for the MessageBox made using the platform's specific MessageBox APIs.
/// </summary>
public int MessageBoxIcon { get; }
public int MessageBoxIcon { get; } = messageBoxIcon;

/// <summary>
/// Gets or sets the Result from the platform's specific MessageBox.
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Common/ProcessStartOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,5 @@ public override string ToString()
{this.Stderr}",
(false, false) => string.Empty,
(false, true) => $"{this.Stderr}",
};
};
}
15 changes: 4 additions & 11 deletions src/Common/Common/RuntimeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,19 @@ internal static class RuntimeHelpers
/// of the current process into an valid runtime identifier that the .NET SDK has.
/// </summary>
/// <returns>
/// An Runtime Identifier string, or null if it cannot be converted into an runtime identifier.
/// An Runtime Identifier <see langword="string" />, or <see cref="string.Empty" /> if it cannot be converted into a runtime identifier.
/// </returns>
public static string GetCurrentRuntimeIdentifier()
=> (

// Desktop Operating Systems.
OperatingSystem.IsWindows(),
=> (OperatingSystem.IsWindows(), /* Desktop Operating Systems. */
OperatingSystem.IsLinux(),
OperatingSystem.IsMacOS(),
OperatingSystem.IsMacCatalyst(),
OperatingSystem.IsFreeBSD(),

// Phone Operting Systems.
OperatingSystem.IsAndroid(),
OperatingSystem.IsAndroid(), /* Phone Operating Systems. */
OperatingSystem.IsIOS(),
OperatingSystem.IsWatchOS(),
OperatingSystem.IsTvOS(),

// Web Browser.
OperatingSystem.IsBrowser()) switch
OperatingSystem.IsBrowser() /* Web Browser. */) switch
{
(true, false, false, false, false, false, false, false, false, false) => $"win-{RuntimeInformation.ProcessArchitecture}",
(false, true, false, false, false, false, false, false, false, false) => $"linux-{RuntimeInformation.ProcessArchitecture}",
Expand Down
4 changes: 4 additions & 0 deletions src/GenericPluginLoader/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ csharp_preferred_modifier_order = public,private,protected,internal,static,exter

# Code-block preferences
csharp_prefer_braces = true:warning
csharp_style_namespace_declarations = file_scoped

# Expression-level preferences
csharp_prefer_simple_default_expression = true:warning
Expand Down Expand Up @@ -152,3 +153,6 @@ dotnet_diagnostic.CA1303.severity = warning

# S3903: Move '%s' into a named namespace.
dotnet_diagnostic.S3903.severity = none

#Remove unnecessary usings/imports
dotnet_diagnostic.IDE0005.severity = suggestion
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class GenericPluginLoader
/// </summary>
public static event EventHandler<MessageEventArgs>? PluginLoaderMessage;

internal Dictionary<string, List<PluginLoadContext>> Contexts { get; } = new();
internal Dictionary<string, List<PluginLoadContext>> Contexts { get; } = [];

/// <summary>
/// Loads plugins with the specified plugin interface type.
Expand Down Expand Up @@ -71,8 +71,8 @@ public ICollection<T> LoadPlugins<T>(string path, bool saveToZip)

// try to load from a zip as well if plugins are installed in both places.
var zippath = $"{path}.zip";
List<T> plugins = new();
List<PluginLoadContext> contexts = new();
List<T> plugins = [];
List<PluginLoadContext> contexts = [];

// handle when path points to a zip file.
if (Directory.Exists(path) || File.Exists(zippath))
Expand All @@ -96,7 +96,7 @@ public ICollection<T> LoadPlugins<T>(string path, bool saveToZip)

if (saveToZip && File.Exists(zippath))
{
Dictionary<string, int> filesInZip = new();
Dictionary<string, int> filesInZip = [];
using (var zipFile = ZipFile.OpenRead(zippath))
{
foreach (var entry in zipFile.Entries)
Expand Down
15 changes: 4 additions & 11 deletions src/GenericPluginLoader/GenericPluginLoader/PluginLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
namespace Elskom.Generic.Libs;

/// <inheritdoc/>
internal sealed class PluginLoadContext : AssemblyLoadContext
/// <param name="name">The name of the load context.</param>
/// <param name="pluginPath">The path the the plugins.</param>
internal sealed class PluginLoadContext(string name, string pluginPath) : AssemblyLoadContext(name, true)
{
private readonly AssemblyDependencyResolver resolver;

/// <summary>
/// Initializes a new instance of the <see cref="PluginLoadContext"/> class.
/// </summary>
/// <param name="name">The name of the load context.</param>
/// <param name="pluginPath">The path the the plugins.</param>
public PluginLoadContext(string name, string pluginPath)
: base(name, true)
=> this.resolver = new(pluginPath);
private readonly AssemblyDependencyResolver resolver = new(pluginPath);

/// <inheritdoc/>
protected override Assembly? Load(AssemblyName assemblyName)
Expand Down
Loading

0 comments on commit 4af6ee0

Please sign in to comment.