Add initial XZ printer

This commit is contained in:
Matt Nadareski
2025-09-30 14:05:34 -04:00
parent 6df712c538
commit 49f6704694
2 changed files with 121 additions and 0 deletions

View File

@@ -75,6 +75,7 @@ namespace SabreTools.Serialization
Wrapper.WiseSectionHeader item => item.PrettyPrint(),
Wrapper.XeMID item => item.PrettyPrint(),
Wrapper.XMID item => item.PrettyPrint(),
Wrapper.XZ item => item.PrettyPrint(),
Wrapper.XZP item => item.PrettyPrint(),
_ => null,
};
@@ -130,6 +131,7 @@ namespace SabreTools.Serialization
Wrapper.WiseSectionHeader item => item.ExportJSON(),
Wrapper.XeMID item => item.ExportJSON(),
Wrapper.XMID item => item.ExportJSON(),
Wrapper.XZ item => item.ExportJSON(),
Wrapper.XZP item => item.ExportJSON(),
_ => string.Empty,
};
@@ -558,6 +560,16 @@ namespace SabreTools.Serialization
return builder;
}
/// <summary>
/// Export the item information as pretty-printed text
/// </summary>
private static StringBuilder PrettyPrint(this Wrapper.XZ item)
{
var builder = new StringBuilder();
XZ.Print(builder, item.Model);
return builder;
}
/// <summary>
/// Export the item information as pretty-printed text
/// </summary>

View File

@@ -0,0 +1,109 @@
using System.Text;
using SabreTools.Data.Models.XZ;
namespace SabreTools.Data.Printers
{
public class XZ : IPrinter<Archive>
{
/// <inheritdoc/>
public void PrintInformation(StringBuilder builder, Archive model)
=> Print(builder, model);
public static void Print(StringBuilder builder, Archive archive)
{
builder.AppendLine("xz Information:");
builder.AppendLine("-------------------------");
builder.AppendLine();
Print(builder, archive.Header);
Print(builder, archive.Blocks);
Print(builder, archive.Index);
Print(builder, archive.Footer);
}
private static void Print(StringBuilder builder, Header? header)
{
builder.AppendLine(" Header Information:");
builder.AppendLine(" -------------------------");
if (header == null)
{
builder.AppendLine(" No header");
builder.AppendLine();
return;
}
builder.AppendLine(header.Signature, " Signature");
builder.AppendLine($" Flags: {header.Flags} (0x{(ushort)header.Flags:X4})");
builder.AppendLine(header.Crc32, " CRC-32");
builder.AppendLine();
}
private static void Print(StringBuilder builder, Block[]? blocks)
{
builder.AppendLine(" Blocks Information:");
builder.AppendLine(" -------------------------");
if (blocks == null || blocks.Length == 0)
{
builder.AppendLine(" No blocks");
builder.AppendLine();
return;
}
for (int i = 0; i < blocks.Length; i++)
{
var block = blocks[i];
builder.AppendLine($" Block {i}:");
builder.AppendLine(block.HeaderSize, " Header size");
builder.AppendLine($" Flags: {block.Flags} (0x{(byte)block.Flags:X2})");
builder.AppendLine(block.CompressedSize, " Compressed size");
builder.AppendLine(block.UncompressedSize, " Uncompressed size");
// TODO: Print filter flags
builder.AppendLine(block.HeaderPadding, " Header padding");
builder.AppendLine(block.Crc32, " CRC-32");
// TODO: Print the actual compressed data length
builder.AppendLine(block.BlockPadding, " Block padding");
builder.AppendLine(block.Check, " Check");
}
builder.AppendLine();
}
private static void Print(StringBuilder builder, Index? index)
{
builder.AppendLine(" Index Information:");
builder.AppendLine(" -------------------------");
if (index == null)
{
builder.AppendLine(" No index");
builder.AppendLine();
return;
}
builder.AppendLine(index.IndexIndicator, " Index indicator");
builder.AppendLine(index.NumberOfRecords, " Number of records");
// TODO: Print records
builder.AppendLine(index.Padding, " Padding");
builder.AppendLine(index.Crc32, " CRC-32");
builder.AppendLine();
}
private static void Print(StringBuilder builder, Footer? footer)
{
builder.AppendLine(" Footer Information:");
builder.AppendLine(" -------------------------");
if (footer == null)
{
builder.AppendLine(" No footer");
builder.AppendLine();
return;
}
builder.AppendLine(footer.Crc32, " CRC-32");
builder.AppendLine(footer.BackwardSize, " Backward size");
builder.AppendLine($" Flags: {footer.Flags} (0x{(ushort)footer.Flags:X4})");
builder.AppendLine(footer.Signature, " Signature");
builder.AppendLine();
}
}
}