2023-09-21 18:52:33 -04:00
|
|
|
|
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;
|
2023-07-12 09:19:33 +02:00
|
|
|
|
using SPTInstaller.Helpers;
|
2023-09-21 10:53:49 -04:00
|
|
|
|
using SPTInstaller.Models.Mirrors;
|
|
|
|
|
using SPTInstaller.Models.Mirrors.Downloaders;
|
|
|
|
|
using Serilog;
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
namespace SPTInstaller.Installer_Tasks;
|
|
|
|
|
|
|
|
|
|
public class DownloadTask : InstallerTaskBase
|
2022-07-09 00:33:55 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
private InternalData _data;
|
2023-09-21 10:53:49 -04:00
|
|
|
|
private List<IMirrorDownloader> _mirrors = new List<IMirrorDownloader>();
|
|
|
|
|
private string _expectedPatcherHash = "";
|
2023-07-12 09:19:33 +02:00
|
|
|
|
|
|
|
|
|
public DownloadTask(InternalData data) : base("Download Files")
|
2022-07-09 00:33:55 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
_data = data;
|
|
|
|
|
}
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
private async Task<IResult> BuildMirrorList()
|
|
|
|
|
{
|
2024-04-27 14:51:54 -04:00
|
|
|
|
foreach (var mirror in _data.PatchInfo.Mirrors)
|
2023-09-21 10:53:49 -04:00
|
|
|
|
{
|
2023-09-21 18:52:33 -04:00
|
|
|
|
_expectedPatcherHash = mirror.Hash;
|
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
switch (mirror.Link)
|
|
|
|
|
{
|
2024-04-27 14:51:54 -04:00
|
|
|
|
case { } l when l.StartsWith("https://mega"):
|
2023-09-21 10:53:49 -04:00
|
|
|
|
_mirrors.Add(new MegaMirrorDownloader(mirror));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
_mirrors.Add(new HttpMirrorDownloader(mirror));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
}
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
return Result.FromSuccess("Mirrors list ready");
|
2023-07-12 09:19:33 +02:00
|
|
|
|
}
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
private async Task<IResult> DownloadPatcherFromMirrors(IProgress<double> progress)
|
|
|
|
|
{
|
2023-09-21 18:52:33 -04:00
|
|
|
|
SetStatus("Downloading Patcher", "Verifying cached patcher ...", progressStyle: ProgressStyle.Indeterminate);
|
2023-09-21 10:53:49 -04:00
|
|
|
|
|
2024-03-23 14:15:48 -04:00
|
|
|
|
if (DownloadCacheHelper.CheckCache("patcher", _expectedPatcherHash, out var cacheFile))
|
2023-07-12 09:19:33 +02:00
|
|
|
|
{
|
2023-09-21 10:53:49 -04:00
|
|
|
|
_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
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
foreach (var mirror in _mirrors)
|
|
|
|
|
{
|
|
|
|
|
SetStatus("Downloading Patcher", mirror.MirrorInfo.Link, progressStyle: ProgressStyle.Indeterminate);
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
_data.PatcherZipInfo = await mirror.Download(progress);
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (_data.PatcherZipInfo != null)
|
2022-07-09 00:33:55 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromError("Failed to download Patcher");
|
|
|
|
|
}
|
2022-07-09 13:14:03 -04:00
|
|
|
|
|
2024-04-25 23:33:20 -07:00
|
|
|
|
private async Task<IResult> DownloadSptAkiFromMirrors(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-AKI", mirror.DownloadUrl, progressStyle: ProgressStyle.Indeterminate);
|
|
|
|
|
|
|
|
|
|
_data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki", mirror.DownloadUrl, progress, mirror.Hash);
|
|
|
|
|
|
|
|
|
|
if (_data.AkiZipInfo != null)
|
|
|
|
|
{
|
|
|
|
|
return Result.FromSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.FromError("Failed to download spt-aki");
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:19:33 +02: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
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (_data.PatchNeeded)
|
|
|
|
|
{
|
|
|
|
|
var buildResult = await BuildMirrorList();
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (!buildResult.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return buildResult;
|
2022-07-09 00:33:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
SetStatus(null, null, 0);
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var patcherDownloadRresult = await DownloadPatcherFromMirrors(progress);
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (!patcherDownloadRresult.Succeeded)
|
2022-07-09 00:33:55 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return patcherDownloadRresult;
|
2022-07-09 00:33:55 -04:00
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
}
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2024-04-25 23:33:20 -07:00
|
|
|
|
return await DownloadSptAkiFromMirrors(progress);
|
2022-07-09 00:33:55 -04:00
|
|
|
|
}
|
|
|
|
|
}
|