Files
BadBuilder/BadBuilder/ConsoleExperiences/DownloadExperience.cs

160 lines
6.9 KiB
C#

using Spectre.Console;
using BadBuilder.Helpers;
using static BadBuilder.Utilities.Constants;
namespace BadBuilder
{
internal partial class Program
{
static async Task<List<ArchiveItem>> DownloadRequiredFiles()
{
bool hasUpdatedDashboard = AnsiConsole.Prompt(
new TextPrompt<bool>("Hast du bereits auf Dashboard-Version [bold]17559[/] aktualisiert?")
.AddChoice(true)
.AddChoice(false)
.DefaultValue(true)
.ChoicesStyle(GreenStyle)
.DefaultValueStyle(OrangeStyle)
.WithConverter(choice => choice ? "j" : "n")
);
ClearConsole();
RequiresDashboardUpdate = !hasUpdatedDashboard;
DownloadItem? dashboardUpdateItem = RequiresDashboardUpdate
? ("Dashboard Update 17559", "https://cdn.niklascfw.de/xbox360/SystemUpdate_17559.zip")
: null;
List<DownloadItem> items = new()
{
("ABadAvatar", "https://cdn.niklascfw.de/xbox360/ABadAvatar-publicbeta1.0.zip"),
("Aurora Dashboard", "https://cdn.niklascfw.de/xbox360/Aurora%200.7b.2%20-%20Release%20Package.rar"),
("XeXmenu", "https://cdn.niklascfw.de/xbox360/MenuData.7z"),
("Rock Band Blitz", "https://cdn.niklascfw.de/xbox360/GameData.zip"),
("Simple 360 NAND Flasher", "https://cdn.niklascfw.de/xbox360/Flasher.7z"),
("XeUnshackle", "https://cdn.niklascfw.de/xbox360/XeUnshackle-BETA-v1_03.zip"),
("BadUpdate", "https://cdn.niklascfw.de/xbox360/Xbox360BadUpdate-Retail-USB-v1.2.zip"),
("BadUpdate Tools", "https://cdn.niklascfw.de/xbox360/Tools.zip"),
};
List<DownloadItem> existingFiles = items.Where(item =>
File.Exists(Path.Combine(DOWNLOAD_DIR, item.url.Split('/').Last()))).ToList();
List<string> choices = items.Select(item =>
existingFiles.Any(e => e.name == item.name)
? $"{item.name} [italic gray](bereits vorhanden)[/]"
: item.name).ToList();
var prompt = new MultiSelectionPrompt<string>()
.Title("Welche Dateien hast du bereits? [gray](Mehrfachauswahl möglich)[/]")
.PageSize(10)
.NotRequired()
.HighlightStyle(GreenStyle)
.AddChoices(choices);
foreach (string choice in choices)
{
if (existingFiles.Any(e => choice.StartsWith(e.name)))
prompt.Select(choice);
}
List<string> selectedItems = AnsiConsole.Prompt(prompt)
.Select(choice => choice.Split(" [")[0])
.ToList();
List<DownloadItem> itemsToDownload = items.Where(item => !selectedItems.Contains(item.name)).ToList();
if (dashboardUpdateItem.HasValue)
{
string updateFileName = dashboardUpdateItem.Value.url.Split('/').Last();
string updateDestination = Path.Combine(DOWNLOAD_DIR, updateFileName);
if (!File.Exists(updateDestination))
{
itemsToDownload.Add(dashboardUpdateItem.Value);
}
}
itemsToDownload.Sort((a, b) => b.name.Length.CompareTo(a.name.Length));
if (!Directory.Exists($"{DOWNLOAD_DIR}"))
Directory.CreateDirectory($"{DOWNLOAD_DIR}");
if (itemsToDownload.Any())
{
HttpClient downloadClient = new();
await AnsiConsole.Progress()
.Columns(
new TaskDescriptionColumn(),
new ProgressBarColumn().FinishedStyle(GreenStyle).CompletedStyle(LightOrangeStyle),
new PercentageColumn().CompletedStyle(GreenStyle),
new RemainingTimeColumn().Style(GrayStyle),
new TransferSpeedColumn()
)
.StartAsync(async ctx =>
{
AnsiConsole.MarkupLine("[#76B900]{0}[/] Lade erforderliche Dateien herunter.", Markup.Escape("[*]"));
await Task.WhenAll(itemsToDownload.Select(async item =>
{
var task = ctx.AddTask(item.name, new ProgressTaskSettings { AutoStart = false });
await DownloadHelper.DownloadFileAsync(downloadClient, task, item.url);
}));
});
string status = "[+]";
AnsiConsole.MarkupInterpolated($"[#76B900]{status}[/] [bold]{itemsToDownload.Count()}[/] Downloads abgeschlossen.\n");
}
else
{
AnsiConsole.MarkupLine("[italic #76B900]Keine Downloads erforderlich. Alle Dateien sind bereits vorhanden.[/]");
}
Console.WriteLine();
foreach (string selectedItem in selectedItems)
{
string expectedFileName = items.First(i => i.name == selectedItem).url.Split('/').Last();
string destinationPath = Path.Combine(DOWNLOAD_DIR, expectedFileName);
if (File.Exists(destinationPath)) continue;
string existingPath = AnsiConsole.Prompt(
new TextPrompt<string>($"Gib den Pfad zum Archiv [bold]{selectedItem}[/] ein:")
.PromptStyle(LightOrangeStyle)
.Validate(path =>
{
return File.Exists(path.Trim().Trim('"'))
? ValidationResult.Success()
: ValidationResult.Error("[red]Datei wurde nicht gefunden.[/]");
})
).Trim().Trim('"');
try
{
File.Copy(existingPath, destinationPath, overwrite: true);
AnsiConsole.MarkupLine($"[italic #76B900][bold]{selectedItem}[/] wurde erfolgreich in das Arbeitsverzeichnis kopiert.[/]\n");
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[italic red]Fehler beim Kopieren von [bold]{selectedItem}[/] in das Arbeitsverzeichnis. Ausnahme: {ex.Message}[/]\n");
}
}
List<ArchiveItem> archives = items
.Select(item => new ArchiveItem(item.name, Path.Combine(DOWNLOAD_DIR, item.url.Split('/').Last())))
.ToList();
if (dashboardUpdateItem.HasValue)
{
string updatePath = Path.Combine(DOWNLOAD_DIR, dashboardUpdateItem.Value.url.Split('/').Last());
archives.Add(new ArchiveItem(dashboardUpdateItem.Value.name, updatePath));
}
return archives;
}
}
}