diff --git a/BinaryObjectScanner.Printing/XZP.cs b/BinaryObjectScanner.Printing/XZP.cs new file mode 100644 index 00000000..6351ce50 --- /dev/null +++ b/BinaryObjectScanner.Printing/XZP.cs @@ -0,0 +1,164 @@ +using System.Text; +using SabreTools.Models.XZP; + +namespace BinaryObjectScanner.Printing +{ + public static class XZP + { + public static void Print(StringBuilder builder, File file) + { + builder.AppendLine("XZP Information:"); + builder.AppendLine("-------------------------"); + builder.AppendLine(); + + Print(builder, file.Header); + Print(builder, file.DirectoryEntries, "Directory"); + Print(builder, file.PreloadDirectoryEntries, "Preload Directory"); + Print(builder, file.PreloadDirectoryMappings); + Print(builder, file.DirectoryItems); + Print(builder, file.Footer); + } + +#if NET48 + private static void Print(StringBuilder builder, Header header) +#else + private static void Print(StringBuilder builder, Header? header) +#endif + { + builder.AppendLine(" Header Information:"); + builder.AppendLine(" -------------------------"); + if (header == null) + { + builder.AppendLine(" No header"); + builder.AppendLine(); + return; + } + + builder.AppendLine(header.Signature, " Signature"); + builder.AppendLine(header.Version, " Version"); + builder.AppendLine(header.PreloadDirectoryEntryCount, " Preload directory entry count"); + builder.AppendLine(header.DirectoryEntryCount, " Directory entry count"); + builder.AppendLine(header.PreloadBytes, " Preload bytes"); + builder.AppendLine(header.HeaderLength, " Header length"); + builder.AppendLine(header.DirectoryItemCount, " Directory item count"); + builder.AppendLine(header.DirectoryItemOffset, " Directory item offset"); + builder.AppendLine(header.DirectoryItemLength, " Directory item length"); + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, DirectoryEntry[] entries, string prefix) +#else + private static void Print(StringBuilder builder, DirectoryEntry?[]? entries, string prefix) +#endif + { + builder.AppendLine($" {prefix} Entries Information:"); + builder.AppendLine(" -------------------------"); + if (entries == null || entries.Length == 0) + { + builder.AppendLine(" No directory entries"); + builder.AppendLine(); + return; + } + + for (int i = 0; i < entries.Length; i++) + { + var entry = entries[i]; + builder.AppendLine($" Directory Entry {i}"); + if (entry == null) + { + builder.AppendLine(" [NULL]"); + continue; + } + + builder.AppendLine(entry.FileNameCRC, " File name CRC"); + builder.AppendLine(entry.EntryLength, " Entry length"); + builder.AppendLine(entry.EntryOffset, " Entry offset"); + } + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, DirectoryMapping[] entries) +#else + private static void Print(StringBuilder builder, DirectoryMapping?[]? entries) +#endif + { + builder.AppendLine(" Preload Directory Mappings Information:"); + builder.AppendLine(" -------------------------"); + if (entries == null || entries.Length == 0) + { + builder.AppendLine(" No preload directory mappings"); + builder.AppendLine(); + return; + } + + for (int i = 0; i < entries.Length; i++) + { + var entry = entries[i]; + builder.AppendLine($" Directory Mapping {i}"); + if (entry == null) + { + builder.AppendLine(" [NULL]"); + continue; + } + + builder.AppendLine(entry.PreloadDirectoryEntryIndex, " Preload directory entry index"); + } + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, DirectoryItem[] entries) +#else + private static void Print(StringBuilder builder, DirectoryItem?[]? entries) +#endif + { + builder.AppendLine(" Directory Items Information:"); + builder.AppendLine(" -------------------------"); + if (entries == null || entries.Length == 0) + { + builder.AppendLine(" No directory items"); + builder.AppendLine(); + return; + } + + for (int i = 0; i < entries.Length; i++) + { + var entry = entries[i]; + builder.AppendLine($" Directory Item {i}"); + if (entry == null) + { + builder.AppendLine(" [NULL]"); + continue; + } + + builder.AppendLine(entry.FileNameCRC, " File name CRC"); + builder.AppendLine(entry.NameOffset, " Name offset"); + builder.AppendLine(entry.Name, " Name"); + builder.AppendLine(entry.TimeCreated, " Time created"); + } + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, Footer footer) +#else + private static void Print(StringBuilder builder, Footer? footer) +#endif + { + builder.AppendLine(" Footer Information:"); + builder.AppendLine(" -------------------------"); + if (footer == null) + { + builder.AppendLine(" No header"); + builder.AppendLine(); + return; + } + + builder.AppendLine(footer.FileLength, " File length"); + builder.AppendLine(footer.Signature, " Signature"); + builder.AppendLine(); + } + } +} \ No newline at end of file diff --git a/BinaryObjectScanner.Wrappers/XZP.cs b/BinaryObjectScanner.Wrappers/XZP.cs index 7b278259..aa5bcab0 100644 --- a/BinaryObjectScanner.Wrappers/XZP.cs +++ b/BinaryObjectScanner.Wrappers/XZP.cs @@ -238,181 +238,10 @@ namespace BinaryObjectScanner.Wrappers 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); - + Printing.XZP.Print(builder, _model); 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}"); - if (directoryEntry == null) - { - builder.AppendLine(" [NULL]"); - continue; - } - - 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}"); - if (preloadDirectoryEntry == null) - { - builder.AppendLine(" [NULL]"); - continue; - } - - 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}"); - if (preloadDirectoryMapping == null) - { - builder.AppendLine(" [NULL]"); - continue; - } - - 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}"); - if (directoryItem == null) - { - builder.AppendLine(" [NULL]"); - continue; - } - - 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 ///