diff --git a/BinaryObjectScanner.Printing/VPK.cs b/BinaryObjectScanner.Printing/VPK.cs new file mode 100644 index 00000000..7674f56e --- /dev/null +++ b/BinaryObjectScanner.Printing/VPK.cs @@ -0,0 +1,155 @@ +using System.Text; +using SabreTools.Models.VPK; + +namespace BinaryObjectScanner.Printing +{ + public static class VPK + { + public static void Print(StringBuilder builder, File file) + { + builder.AppendLine("VPK Information:"); + builder.AppendLine("-------------------------"); + builder.AppendLine(); + + Print(builder, file.Header); + Print(builder, file.ExtendedHeader); + Print(builder, file.ArchiveHashes); + Print(builder, file.DirectoryItems); + } + +#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.DirectoryLength, " Directory length"); + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, ExtendedHeader header) +#else + private static void Print(StringBuilder builder, ExtendedHeader? header) +#endif + { + builder.AppendLine(" Extended Header Information:"); + builder.AppendLine(" -------------------------"); + if (header == null) + { + builder.AppendLine(" No extended header"); + builder.AppendLine(); + return; + } + + builder.AppendLine(header.Dummy0, " Dummy 0"); + builder.AppendLine(header.ArchiveHashLength, " Archive hash length"); + builder.AppendLine(header.ExtraLength, " Extra length"); + builder.AppendLine(header.Dummy1, " Dummy 1"); + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, ArchiveHash[] entries) +#else + private static void Print(StringBuilder builder, ArchiveHash?[]? entries) +#endif + { + builder.AppendLine(" Archive Hashes Information:"); + builder.AppendLine(" -------------------------"); + if (entries == null || entries.Length == 0) + { + builder.AppendLine(" No archive hashes"); + builder.AppendLine(); + return; + } + + for (int i = 0; i < entries.Length; i++) + { + var entry = entries[i]; + builder.AppendLine($" Archive Hash {i}"); + if (entry == null) + { + builder.AppendLine(" [NULL]"); + continue; + } + + builder.AppendLine(entry.ArchiveIndex, " Archive index"); + builder.AppendLine(entry.ArchiveOffset, " Archive offset"); + builder.AppendLine(entry.Length, " Length"); + builder.AppendLine(entry.Hash, " Hash"); + } + 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]"); + builder.AppendLine(); + continue; + } + + builder.AppendLine(entry.Extension, " Extension"); + builder.AppendLine(entry.Path, " Path"); + builder.AppendLine(entry.Name, " Name"); + builder.AppendLine(); + + Print(builder, entry.DirectoryEntry); + // TODO: Print out preload data? + } + builder.AppendLine(); + } + +#if NET48 + private static void Print(StringBuilder builder, DirectoryEntry entry) +#else + private static void Print(StringBuilder builder, DirectoryEntry? entry) +#endif + { + builder.AppendLine(" Directory Entry:"); + builder.AppendLine(" -------------------------"); + if (entry == null) + { + builder.AppendLine(" [NULL]"); + return; + } + + builder.AppendLine(entry.CRC, " CRC"); + builder.AppendLine(entry.PreloadBytes, " Preload bytes"); + builder.AppendLine(entry.ArchiveIndex, " Archive index"); + builder.AppendLine(entry.EntryOffset, " Entry offset"); + builder.AppendLine(entry.EntryLength, " Entry length"); + builder.AppendLine(entry.Dummy0, " Dummy 0"); + } + } +} \ No newline at end of file diff --git a/BinaryObjectScanner.Wrappers/VPK.cs b/BinaryObjectScanner.Wrappers/VPK.cs index eb815874..62f3d689 100644 --- a/BinaryObjectScanner.Wrappers/VPK.cs +++ b/BinaryObjectScanner.Wrappers/VPK.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Linq; using System.Text; @@ -246,148 +245,10 @@ namespace BinaryObjectScanner.Wrappers public override StringBuilder PrettyPrint() { StringBuilder builder = new StringBuilder(); - - builder.AppendLine("VPK Information:"); - builder.AppendLine("-------------------------"); - builder.AppendLine(); - - PrintHeader(builder); - PrintExtendedHeader(builder); - PrintArchiveHashes(builder); - PrintDirectoryItems(builder); - + Printing.VPK.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} (0x{Signature:X})"); - builder.AppendLine($" Version: {Version} (0x{Version:X})"); - builder.AppendLine($" Directory length: {DirectoryLength} (0x{DirectoryLength:X})"); - builder.AppendLine(); - } - - /// - /// Print extended header information - /// - /// StringBuilder to append information to - private void PrintExtendedHeader(StringBuilder builder) - { - builder.AppendLine(" Extended Header Information:"); - builder.AppendLine(" -------------------------"); - if (_model.ExtendedHeader == null) - { - builder.AppendLine(" No extended header"); - } - else - { - builder.AppendLine($" Dummy 0: {Dummy0} (0x{Dummy0:X})"); - builder.AppendLine($" Archive hash length: {ArchiveHashLength} (0x{ArchiveHashLength:X})"); - builder.AppendLine($" Extra length: {ExtraLength} (0x{ExtraLength:X})"); - builder.AppendLine($" Dummy 1: {Dummy1} (0x{Dummy1:X})"); - builder.AppendLine(); - } - } - - /// - /// Print archive hashes information - /// - /// StringBuilder to append information to - private void PrintArchiveHashes(StringBuilder builder) - { - builder.AppendLine(" Archive Hashes Information:"); - builder.AppendLine(" -------------------------"); - if (ArchiveHashes == null || ArchiveHashes.Length == 0) - { - builder.AppendLine(" No archive hashes"); - } - else - { - for (int i = 0; i < ArchiveHashes.Length; i++) - { - var archiveHash = ArchiveHashes[i]; - builder.AppendLine($" Archive Hash {i}"); - if (archiveHash == null) - { - builder.AppendLine(" [NULL]"); - continue; - } - - builder.AppendLine($" Archive index: {archiveHash.ArchiveIndex} (0x{archiveHash.ArchiveIndex:X})"); - builder.AppendLine($" Archive offset: {archiveHash.ArchiveOffset} (0x{archiveHash.ArchiveOffset:X})"); - builder.AppendLine($" Length: {archiveHash.Length} (0x{archiveHash.Length:X})"); - builder.AppendLine($" Hash: {(archiveHash.Hash == null ? "[NULL]" : BitConverter.ToString(archiveHash.Hash).Replace("-", string.Empty))}"); - } - } - 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]"); - } - else - { - builder.AppendLine($" Extension: {directoryItem.Extension}"); - builder.AppendLine($" Path: {directoryItem.Path}"); - builder.AppendLine($" Name: {directoryItem.Name}"); - PrintDirectoryEntry(directoryItem.DirectoryEntry, builder); - // TODO: Print out preload data? - } - } - } - builder.AppendLine(); - } - - /// - /// Print directory entry information - /// - /// StringBuilder to append information to -#if NET48 - private void PrintDirectoryEntry(SabreTools.Models.VPK.DirectoryEntry directoryEntry, StringBuilder builder) -#else - private void PrintDirectoryEntry(SabreTools.Models.VPK.DirectoryEntry? directoryEntry, StringBuilder builder) -#endif - { - if (directoryEntry == null) - { - builder.AppendLine(" Directory entry: [NULL]"); - } - else - { - builder.AppendLine($" Directory entry CRC: {directoryEntry.CRC} (0x{directoryEntry.CRC:X})"); - builder.AppendLine($" Directory entry preload bytes: {directoryEntry.PreloadBytes} (0x{directoryEntry.PreloadBytes:X})"); - builder.AppendLine($" Directory entry archive index: {directoryEntry.ArchiveIndex} (0x{directoryEntry.ArchiveIndex:X})"); - builder.AppendLine($" Directory entry entry offset: {directoryEntry.EntryOffset} (0x{directoryEntry.EntryOffset:X})"); - builder.AppendLine($" Directory entry entry length: {directoryEntry.EntryLength} (0x{directoryEntry.EntryLength:X})"); - builder.AppendLine($" Directory entry dummy 0: {directoryEntry.Dummy0} (0x{directoryEntry.Dummy0:X})"); - } - } - #if NET6_0_OR_GREATER ///