diff --git a/BurnOutSharp/FileType/BSP.cs b/BurnOutSharp/FileType/BSP.cs new file mode 100644 index 00000000..669ee1da --- /dev/null +++ b/BurnOutSharp/FileType/BSP.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using BurnOutSharp.Interfaces; +using static BurnOutSharp.Utilities.Dictionary; + +namespace BurnOutSharp.FileType +{ + /// + /// Valve Package File + /// + public class BSP : IScannable + { + /// + public ConcurrentDictionary> Scan(Scanner scanner, string file) + { + if (!File.Exists(file)) + return null; + + using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + return Scan(scanner, fs, file); + } + } + + /// + public ConcurrentDictionary> Scan(Scanner scanner, Stream stream, string file) + { + // If the BSP file itself fails + try + { + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(tempPath); + + // Create the wrapper + Wrappers.BSP bsp = Wrappers.BSP.Create(stream); + if (bsp == null) + return null; + + // Loop through and extract all files + bsp.ExtractAllLumps(tempPath); + bsp.ExtractAllTextures(tempPath); + + // Collect and format all found protections + var protections = scanner.GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (scanner.IncludeDebug) Console.WriteLine(ex); + } + + // Remove temporary path references + StripFromKeys(protections, tempPath); + + return protections; + } + catch (Exception ex) + { + if (scanner.IncludeDebug) Console.WriteLine(ex); + } + + return null; + } + } +} diff --git a/BurnOutSharp/Tools/Utilities.cs b/BurnOutSharp/Tools/Utilities.cs index 1d7418dc..00152139 100644 --- a/BurnOutSharp/Tools/Utilities.cs +++ b/BurnOutSharp/Tools/Utilities.cs @@ -312,8 +312,7 @@ namespace BurnOutSharp.Tools #region BSP - if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.BSP; + // Shares an extension with VBSP so it can't be used accurately #endregion @@ -574,8 +573,7 @@ namespace BurnOutSharp.Tools #region VBSP - if (extension.Equals("bsp", StringComparison.OrdinalIgnoreCase)) - return SupportedFileType.VBSP; + // Shares an extension with BSP so it can't be used accurately #endregion @@ -619,7 +617,7 @@ namespace BurnOutSharp.Tools switch (fileType) { case SupportedFileType.BFPK: return new FileType.BFPK(); - case SupportedFileType.BSP: return new FileType.Valve(); + case SupportedFileType.BSP: return new FileType.BSP(); case SupportedFileType.BZip2: return new FileType.BZip2(); case SupportedFileType.Executable: return new FileType.Executable(); case SupportedFileType.GCF: return new FileType.Valve();