Files
BinaryObjectScanner/Test/Printer.cs

151 lines
5.0 KiB
C#
Raw Permalink Normal View History

2023-01-04 10:23:03 -08:00
using System;
using System.IO;
2024-05-15 12:24:40 -04:00
#if NET452_OR_GREATER || NETCOREAPP
using System.Text;
#endif
2024-04-17 12:12:01 -04:00
using SabreTools.IO.Extensions;
2024-04-26 22:09:05 -04:00
using SabreTools.Serialization.Wrappers;
using SPrinter = SabreTools.Serialization.Printer;
2023-01-04 10:23:03 -08:00
namespace Test
{
2024-05-15 12:24:40 -04:00
internal class Printer
2023-01-04 10:23:03 -08:00
{
2024-05-15 12:24:40 -04:00
#region Options
/// <inheritdoc cref="BinaryObjectScanner.Options.IncludeDebug"/>
public bool IncludeDebug => _options?.IncludeDebug ?? false;
/// <summary>
/// Options object for configuration
/// </summary>
private readonly BinaryObjectScanner.Options _options;
#endregion
2023-01-04 10:23:03 -08:00
/// <summary>
2024-05-15 12:24:40 -04:00
/// Constructor
2023-01-04 10:23:03 -08:00
/// </summary>
2024-05-15 12:24:40 -04:00
/// <param name="includeDebug">Enable including debug information</param>
public Printer(bool includeDebug)
{
this._options = new BinaryObjectScanner.Options
{
IncludeDebug = includeDebug,
};
#if NET462_OR_GREATER || NETCOREAPP
// Register the codepages
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}/// <summary>
/// Wrapper to print information for a single path
/// </summary>
/// <param name="path">File or directory path</param>
/// <param name="json">Enable JSON output, if supported</param>
/// <param name="debug">Enable debug output</param>
public void PrintPathInfo(string path, bool json, bool debug)
2023-01-04 10:23:03 -08:00
{
Console.WriteLine($"Checking possible path: {path}");
// Check if the file or directory exists
if (File.Exists(path))
{
2023-01-13 15:15:30 -08:00
PrintFileInfo(path, json, debug);
2023-01-04 10:23:03 -08:00
}
else if (Directory.Exists(path))
{
2024-04-26 22:09:05 -04:00
foreach (string file in IOExtensions.SafeEnumerateFiles(path, "*", SearchOption.AllDirectories))
2023-01-04 10:23:03 -08:00
{
2023-01-13 15:15:30 -08:00
PrintFileInfo(file, json, debug);
2023-01-04 10:23:03 -08:00
}
}
else
{
Console.WriteLine($"{path} does not exist, skipping...");
}
}
/// <summary>
/// Print information for a single file, if possible
/// </summary>
2024-05-15 12:24:40 -04:00
private void PrintFileInfo(string file, bool json, bool debug)
2023-01-04 10:23:03 -08:00
{
Console.WriteLine($"Attempting to print info for {file}");
try
{
using Stream stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
2023-01-04 10:23:03 -08:00
2024-05-15 12:24:40 -04:00
// Get the extension for certain checks
string extension = Path.GetExtension(file).ToLower().TrimStart('.');
2023-01-04 10:23:03 -08:00
2024-05-15 12:24:40 -04:00
// Get the first 16 bytes for matching
byte[] magic = new byte[16];
try
{
stream.Read(magic, 0, 16);
stream.Seek(0, SeekOrigin.Begin);
}
catch (Exception ex)
{
2024-05-15 12:24:40 -04:00
if (IncludeDebug) Console.WriteLine(ex);
return;
}
2023-01-04 10:23:03 -08:00
2024-05-15 12:24:40 -04:00
// Get the file type
WrapperType ft = WrapperFactory.GetFileType(magic, extension);
// Print out the file format
Console.WriteLine($"File format found: {ft}");
2023-01-04 10:23:03 -08:00
// Setup the wrapper to print
var wrapper = WrapperFactory.CreateWrapper(ft, stream);
2023-01-04 10:23:03 -08:00
// If we don't have a wrapper
if (wrapper == null)
{
Console.WriteLine($"Either {ft} is not supported or something went wrong during parsing!");
Console.WriteLine();
return;
}
// Get the base info output name
string filenameBase = $"info-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}";
#if NET6_0_OR_GREATER
2024-04-17 12:12:01 -04:00
// If we have the JSON flag
if (json)
{
// Create the output data
string serializedData = wrapper.ExportJSON();
Console.WriteLine(serializedData);
// Write the output data
using var jsw = new StreamWriter(File.OpenWrite($"{filenameBase}.json"));
jsw.WriteLine(serializedData);
}
#endif
2024-04-17 12:12:01 -04:00
// Create the output data
2024-04-26 22:09:05 -04:00
var builder = SPrinter.ExportStringBuilder(wrapper);
2024-04-17 13:41:00 -04:00
if (builder == null)
{
Console.WriteLine("No item information could be generated");
return;
}
// Write the output data
2024-04-17 13:41:00 -04:00
Console.WriteLine(builder);
using var sw = new StreamWriter(File.OpenWrite($"{filenameBase}.txt"));
sw.WriteLine(builder.ToString());
}
catch (Exception ex)
{
Console.WriteLine(debug ? ex : "[Exception opening file, please try again]");
Console.WriteLine();
}
2023-01-04 10:23:03 -08:00
}
}
}