2022-07-09 00:33:55 -04:00
using Gitea.Api ;
using Gitea.Client ;
2023-05-11 23:11:39 -04:00
using SPTInstaller.Interfaces ;
using SPTInstaller.Models ;
2022-07-09 00:33:55 -04:00
using System.Threading.Tasks ;
2023-07-12 09:19:33 +02:00
using SPTInstaller.Helpers ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
namespace SPTInstaller.Installer_Tasks ;
public class ReleaseCheckTask : InstallerTaskBase
2022-07-09 00:33:55 -04:00
{
2023-07-12 09:19:33 +02:00
private InternalData _data ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
public ReleaseCheckTask ( InternalData data ) : base ( "Release Checks" )
{
_data = data ;
}
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
public override async Task < IResult > TaskOperation ( )
{
try
2022-07-09 00:33:55 -04:00
{
2023-07-12 09:19:33 +02:00
var repo = new RepositoryApi ( Configuration . Default ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
SetStatus ( "Checking SPT Releases" , "" , null , ProgressStyle . Indeterminate ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
var akiRepoReleases = await repo . RepoListReleasesAsync ( "SPT-AKI" , "Stable-releases" ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
SetStatus ( "Checking for Patches" , "" , null , ProgressStyle . Indeterminate ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
var patchRepoReleases = await repo . RepoListReleasesAsync ( "SPT-AKI" , "Downgrade-Patches" ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
var latestAkiRelease = akiRepoReleases . FindAll ( x = > ! x . Prerelease ) [ 0 ] ;
var latestAkiVersion = latestAkiRelease . Name . Replace ( '(' , ' ' ) . Replace ( ')' , ' ' ) . Split ( ' ' ) [ 3 ] ;
var comparePatchToAki = patchRepoReleases ? . Find ( x = > x . Name . Contains ( _data . OriginalGameVersion ) & & x . Name . Contains ( latestAkiVersion ) ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
_data . PatcherMirrorsLink = comparePatchToAki ? . Assets [ 0 ] . BrowserDownloadUrl ;
_data . AkiReleaseDownloadLink = latestAkiRelease . Assets [ 0 ] . BrowserDownloadUrl ;
_data . AkiReleaseHash = FileHashHelper . GetGiteaReleaseHash ( latestAkiRelease ) ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
int IntAkiVersion = int . Parse ( latestAkiVersion ) ;
int IntGameVersion = int . Parse ( _data . OriginalGameVersion ) ;
bool patchNeedCheck = false ;
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
if ( IntGameVersion > IntAkiVersion )
{
patchNeedCheck = true ;
}
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
if ( IntGameVersion < IntAkiVersion )
{
return Result . FromError ( "Your client is outdated. Please update EFT" ) ;
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
if ( IntGameVersion = = IntAkiVersion )
{
patchNeedCheck = false ;
}
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
if ( comparePatchToAki = = null & & patchNeedCheck )
{
2024-04-05 15:07:58 -04:00
return Result . FromError ( "No patcher available for your version. A patcher is usually created within 24 hours of an EFT update.\nYou can join our discord and watch the dev-webhooks channel for '[SPT-AKI/Downgrade-Patches] Release created' to know when a patcher is available" ) ;
2023-07-12 09:19:33 +02:00
}
2022-07-09 00:33:55 -04:00
2023-07-12 09:19:33 +02:00
_data . PatchNeeded = patchNeedCheck ;
2022-07-09 00:33:55 -04:00
2023-08-02 23:32:44 -04:00
string status = $"Current Release: {latestAkiVersion} - {(_data.PatchNeeded ? " Patch Available " : " No Patch Needed ")}" ;
2023-07-12 09:19:33 +02:00
SetStatus ( null , status ) ;
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
}
}
2023-07-12 09:19:33 +02:00
}