Create Initializer class

This commit is contained in:
Matt Nadareski
2022-05-01 14:16:53 -07:00
parent aac3c391db
commit 478f28b513
3 changed files with 76 additions and 47 deletions

View File

@@ -2,8 +2,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using BurnOutSharp.ExecutableType.Microsoft.NE;
@@ -14,20 +12,24 @@ namespace BurnOutSharp.FileType
{
public class Executable : IScannable
{
#region Checking Class Instances
/// <summary>
/// Cache for all IContentCheck types
/// </summary>
private static readonly IEnumerable<IContentCheck> contentCheckClasses = InitContentCheckClasses();
private static readonly IEnumerable<IContentCheck> contentCheckClasses = Initializer.InitContentCheckClasses();
/// <summary>
/// Cache for all INEContentCheck types
/// </summary>
private static readonly IEnumerable<INEContentCheck> neContentCheckClasses = InitNEContentCheckClasses();
private static readonly IEnumerable<INEContentCheck> neContentCheckClasses = Initializer.InitNEContentCheckClasses();
/// <summary>
/// Cache for all IPEContentCheck types
/// </summary>
private static readonly IEnumerable<IPEContentCheck> peContentCheckClasses = InitPEContentCheckClasses();
private static readonly IEnumerable<IPEContentCheck> peContentCheckClasses = Initializer.InitPEContentCheckClasses();
#endregion
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
@@ -179,36 +181,6 @@ namespace BurnOutSharp.FileType
#region Helpers
/// <summary>
/// Initialize all IContentCheck implementations
/// </summary>
private static IEnumerable<IContentCheck> InitContentCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IContentCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IContentCheck);
}
/// <summary>
/// Initialize all INEContentCheck implementations
/// </summary>
private static IEnumerable<INEContentCheck> InitNEContentCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(INEContentCheck)) != null)
.Select(t => Activator.CreateInstance(t) as INEContentCheck);
}
/// <summary>
/// Initialize all IPEContentCheck implementations
/// </summary>
private static IEnumerable<IPEContentCheck> InitPEContentCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IPEContentCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IPEContentCheck);
}
/// <summary>
/// Check to see if a protection should be added or not
/// </summary>

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace BurnOutSharp
{
/// <summary>
/// Internal static initializers using reflection
/// </summary>
/// <remarks>
/// These can probably be consolidated if Type variables are used
/// </remarks>
internal static class Initializer
{
/// <summary>
/// Initialize all IContentCheck implementations
/// </summary>
public static IEnumerable<IContentCheck> InitContentCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IContentCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IContentCheck);
}
/// <summary>
/// Initialize all INEContentCheck implementations
/// </summary>
public static IEnumerable<INEContentCheck> InitNEContentCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(INEContentCheck)) != null)
.Select(t => Activator.CreateInstance(t) as INEContentCheck);
}
/// <summary>
/// Initialize all IPathCheck implementations
/// </summary>
public static IEnumerable<IPathCheck> InitPathCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IPathCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IPathCheck);
}
/// <summary>
/// Initialize all IPEContentCheck implementations
/// </summary>
public static IEnumerable<IPEContentCheck> InitPEContentCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IPEContentCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IPEContentCheck);
}
}
}

View File

@@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using BurnOutSharp.FileType;
using BurnOutSharp.Tools;
@@ -12,6 +11,8 @@ namespace BurnOutSharp
{
public class Scanner
{
#region Options
/// <summary>
/// Determines whether archives are decompressed and scanned
/// </summary>
@@ -32,10 +33,16 @@ namespace BurnOutSharp
/// </summary>
public IProgress<ProtectionProgress> FileProgress { get; private set; }
#endregion
#region Checking Class Instances
/// <summary>
/// Cache for all IPathCheck types
/// </summary>
private static readonly IEnumerable<IPathCheck> pathCheckClasses = InitPathCheckClasses();
private static readonly IEnumerable<IPathCheck> pathCheckClasses = Initializer.InitPathCheckClasses();
#endregion
/// <summary>
/// Constructor
@@ -52,6 +59,8 @@ namespace BurnOutSharp
FileProgress = fileProgress;
}
#region Scanning
/// <summary>
/// Scan a single path and get all found protections
/// </summary>
@@ -425,15 +434,7 @@ namespace BurnOutSharp
return protections;
}
/// <summary>
/// Initialize all IPathCheck implementations
/// </summary>
private static IEnumerable<IPathCheck> InitPathCheckClasses()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IPathCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IPathCheck);
}
#endregion
}
}