using System; using System.IO; using BinaryObjectScanner.Interfaces; namespace BinaryObjectScanner.FileType { /// /// NovaLogic Game Archive Format /// public class PFF : IExtractable { /// #if NET48 public string Extract(string file, bool includeDebug) #else public string? Extract(string file, bool includeDebug) #endif { if (!File.Exists(file)) return null; using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { return Extract(fs, file, includeDebug); } } /// #if NET48 public string Extract(Stream stream, string file, bool includeDebug) #else public string? Extract(Stream stream, string file, bool includeDebug) #endif { 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); // Extract all files ExtractAll(pff, tempPath); return tempPath; } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex.Message); return null; } } /// /// Extract all segments from the PFF to an output directory /// /// Output directory to write to /// True if all segments extracted, false otherwise public static bool ExtractAll(SabreTools.Serialization.Wrappers.PFF item, string outputDirectory) { // If we have no segments if (item.Model.Segments == null || item.Model.Segments.Length == 0) return false; // Loop through and extract all files to the output bool allExtracted = true; for (int i = 0; i < item.Model.Segments.Length; i++) { allExtracted &= ExtractSegment(item, i, outputDirectory); } return allExtracted; } /// /// Extract a segment from the PFF to an output directory by index /// /// Segment index to extract /// Output directory to write to /// True if the segment extracted, false otherwise public static bool ExtractSegment(SabreTools.Serialization.Wrappers.PFF item, int index, string outputDirectory) { // If we have no segments if (item.Model.Header?.NumberOfFiles == null || item.Model.Header.NumberOfFiles == 0 || item.Model.Segments == null || item.Model.Segments.Length == 0) return false; // If we have an invalid index if (index < 0 || index >= item.Model.Segments.Length) return false; // Get the segment information var file = item.Model.Segments[index]; if (file == null) return false; // Get the read index and length int offset = (int)file.FileLocation; int size = (int)file.FileSize; try { // Ensure the output directory exists Directory.CreateDirectory(outputDirectory); // Create the output path string filePath = Path.Combine(outputDirectory, file.FileName ?? $"file{index}"); using (FileStream fs = File.OpenWrite(filePath)) { // Read the data block #if NET48 byte[] data = item.ReadFromDataSource(offset, size); #else byte[]? data = item.ReadFromDataSource(offset, size); #endif if (data == null) return false; // Write the data -- TODO: Compressed data? fs.Write(data, 0, size); } return true; } catch { return false; } } } }