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()
|
|
|
|
|
{
|
2023-09-21 10:53:49 -04:00
|
|
|
|
var progress = new Progress<double>((d) => { SetStatus("Downloading Mirror List", "", (int)Math.Floor(d), ProgressStyle.Shown);});
|
2023-07-12 09:19:33 +02:00
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
var file = await DownloadCacheHelper.DownloadFileAsync("mirrors.json", _data.PatcherMirrorsLink, progress);
|
2023-07-12 09:19:33 +02:00
|
|
|
|
|
|
|
|
|
if (file == null)
|
2022-07-09 00:33:55 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromError("Failed to download mirror list");
|
2022-07-09 00:33:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var mirrorsList = JsonConvert.DeserializeObject<List<DownloadMirror>>(File.ReadAllText(file.FullName));
|
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
if (mirrorsList == null)
|
|
|
|
|
return Result.FromError("Failed to deserialize mirrors list");
|
2022-07-09 00:33:55 -04:00
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
foreach (var mirror in mirrorsList)
|
|
|
|
|
{
|
2023-09-21 18:52:33 -04:00
|
|
|
|
_expectedPatcherHash = mirror.Hash;
|
|
|
|
|
|
2023-09-21 10:53:49 -04:00
|
|
|
|
switch (mirror.Link)
|
|
|
|
|
{
|
|
|
|
|
case string l when l.StartsWith("https://mega"):
|
|
|
|
|
_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
|
|
|
|
|
|
|
|
|
if (DownloadCacheHelper.CheckCache("patcher.zip", _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
|
|
|
|
|
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
|
|
|
|
|
2023-07-12 09:19:33 +02: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
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
|
|
|
|
|
return Result.FromSuccess();
|
2022-07-09 00:33:55 -04:00
|
|
|
|
}
|
|
|
|
|
}
|