119 lines
3.7 KiB
C#
Raw Normal View History

2022-07-09 13:14:03 -04:00
using CG.Web.MegaApiClient;
using Newtonsoft.Json;
2023-05-11 23:11:39 -04:00
using SPTInstaller.Interfaces;
using SPTInstaller.Models;
2022-07-09 00:33:55 -04:00
using System.Collections.Generic;
using System.Threading.Tasks;
using SPTInstaller.Helpers;
using SPTInstaller.Models.Mirrors;
using SPTInstaller.Models.Mirrors.Downloaders;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Serilog;
2022-07-09 00:33:55 -04:00
namespace SPTInstaller.Installer_Tasks;
public class DownloadTask : InstallerTaskBase
2022-07-09 00:33:55 -04:00
{
private InternalData _data;
private List<IMirrorDownloader> _mirrors = new List<IMirrorDownloader>();
private string _expectedPatcherHash = "";
public DownloadTask(InternalData data) : base("Download Files")
2022-07-09 00:33:55 -04:00
{
_data = data;
}
2022-07-09 00:33:55 -04:00
private async Task<IResult> BuildMirrorList()
{
var progress = new Progress<double>((d) => { SetStatus("Downloading Mirror List", "", (int)Math.Floor(d), ProgressStyle.Shown);});
var file = await DownloadCacheHelper.DownloadFileAsync("mirrors.json", _data.PatcherMirrorsLink, progress);
if (file == null)
2022-07-09 00:33:55 -04:00
{
return Result.FromError("Failed to download mirror list");
2022-07-09 00:33:55 -04:00
}
var mirrorsList = JsonConvert.DeserializeObject<List<DownloadMirror>>(File.ReadAllText(file.FullName));
if (mirrorsList == null)
return Result.FromError("Failed to deserialize mirrors list");
2022-07-09 00:33:55 -04:00
foreach (var mirror in mirrorsList)
{
switch (mirror.Link)
{
case string l when l.StartsWith("https://mega"):
_mirrors.Add(new MegaMirrorDownloader(mirror));
break;
default:
_mirrors.Add(new HttpMirrorDownloader(mirror));
break;
}
}
2022-07-09 00:33:55 -04:00
return Result.FromSuccess("Mirrors list ready");
}
2022-07-09 00:33:55 -04:00
private async Task<IResult> DownloadPatcherFromMirrors(IProgress<double> progress)
{
SetStatus("Downloading Patcher", "Checking cache ...", progressStyle: ProgressStyle.Indeterminate);
if (DownloadCacheHelper.CheckCache("patcher.zip", _expectedPatcherHash, out var cacheFile))
{
_data.PatcherZipInfo = cacheFile;
Log.Information("Using cached file {fileName} - Hash: {hash}", _data.PatcherZipInfo.Name, _expectedPatcherHash);
return Result.FromSuccess();
}
2022-07-09 00:33:55 -04:00
foreach (var mirror in _mirrors)
{
SetStatus("Downloading Patcher", mirror.MirrorInfo.Link, progressStyle: ProgressStyle.Indeterminate);
2022-07-09 00:33:55 -04:00
_data.PatcherZipInfo = await mirror.Download(progress);
2022-07-09 00:33:55 -04:00
if (_data.PatcherZipInfo != null)
2022-07-09 00:33:55 -04:00
{
return Result.FromSuccess();
}
}
2022-07-09 00:33:55 -04:00
return Result.FromError("Failed to download Patcher");
}
2022-07-09 13:14:03 -04:00
public override async Task<IResult> TaskOperation()
{
var progress = new Progress<double>((d) => { SetStatus(null, null, (int)Math.Floor(d)); });
2022-07-09 00:33:55 -04:00
if (_data.PatchNeeded)
{
var buildResult = await BuildMirrorList();
2022-07-09 00:33:55 -04:00
if (!buildResult.Succeeded)
{
return buildResult;
2022-07-09 00:33:55 -04:00
}
SetStatus(null, null, 0);
2022-07-09 00:33:55 -04:00
var patcherDownloadRresult = await DownloadPatcherFromMirrors(progress);
2022-07-09 00:33:55 -04:00
if (!patcherDownloadRresult.Succeeded)
2022-07-09 00:33:55 -04:00
{
return patcherDownloadRresult;
2022-07-09 00:33:55 -04:00
}
}
2022-07-09 00:33:55 -04:00
SetStatus("Downloading SPT-AKI", _data.AkiReleaseDownloadLink, 0);
_data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki.zip", _data.AkiReleaseDownloadLink, progress, _data.AkiReleaseHash);
if (_data.AkiZipInfo == null)
{
return Result.FromError("Failed to download spt-aki");
2022-07-09 00:33:55 -04:00
}
return Result.FromSuccess();
2022-07-09 00:33:55 -04:00
}
}