diff --git a/BinaryObjectScanner/FileType/AACSMediaKeyBlock.cs b/BinaryObjectScanner/FileType/AACSMediaKeyBlock.cs index 5d862741..31ea7095 100644 --- a/BinaryObjectScanner/FileType/AACSMediaKeyBlock.cs +++ b/BinaryObjectScanner/FileType/AACSMediaKeyBlock.cs @@ -16,10 +16,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// diff --git a/BinaryObjectScanner/FileType/BDPlusSVM.cs b/BinaryObjectScanner/FileType/BDPlusSVM.cs index a16f467e..d670466b 100644 --- a/BinaryObjectScanner/FileType/BDPlusSVM.cs +++ b/BinaryObjectScanner/FileType/BDPlusSVM.cs @@ -15,10 +15,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// diff --git a/BinaryObjectScanner/FileType/BFPK.cs b/BinaryObjectScanner/FileType/BFPK.cs index 2df1f377..5bab294a 100644 --- a/BinaryObjectScanner/FileType/BFPK.cs +++ b/BinaryObjectScanner/FileType/BFPK.cs @@ -14,38 +14,35 @@ namespace BinaryObjectScanner.FileType public class BFPK : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var bfpk = SabreTools.Serialization.Wrappers.BFPK.Create(stream); if (bfpk == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Extract all files - ExtractAll(bfpk, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(bfpk, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/BSP.cs b/BinaryObjectScanner/FileType/BSP.cs index 2479f4aa..425c0cb6 100644 --- a/BinaryObjectScanner/FileType/BSP.cs +++ b/BinaryObjectScanner/FileType/BSP.cs @@ -11,41 +11,36 @@ namespace BinaryObjectScanner.FileType public class BSP : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var bsp = SabreTools.Serialization.Wrappers.BSP.Create(stream); if (bsp == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAllLumps(bsp, tempPath); - ExtractAllTextures(bsp, tempPath); + Directory.CreateDirectory(outDir); + ExtractAllLumps(bsp, outDir); + ExtractAllTextures(bsp, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/BZip2.cs b/BinaryObjectScanner/FileType/BZip2.cs index 5e9b0482..3975fe0a 100644 --- a/BinaryObjectScanner/FileType/BZip2.cs +++ b/BinaryObjectScanner/FileType/BZip2.cs @@ -14,46 +14,44 @@ namespace BinaryObjectScanner.FileType public class BZip2 : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - if (stream == null) - return null; + if (stream == null || !stream.CanRead) + return false; #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + // Try opening the stream + using var bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true); - using (var bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true)) - { - string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString()); - using (FileStream fs = File.OpenWrite(tempFile)) - { - bz2File.CopyTo(fs); - } - } + // Create the output file path + Directory.CreateDirectory(outDir); + string tempFile = Path.Combine(outDir, Guid.NewGuid().ToString()); - return tempPath; + // Extract the file + using FileStream fs = File.OpenWrite(tempFile); + bz2File.CopyTo(fs); + + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/CFB.cs b/BinaryObjectScanner/FileType/CFB.cs index d89a1f43..f33f310b 100644 --- a/BinaryObjectScanner/FileType/CFB.cs +++ b/BinaryObjectScanner/FileType/CFB.cs @@ -14,79 +14,75 @@ namespace BinaryObjectScanner.FileType public class CFB : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { #if NET20 || NET35 // Not supported for .NET Framework 2.0 or .NET Framework 3.5 due to library support - return null; + return false; #else try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (CompoundFile msi = new CompoundFile(stream, CFSUpdateMode.ReadOnly, CFSConfiguration.Default)) + using var msi = new CompoundFile(stream, CFSUpdateMode.ReadOnly, CFSConfiguration.Default); + msi.RootStorage.VisitEntries((e) => { - msi.RootStorage.VisitEntries((e) => + try { - try + if (!e.IsStream) + return; + + var str = msi.RootStorage.GetStream(e.Name); + if (str == null) + return; + + byte[] strData = str.GetData(); + if (strData == null) + return; + + var decoded = DecodeStreamName(e.Name)?.TrimEnd('\0'); + if (decoded == null) + return; + + byte[] nameBytes = Encoding.UTF8.GetBytes(e.Name); + + // UTF-8 encoding of 0x4840. + if (nameBytes[0] == 0xe4 && nameBytes[1] == 0xa1 && nameBytes[2] == 0x80) + decoded = decoded.Substring(3); + + foreach (char c in Path.GetInvalidFileNameChars()) { - if (!e.IsStream) - return; - - var str = msi.RootStorage.GetStream(e.Name); - if (str == null) - return; - - byte[] strData = str.GetData(); - if (strData == null) - return; - - var decoded = DecodeStreamName(e.Name)?.TrimEnd('\0'); - if (decoded == null) - return; - - byte[] nameBytes = Encoding.UTF8.GetBytes(e.Name); - - // UTF-8 encoding of 0x4840. - if (nameBytes[0] == 0xe4 && nameBytes[1] == 0xa1 && nameBytes[2] == 0x80) - decoded = decoded.Substring(3); - - foreach (char c in Path.GetInvalidFileNameChars()) - { - decoded = decoded.Replace(c, '_'); - } - - string filename = Path.Combine(tempPath, decoded); - using (Stream fs = File.OpenWrite(filename)) - { - fs.Write(strData, 0, strData.Length); - } + decoded = decoded.Replace(c, '_'); } - catch (Exception ex) - { - if (includeDebug) Console.WriteLine(ex); - } - }, recursive: true); - } - return tempPath; + string tempFile = Path.Combine(outDir, decoded); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + using Stream fs = File.OpenWrite(tempFile); + fs.Write(strData, 0, strData.Length); + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); + } + }, recursive: true); + + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #endif } diff --git a/BinaryObjectScanner/FileType/GCF.cs b/BinaryObjectScanner/FileType/GCF.cs index efc33d65..620017fd 100644 --- a/BinaryObjectScanner/FileType/GCF.cs +++ b/BinaryObjectScanner/FileType/GCF.cs @@ -11,40 +11,35 @@ namespace BinaryObjectScanner.FileType public class GCF : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var gcf = SabreTools.Serialization.Wrappers.GCF.Create(stream); if (gcf == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAll(gcf, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(gcf, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/GZIP.cs b/BinaryObjectScanner/FileType/GZIP.cs index ec1937de..1c2dbb05 100644 --- a/BinaryObjectScanner/FileType/GZIP.cs +++ b/BinaryObjectScanner/FileType/GZIP.cs @@ -14,61 +14,59 @@ namespace BinaryObjectScanner.FileType public class GZIP : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - if (stream == null) - return null; + if (stream == null || !stream.CanRead) + return false; #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (GZipArchive zipFile = GZipArchive.Open(stream)) + using var zipFile = GZipArchive.Open(stream); + foreach (var entry in zipFile.Entries) { - foreach (var entry in zipFile.Entries) + try { - try - { - // If the entry is a directory - if (entry.IsDirectory) - continue; + // If the entry is a directory + if (entry.IsDirectory) + continue; - // If the entry has an invalid key - if (entry.Key == null) - continue; + // If the entry has an invalid key + if (entry.Key == null) + continue; - string tempFile = Path.Combine(tempPath, entry.Key); - entry.WriteToFile(tempFile); - } - catch (Exception ex) - { - if (includeDebug) Console.WriteLine(ex); - } + string tempFile = Path.Combine(outDir, entry.Key); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + entry.WriteToFile(tempFile); + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/InstallShieldArchiveV3.cs b/BinaryObjectScanner/FileType/InstallShieldArchiveV3.cs index cb50a24c..a2d51407 100644 --- a/BinaryObjectScanner/FileType/InstallShieldArchiveV3.cs +++ b/BinaryObjectScanner/FileType/InstallShieldArchiveV3.cs @@ -1,6 +1,7 @@ using System; using System.IO; using BinaryObjectScanner.Interfaces; +using ISv3 = UnshieldSharp.Archive.InstallShieldArchiveV3; namespace BinaryObjectScanner.FileType { @@ -10,30 +11,26 @@ namespace BinaryObjectScanner.FileType public class InstallShieldArchiveV3 : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - UnshieldSharp.Archive.InstallShieldArchiveV3 archive = new UnshieldSharp.Archive.InstallShieldArchiveV3(file); + var archive = new ISv3(file); foreach (var cfile in archive.Files) { try { - string tempFile = Path.Combine(tempPath, cfile.Key); + string tempFile = Path.Combine(outDir, cfile.Key); var directoryName = Path.GetDirectoryName(tempFile); if (directoryName != null && !Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName); @@ -53,12 +50,12 @@ namespace BinaryObjectScanner.FileType } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } } diff --git a/BinaryObjectScanner/FileType/InstallShieldCAB.cs b/BinaryObjectScanner/FileType/InstallShieldCAB.cs index f67ecc7d..35a60e3b 100644 --- a/BinaryObjectScanner/FileType/InstallShieldCAB.cs +++ b/BinaryObjectScanner/FileType/InstallShieldCAB.cs @@ -12,17 +12,17 @@ namespace BinaryObjectScanner.FileType public class InstallShieldCAB : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { // Get the name of the first cabinet file or header var directory = Path.GetDirectoryName(file); @@ -50,17 +50,13 @@ namespace BinaryObjectScanner.FileType // If we have anything but the first file if (!shouldScanCabinet) - return null; + return false; try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - var cabfile = InstallShieldCabinet.Open(file); if (cabfile?.HeaderList == null) - return null; + return false; for (int i = 0; i < cabfile.HeaderList.FileCount; i++) { @@ -74,11 +70,11 @@ namespace BinaryObjectScanner.FileType try { string? filename = cabfile.HeaderList.GetFileName(i); - tempFile = Path.Combine(tempPath, filename ?? string.Empty); + tempFile = Path.Combine(outDir, filename ?? string.Empty); } catch { - tempFile = Path.Combine(tempPath, $"BAD_FILENAME{i}"); + tempFile = Path.Combine(outDir, $"BAD_FILENAME{i}"); } cabfile.FileSave(i, tempFile); @@ -89,12 +85,12 @@ namespace BinaryObjectScanner.FileType } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } } diff --git a/BinaryObjectScanner/FileType/LDSCRYPT.cs b/BinaryObjectScanner/FileType/LDSCRYPT.cs index 44e80d65..e61ca540 100644 --- a/BinaryObjectScanner/FileType/LDSCRYPT.cs +++ b/BinaryObjectScanner/FileType/LDSCRYPT.cs @@ -16,10 +16,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// diff --git a/BinaryObjectScanner/FileType/MPQ.cs b/BinaryObjectScanner/FileType/MPQ.cs index 92bd0811..9a375ae5 100644 --- a/BinaryObjectScanner/FileType/MPQ.cs +++ b/BinaryObjectScanner/FileType/MPQ.cs @@ -12,71 +12,68 @@ namespace BinaryObjectScanner.FileType public class MPQ : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } // TODO: Add stream opening support /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { #if NET20 || NET35 || NET40 || !WIN // Not supported for old .NET due to feature requirements // Not supported in non-Windows builds due to DLL requirements - return null; + return false; #else try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + // Try to open the archive and listfile + var mpqArchive = new MpqArchive(file, FileAccess.Read); + string? listfile = null; + MpqFileStream listStream = mpqArchive.OpenFile("(listfile)"); - using (var mpqArchive = new MpqArchive(file, FileAccess.Read)) + // If we can't read the listfile, we just return + if (!listStream.CanRead) + return null; + + // Read the listfile in for processing + using (var sr = new StreamReader(listStream)) { - // Try to open the listfile - string? listfile = null; - MpqFileStream listStream = mpqArchive.OpenFile("(listfile)"); + listfile = sr.ReadToEnd(); + } - // If we can't read the listfile, we just return - if (!listStream.CanRead) - return null; + // Split the listfile by newlines + string[] listfileLines = listfile.Replace("\r\n", "\n").Split('\n'); - // Read the listfile in for processing - using (var sr = new StreamReader(listStream)) + // Loop over each entry + foreach (string sub in listfileLines) + { + try { - listfile = sr.ReadToEnd(); + string tempFile = Path.Combine(outDir, sub); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + mpqArchive.ExtractFile(sub, tempFile); } - - // Split the listfile by newlines - string[] listfileLines = listfile.Replace("\r\n", "\n").Split('\n'); - - // Loop over each entry - foreach (string sub in listfileLines) + catch (System.Exception ex) { - try - { - string tempFile = Path.Combine(tempPath, sub); - Directory.CreateDirectory(Path.GetDirectoryName(tempFile)); - mpqArchive.ExtractFile(sub, tempFile); - } - catch (System.Exception ex) - { - if (includeDebug) System.Console.WriteLine(ex); - } + if (includeDebug) System.Console.WriteLine(ex); } } - return tempPath; + return true; } catch (System.Exception ex) { if (includeDebug) System.Console.WriteLine(ex); - return null; + return false; } #endif } diff --git a/BinaryObjectScanner/FileType/MicrosoftCAB.cs b/BinaryObjectScanner/FileType/MicrosoftCAB.cs index cf006410..cc158b8f 100644 --- a/BinaryObjectScanner/FileType/MicrosoftCAB.cs +++ b/BinaryObjectScanner/FileType/MicrosoftCAB.cs @@ -14,53 +14,50 @@ namespace BinaryObjectScanner.FileType public class MicrosoftCAB : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { #if NET20 || NET35 || !WIN // Not supported for old .NET due to feature requirements // Not supported in non-Windows builds due to DLL requirements - return null; + return false; #else try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), System.Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (var cabArchive = new MSCabinet(file)) + // Loop over each entry + var cabArchive = new MSCabinet(file); + foreach (var compressedFile in cabArchive.GetFiles()) { - // Loop over each entry - foreach (var compressedFile in cabArchive.GetFiles()) + try { - try - { - string tempFile = Path.Combine(tempPath, compressedFile.Filename); - Directory.CreateDirectory(Path.GetDirectoryName(tempFile)); - compressedFile.ExtractTo(tempFile); - } - catch (System.Exception ex) - { - if (includeDebug) System.Console.WriteLine(ex); - } + string tempFile = Path.Combine(tempPath, compressedFile.Filename); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + compressedFile.ExtractTo(tempFile); + } + catch (System.Exception ex) + { + if (includeDebug) System.Console.WriteLine(ex); } } - return tempPath; + return true; } catch (System.Exception ex) { if (includeDebug) System.Console.WriteLine(ex); - return null; + return false; } #endif } diff --git a/BinaryObjectScanner/FileType/MicrosoftLZ.cs b/BinaryObjectScanner/FileType/MicrosoftLZ.cs index 5a2fd7ae..f8927a92 100644 --- a/BinaryObjectScanner/FileType/MicrosoftLZ.cs +++ b/BinaryObjectScanner/FileType/MicrosoftLZ.cs @@ -12,29 +12,23 @@ namespace BinaryObjectScanner.FileType public class MicrosoftLZ : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - var data = Decompressor.Decompress(stream); if (data == null) - return null; + return false; // Create the temp filename string tempFile = "temp.bin"; @@ -49,20 +43,20 @@ namespace BinaryObjectScanner.FileType tempFile += "l"; } - tempFile = Path.Combine(tempPath, tempFile); + tempFile = Path.Combine(outDir, tempFile); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); - // Write the file data to a temp file - using (Stream tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) - { - tempStream.Write(data, 0, data.Length); - } + using Stream tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); + tempStream.Write(data, 0, data.Length); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } } diff --git a/BinaryObjectScanner/FileType/PAK.cs b/BinaryObjectScanner/FileType/PAK.cs index 980a1826..90979060 100644 --- a/BinaryObjectScanner/FileType/PAK.cs +++ b/BinaryObjectScanner/FileType/PAK.cs @@ -10,40 +10,35 @@ namespace BinaryObjectScanner.FileType public class PAK : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var pak = SabreTools.Serialization.Wrappers.PAK.Create(stream); if (pak == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAll(pak, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(pak, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/PFF.cs b/BinaryObjectScanner/FileType/PFF.cs index a5a03413..f94711d1 100644 --- a/BinaryObjectScanner/FileType/PFF.cs +++ b/BinaryObjectScanner/FileType/PFF.cs @@ -10,40 +10,35 @@ namespace BinaryObjectScanner.FileType public class PFF : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var pff = SabreTools.Serialization.Wrappers.PFF.Create(stream); if (pff == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Extract all files - ExtractAll(pff, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(pff, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex.Message); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/PKZIP.cs b/BinaryObjectScanner/FileType/PKZIP.cs index fa725a98..f5e4f02c 100644 --- a/BinaryObjectScanner/FileType/PKZIP.cs +++ b/BinaryObjectScanner/FileType/PKZIP.cs @@ -14,68 +14,63 @@ namespace BinaryObjectScanner.FileType public class PKZIP : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - if (stream == null) - return null; + if (stream == null || !stream.CanRead) + return false; #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (ZipArchive zipFile = ZipArchive.Open(stream)) + using var zipFile = ZipArchive.Open(stream); + foreach (var entry in zipFile.Entries) { - foreach (var entry in zipFile.Entries) + try { - try - { - // If the entry is a directory - if (entry.IsDirectory) - continue; + // If the entry is a directory + if (entry.IsDirectory) + continue; - // If the entry has an invalid key - if (entry.Key == null) - continue; + // If the entry has an invalid key + if (entry.Key == null) + continue; - // If the entry is partial due to an incomplete multi-part archive, skip it - if (!entry.IsComplete) - continue; + // If the entry is partial due to an incomplete multi-part archive, skip it + if (!entry.IsComplete) + continue; - string tempFile = Path.Combine(tempPath, entry.Key); - var directoryName = Path.GetDirectoryName(tempFile); - if (directoryName != null && !Directory.Exists(directoryName)) - Directory.CreateDirectory(directoryName); - entry.WriteToFile(tempFile); - } - catch (Exception ex) - { - if (includeDebug) Console.WriteLine(ex); - } + string tempFile = Path.Combine(outDir, entry.Key); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + entry.WriteToFile(tempFile); + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/PLJ.cs b/BinaryObjectScanner/FileType/PLJ.cs index 156a750e..1361c3d3 100644 --- a/BinaryObjectScanner/FileType/PLJ.cs +++ b/BinaryObjectScanner/FileType/PLJ.cs @@ -16,10 +16,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// diff --git a/BinaryObjectScanner/FileType/Quantum.cs b/BinaryObjectScanner/FileType/Quantum.cs index 9757146e..27251fbd 100644 --- a/BinaryObjectScanner/FileType/Quantum.cs +++ b/BinaryObjectScanner/FileType/Quantum.cs @@ -10,40 +10,35 @@ namespace BinaryObjectScanner.FileType public class Quantum : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var quantum = SabreTools.Serialization.Wrappers.Quantum.Create(stream); if (quantum == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Extract all files - ExtractAll(quantum, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(quantum, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex.Message); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/RAR.cs b/BinaryObjectScanner/FileType/RAR.cs index 4e0983b9..e99780c9 100644 --- a/BinaryObjectScanner/FileType/RAR.cs +++ b/BinaryObjectScanner/FileType/RAR.cs @@ -14,68 +14,63 @@ namespace BinaryObjectScanner.FileType public class RAR : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - if (stream == null) - return null; + if (stream == null || !stream.CanRead) + return false; #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (RarArchive rarFile = RarArchive.Open(stream)) + using RarArchive rarFile = RarArchive.Open(stream); + foreach (var entry in rarFile.Entries) { - foreach (var entry in rarFile.Entries) + try { - try - { - // If the entry is a directory - if (entry.IsDirectory) - continue; + // If the entry is a directory + if (entry.IsDirectory) + continue; - // If the entry has an invalid key - if (entry.Key == null) - continue; + // If the entry has an invalid key + if (entry.Key == null) + continue; - // If we have a partial entry due to an incomplete multi-part archive, skip it - if (!entry.IsComplete) - continue; + // If we have a partial entry due to an incomplete multi-part archive, skip it + if (!entry.IsComplete) + continue; - string tempFile = Path.Combine(tempPath, entry.Key); - var directoryName = Path.GetDirectoryName(tempFile); - if (directoryName != null && !Directory.Exists(directoryName)) - Directory.CreateDirectory(directoryName); - entry.WriteToFile(tempFile); - } - catch (Exception ex) - { - if (includeDebug) Console.WriteLine(ex); - } + string tempFile = Path.Combine(outDir, entry.Key); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + entry.WriteToFile(tempFile); + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/RealArcadeInstaller.cs b/BinaryObjectScanner/FileType/RealArcadeInstaller.cs index 5033ac6d..39601e2b 100644 --- a/BinaryObjectScanner/FileType/RealArcadeInstaller.cs +++ b/BinaryObjectScanner/FileType/RealArcadeInstaller.cs @@ -18,10 +18,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// diff --git a/BinaryObjectScanner/FileType/RealArcadeMezzanine.cs b/BinaryObjectScanner/FileType/RealArcadeMezzanine.cs index 9b5a7ff3..ad51adba 100644 --- a/BinaryObjectScanner/FileType/RealArcadeMezzanine.cs +++ b/BinaryObjectScanner/FileType/RealArcadeMezzanine.cs @@ -18,10 +18,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// diff --git a/BinaryObjectScanner/FileType/SFFS.cs b/BinaryObjectScanner/FileType/SFFS.cs index 9a300065..f568b1ad 100644 --- a/BinaryObjectScanner/FileType/SFFS.cs +++ b/BinaryObjectScanner/FileType/SFFS.cs @@ -17,10 +17,8 @@ namespace BinaryObjectScanner.FileType if (!File.Exists(file)) return null; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Detect(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Detect(fs, file, includeDebug); } /// @@ -43,21 +41,19 @@ namespace BinaryObjectScanner.FileType } /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - return null; + return false; } } } diff --git a/BinaryObjectScanner/FileType/SGA.cs b/BinaryObjectScanner/FileType/SGA.cs index 3fa4f505..2e5dd8c1 100644 --- a/BinaryObjectScanner/FileType/SGA.cs +++ b/BinaryObjectScanner/FileType/SGA.cs @@ -13,38 +13,35 @@ namespace BinaryObjectScanner.FileType public class SGA : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var sga = SabreTools.Serialization.Wrappers.SGA.Create(stream); if (sga == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAll(sga, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(sga, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/SevenZip.cs b/BinaryObjectScanner/FileType/SevenZip.cs index 1c8927ef..814beacb 100644 --- a/BinaryObjectScanner/FileType/SevenZip.cs +++ b/BinaryObjectScanner/FileType/SevenZip.cs @@ -14,68 +14,63 @@ namespace BinaryObjectScanner.FileType public class SevenZip : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - if (stream == null) - return null; + if (stream == null || !stream.CanRead) + return false; #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (SevenZipArchive sevenZipFile = SevenZipArchive.Open(stream)) + using var sevenZipFile = SevenZipArchive.Open(stream); + foreach (var entry in sevenZipFile.Entries) { - foreach (var entry in sevenZipFile.Entries) + try { - try - { - // If the entry is a directory - if (entry.IsDirectory) - continue; + // If the entry is a directory + if (entry.IsDirectory) + continue; - // If the entry has an invalid key - if (entry.Key == null) - continue; + // If the entry has an invalid key + if (entry.Key == null) + continue; - // If we have a partial entry due to an incomplete multi-part archive, skip it - if (!entry.IsComplete) - continue; + // If we have a partial entry due to an incomplete multi-part archive, skip it + if (!entry.IsComplete) + continue; - string tempFile = Path.Combine(tempPath, entry.Key); - var directoryName = Path.GetDirectoryName(tempFile); - if (directoryName != null && !Directory.Exists(directoryName)) - Directory.CreateDirectory(directoryName); - entry.WriteToFile(tempFile); - } - catch (Exception ex) - { - if (includeDebug) Console.WriteLine(ex); - } + string tempFile = Path.Combine(outDir, entry.Key); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + entry.WriteToFile(tempFile); + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/TapeArchive.cs b/BinaryObjectScanner/FileType/TapeArchive.cs index c0c4f21a..bb9cf77c 100644 --- a/BinaryObjectScanner/FileType/TapeArchive.cs +++ b/BinaryObjectScanner/FileType/TapeArchive.cs @@ -14,69 +14,63 @@ namespace BinaryObjectScanner.FileType public class TapeArchive : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { - if (stream == null) - return null; + if (stream == null || !stream.CanRead) + return false; #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - - using (TarArchive tarFile = TarArchive.Open(stream)) + using var tarFile = TarArchive.Open(stream); + foreach (var entry in tarFile.Entries) { - foreach (var entry in tarFile.Entries) + try { - try - { - // If the entry is a directory - if (entry.IsDirectory) - continue; + // If the entry is a directory + if (entry.IsDirectory) + continue; - // If the entry has an invalid key - if (entry.Key == null) - continue; + // If the entry has an invalid key + if (entry.Key == null) + continue; - // If we have a partial entry due to an incomplete multi-part archive, skip it - if (!entry.IsComplete) - continue; + // If we have a partial entry due to an incomplete multi-part archive, skip it + if (!entry.IsComplete) + continue; - // TODO: Fix bug with extracting folders from tar. - string tempFile = Path.Combine(tempPath, entry.Key); - var directoryName = Path.GetDirectoryName(tempFile); - if (directoryName != null && !Directory.Exists(directoryName)) - Directory.CreateDirectory(directoryName); - entry.WriteToFile(tempFile); - } - catch (Exception ex) - { - if (includeDebug) Console.WriteLine(ex); - } + string tempFile = Path.Combine(outDir, entry.Key); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); + + entry.WriteToFile(tempFile); + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); } } - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/VBSP.cs b/BinaryObjectScanner/FileType/VBSP.cs index 1b2ede1f..a154dd86 100644 --- a/BinaryObjectScanner/FileType/VBSP.cs +++ b/BinaryObjectScanner/FileType/VBSP.cs @@ -10,40 +10,35 @@ namespace BinaryObjectScanner.FileType public class VBSP : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var vbsp = SabreTools.Serialization.Wrappers.VBSP.Create(stream); if (vbsp == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAllLumps(vbsp, tempPath); + Directory.CreateDirectory(outDir); + ExtractAllLumps(vbsp, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex.ToString()); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/VPK.cs b/BinaryObjectScanner/FileType/VPK.cs index 4ccf2994..380cc22d 100644 --- a/BinaryObjectScanner/FileType/VPK.cs +++ b/BinaryObjectScanner/FileType/VPK.cs @@ -12,40 +12,35 @@ namespace BinaryObjectScanner.FileType public class VPK : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var vpk = SabreTools.Serialization.Wrappers.VPK.Create(stream); if (vpk == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAll(vpk, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(vpk, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/WAD.cs b/BinaryObjectScanner/FileType/WAD.cs index 25162401..d6fcb4e4 100644 --- a/BinaryObjectScanner/FileType/WAD.cs +++ b/BinaryObjectScanner/FileType/WAD.cs @@ -10,40 +10,35 @@ namespace BinaryObjectScanner.FileType public class WAD : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var wad = SabreTools.Serialization.Wrappers.WAD.Create(stream); if (wad == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAllLumps(wad, tempPath); + Directory.CreateDirectory(outDir); + ExtractAllLumps(wad, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/FileType/XZ.cs b/BinaryObjectScanner/FileType/XZ.cs index 6c79e0dd..239c1cf7 100644 --- a/BinaryObjectScanner/FileType/XZ.cs +++ b/BinaryObjectScanner/FileType/XZ.cs @@ -13,43 +13,41 @@ namespace BinaryObjectScanner.FileType public class XZ : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - return Extract(fs, file, includeDebug); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { #if NET462_OR_GREATER || NETCOREAPP try { - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + // Try opening the stream + using var xzFile = new XZStream(stream); - using (XZStream xzFile = new XZStream(stream)) - { - string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString()); - using (FileStream fs = File.OpenWrite(tempFile)) - { - xzFile.CopyTo(fs); - } - } + // Create the output file path + Directory.CreateDirectory(outDir); + string tempFile = Path.Combine(outDir, Guid.NewGuid().ToString()); - return tempPath; + // Extract the file + using FileStream fs = File.OpenWrite(tempFile); + xzFile.CopyTo(fs); + + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } #else - return null; + return false; #endif } } diff --git a/BinaryObjectScanner/FileType/XZP.cs b/BinaryObjectScanner/FileType/XZP.cs index bad180e2..a9d564dd 100644 --- a/BinaryObjectScanner/FileType/XZP.cs +++ b/BinaryObjectScanner/FileType/XZP.cs @@ -11,40 +11,35 @@ namespace BinaryObjectScanner.FileType public class XZP : IExtractable { /// - public string? Extract(string file, bool includeDebug) + public bool Extract(string file, string outDir, bool includeDebug) { if (!File.Exists(file)) - return null; + return false; - using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - return Extract(fs, file, includeDebug); - } + using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return Extract(fs, file, outDir, includeDebug); } /// - public string? Extract(Stream? stream, string file, bool includeDebug) + public bool Extract(Stream? stream, string file, string outDir, bool includeDebug) { try { // Create the wrapper var xzp = SabreTools.Serialization.Wrappers.XZP.Create(stream); if (xzp == null) - return null; - - // Create a temp output directory - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); + return false; // Loop through and extract all files - ExtractAll(xzp, tempPath); + Directory.CreateDirectory(outDir); + ExtractAll(xzp, outDir); - return tempPath; + return true; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); - return null; + return false; } } diff --git a/BinaryObjectScanner/Handler.cs b/BinaryObjectScanner/Handler.cs index d41a2a4d..0783c242 100644 --- a/BinaryObjectScanner/Handler.cs +++ b/BinaryObjectScanner/Handler.cs @@ -94,17 +94,19 @@ namespace BinaryObjectScanner try { // Extract and get the output path - var tempPath = impl.Extract(stream, fileName, scanner.IncludeDebug); - if (tempPath == null) - return null; + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + bool extracted = impl.Extract(stream, fileName, tempPath, scanner.IncludeDebug); // Collect and format all found protections - var subProtections = scanner.GetProtections(tempPath); + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = scanner.GetProtections(tempPath); // If temp directory cleanup fails try { - Directory.Delete(tempPath, true); + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); } catch (Exception ex) { diff --git a/BinaryObjectScanner/Interfaces/IExtractable.cs b/BinaryObjectScanner/Interfaces/IExtractable.cs index 3117ddc7..1ac46cc3 100644 --- a/BinaryObjectScanner/Interfaces/IExtractable.cs +++ b/BinaryObjectScanner/Interfaces/IExtractable.cs @@ -5,26 +5,26 @@ namespace BinaryObjectScanner.Interfaces /// /// Mark a file type as being able to be extracted /// - /// TODO: Change to have output directory passed in - /// TODO: Change to return a bool public interface IExtractable { /// /// Extract a file to a temporary path, if possible /// /// Path to the input file + /// Path to the output directory /// True to include debug data, false otherwise - /// Path to extracted files, null on error + /// Indicates if the extractable was successfully extracted /// Ideally, this should just point to the other extract implementation. - string? Extract(string file, bool includeDebug); + bool Extract(string file, string outDir, bool includeDebug); /// /// Extract a stream to a temporary path, if possible /// /// Stream representing the input file /// Path to the input file + /// Path to the output directory /// True to include debug data, false otherwise - /// Path to extracted files, null on error - string? Extract(Stream? stream, string file, bool includeDebug); + /// Indicates if the extractable was successfully extracted + bool Extract(Stream? stream, string file, string outDir, bool includeDebug); } }