Files
SabreTools.RedumpLib/RedumpTool/Features/PacksFeature.cs
2026-06-21 22:37:54 -04:00

116 lines
3.8 KiB
C#

using System;
using SabreTools.CommandLine.Inputs;
using SabreTools.RedumpLib.RedumpInfo;
using SabreTools.RedumpLib.RedumpOrg;
namespace RedumpTool.Features
{
internal sealed class PacksFeature : BaseFeature
{
#region Feature Definition
public const string DisplayName = "packs";
private static readonly string[] _flags = ["packs"];
private const string _description = "Download available packs";
#endregion
#region Inputs
private const string _subfoldersName = "subfolders";
internal readonly FlagInput SubfoldersInput = new(_subfoldersName, ["-s", "--subfolders"], "Download packs to named subfolders");
#endregion
public PacksFeature()
: base(DisplayName, _flags, _description)
{
RequiresInputs = false;
// Common
Add(DebugInput);
Add(OutputInput);
Add(UsernameInput);
Add(PasswordInput);
Add(AttemptCountInput);
Add(TimeoutInput);
Add(ForceDownloadInput);
Add(ForceContinueInput);
Add(OldSiteInput);
// Specific
Add(SubfoldersInput);
}
/// <inheritdoc/>
public override bool Execute()
{
// Get common values
string? outDir = OutputInput.Value;
string? username = UsernameInput.Value;
string? password = PasswordInput.Value;
int? attemptCount = AttemptCountInput.Value;
int? timeout = TimeoutInput.Value;
bool forceDownload = ForceDownloadInput.Value;
bool forceContinue = ForceContinueInput.Value;
bool oldSite = OldSiteInput.Value;
// Get specific values
bool useSubfolders = SubfoldersInput.Value;
// Output directory validation
if (!ValidateAndCreateOutputDirectory(outDir))
return false;
// If connecting to redump.org
if (oldSite)
{
// Update redump.org client properties
_orgClient.Debug = DebugInput.Value;
if (attemptCount != null && attemptCount > 0)
_orgClient.AttemptCount = attemptCount.Value;
if (timeout != null && timeout > 0)
_orgClient.Timeout = TimeSpan.FromSeconds(timeout.Value);
_orgClient.Overwrite = forceDownload;
_orgClient.IgnoreErrors = forceContinue;
// Login to redump.org, if necessary
_orgClient.Login(username, password).Wait();
// Start the processing
var processingTask = _orgClient.DownloadAllPacks(outDir, useSubfolders);
// Retrieve the result
processingTask.Wait();
return processingTask.Result;
}
else
{
// Update redump.info client properties
_client.Debug = DebugInput.Value;
if (attemptCount != null && attemptCount > 0)
_client.AttemptCount = attemptCount.Value;
if (timeout != null && timeout > 0)
_client.Timeout = TimeSpan.FromSeconds(timeout.Value);
_client.Overwrite = forceDownload;
_client.IgnoreErrors = forceContinue;
// Login to redump.info, if necessary
_client.Login(username, password).Wait();
// Start the processing
var processingTask = _client.DownloadAllPacks(outDir, useSubfolders);
// Retrieve the result
processingTask.Wait();
return processingTask.Result;
}
}
/// <inheritdoc/>
public override bool VerifyInputs() => true;
}
}