using System.IO; using System.Linq; using System.Text; namespace BinaryObjectScanner.Wrappers { public class XZP : WrapperBase { #region Descriptive Properties /// public override string Description => "Xbox Package File (XZP)"; #endregion #region Pass-Through Properties #region Header /// public string Signature => _file.Header.Signature; /// public uint Version => _file.Header.Version; /// public uint PreloadDirectoryEntryCount => _file.Header.PreloadDirectoryEntryCount; /// public uint DirectoryEntryCount => _file.Header.DirectoryEntryCount; /// public uint PreloadBytes => _file.Header.PreloadBytes; /// public uint HeaderLength => _file.Header.HeaderLength; /// public uint DirectoryItemCount => _file.Header.DirectoryItemCount; /// public uint DirectoryItemOffset => _file.Header.DirectoryItemOffset; /// public uint DirectoryItemLength => _file.Header.DirectoryItemLength; #endregion #region Directory Entries /// public Models.XZP.DirectoryEntry[] DirectoryEntries => _file.DirectoryEntries; #endregion #region Preload Directory Entries /// public Models.XZP.DirectoryEntry[] PreloadDirectoryEntries => _file.PreloadDirectoryEntries; #endregion #region Preload Directory Entries /// public Models.XZP.DirectoryMapping[] PreloadDirectoryMappings => _file.PreloadDirectoryMappings; #endregion #region Directory Items /// public Models.XZP.DirectoryItem[] DirectoryItems => _file.DirectoryItems; #endregion #region Footer /// public uint F_FileLength => _file.Footer.FileLength; /// public string F_Signature => _file.Footer.Signature; #endregion #endregion #region Extension Properties // TODO: Figure out what extensions are needed #endregion #region Instance Variables /// /// Internal representation of the XZP /// private Models.XZP.File _file; #endregion #region Constructors /// /// Private constructor /// private XZP() { } /// /// Create a XZP from a byte array and offset /// /// Byte array representing the XZP /// Offset within the array to parse /// A XZP wrapper on success, null on failure public static XZP Create(byte[] data, int offset) { // 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 XZP from a Stream /// /// Stream representing the XZP /// A XZP wrapper on success, null on failure public static XZP Create(Stream data) { // If the data is invalid if (data == null || data.Length == 0 || !data.CanSeek || !data.CanRead) return null; var file = Builders.XZP.ParseFile(data); if (file == null) return null; var wrapper = new XZP { _file = file, _dataSource = DataSource.Stream, _streamData = data, }; return wrapper; } #endregion #region Printing /// public override StringBuilder PrettyPrint() { StringBuilder builder = new StringBuilder(); builder.AppendLine("XZP Information:"); builder.AppendLine("-------------------------"); builder.AppendLine(); PrintHeader(builder); PrintDirectoryEntries(builder); PrintPreloadDirectoryEntries(builder); PrintPreloadDirectoryMappings(builder); PrintDirectoryItems(builder); PrintFooter(builder); return builder; } /// /// Print header information /// /// StringBuilder to append information to private void PrintHeader(StringBuilder builder) { builder.AppendLine(" Header Information:"); builder.AppendLine(" -------------------------"); builder.AppendLine($" Signature: {Signature}"); builder.AppendLine($" Version: {Version} (0x{Version:X})"); builder.AppendLine($" Preload directory entry count: {PreloadDirectoryEntryCount} (0x{PreloadDirectoryEntryCount:X})"); builder.AppendLine($" Directory entry count: {DirectoryEntryCount} (0x{DirectoryEntryCount:X})"); builder.AppendLine($" Preload bytes: {PreloadBytes} (0x{PreloadBytes:X})"); builder.AppendLine($" Header length: {HeaderLength} (0x{HeaderLength:X})"); builder.AppendLine($" Directory item count: {DirectoryItemCount} (0x{DirectoryItemCount:X})"); builder.AppendLine($" Directory item offset: {DirectoryItemOffset} (0x{DirectoryItemOffset:X})"); builder.AppendLine($" Directory item length: {DirectoryItemLength} (0x{DirectoryItemLength:X})"); builder.AppendLine(); } /// /// Print directory entries information /// /// StringBuilder to append information to private void PrintDirectoryEntries(StringBuilder builder) { builder.AppendLine(" Directory Entries Information:"); builder.AppendLine(" -------------------------"); if (DirectoryEntries == null || DirectoryEntries.Length == 0) { builder.AppendLine(" No directory entries"); } else { for (int i = 0; i < DirectoryEntries.Length; i++) { var directoryEntry = DirectoryEntries[i]; builder.AppendLine($" Directory Entry {i}"); builder.AppendLine($" File name CRC: {directoryEntry.FileNameCRC} (0x{directoryEntry.FileNameCRC:X})"); builder.AppendLine($" Entry length: {directoryEntry.EntryLength} (0x{directoryEntry.EntryLength:X})"); builder.AppendLine($" Entry offset: {directoryEntry.EntryOffset} (0x{directoryEntry.EntryOffset:X})"); } } builder.AppendLine(); } /// /// Print preload directory entries information /// /// StringBuilder to append information to private void PrintPreloadDirectoryEntries(StringBuilder builder) { builder.AppendLine(" Preload Directory Entries Information:"); builder.AppendLine(" -------------------------"); if (PreloadDirectoryEntries == null || PreloadDirectoryEntries.Length == 0) { builder.AppendLine(" No preload directory entries"); } else { for (int i = 0; i < PreloadDirectoryEntries.Length; i++) { var preloadDirectoryEntry = PreloadDirectoryEntries[i]; builder.AppendLine($" Directory Entry {i}"); builder.AppendLine($" File name CRC: {preloadDirectoryEntry.FileNameCRC} (0x{preloadDirectoryEntry.FileNameCRC:X})"); builder.AppendLine($" Entry length: {preloadDirectoryEntry.EntryLength} (0x{preloadDirectoryEntry.EntryLength:X})"); builder.AppendLine($" Entry offset: {preloadDirectoryEntry.EntryOffset} (0x{preloadDirectoryEntry.EntryOffset:X})"); } } builder.AppendLine(); } /// /// Print preload directory mappings information /// /// StringBuilder to append information to private void PrintPreloadDirectoryMappings(StringBuilder builder) { builder.AppendLine(" Preload Directory Mappings Information:"); builder.AppendLine(" -------------------------"); if (PreloadDirectoryMappings == null || PreloadDirectoryMappings.Length == 0) { builder.AppendLine(" No preload directory mappings"); } else { for (int i = 0; i < PreloadDirectoryMappings.Length; i++) { var preloadDirectoryMapping = PreloadDirectoryMappings[i]; builder.AppendLine($" Directory Mapping {i}"); builder.AppendLine($" Preload directory entry index: {preloadDirectoryMapping.PreloadDirectoryEntryIndex} (0x{preloadDirectoryMapping.PreloadDirectoryEntryIndex:X})"); } } builder.AppendLine(); } /// /// Print directory items information /// /// StringBuilder to append information to private void PrintDirectoryItems(StringBuilder builder) { builder.AppendLine(" Directory Items Information:"); builder.AppendLine(" -------------------------"); if (DirectoryItems == null || DirectoryItems.Length == 0) { builder.AppendLine(" No directory items"); } else { for (int i = 0; i < DirectoryItems.Length; i++) { var directoryItem = DirectoryItems[i]; builder.AppendLine($" Directory Item {i}"); builder.AppendLine($" File name CRC: {directoryItem.FileNameCRC} (0x{directoryItem.FileNameCRC:X})"); builder.AppendLine($" Name offset: {directoryItem.NameOffset} (0x{directoryItem.NameOffset:X})"); builder.AppendLine($" Name: {directoryItem.Name ?? "[NULL]"}"); builder.AppendLine($" Time created: {directoryItem.TimeCreated} (0x{directoryItem.TimeCreated:X})"); } } builder.AppendLine(); } /// /// Print footer information /// /// StringBuilder to append information to private void PrintFooter(StringBuilder builder) { builder.AppendLine(" Footer Information:"); builder.AppendLine(" -------------------------"); builder.AppendLine($" File length: {F_FileLength} (0x{F_FileLength:X})"); builder.AppendLine($" Signature: {F_Signature}"); builder.AppendLine(); } #if NET6_0_OR_GREATER /// public override string ExportJSON() => System.Text.Json.JsonSerializer.Serialize(_file, _jsonSerializerOptions); #endif #endregion #region Extraction /// /// Extract all files from the XZP 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 entries if (DirectoryEntries == null || DirectoryEntries.Length == 0) return false; // Loop through and extract all files to the output bool allExtracted = true; for (int i = 0; i < DirectoryEntries.Length; i++) { allExtracted &= ExtractFile(i, outputDirectory); } return allExtracted; } /// /// Extract a file from the XZP 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 entries if (DirectoryEntries == null || DirectoryEntries.Length == 0) return false; // If we have no directory items if (DirectoryItems == null || DirectoryItems.Length == 0) return false; // If the directory entry index is invalid if (index < 0 || index >= DirectoryEntries.Length) return false; // Get the directory entry var directoryEntry = DirectoryEntries[index]; if (directoryEntry == null) return false; // Get the associated directory item var directoryItem = DirectoryItems.Where(di => di.FileNameCRC == directoryEntry.FileNameCRC).FirstOrDefault(); if (directoryItem == null) return false; // Load the item data byte[] data = ReadFromDataSource((int)directoryEntry.EntryOffset, (int)directoryEntry.EntryLength); // Create the filename string filename = directoryItem.Name; // If we have an invalid output directory if (string.IsNullOrWhiteSpace(outputDirectory)) return false; // Create the full output path filename = Path.Combine(outputDirectory, filename); // Ensure the output directory is created Directory.CreateDirectory(Path.GetDirectoryName(filename)); // 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 } }