From e70f8d7220b7917ba23e06caa6bd40690dc6091e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 17 Apr 2024 13:41:00 -0400 Subject: [PATCH] Reduce unncessary printing code --- .../Utilities/WrapperFactory.cs | 4 +- Test/Printer.cs | 443 +----------------- 2 files changed, 10 insertions(+), 437 deletions(-) diff --git a/BinaryObjectScanner/Utilities/WrapperFactory.cs b/BinaryObjectScanner/Utilities/WrapperFactory.cs index ff4130c2..9e402846 100644 --- a/BinaryObjectScanner/Utilities/WrapperFactory.cs +++ b/BinaryObjectScanner/Utilities/WrapperFactory.cs @@ -11,7 +11,7 @@ namespace BinaryObjectScanner.Utilities /// /// Create an instance of a wrapper based on file type /// - public static object? CreateWrapper(SupportedFileType fileType, Stream? data) + public static IWrapper? CreateWrapper(SupportedFileType fileType, Stream? data) { switch (fileType) { @@ -60,7 +60,7 @@ namespace BinaryObjectScanner.Utilities /// /// Stream data to parse /// IWrapper representing the executable, null on error - public static object? CreateExecutableWrapper(Stream? stream) + public static IWrapper? CreateExecutableWrapper(Stream? stream) { // If we have no stream if (stream == null) diff --git a/Test/Printer.cs b/Test/Printer.cs index 4a38b763..421399c2 100644 --- a/Test/Printer.cs +++ b/Test/Printer.cs @@ -1,9 +1,8 @@ using System; using System.IO; -using System.Text; using BinaryObjectScanner.Utilities; using SabreTools.IO.Extensions; -using SabreTools.Serialization.Wrappers; +using SabreTools.Printing; namespace Test { @@ -96,10 +95,15 @@ namespace Test #endif // Create the output data - var builder = wrapper.PrettyPrint(); - Console.WriteLine(builder); + var builder = wrapper.ExportStringBuilder(); + if (builder == null) + { + Console.WriteLine("No item information could be generated"); + return; + } // Write the output data + Console.WriteLine(builder); using var sw = new StreamWriter(File.OpenWrite($"{filenameBase}.txt")); sw.WriteLine(builder.ToString()); } @@ -109,436 +113,5 @@ namespace Test Console.WriteLine(); } } - - #region Printing Implementations - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this object wrapper) - { - return wrapper switch - { - AACSMediaKeyBlock item => item.PrettyPrint(), - BDPlusSVM item => item.PrettyPrint(), - BFPK item => item.PrettyPrint(), - BSP item => item.PrettyPrint(), - CFB item => item.PrettyPrint(), - CIA item => item.PrettyPrint(), - GCF item => item.PrettyPrint(), - InstallShieldCabinet item => item.PrettyPrint(), - IRD item => item.PrettyPrint(), - LinearExecutable item => item.PrettyPrint(), - MicrosoftCabinet item => item.PrettyPrint(), - MoPaQ item => item.PrettyPrint(), - MSDOS item => item.PrettyPrint(), - N3DS item => item.PrettyPrint(), - NCF item => item.PrettyPrint(), - NewExecutable item => item.PrettyPrint(), - Nitro item => item.PrettyPrint(), - PAK item => item.PrettyPrint(), - PFF item => item.PrettyPrint(), - PIC item => item.PrettyPrint(), - PlayJAudioFile item => item.PrettyPrint(), - PlayJPlaylist item => item.PrettyPrint(), - PortableExecutable item => item.PrettyPrint(), - Quantum item => item.PrettyPrint(), - SGA item => item.PrettyPrint(), - VBSP item => item.PrettyPrint(), - VPK item => item.PrettyPrint(), - WAD item => item.PrettyPrint(), - XeMID item => item.PrettyPrint(), - XMID item => item.PrettyPrint(), - XZP item => item.PrettyPrint(), - _ => new StringBuilder(), - }; ; - } - -#if NET6_0_OR_GREATER - /// - /// Export the item information as JSON - /// - private static string ExportJSON(this object wrapper) - { - return wrapper switch - { - AACSMediaKeyBlock item => item.ExportJSON(), - BDPlusSVM item => item.ExportJSON(), - BFPK item => item.ExportJSON(), - BSP item => item.ExportJSON(), - CFB item => item.ExportJSON(), - CIA item => item.ExportJSON(), - GCF item => item.ExportJSON(), - InstallShieldCabinet item => item.ExportJSON(), - IRD item => item.ExportJSON(), - LinearExecutable item => item.ExportJSON(), - MicrosoftCabinet item => item.ExportJSON(), - MoPaQ item => item.ExportJSON(), - MSDOS item => item.ExportJSON(), - N3DS item => item.ExportJSON(), - NCF item => item.ExportJSON(), - NewExecutable item => item.ExportJSON(), - Nitro item => item.ExportJSON(), - PAK item => item.ExportJSON(), - PFF item => item.ExportJSON(), - PIC item => item.ExportJSON(), - PlayJAudioFile item => item.ExportJSON(), - PlayJPlaylist item => item.ExportJSON(), - PortableExecutable item => item.ExportJSON(), - Quantum item => item.ExportJSON(), - SGA item => item.ExportJSON(), - VBSP item => item.ExportJSON(), - VPK item => item.ExportJSON(), - WAD item => item.ExportJSON(), - XeMID item => item.ExportJSON(), - XMID item => item.ExportJSON(), - XZP item => item.ExportJSON(), - _ => string.Empty, - }; - } -#endif - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this AACSMediaKeyBlock item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.AACSMediaKeyBlock.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this BDPlusSVM item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.BDPlusSVM.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this BFPK item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.BFPK.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this BSP item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.BSP.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this CFB item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.CFB.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this CIA item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.CIA.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this GCF item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.GCF.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this InstallShieldCabinet item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.InstallShieldCabinet.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this IRD item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.IRD.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this LinearExecutable item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.LinearExecutable.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this MicrosoftCabinet item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.MicrosoftCabinet.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this MoPaQ item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.MoPaQ.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this MSDOS item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.MSDOS.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this N3DS item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.N3DS.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this NCF item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.NCF.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this NewExecutable item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.NewExecutable.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this Nitro item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.Nitro.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this PAK item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.PAK.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this PFF item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.PFF.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this PIC item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.PIC.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this PlayJAudioFile item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.PlayJAudioFile.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this PlayJPlaylist item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.PlayJAudioPlaylist.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this PortableExecutable item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.PortableExecutable.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this Quantum item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.Quantum.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this SGA item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.SGA.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this VBSP item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.VBSP.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this VPK item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.VPK.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this WAD item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.WAD.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this XeMID item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.XeMID.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this XMID item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.XMID.Print(builder, item.Model); - return builder; - } - - /// - /// Export the item information as pretty-printed text - /// - private static StringBuilder PrettyPrint(this XZP item) - { - Console.WriteLine($"{item.Description()} wrapper created successfully!"); - var builder = new StringBuilder(); - SabreTools.Printing.XZP.Print(builder, item.Model); - return builder; - } - - #endregion } } \ No newline at end of file