115 lines
3.7 KiB
C#
Raw Normal View History

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 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 = "";
2024-05-01 10:31:55 -04:00
public DownloadTask(InternalData data) : base("Download Files")
2022-07-09 00:33:55 -04:00
{
_data = data;
}
2024-05-01 10:31:55 -04:00
private async Task<IResult> BuildMirrorList()
{
2024-04-27 14:51:54 -04:00
foreach (var mirror in _data.PatchInfo.Mirrors)
{
_expectedPatcherHash = mirror.Hash;
2024-05-01 10:31:55 -04:00
switch (mirror.Link)
{
2024-04-27 14:51:54 -04:00
case { } l when l.StartsWith("https://mega"):
_mirrors.Add(new MegaMirrorDownloader(mirror));
break;
default:
_mirrors.Add(new HttpMirrorDownloader(mirror));
break;
}
}
2024-05-01 10:31:55 -04:00
return Result.FromSuccess("Mirrors list ready");
}
2024-05-01 10:31:55 -04:00
private async Task<IResult> DownloadPatcherFromMirrors(IProgress<double> progress)
{
SetStatus("Downloading Patcher", "Verifying cached patcher ...", progressStyle: ProgressStyle.Indeterminate);
2024-05-01 10:31:55 -04:00
2024-05-04 16:18:49 -04:00
if (DownloadCacheHelper.CheckCacheHash("patcher", _expectedPatcherHash, out var cacheFile))
{
_data.PatcherZipInfo = cacheFile;
2024-05-01 10:31:55 -04:00
Log.Information("Using cached file {fileName} - Hash: {hash}", _data.PatcherZipInfo.Name,
_expectedPatcherHash);
return Result.FromSuccess();
}
2024-05-01 10:31:55 -04:00
foreach (var mirror in _mirrors)
{
SetStatus("Downloading Patcher", mirror.MirrorInfo.Link, progressStyle: ProgressStyle.Indeterminate);
2024-05-01 10:31:55 -04:00
_data.PatcherZipInfo = await mirror.Download(progress);
2024-05-01 10:31:55 -04:00
if (_data.PatcherZipInfo != null)
2022-07-09 00:33:55 -04:00
{
return Result.FromSuccess();
}
}
2024-05-01 10:31:55 -04:00
return Result.FromError("Failed to download Patcher");
}
2024-05-01 10:31:55 -04:00
private async Task<IResult> DownloadSPTFromMirrors(IProgress<double> progress)
{
// Note that GetOrDownloadFileAsync handles the cached file hash check, so we don't need to check it first
foreach (var mirror in _data.ReleaseInfo.Mirrors)
{
SetStatus("Downloading SPT", mirror.DownloadUrl, progressStyle: ProgressStyle.Indeterminate);
2024-05-01 10:31:55 -04:00
_data.SPTZipInfo =
await DownloadCacheHelper.GetOrDownloadFileAsync("SPT", mirror.DownloadUrl, progress, mirror.Hash);
2024-05-01 10:31:55 -04:00
if (_data.SPTZipInfo != null)
{
return Result.FromSuccess();
}
}
2024-05-01 10:31:55 -04:00
return Result.FromError("Failed to download SPT");
}
2024-05-01 10:31:55 -04:00
public override async Task<IResult> TaskOperation()
{
var progress = new Progress<double>((d) => { SetStatus(null, null, (int)Math.Floor(d)); });
2024-05-01 10:31:55 -04:00
if (_data.PatchNeeded)
{
var buildResult = await BuildMirrorList();
2024-05-01 10:31:55 -04:00
if (!buildResult.Succeeded)
{
return buildResult;
2022-07-09 00:33:55 -04:00
}
2024-05-01 10:31:55 -04:00
SetStatus(null, null, 0);
2024-05-01 10:31:55 -04:00
var patcherDownloadRresult = await DownloadPatcherFromMirrors(progress);
2024-05-01 10:31:55 -04:00
if (!patcherDownloadRresult.Succeeded)
2022-07-09 00:33:55 -04:00
{
return patcherDownloadRresult;
2022-07-09 00:33:55 -04:00
}
}
2024-05-01 10:31:55 -04:00
return await DownloadSPTFromMirrors(progress);
2022-07-09 00:33:55 -04:00
}
}