SPT-AKI-Installer/Aki.Helper/DownloadHelper.cs

105 lines
3.8 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
2022-06-07 20:34:09 +01:00
using System.Threading;
using Gitea.Api;
using Gitea.Client;
using Gitea.Model;
2022-06-07 20:34:09 +01:00
using Spectre.Console;
using HttpClientProgress;
namespace SPT_AKI_Installer.Aki.Helper
{
public static class DownloadHelper
{
public static bool patchNeedCheck;
public static string akiLink;
public static string patcherLink;
public static async Task ReleaseCheck()
{
Configuration.Default.BasePath = "https://dev.sp-tarkov.com/api/v1";
var repo = new RepositoryApi(Configuration.Default);
try
{
var patchRepoReleases = await repo.RepoListReleasesAsync("SPT-AKI", "Downgrade-Patches");
var akiRepoReleases = await repo.RepoListReleasesAsync("SPT-AKI", "Stable-releases");
var latestAkiRelease = akiRepoReleases.FindAll(x => !x.Prerelease)[0];
var latestAkiVersion = latestAkiRelease.Name.Replace('(', ' ').Replace(')', ' ').Split(' ')[3];
var comparePatchToAki = patchRepoReleases?.Find(x => x.Name.Contains(PreCheckHelper.gameVersion) && x.Name.Contains(latestAkiVersion));
patcherLink = comparePatchToAki?.Assets[0].BrowserDownloadUrl;
akiLink = latestAkiRelease.Assets[0].BrowserDownloadUrl;
int IntAkiVersion = int.Parse(latestAkiVersion);
int IntGameVersion = int.Parse(PreCheckHelper.gameVersion);
if (IntGameVersion > IntAkiVersion)
{
patchNeedCheck = true;
}
if (IntGameVersion < IntAkiVersion)
{
LogHelper.Info("Client is older than current Aki Version, Please update!");
LogHelper.Warning("Press enter to close the app!");
Console.ReadKey();
Environment.Exit(0);
}
if (IntGameVersion == IntAkiVersion)
{
patchNeedCheck = false;
}
if(comparePatchToAki == null && patchNeedCheck)
{
LogHelper.Warning("There is no current patcher for your client version and its needed");
LogHelper.Warning("Please try again later once a new patcher is uploaded.");
LogHelper.Warning("Press enter to close the app!");
Console.ReadKey();
Environment.Exit(0);
}
}
catch (Exception)
{
//request failed
LogHelper.Info("Request Failed");
}
}
2022-06-07 20:34:09 +01:00
public static async Task DownloadFile(string targetFilePath, string targetLink, string newFileName)
{
2022-06-07 20:34:09 +01:00
await AnsiConsole.Progress().Columns(
new PercentageColumn(),
new TaskDescriptionColumn(),
new ProgressBarColumn(),
new ElapsedTimeColumn(),
new SpinnerColumn()
).StartAsync(async (ProgressContext context) =>
{
2022-06-07 20:34:09 +01:00
var task = context.AddTask("Downloading File");
var client = new HttpClient();
var docUrl = targetLink;
var filePath = Path.Join(targetFilePath, newFileName);
// Setup your progress reporter
var progress = new Progress<float>((float progress) =>
{
task.Value = progress;
});
// Use the provided extension method
using (var file = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
await client.DownloadDataAsync(docUrl, file, progress);
});
}
}
}