99 lines
3.8 KiB
C#
Raw Normal View History

2024-04-27 14:51:54 -04:00
using SPTInstaller.Interfaces;
2023-05-11 23:11:39 -04:00
using SPTInstaller.Models;
2022-07-09 00:33:55 -04:00
using System.Threading.Tasks;
using SPTInstaller.Helpers;
using Newtonsoft.Json;
2024-04-27 14:51:54 -04:00
using SPTInstaller.Models.Mirrors;
using SPTInstaller.Models.ReleaseInfo;
2022-07-09 00:33:55 -04:00
namespace SPTInstaller.Installer_Tasks;
public class ReleaseCheckTask : InstallerTaskBase
2022-07-09 00:33:55 -04:00
{
private InternalData _data;
2024-05-01 10:31:55 -04:00
public ReleaseCheckTask(InternalData data) : base("Release Checks")
{
_data = data;
}
2024-05-01 10:31:55 -04:00
public override async Task<IResult> TaskOperation()
{
try
2022-07-09 00:33:55 -04:00
{
SetStatus("Checking SPT Releases", "", null, ProgressStyle.Indeterminate);
2024-05-01 10:31:55 -04:00
var progress = new Progress<double>((d) => { SetStatus(null, null, (int)Math.Floor(d)); });
2024-05-01 10:31:55 -04:00
var akiReleaseInfoFile =
2024-05-04 16:18:49 -04:00
await DownloadCacheHelper.GetOrDownloadFileAsync("release.json", DownloadCacheHelper.ReleaseMirrorUrl,
progress, DownloadCacheHelper.SuggestedTtl);
if (akiReleaseInfoFile == null)
{
return Result.FromError("Failed to download release metadata");
}
2024-05-01 10:31:55 -04:00
var akiReleaseInfo =
JsonConvert.DeserializeObject<ReleaseInfo>(File.ReadAllText(akiReleaseInfoFile.FullName));
SetStatus("Checking for Patches", "", null, ProgressStyle.Indeterminate);
2024-05-01 10:31:55 -04:00
2024-04-27 14:51:54 -04:00
var akiPatchMirrorsFile =
2024-05-04 16:18:49 -04:00
await DownloadCacheHelper.GetOrDownloadFileAsync("mirrors.json", DownloadCacheHelper.PatchMirrorUrl,
progress, DownloadCacheHelper.SuggestedTtl);
2024-05-01 10:31:55 -04:00
2024-04-27 14:51:54 -04:00
if (akiPatchMirrorsFile == null)
{
2024-04-27 14:51:54 -04:00
return Result.FromError("Failed to download patch mirror data");
}
2024-04-27 14:51:54 -04:00
2024-05-01 10:31:55 -04:00
var patchMirrorInfo =
JsonConvert.DeserializeObject<PatchInfo>(File.ReadAllText(akiPatchMirrorsFile.FullName));
2024-04-27 14:51:54 -04:00
if (akiReleaseInfo == null || patchMirrorInfo == null)
{
return Result.FromError("An error occurred while deserializing aki or patch data");
}
_data.ReleaseInfo = akiReleaseInfo;
_data.PatchInfo = patchMirrorInfo;
int intAkiVersion = int.Parse(akiReleaseInfo.ClientVersion);
int intGameVersion = int.Parse(_data.OriginalGameVersion);
// note: it's possible the game version could be lower than the aki version and still need a patch if the major version numbers change
// : it's probably a low chance though
bool patchNeedCheck = intGameVersion > intAkiVersion;
2024-05-01 10:31:55 -04:00
2024-04-27 14:51:54 -04:00
if (intGameVersion < intAkiVersion)
{
return Result.FromError("Your client is outdated. Please update EFT");
}
2024-05-01 10:31:55 -04:00
2024-04-27 14:51:54 -04:00
if (intGameVersion == intAkiVersion)
{
patchNeedCheck = false;
}
2024-05-01 10:31:55 -04:00
if ((intGameVersion != patchMirrorInfo.SourceClientVersion ||
intAkiVersion != patchMirrorInfo.TargetClientVersion) && patchNeedCheck)
{
2024-05-01 10:31:55 -04:00
return Result.FromError(
"No patcher available for your version.\nA patcher is usually created within 24 hours of an EFT update.");
}
2024-05-01 10:31:55 -04:00
_data.PatchNeeded = patchNeedCheck;
2024-05-01 10:31:55 -04:00
string status =
$"Current Release: {akiReleaseInfo.ClientVersion} - {(_data.PatchNeeded ? "Patch Available" : "No Patch Needed")}";
SetStatus(null, status);
2024-05-01 10:31:55 -04:00
return Result.FromSuccess(status);
}
catch (Exception ex)
{
//request failed
return Result.FromError($"Request Failed:\n{ex.Message}");
2022-07-09 00:33:55 -04:00
}
}
}