using System.IO; using System.Text; namespace BinaryObjectScanner.Wrappers { public class PAK : WrapperBase { #region Descriptive Properties /// public override string DescriptionString => "Half-Life Package File (PAK)"; #endregion #region Pass-Through Properties #region Header /// #if NET48 public string Signature => _model.Header.Signature; #else public string? Signature => _model.Header?.Signature; #endif /// #if NET48 public uint DirectoryOffset => _model.Header.DirectoryOffset; #else public uint? DirectoryOffset => _model.Header?.DirectoryOffset; #endif /// #if NET48 public uint DirectoryLength => _model.Header.DirectoryLength; #else public uint? DirectoryLength => _model.Header?.DirectoryLength; #endif #endregion #region Directory Items /// #if NET48 public SabreTools.Models.PAK.DirectoryItem[] DirectoryItems => _model.DirectoryItems; #else public SabreTools.Models.PAK.DirectoryItem?[]? DirectoryItems => _model.DirectoryItems; #endif #endregion #endregion #region Extension Properties // TODO: Figure out what extensions are needed #endregion #region Constructors /// #if NET48 public PAK(SabreTools.Models.PAK.File model, byte[] data, int offset) #else public PAK(SabreTools.Models.PAK.File? model, byte[]? data, int offset) #endif : base(model, data, offset) { // All logic is handled by the base class } /// #if NET48 public PAK(SabreTools.Models.PAK.File model, Stream data) #else public PAK(SabreTools.Models.PAK.File? model, Stream? data) #endif : base(model, data) { // All logic is handled by the base class } /// /// Create a PAK from a byte array and offset /// /// Byte array representing the PAK /// Offset within the array to parse /// A PAK wrapper on success, null on failure #if NET48 public static PAK Create(byte[] data, int offset) #else public static PAK? Create(byte[]? data, int offset) #endif { // If the data is invalid if (data == null) return null; // If the offset is out of bounds if (offset < 0 || offset >= data.Length) return null; // Create a memory stream and use that MemoryStream dataStream = new MemoryStream(data, offset, data.Length - offset); return Create(dataStream); } /// /// Create a PAK from a Stream /// /// Stream representing the PAK /// A PAK wrapper on success, null on failure #if NET48 public static PAK Create(Stream data) #else public static PAK? Create(Stream? data) #endif { // If the data is invalid if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead) return null; var file = new SabreTools.Serialization.Streams.PAK().Deserialize(data); if (file == null) return null; try { return new PAK(file, data); } catch { return null; } } #endregion #region Printing /// public override StringBuilder PrettyPrint() { StringBuilder builder = new StringBuilder(); Printing.PAK.Print(builder, _model); return builder; } #if NET6_0_OR_GREATER /// public override string ExportJSON() => System.Text.Json.JsonSerializer.Serialize(_model, _jsonSerializerOptions); #endif #endregion #region Extraction /// /// Extract all files from the PAK to an output directory /// /// Output directory to write to /// True if all files extracted, false otherwise public bool ExtractAll(string outputDirectory) { // If we have no directory items if (DirectoryItems == null || DirectoryItems.Length == 0) return false; // Loop through and extract all files to the output bool allExtracted = true; for (int i = 0; i < DirectoryItems.Length; i++) { allExtracted &= ExtractFile(i, outputDirectory); } return allExtracted; } /// /// Extract a file from the PAK to an output directory by index /// /// File index to extract /// Output directory to write to /// True if the file extracted, false otherwise public bool ExtractFile(int index, string outputDirectory) { // If we have no directory items if (DirectoryItems == null || DirectoryItems.Length == 0) return false; // If the directory item index is invalid if (index < 0 || index >= DirectoryItems.Length) return false; // Get the directory item var directoryItem = DirectoryItems[index]; if (directoryItem == null) return false; // Read the item data #if NET48 byte[] data = ReadFromDataSource((int)directoryItem.ItemOffset, (int)directoryItem.ItemLength); #else byte[]? data = ReadFromDataSource((int)directoryItem.ItemOffset, (int)directoryItem.ItemLength); #endif if (data == null) return false; // Create the filename #if NET48 string filename = directoryItem.ItemName; #else string? filename = directoryItem.ItemName; #endif // If we have an invalid output directory if (string.IsNullOrWhiteSpace(outputDirectory)) return false; // Create the full output path filename = Path.Combine(outputDirectory, filename ?? $"file{index}"); // Ensure the output directory is created #if NET48 string directoryName = Path.GetDirectoryName(filename); #else string? directoryName = Path.GetDirectoryName(filename); #endif if (directoryName != null) Directory.CreateDirectory(directoryName); // Try to write the data try { // Open the output file for writing using (Stream fs = File.OpenWrite(filename)) { fs.Write(data, 0, data.Length); } } catch { return false; } return true; } #endregion } }