From dc3914e97679fdc9fa5229945e9ef5a066de0db9 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 25 Dec 2022 22:55:48 -0800 Subject: [PATCH] Use new GCF extraction code --- BurnOutSharp/FileType/GCF.cs | 69 +++++++++++++++++++++++++++++++++ BurnOutSharp/Tools/Utilities.cs | 2 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 BurnOutSharp/FileType/GCF.cs diff --git a/BurnOutSharp/FileType/GCF.cs b/BurnOutSharp/FileType/GCF.cs new file mode 100644 index 00000000..54f0ee27 --- /dev/null +++ b/BurnOutSharp/FileType/GCF.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using BurnOutSharp.Interfaces; +using static BurnOutSharp.Utilities.Dictionary; + +namespace BurnOutSharp.FileType +{ + /// + /// Half-Life Game Cache File + /// + public class GCF : 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.GCF gcf = Wrappers.GCF.Create(stream); + if (gcf == null) + return null; + + // Loop through and extract all files + gcf.ExtractAll(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 00152139..cb639c66 100644 --- a/BurnOutSharp/Tools/Utilities.cs +++ b/BurnOutSharp/Tools/Utilities.cs @@ -620,7 +620,7 @@ namespace BurnOutSharp.Tools 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(); + case SupportedFileType.GCF: return new FileType.GCF(); case SupportedFileType.GZIP: return new FileType.GZIP(); //case FileTypes.IniFile: return new FileType.IniFile(); case SupportedFileType.InstallShieldArchiveV3: return new FileType.InstallShieldArchiveV3();