Files

154 lines
5.9 KiB
C#
Raw Permalink Normal View History

2025-12-19 15:58:45 -07:00
using Mono.Options;
2025-04-13 15:18:03 -06:00
using Repackinator.Core.Helpers;
2025-12-19 15:25:06 -07:00
using XboxToolkit;
using XboxToolkit.Interface;
2025-04-13 15:18:03 -06:00
using Repackinator.Core.Models;
2023-01-04 21:08:49 -08:00
2025-12-19 15:58:45 -07:00
namespace Repackinator.Shell.Console
2023-01-04 21:08:49 -08:00
{
public static class ConsoleChecksum
{
2023-01-11 20:44:58 +01:00
public const string Action = "Checksum";
2023-01-04 21:08:49 -08:00
public static string Input { get; set; } = string.Empty;
2025-12-19 16:23:18 -07:00
public static string Output { get; set; } = string.Empty;
2023-01-04 21:08:49 -08:00
public static bool ShowHelp { get; set; } = false;
public static bool Wait { get; set; } = false;
2025-12-19 16:23:18 -07:00
public static bool Quiet { get; set; } = false;
2023-01-04 21:08:49 -08:00
public static OptionSet GetOptions()
{
return new OptionSet {
{ "i|input=", "Input file", i => Input = i },
2025-12-19 16:23:18 -07:00
{ "o|output=", "Output file (optional, defaults to console)", o => Output = o },
2023-01-04 21:08:49 -08:00
{ "h|help", "show help", h => ShowHelp = h != null },
2025-12-19 16:23:18 -07:00
{ "w|wait", "Wait on exit", w => Wait = w != null },
{ "q|quiet", "Suppress status output", q => Quiet = q != null }
2023-01-04 21:08:49 -08:00
};
}
public static void ShowOptionDescription()
{
2023-01-11 20:44:58 +01:00
System.Console.WriteLine();
System.Console.WriteLine("Checksum Action...");
System.Console.WriteLine();
System.Console.WriteLine("This action is used to checksum xbox disk image sectors after any decompression if applicable.");
System.Console.WriteLine();
GetOptions().WriteOptionDescriptions(System.Console.Out);
2023-01-04 21:08:49 -08:00
}
public static void Process(string version, string[] args)
{
var config = Config.LoadConfig();
try
{
var options = GetOptions();
options.Parse(args);
if (ShowHelp)
{
2023-01-11 20:44:58 +01:00
ConsoleUtil.ShowHelpHeaderForAction(version, Action, options);
2023-01-04 21:08:49 -08:00
ConsoleUtil.ProcessWait(Wait);
return;
}
2025-12-19 17:31:03 -07:00
if (string.IsNullOrEmpty(Input))
{
throw new OptionException("Input file not specified.", "input");
}
2023-01-04 21:08:49 -08:00
if (!File.Exists(Input))
{
2025-12-19 17:31:03 -07:00
throw new OptionException("Input file does not exist.", "input");
2023-01-04 21:08:49 -08:00
}
2025-12-19 16:23:18 -07:00
StreamWriter? outputWriter = null;
if (!string.IsNullOrEmpty(Output))
2023-01-04 21:08:49 -08:00
{
2025-12-19 16:23:18 -07:00
var outputPath = Path.GetFullPath(Output);
var outputDir = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir))
{
Directory.CreateDirectory(outputDir);
}
outputWriter = new StreamWriter(outputPath, false, System.Text.Encoding.UTF8);
2023-01-04 21:08:49 -08:00
}
2025-12-19 16:23:18 -07:00
try
2023-01-04 21:08:49 -08:00
{
2025-12-19 16:23:18 -07:00
if (!Quiet)
2023-01-04 21:08:49 -08:00
{
2025-12-19 16:23:18 -07:00
System.Console.WriteLine("Calculating Checksum From:");
var slices = ContainerUtility.GetSlicesFromFile(Input);
foreach (var slice in slices)
2025-12-19 15:25:06 -07:00
{
2025-12-19 16:23:18 -07:00
System.Console.WriteLine(Path.GetFileName(slice));
}
System.Console.WriteLine("Calculating Checksums...");
2025-12-19 15:25:06 -07:00
}
2025-12-19 16:23:18 -07:00
if (!ContainerUtility.TryAutoDetectContainerType(Input, out var containerReader) || containerReader == null)
2025-12-19 15:25:06 -07:00
{
2025-12-19 16:23:18 -07:00
throw new Exception("Unable to detect container type.");
}
using (containerReader)
{
if (!containerReader.TryMount())
{
throw new Exception("Unable to mount container.");
}
try
{
var previousProgress = -1.0f;
var result = ContainerUtility.GetChecksumFromContainer(containerReader, p =>
{
var amount = (float)Math.Round(p * 100);
if (!Quiet && amount != previousProgress)
{
System.Console.Write($"Progress {amount}%");
System.Console.CursorLeft = 0;
previousProgress = amount;
}
}, default);
var checksumLine = $"SHA256 = {result}";
if (outputWriter != null)
{
outputWriter.WriteLine(result);
}
else
{
System.Console.WriteLine(checksumLine);
}
}
finally
{
containerReader.Dismount();
}
}
if (!Quiet)
{
System.Console.WriteLine();
if (outputWriter != null)
{
System.Console.WriteLine($"Checksum saved to: {Output}");
}
System.Console.WriteLine("Checksum completed.");
2025-12-19 15:25:06 -07:00
}
}
2025-12-19 16:23:18 -07:00
finally
{
outputWriter?.Dispose();
}
2023-01-04 21:08:49 -08:00
}
catch (OptionException e)
{
2025-12-19 16:19:12 -07:00
ConsoleUtil.ShowOptionException(e, Action, version);
2023-01-04 21:08:49 -08:00
}
2025-12-20 19:58:42 -07:00
ConsoleUtil.ProcessWait(Wait);
2023-01-04 21:08:49 -08:00
}
}
}
2025-12-19 15:58:45 -07:00