126 lines
4.2 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.Aki.Helper;
using SPTInstaller.Interfaces;
using SPTInstaller.Models;
2022-07-09 00:33:55 -04:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
2023-05-11 23:11:39 -04:00
namespace SPTInstaller.Installer_Tasks
2022-07-09 00:33:55 -04:00
{
2023-05-11 23:11:39 -04:00
public class DownloadTask : InstallerTaskBase
2022-07-09 00:33:55 -04:00
{
private InternalData _data;
2023-05-11 23:11:39 -04:00
public DownloadTask(InternalData data) : base("Download Files")
2022-07-09 00:33:55 -04:00
{
_data = data;
}
2023-05-11 23:11:39 -04:00
private async Task<IResult> BuildMirrorList()
2022-07-09 00:33:55 -04:00
{
2023-05-14 22:35:06 -04:00
var progress = new Progress<double>((d) => { SetStatus("Downloading Mirror List", "", (int)Math.Floor(d));});
2022-07-09 00:33:55 -04:00
2023-03-04 15:33:37 -05:00
var file = await DownloadCacheHelper.GetOrDownloadFileAsync("mirrors.json", _data.PatcherMirrorsLink, progress);
2022-07-09 00:33:55 -04:00
2023-03-04 15:33:37 -05:00
if (file == null)
2022-07-09 00:33:55 -04:00
{
2023-05-11 23:11:39 -04:00
return Result.FromError("Failed to download mirror list");
2022-07-09 00:33:55 -04:00
}
2023-03-04 15:33:37 -05:00
var mirrorsList = JsonConvert.DeserializeObject<List<DownloadMirror>>(File.ReadAllText(file.FullName));
2022-07-09 00:33:55 -04:00
2023-03-04 15:33:37 -05:00
if (mirrorsList is List<DownloadMirror> mirrors)
2022-07-09 00:33:55 -04:00
{
_data.PatcherReleaseMirrors = mirrors;
2023-05-11 23:11:39 -04:00
return Result.FromSuccess();
2022-07-09 00:33:55 -04:00
}
2023-05-11 23:11:39 -04:00
return Result.FromError("Failed to deserialize mirrors list");
2022-07-09 00:33:55 -04:00
}
2023-05-11 23:11:39 -04:00
private async Task<IResult> DownloadPatcherFromMirrors(IProgress<double> progress)
2022-07-09 00:33:55 -04:00
{
2022-07-11 21:19:47 -04:00
foreach (var mirror in _data.PatcherReleaseMirrors)
2022-07-09 00:33:55 -04:00
{
2023-05-14 22:35:06 -04:00
SetStatus($"Downloading Patcher", mirror.Link);
2022-07-09 00:33:55 -04:00
// mega is a little weird since they use encryption, but thankfully there is a great library for their api :)
2022-07-11 21:19:47 -04:00
if (mirror.Link.StartsWith("https://mega"))
2022-07-09 00:33:55 -04:00
{
var megaClient = new MegaApiClient();
await megaClient.LoginAnonymousAsync();
// if mega fails to connect, try the next mirror
if (!megaClient.IsLoggedIn) continue;
try
{
2022-07-11 21:19:47 -04:00
using var megaDownloadStream = await megaClient.DownloadAsync(new Uri(mirror.Link), progress);
2022-07-09 00:33:55 -04:00
2023-03-04 15:33:37 -05:00
_data.PatcherZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("patcher.zip", megaDownloadStream, mirror.Hash);
2022-07-11 21:19:47 -04:00
2023-03-04 15:33:37 -05:00
if(_data.PatcherZipInfo == null)
2022-07-11 21:19:47 -04:00
{
2023-03-04 15:33:37 -05:00
continue;
2022-07-11 21:19:47 -04:00
}
2023-05-11 23:11:39 -04:00
return Result.FromSuccess();
2022-07-09 00:33:55 -04:00
}
2023-03-04 15:33:37 -05:00
catch
2022-07-09 00:33:55 -04:00
{
//most likely a 509 (Bandwidth limit exceeded) due to mega's user quotas.
continue;
}
}
2023-03-04 15:33:37 -05:00
_data.PatcherZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("patcher.zip", mirror.Link, progress, mirror.Hash);
2022-07-09 00:33:55 -04:00
2023-03-04 15:33:37 -05:00
if (_data.PatcherZipInfo != null)
2022-07-09 00:33:55 -04:00
{
2023-05-11 23:11:39 -04:00
return Result.FromSuccess();
2022-07-09 00:33:55 -04:00
}
}
2023-05-11 23:11:39 -04:00
return Result.FromError("Failed to download Patcher");
2022-07-09 00:33:55 -04:00
}
2023-05-11 23:11:39 -04:00
public override async Task<IResult> TaskOperation()
2022-07-09 00:33:55 -04:00
{
var progress = new Progress<double>((d) => { SetStatus(null, null, (int)Math.Floor(d)); });
2023-05-11 23:11:39 -04:00
2022-07-09 00:33:55 -04:00
if (_data.PatchNeeded)
{
var buildResult = await BuildMirrorList();
if (!buildResult.Succeeded)
{
return buildResult;
}
2022-07-09 13:14:03 -04:00
SetStatus(null, null, 0);
2022-07-09 00:33:55 -04:00
2022-07-09 13:08:41 -04:00
var patcherDownloadRresult = await DownloadPatcherFromMirrors(progress);
2022-07-09 00:33:55 -04:00
if (!patcherDownloadRresult.Succeeded)
{
return patcherDownloadRresult;
}
}
SetStatus("Downloading SPT-AKI", _data.AkiReleaseDownloadLink, 0);
2022-07-09 00:33:55 -04:00
2023-05-11 23:11:39 -04:00
_data.AkiZipInfo = await DownloadCacheHelper.GetOrDownloadFileAsync("sptaki.zip", _data.AkiReleaseDownloadLink, progress, _data.AkiReleaseHash);
2022-07-09 00:33:55 -04:00
2023-03-04 15:33:37 -05:00
if (_data.AkiZipInfo == null)
2022-07-09 00:33:55 -04:00
{
2023-05-11 23:11:39 -04:00
return Result.FromError("Failed to download spt-aki");
2022-07-09 00:33:55 -04:00
}
2023-05-11 23:11:39 -04:00
return Result.FromSuccess();
2022-07-09 00:33:55 -04:00
}
}
}