From 0725f4ba0fb217c27124869d38de8a943d33c0f1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 29 Jul 2025 20:35:06 -0400 Subject: [PATCH] Use Serialization implementation of BFPK extraction --- .../FileType/BFPKTests.cs | 24 ----- BinaryObjectScanner/FileType/BFPK.cs | 90 +------------------ 2 files changed, 1 insertion(+), 113 deletions(-) diff --git a/BinaryObjectScanner.Test/FileType/BFPKTests.cs b/BinaryObjectScanner.Test/FileType/BFPKTests.cs index bf86df65..b4a87679 100644 --- a/BinaryObjectScanner.Test/FileType/BFPKTests.cs +++ b/BinaryObjectScanner.Test/FileType/BFPKTests.cs @@ -40,29 +40,5 @@ namespace BinaryObjectScanner.Test.FileType bool actual = extractable.Extract(stream, file, outDir, includeDebug: false); Assert.False(actual); } - - [Fact] - public void ExtractAll_EmptyModel_False() - { - var model = new SabreTools.Models.BFPK.Archive(); - var data = new MemoryStream(); - var item = new SabreTools.Serialization.Wrappers.BFPK(model, data); - string outputDirectory = string.Empty; - - bool actual = BFPK.ExtractAll(item, outputDirectory); - Assert.False(actual); - } - - [Fact] - public void ExtractFile_EmptyModel_False() - { - var model = new SabreTools.Models.BFPK.Archive(); - var data = new MemoryStream(); - var item = new SabreTools.Serialization.Wrappers.BFPK(model, data); - string outputDirectory = string.Empty; - - bool actual = BFPK.ExtractFile(item, 0, outputDirectory); - Assert.False(actual); - } } } diff --git a/BinaryObjectScanner/FileType/BFPK.cs b/BinaryObjectScanner/FileType/BFPK.cs index 3d3fec55..1f084fda 100644 --- a/BinaryObjectScanner/FileType/BFPK.cs +++ b/BinaryObjectScanner/FileType/BFPK.cs @@ -1,6 +1,5 @@ using System.IO; using BinaryObjectScanner.Interfaces; -using SabreTools.Compression.Deflate; namespace BinaryObjectScanner.FileType { @@ -29,96 +28,9 @@ namespace BinaryObjectScanner.FileType // Extract all files Directory.CreateDirectory(outDir); - ExtractAll(bfpk, outDir); + bfpk.ExtractAll(outDir); return true; } - - /// - /// Extract all files from the BFPK to an output directory - /// - /// Output directory to write to - /// True if all files extracted, false otherwise - public static bool ExtractAll(SabreTools.Serialization.Wrappers.BFPK item, string outputDirectory) - { - // If we have no files - if (item.Model.Files == null || item.Model.Files.Length == 0) - return false; - - // Loop through and extract all files to the output - bool allExtracted = true; - for (int i = 0; i < item.Model.Files.Length; i++) - { - allExtracted &= ExtractFile(item, i, outputDirectory); - } - - return allExtracted; - } - - /// - /// Extract a file from the BFPK to an output directory by index - /// - /// File index to extract - /// Output directory to write to - /// True if the file extracted, false otherwise - public static bool ExtractFile(SabreTools.Serialization.Wrappers.BFPK item, int index, string outputDirectory) - { - // If we have no files - if (item.Model.Files == null || item.Model.Files.Length == 0) - return false; - - // If we have an invalid index - if (index < 0 || index >= item.Model.Files.Length) - return false; - - // Get the file information - var file = item.Model.Files[index]; - if (file == null) - return false; - - // Get the read index and length - int offset = file.Offset + 4; - int compressedSize = file.CompressedSize; - - // Some files can lack the length prefix - if (compressedSize > item.GetEndOfFile()) - { - offset -= 4; - compressedSize = file.UncompressedSize; - } - - try - { - // Ensure the output directory exists - Directory.CreateDirectory(outputDirectory); - - // Create the output path - string filePath = Path.Combine(outputDirectory, file.Name ?? $"file{index}"); - using FileStream fs = File.OpenWrite(filePath); - - // Read the data block - var data = item.ReadFromDataSource(offset, compressedSize); - if (data == null) - return false; - - // If we have uncompressed data - if (compressedSize == file.UncompressedSize) - { - fs.Write(data, 0, compressedSize); - } - else - { - MemoryStream ms = new MemoryStream(data); - ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress); - zs.CopyTo(fs); - } - - return true; - } - catch - { - return false; - } - } } }