Skip to content

Commit

Permalink
Implemented property pattern matching (#469)
Browse files Browse the repository at this point in the history
Implemented property pattern matching. Makes the code more readable, easier to understand and more modern since it was introduced in C# 8+
  • Loading branch information
HotCakeX authored Dec 21, 2024
1 parent 34a4000 commit 8ab8019
Show file tree
Hide file tree
Showing 23 changed files with 63 additions and 63 deletions.
6 changes: 3 additions & 3 deletions AppControl Manager/Logic/GetFilesFast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal static List<FileInfo> GetFilesFast(
};

// If custom extensions are provided, use them and make them case-insensitive
if (extensionsToFilterBy is not null && extensionsToFilterBy.Length > 0)
if (extensionsToFilterBy is { Length: > 0 })
{
extensions = new HashSet<string>(extensionsToFilterBy, StringComparer.OrdinalIgnoreCase);
}
Expand All @@ -93,7 +93,7 @@ internal static List<FileInfo> GetFilesFast(
List<Task> tasks = [];

// Process directories if provided
if (directories is not null && directories.Length > 0)
if (directories is { Length: > 0 })
{
foreach (DirectoryInfo directory in directories)
{
Expand Down Expand Up @@ -163,7 +163,7 @@ internal static List<FileInfo> GetFilesFast(
}

// If files are provided, process them
if (files is not null && files.Length > 0)
if (files is { Length: > 0 })
{
foreach (FileInfo file in files)
{
Expand Down
2 changes: 1 addition & 1 deletion AppControl Manager/Logic/Main/AppControlSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ internal static ConcurrentDictionary<string, SimulationOutput> Invoke(
// Loop through each .cat security catalog on the system - If user selected custom CatRoot folders then use them instead
DirectoryInfo[] catRootDirectories = [];

if (catRootPath is not null && catRootPath.Count > 0)
if (catRootPath is { Count: > 0 })
{
catRootDirectories = [.. catRootPath.Select(dir => new DirectoryInfo(dir))];
}
Expand Down
2 changes: 1 addition & 1 deletion AppControl Manager/Logic/MoveUserModeToKernelMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void Move(string filePath)
XmlNode? allowedSigners12 = codeIntegrityPolicy.UMCI_SigningScenarioNode?.SelectSingleNode("./ns:ProductSigners/ns:AllowedSigners", codeIntegrityPolicy.NamespaceManager);

// If AllowedSigners node exists in SigningScenario 12 and has child nodes
if (allowedSigners12 is not null && allowedSigners12.HasChildNodes)
if (allowedSigners12 is { HasChildNodes: true })
{
// Loop through each child node of AllowedSigners in SigningScenario 12
foreach (XmlNode allowedSignerNode in allowedSigners12.ChildNodes)
Expand Down
4 changes: 2 additions & 2 deletions AppControl Manager/Logic/PolicyFileSigningStatusDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static IntelGathering.SignatureStatus Check(string policyXMLPath)
// Check if SupplementalPolicySigners exists and has child nodes
XmlNodeList? supplementalPolicySignersNodes = codeIntegrityPolicy.SiPolicyNode.SelectNodes("ns:SupplementalPolicySigners/ns:SupplementalPolicySigner", codeIntegrityPolicy.NamespaceManager);

if (supplementalPolicySignersNodes is not null && supplementalPolicySignersNodes.Count > 0)
if (supplementalPolicySignersNodes is { Count: > 0 })
{
// Get unique SignerIds from SupplementalPolicySigners
foreach (XmlElement node in supplementalPolicySignersNodes)
Expand All @@ -40,7 +40,7 @@ internal static IntelGathering.SignatureStatus Check(string policyXMLPath)
// Check if UpdatePolicySigners exists and has child nodes
XmlNodeList? updatePolicySignersNodes = codeIntegrityPolicy.SiPolicyNode.SelectNodes("ns:UpdatePolicySigners/ns:UpdatePolicySigner", codeIntegrityPolicy.NamespaceManager);

if (updatePolicySignersNodes is not null && updatePolicySignersNodes.Count > 0)
if (updatePolicySignersNodes is { Count: > 0 })
{
// Get unique SignerIds from UpdatePolicySigners
foreach (XmlElement node in updatePolicySignersNodes)
Expand Down
2 changes: 1 addition & 1 deletion AppControl Manager/Logic/RemoveSupplementalSigners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal static void RemoveSupplementalSigners(string path)
// Check if SupplementalPolicySigners exists and has child nodes
XmlNodeList? supplementalPolicySignersNodes = codeIntegrityPolicy.SiPolicyNode.SelectNodes("ns:SupplementalPolicySigners", codeIntegrityPolicy.NamespaceManager);

if (supplementalPolicySignersNodes is not null && supplementalPolicySignersNodes.Count > 0)
if (supplementalPolicySignersNodes is { Count: > 0 })
{
Logger.Write("Removing the SupplementalPolicySigners blocks and corresponding Signers");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ private void BrowseForFoldersButton_Click(object sender, RoutedEventArgs e)

List<string>? selectedFolders = FileDialogHelper.ShowMultipleDirectoryPickerDialog();

if (selectedFolders is not null && selectedFolders.Count > 0)
if (selectedFolders is { Count: > 0 })
{
// Add each folder to the HashSet of the selected directories
foreach (string folder in selectedFolders)
Expand Down
8 changes: 4 additions & 4 deletions AppControl Manager/Pages/CreateDenyPolicy.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void FilesAndFoldersBrowseForFilesSettingsCard_Click(object sender, Rout

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand Down Expand Up @@ -330,7 +330,7 @@ private void FilesAndFoldersBrowseForFilesButton_Click(object sender, RoutedEven

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -353,7 +353,7 @@ private void FilesAndFoldersBrowseForFoldersSettingsCard_Click(object sender, Ro

List<string>? selectedDirectories = FileDialogHelper.ShowMultipleDirectoryPickerDialog();

if (selectedDirectories is not null && selectedDirectories.Count > 0)
if (selectedDirectories is { Count: > 0 })
{
foreach (string dir in selectedDirectories)
{
Expand All @@ -378,7 +378,7 @@ private void FilesAndFoldersBrowseForFoldersButton_Click(object sender, RoutedEv
{
List<string>? selectedDirectories = FileDialogHelper.ShowMultipleDirectoryPickerDialog();

if (selectedDirectories is not null && selectedDirectories.Count > 0)
if (selectedDirectories is { Count: > 0 })
{
foreach (string dir in selectedDirectories)
{
Expand Down
12 changes: 6 additions & 6 deletions AppControl Manager/Pages/CreateSupplementalPolicy.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void FilesAndFoldersBrowseForFilesSettingsCard_Click(object sender, Rout

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand Down Expand Up @@ -167,7 +167,7 @@ private void FilesAndFoldersBrowseForFilesButton_Click(object sender, RoutedEven

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -191,7 +191,7 @@ private void FilesAndFoldersBrowseForFoldersSettingsCard_Click(object sender, Ro

List<string>? selectedDirectories = FileDialogHelper.ShowMultipleDirectoryPickerDialog();

if (selectedDirectories is not null && selectedDirectories.Count > 0)
if (selectedDirectories is { Count: > 0 })
{
foreach (string dir in selectedDirectories)
{
Expand All @@ -217,7 +217,7 @@ private void FilesAndFoldersBrowseForFoldersButton_Click(object sender, RoutedEv
{
List<string>? selectedDirectories = FileDialogHelper.ShowMultipleDirectoryPickerDialog();

if (selectedDirectories is not null && selectedDirectories.Count > 0)
if (selectedDirectories is { Count: > 0 })
{
foreach (string dir in selectedDirectories)
{
Expand Down Expand Up @@ -667,7 +667,7 @@ private void CertificatesBrowseForCertsButton_Click(object sender, RoutedEventAr

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -682,7 +682,7 @@ private void CertificatesBrowseForCertsSettingsCard_Click(object sender, RoutedE

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand Down
8 changes: 4 additions & 4 deletions AppControl Manager/Pages/Deployment.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void BrowseForXMLPolicyFilesButton_Click(object sender, RoutedEventArgs

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -185,7 +185,7 @@ private void BrowseForXMLPolicyFilesSettingsCard_Click(object sender, RoutedEven

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -200,7 +200,7 @@ private void BrowseForCIPBinaryFilesButton_Click(object sender, RoutedEventArgs

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -215,7 +215,7 @@ private void BrowseForCIPBinaryFilesSettingsCard_Click(object sender, RoutedEven

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand Down
2 changes: 1 addition & 1 deletion AppControl Manager/Pages/GitHubDocumentation.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void WebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.
{

// Check if the WebView2 control or its CoreWebView2 instance is disposed
if (GitHubDocumentationWebView2 is not null && GitHubDocumentationWebView2.CoreWebView2 is not null)
if (GitHubDocumentationWebView2 is { CoreWebView2: not null })
{
BackButton.IsEnabled = GitHubDocumentationWebView2.CanGoBack;
ForwardButton.IsEnabled = GitHubDocumentationWebView2.CanGoForward;
Expand Down
4 changes: 2 additions & 2 deletions AppControl Manager/Pages/MergePolicies.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void OtherPoliciesBrowseButton_Click(object sender, RoutedEventArgs e)

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand All @@ -183,7 +183,7 @@ private void OtherPoliciesSettingsCard_Click(object sender, RoutedEventArgs e)

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
foreach (string file in selectedFiles)
{
Expand Down
2 changes: 1 addition & 1 deletion AppControl Manager/Pages/MicrosoftDocumentation.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void WebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.
try
{
// Check if the WebView2 control or its CoreWebView2 instance is disposed
if (MicrosoftDocumentationWebView2 is not null && MicrosoftDocumentationWebView2.CoreWebView2 is not null)
if (MicrosoftDocumentationWebView2 is { CoreWebView2: not null })
{
BackButton.IsEnabled = MicrosoftDocumentationWebView2.CanGoBack;
ForwardButton.IsEnabled = MicrosoftDocumentationWebView2.CanGoForward;
Expand Down
4 changes: 2 additions & 2 deletions AppControl Manager/Pages/Simulation.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void SelectFilesButton_Click(object sender, RoutedEventArgs e)

List<string>? selectedFiles = FileDialogHelper.ShowMultipleFilePickerDialog(filter);

if (selectedFiles is not null && selectedFiles.Count != 0)
if (selectedFiles is { Count: > 0 })
{
filePaths = [.. selectedFiles];
}
Expand All @@ -193,7 +193,7 @@ private void CatRootPathsButton_Click(object sender, RoutedEventArgs e)
{
List<string>? selectedCatRoots = FileDialogHelper.ShowMultipleDirectoryPickerDialog();

if (selectedCatRoots is not null && selectedCatRoots.Count > 0)
if (selectedCatRoots is { Count: > 0 })
{
catRootPaths = selectedCatRoots;
}
Expand Down
2 changes: 1 addition & 1 deletion AppControl Manager/Pages/Update.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private async void CheckForUpdateButton_Click(object sender, RoutedEventArgs e)
}

// If a new version is available or user supplied a custom MSIX path to be installed
if ((updateCheckResult is not null && updateCheckResult.IsNewVersionAvailable) || useCustomMSIXPath)
if ((updateCheckResult is { IsNewVersionAvailable: true }) || useCustomMSIXPath)
{
string msg1;

Expand Down
8 changes: 4 additions & 4 deletions AppControl Manager/SiPolicy/Merger.Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static HashSet<AllowRule> CollectAllowRules(List<SiPolicy> siPolicies)
// Get all possible FileRuleRef items from the current signing scenario
FileRuleRef[]? possibleFileRuleRef = signingScenario.ProductSigners?.FileRulesRef?.FileRuleRef;

if (possibleFileRuleRef is not null && possibleFileRuleRef.Length > 0)
if (possibleFileRuleRef is { Length: > 0 })
{
foreach (FileRuleRef fileRuleRef in possibleFileRuleRef)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ internal static HashSet<DenyRule> CollectDenyRules(List<SiPolicy> siPolicies)
// Get all possible FileRuleRef items from the current signing scenario
FileRuleRef[]? possibleFileRuleRef = signingScenario.ProductSigners?.FileRulesRef?.FileRuleRef;

if (possibleFileRuleRef is not null && possibleFileRuleRef.Length > 0)
if (possibleFileRuleRef is { Length: > 0 })
{
foreach (FileRuleRef fileRuleRef in possibleFileRuleRef)
{
Expand Down Expand Up @@ -164,7 +164,7 @@ internal static SignerCollection CollectSignerRules(List<SiPolicy> siPolicies)
AllowedSigner[]? allowedSigners = possibleProdSigners.AllowedSigners?.AllowedSigner;
DeniedSigner[]? deniedSigners = possibleProdSigners.DeniedSigners?.DeniedSigner;

if (allowedSigners is not null && allowedSigners.Length > 0)
if (allowedSigners is { Length: > 0 })
{
// Process Allowed Signers
foreach (AllowedSigner item in allowedSigners)
Expand All @@ -189,7 +189,7 @@ internal static SignerCollection CollectSignerRules(List<SiPolicy> siPolicies)
}
}

if (deniedSigners is not null && deniedSigners.Length > 0)
if (deniedSigners is { Length: > 0 })
{
// Process Denied Signers
foreach (DeniedSigner item in deniedSigners)
Expand Down
4 changes: 2 additions & 2 deletions AppControl Manager/SiPolicy/Merger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal static void Merge(string mainXmlFilePath, HashSet<string> otherXmlFileP
};

// Add miscellaneous settings to the User Mode Signing Scenario from the Main XML
if (mainXMLUserModeSigningScenario is not null && mainXMLUserModeSigningScenario.MinimumHashAlgorithmSpecified)
if (mainXMLUserModeSigningScenario is { MinimumHashAlgorithmSpecified: true })
{
UMCISigningScenario.MinimumHashAlgorithmSpecified = true;
UMCISigningScenario.MinimumHashAlgorithm = mainXMLUserModeSigningScenario.MinimumHashAlgorithm;
Expand Down Expand Up @@ -191,7 +191,7 @@ internal static void Merge(string mainXmlFilePath, HashSet<string> otherXmlFileP


// Add miscellaneous settings to the Kernel Mode Signing Scenario from the Main XML
if (mainXMLKernelModeSigningScenario is not null && mainXMLKernelModeSigningScenario.MinimumHashAlgorithmSpecified)
if (mainXMLKernelModeSigningScenario is { MinimumHashAlgorithmSpecified: true })
{
KMCISigningScenario.MinimumHashAlgorithmSpecified = true;
KMCISigningScenario.MinimumHashAlgorithm = mainXMLKernelModeSigningScenario.MinimumHashAlgorithm;
Expand Down
Loading

0 comments on commit 8ab8019

Please sign in to comment.