diff --git a/Test/Program.cs b/Test/Program.cs index 5333939d..3b74f038 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using BurnOutSharp; @@ -22,45 +23,72 @@ namespace Test ScanPackers = true, }; + // Loop through the input paths foreach (string arg in args) { - try - { - var protections = scanner.GetProtections(arg); - if (protections != null) - { - using (StreamWriter sw = new StreamWriter(File.OpenWrite($"{DateTime.Now:yyyy-MM-dd_HHmmss}.txt"))) - { - foreach (string key in protections.Keys) - { - // Skip over files with no protection - if (protections[key] == null || !protections[key].Any()) - continue; - - string line = $"{key}: {string.Join(", ", protections[key])}"; - Console.WriteLine(line); - sw.WriteLine(line); - } - } - } - else - { - Console.WriteLine($"No protections found for {arg}"); - } - } - catch (Exception ex) - { - using (StreamWriter sw = new StreamWriter(File.OpenWrite($"{DateTime.Now:yyyy-MM-dd_HHmmss}-exception.txt"))) - { - sw.WriteLine(ex.Message); - } - } + GetAndWriteProtections(scanner, arg); } Console.WriteLine("Press any button to close..."); Console.ReadLine(); } + /// + /// Wrapper to get and log protections for a single path + /// + /// Scanner object to use + /// File or directory path + private static void GetAndWriteProtections(Scanner scanner, string path) + { + // An invalid path can't be scanned + if (!Directory.Exists(path) && !File.Exists(path)) + { + Console.WriteLine($"{path} does not exist, skipping..."); + return; + } + + try + { + var protections = scanner.GetProtections(path); + WriteProtectionResultFile(path, protections); + } + catch (Exception ex) + { + using (StreamWriter sw = new StreamWriter(File.OpenWrite($"{DateTime.Now:yyyy-MM-dd_HHmmss}-exception.txt"))) + { + sw.WriteLine(ex.Message); + } + } + } + + /// + /// Write the protection results from a single path to file, if possible + /// + /// File or directory path + /// Dictionary of protections found, if any + private static void WriteProtectionResultFile(string path, Dictionary> protections) + { + if (protections == null) + { + Console.WriteLine($"No protections found for {path}"); + return; + } + + using (var sw = new StreamWriter(File.OpenWrite($"{DateTime.Now:yyyy-MM-dd_HHmmss}.txt"))) + { + foreach (string key in protections.Keys) + { + // Skip over files with no protection + if (protections[key] == null || !protections[key].Any()) + continue; + + string line = $"{key}: {string.Join(", ", protections[key])}"; + Console.WriteLine(line); + sw.WriteLine(line); + } + } + } + private static void Changed(object source, ProtectionProgress value) { Console.WriteLine($"{value.Percentage * 100:N2}%: {value.Filename} - {value.Protection}");