SPT-AKI-Installer/Aki.Core/Tasks/DownloadTask.cs

143 lines
4.9 KiB
C#
Raw Normal View History

2022-07-09 13:14:03 -04:00
using CG.Web.MegaApiClient;
using Newtonsoft.Json;
2022-07-09 00:33:55 -04:00
using SPT_AKI_Installer.Aki.Core.Model;
using SPT_AKI_Installer.Aki.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace SPT_AKI_Installer.Aki.Core.Tasks
{
public class DownloadTask : LiveTableTask
{
private InternalData _data;
public DownloadTask(InternalData data) : base("Download Files", false)
{
_data = data;
}
private async Task<GenericResult> BuildMirrorList()
{
var mirrorListInfo = new FileInfo(Path.Join(_data.TargetInstallPath, "mirrors.json"));
2022-07-09 13:08:41 -04:00
SetStatus("Downloading Mirror List", false);
2022-07-09 00:33:55 -04:00
var progress = new Progress<double>((d) => { Progress = (int)Math.Floor(d); });
var downloadResult = await DownloadHelper.DownloadFile(mirrorListInfo, _data.PatcherMirrorsLink, progress);
if (!downloadResult.Succeeded)
{
return downloadResult;
}
2022-07-11 21:19:47 -04:00
var blah = JsonConvert.DeserializeObject<List<DownloadMirror>>(File.ReadAllText(mirrorListInfo.FullName));
2022-07-09 00:33:55 -04:00
2022-07-11 21:19:47 -04:00
if (blah is List<DownloadMirror> mirrors)
2022-07-09 00:33:55 -04:00
{
_data.PatcherReleaseMirrors = mirrors;
return GenericResult.FromSuccess();
}
return GenericResult.FromError("Failed to deserialize mirrors list");
}
2022-07-09 13:08:41 -04:00
private async Task<GenericResult> 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
{
2022-07-11 21:19:47 -04:00
SetStatus($"Downloading Patcher: {mirror.Link}", false);
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 13:08:41 -04:00
using var patcherFileStream = _data.PatcherZipInfo.Open(FileMode.Create);
2022-07-09 00:33:55 -04:00
{
await megaDownloadStream.CopyToAsync(patcherFileStream);
}
2022-07-11 21:19:47 -04:00
patcherFileStream.Close();
if(!DownloadHelper.FileHashCheck(_data.PatcherZipInfo, mirror.Hash))
{
return GenericResult.FromError("Hash mismatch");
}
2022-07-09 00:33:55 -04:00
return GenericResult.FromSuccess();
}
2022-07-09 13:14:03 -04:00
catch (Exception)
2022-07-09 00:33:55 -04:00
{
//most likely a 509 (Bandwidth limit exceeded) due to mega's user quotas.
continue;
}
}
2022-07-11 21:19:47 -04:00
var result = await DownloadHelper.DownloadFile(_data.PatcherZipInfo, mirror.Link, progress, mirror.Hash);
2022-07-09 00:33:55 -04:00
2022-07-09 13:14:03 -04:00
if (result.Succeeded)
2022-07-09 00:33:55 -04:00
{
return GenericResult.FromSuccess();
}
}
return GenericResult.FromError("Failed to download Patcher");
}
public override async Task<GenericResult> RunAsync()
{
2022-07-09 13:08:41 -04:00
_data.PatcherZipInfo = new FileInfo(Path.Join(_data.TargetInstallPath, "patcher.zip"));
2022-07-09 13:14:03 -04:00
_data.AkiZipInfo = new FileInfo(Path.Join(_data.TargetInstallPath, "sptaki.zip"));
2022-07-09 00:33:55 -04:00
if (_data.PatchNeeded)
{
2022-07-09 13:08:41 -04:00
if (_data.PatcherZipInfo.Exists) _data.PatcherZipInfo.Delete();
2022-07-09 00:33:55 -04:00
var buildResult = await BuildMirrorList();
if (!buildResult.Succeeded)
{
return buildResult;
}
2022-07-09 13:14:03 -04:00
2022-07-09 00:33:55 -04:00
Progress = 0;
var progress = new Progress<double>((d) => { Progress = (int)Math.Floor(d); });
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;
}
}
2022-07-09 13:08:41 -04:00
if (_data.AkiZipInfo.Exists) _data.AkiZipInfo.Delete();
2022-07-09 00:33:55 -04:00
SetStatus("Downloading SPT-AKI", false);
Progress = 0;
2022-07-09 13:14:03 -04:00
2022-07-09 00:33:55 -04:00
var akiProgress = new Progress<double>((d) => { Progress = (int)Math.Floor(d); });
2022-07-09 13:08:41 -04:00
var releaseDownloadResult = await DownloadHelper.DownloadFile(_data.AkiZipInfo, _data.AkiReleaseDownloadLink, akiProgress);
2022-07-09 00:33:55 -04:00
if (!releaseDownloadResult.Succeeded)
{
return releaseDownloadResult;
}
return GenericResult.FromSuccess();
}
}
}