diff --git a/BurnOutSharp/IPathCheck.cs b/BurnOutSharp/IPathCheck.cs index 4fd8c80b..7558944f 100644 --- a/BurnOutSharp/IPathCheck.cs +++ b/BurnOutSharp/IPathCheck.cs @@ -10,6 +10,7 @@ namespace BurnOutSharp /// Path to check for protection indicators /// Enumerable of strings representing files in a directory /// This can do some limited content checking as well, but it's suggested to use IContentCheck instead, if possible + /// TODO: This should return a dictionary of file to protection mappings instead of just a single string string CheckDirectoryPath(string path, IEnumerable files); /// diff --git a/BurnOutSharp/Scanner.cs b/BurnOutSharp/Scanner.cs index 6d2f099d..65808cde 100644 --- a/BurnOutSharp/Scanner.cs +++ b/BurnOutSharp/Scanner.cs @@ -86,7 +86,7 @@ namespace BurnOutSharp var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).ToList(); // Scan for path-detectable protections - var directoryPathProtections = GetPathProtections(path, true, files); + var directoryPathProtections = GetDirectoryPathProtections(path, files); Utilities.AppendToDictionary(protections, directoryPathProtections); // Scan each file in directory separately @@ -104,7 +104,7 @@ namespace BurnOutSharp FileProgress?.Report(new ProtectionProgress(reportableFileName, i / (float)files.Count(), "Checking file" + (file != reportableFileName ? " from archive" : string.Empty))); // Scan for path-detectable protections - var filePathProtections = GetPathProtections(file, false); + var filePathProtections = GetFilePathProtections(file); Utilities.AppendToDictionary(protections, filePathProtections); // Scan for content-detectable protections @@ -139,7 +139,7 @@ namespace BurnOutSharp FileProgress?.Report(new ProtectionProgress(reportableFileName, 0, "Checking file" + (path != reportableFileName ? " from archive" : string.Empty))); // Scan for path-detectable protections - var filePathProtections = GetPathProtections(path, false); + var filePathProtections = GetFilePathProtections(path); Utilities.AppendToDictionary(protections, filePathProtections); // Scan for content-detectable protections @@ -174,37 +174,56 @@ namespace BurnOutSharp return protections; } - + /// /// Get the path-detectable protections associated with a single path /// - /// Path of the directory or file to scan - /// True if the path is a directory, false otherwise - /// Files contained within if the path is a directory + /// Path of the directory to scan + /// Files contained within /// Dictionary of list of strings representing the found protections - private Dictionary> GetPathProtections(string path, bool isDirectory, List files = null) + private Dictionary> GetDirectoryPathProtections(string path, List files) + { // Create an empty list for protections List protections = new List(); - // Get all IPathCheck implementations - if (pathCheckClasses == null) - { - pathCheckClasses = Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface("IPathCheck") != null); - } + // Initialize the needed classes + InitPathCheckClasses(); // Iterate through all path checks foreach (var pathCheckClass in pathCheckClasses) { IPathCheck pathCheck = Activator.CreateInstance(pathCheckClass) as IPathCheck; - - string protection = null; - if (isDirectory) - protection = pathCheck.CheckDirectoryPath(path, files); - else - protection = pathCheck.CheckFilePath(path); + string protection = pathCheck.CheckDirectoryPath(path, files);; + if (!string.IsNullOrWhiteSpace(protection)) + protections.Add(protection); + } + // Create and return the dictionary + return new Dictionary> + { + [path] = protections + }; + } + + /// + /// Get the path-detectable protections associated with a single path + /// + /// Path of the file to scan + /// Dictionary of list of strings representing the found protections + private Dictionary> GetFilePathProtections(string path) + { + // Create an empty list for protections + List protections = new List(); + + // 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); if (!string.IsNullOrWhiteSpace(protection)) protections.Add(protection); } @@ -385,5 +404,17 @@ namespace BurnOutSharp return protections; } + + /// + /// Initialize all IPathCheck implementations + /// + private void InitPathCheckClasses() + { + if (pathCheckClasses == null) + { + pathCheckClasses = Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.IsClass && t.GetInterface("IPathCheck") != null); + } + } } }