Optimize checking with better caching

This commit is contained in:
Matt Nadareski
2021-03-23 10:04:09 -07:00
parent 921292e077
commit 7d13b8c9db
2 changed files with 22 additions and 30 deletions

View File

@@ -12,7 +12,7 @@ namespace BurnOutSharp.FileType
/// <summary>
/// Cache for all IContentCheck types
/// </summary>
private static IEnumerable<Type> contentCheckClasses = null;
private static IEnumerable<IContentCheck> contentCheckClasses = InitContentCheckClasses();
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
@@ -77,30 +77,22 @@ namespace BurnOutSharp.FileType
// Files can be protected in multiple ways
var protections = new Dictionary<string, List<string>>();
// Get all IContentCheck implementations
if (contentCheckClasses == null)
{
contentCheckClasses = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface("IContentCheck") != null);
}
// Iterate through all content checks
foreach (var contentCheckClass in contentCheckClasses)
{
IContentCheck contentCheck = Activator.CreateInstance(contentCheckClass) as IContentCheck;
string protection = contentCheck.CheckContents(file, fileContent, scanner.IncludePosition);
string protection = contentCheckClass.CheckContents(file, fileContent, scanner.IncludePosition);
// If we have a valid content check based on settings
if (!contentCheckClass.Namespace.ToLowerInvariant().Contains("packertype") || scanner.ScanPackers)
if (!contentCheckClass.GetType().Namespace.ToLowerInvariant().Contains("packertype") || scanner.ScanPackers)
{
if (!string.IsNullOrWhiteSpace(protection))
Utilities.AppendToDictionary(protections, file, protection);
}
// If we have an IScannable implementation
if (contentCheckClass.GetInterface("IScannable") != null)
if (contentCheckClass is IScannable)
{
IScannable scannable = Activator.CreateInstance(contentCheckClass) as IScannable;
IScannable scannable = contentCheckClass as IScannable;
if (file != null && !string.IsNullOrEmpty(protection))
{
var subProtections = scannable.Scan(scanner, null, file);
@@ -112,5 +104,15 @@ namespace BurnOutSharp.FileType
return protections;
}
/// <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);
}
}
}

View File

@@ -37,7 +37,7 @@ namespace BurnOutSharp
/// <summary>
/// Cache for all IPathCheck types
/// </summary>
private static IEnumerable<Type> pathCheckClasses = null;
private static IEnumerable<IPathCheck> pathCheckClasses = InitPathCheckClasses();
/// <summary>
/// Constructor
@@ -186,14 +186,10 @@ namespace BurnOutSharp
// Create an empty list for protections
List<string> protections = new List<string>();
// Initialize the needed classes
InitPathCheckClasses();
// Iterate through all path checks
foreach (var pathCheckClass in pathCheckClasses)
{
IPathCheck pathCheck = Activator.CreateInstance(pathCheckClass) as IPathCheck;
string protection = pathCheck.CheckDirectoryPath(path, files);
string protection = pathCheckClass.CheckDirectoryPath(path, files);
if (!string.IsNullOrWhiteSpace(protection))
protections.Add(protection);
}
@@ -215,14 +211,10 @@ namespace BurnOutSharp
// Create an empty list for protections
List<string> protections = new List<string>();
// Initialize the needed classes
InitPathCheckClasses();
// Iterate through all path checks
foreach (var pathCheckClass in pathCheckClasses)
{
IPathCheck pathCheck = Activator.CreateInstance(pathCheckClass) as IPathCheck;
string protection = pathCheck.CheckFilePath(path);
string protection = pathCheckClass.CheckFilePath(path);
if (!string.IsNullOrWhiteSpace(protection))
protections.Add(protection);
}
@@ -407,13 +399,11 @@ namespace BurnOutSharp
/// <summary>
/// Initialize all IPathCheck implementations
/// </summary>
private static void InitPathCheckClasses()
private static IEnumerable<IPathCheck> InitPathCheckClasses()
{
if (pathCheckClasses == null)
{
pathCheckClasses = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface("IPathCheck") != null);
}
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.GetInterface(nameof(IPathCheck)) != null)
.Select(t => Activator.CreateInstance(t) as IPathCheck);
}
}
}