Files

148 lines
5.5 KiB
C#
Raw Permalink Normal View History

2025-12-19 15:58:45 -07:00
using Mono.Options;
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;
using Repackinator.Core.Helpers;
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 ConsoleExtract
{
2023-01-11 20:44:58 +01:00
public const string Action = "Extract";
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 directory (optional, defaults to folder with input name)", 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("Extract Action...");
System.Console.WriteLine();
System.Console.WriteLine("This action is used to extract files from xbox disk image.");
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
string outputPath;
if (!string.IsNullOrEmpty(Output))
2023-01-04 21:08:49 -08:00
{
2025-12-19 16:23:18 -07:00
outputPath = Path.GetFullPath(Output);
Directory.CreateDirectory(outputPath);
}
else
{
// Default behavior: use folder with input name
var inputDir = Path.GetDirectoryName(Input);
if (inputDir == null)
{
throw new IOException("Unable to get directory name from input.");
}
var baseName = Path.GetFileNameWithoutExtension(Input);
var subExtension = Path.GetExtension(baseName);
if (subExtension.Equals(".1") || subExtension.Equals(".2"))
{
baseName = Path.GetFileNameWithoutExtension(baseName);
}
outputPath = Path.Combine(inputDir, baseName);
Directory.CreateDirectory(outputPath);
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("Extracting From:");
var slices = ContainerUtility.GetSlicesFromFile(Input);
foreach (var slice in slices)
{
System.Console.WriteLine(Path.GetFileName(slice));
}
System.Console.WriteLine($"To: {outputPath}");
System.Console.WriteLine("Extracting...");
2023-01-04 21:08:49 -08:00
}
2025-12-19 15:25:06 -07:00
if (!ContainerUtility.TryAutoDetectContainerType(Input, out var containerReader) || containerReader == null)
2025-04-13 15:18:03 -06:00
{
2025-12-19 15:25:06 -07:00
throw new Exception("Unable to detect container type.");
}
using (containerReader)
{
if (!containerReader.TryMount())
2023-01-04 21:08:49 -08:00
{
2025-12-19 15:25:06 -07:00
throw new Exception("Unable to mount container.");
2023-01-04 21:08:49 -08:00
}
2025-12-19 15:25:06 -07:00
try
2023-01-04 21:08:49 -08:00
{
2025-12-19 21:46:17 -07:00
Action<string>? extractProgress = null;
if (!Quiet)
2023-01-04 21:08:49 -08:00
{
2025-12-19 21:46:17 -07:00
extractProgress = new Action<string>((filename) =>
2025-12-19 15:25:06 -07:00
{
2025-12-19 21:46:17 -07:00
System.Console.WriteLine($" {filename}");
});
}
2025-12-19 15:25:06 -07:00
2025-12-19 21:46:17 -07:00
if (!ContainerUtility.ExtractFilesFromContainer(containerReader, outputPath, extractProgress))
2025-12-19 15:25:06 -07:00
{
throw new Exception("Unable to extract files.");
2023-01-04 21:08:49 -08:00
}
}
2025-12-19 15:25:06 -07:00
finally
2023-01-04 21:08:49 -08:00
{
2025-12-19 15:25:06 -07:00
containerReader.Dismount();
2023-01-04 21:08:49 -08:00
}
2025-12-19 15:25:06 -07:00
}
2023-01-04 21:08:49 -08:00
2025-12-19 16:23:18 -07:00
if (!Quiet)
{
System.Console.WriteLine();
System.Console.WriteLine("Extract completed.");
}
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