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 ;
2023-07-12 09:19:33 +02:00
using SPTInstaller.Helpers ;
2024-04-25 23:33:20 -07:00
using Newtonsoft.Json ;
2024-04-27 14:51:54 -04:00
using SPTInstaller.Models.Mirrors ;
2024-04-26 09:52:41 -07:00
using SPTInstaller.Models.ReleaseInfo ;
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 ;
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02:00
public ReleaseCheckTask ( InternalData data ) : base ( "Release Checks" )
{
_data = data ;
}
2024-05-01 10:31: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
SetStatus ( "Checking SPT Releases" , "" , null , ProgressStyle . Indeterminate ) ;
2024-05-01 10:31:55 -04:00
2024-04-25 23:33:20 -07:00
var progress = new Progress < double > ( ( d ) = > { SetStatus ( null , null , ( int ) Math . Floor ( d ) ) ; } ) ;
2024-05-04 16:18:49 -04:00
2025-01-03 15:09:54 -05:00
ReleaseInfo ? sptReleaseInfo = null ;
PatchInfo ? patchMirrorInfo = null ;
2024-05-01 10:31:55 -04:00
2025-01-03 15:09:54 -05:00
int retries = 1 ;
2025-01-03 12:07:38 -05:00
2025-01-03 15:09:54 -05:00
while ( retries > = 0 )
2025-01-03 12:07:38 -05:00
{
2025-01-03 15:09:54 -05:00
retries - - ;
2025-01-03 12:07:38 -05:00
2025-01-03 15:09:54 -05:00
try
{
var sptReleaseInfoFile =
await DownloadCacheHelper . GetOrDownloadFileAsync ( "release.json" , DownloadCacheHelper . ReleaseMirrorUrl ,
progress , DownloadCacheHelper . SuggestedTtl ) ;
if ( sptReleaseInfoFile = = null )
{
return Result . FromError ( "Failed to download release metadata, try clicking the 'Whats this' button below followed by the 'Clear Metadata cache' button" ) ;
}
SetStatus ( "Checking for Patches" , "" , null , ProgressStyle . Indeterminate ) ;
var sptPatchMirrorsFile =
await DownloadCacheHelper . GetOrDownloadFileAsync ( "mirrors.json" , DownloadCacheHelper . PatchMirrorUrl ,
progress , DownloadCacheHelper . SuggestedTtl ) ;
if ( sptPatchMirrorsFile = = null )
{
return Result . FromError ( "Failed to download patch mirror data, try clicking the 'Whats this' button below followed by the 'Clear Metadata cache' button" ) ;
}
sptReleaseInfo =
JsonConvert . DeserializeObject < ReleaseInfo > ( File . ReadAllText ( sptReleaseInfoFile . FullName ) ) ;
patchMirrorInfo =
JsonConvert . DeserializeObject < PatchInfo > ( File . ReadAllText ( sptPatchMirrorsFile . FullName ) ) ;
break ;
}
catch ( Exception ex )
{
if ( retries > = 0 )
{
SetStatus ( "Clearing cache and retrying ..." , "" , null , ProgressStyle . Indeterminate ) ;
await Task . Delay ( 1000 ) ;
DownloadCacheHelper . ClearMetadataCache ( ) ;
continue ;
}
return Result . FromError (
$"An error occurred while deserializing SPT or patch data.\n\nMost likely we are uploading a new patch.\nPlease wait and try again in an hour\n\nERROR: {ex.Message}" ) ;
}
2025-01-03 12:07:38 -05:00
}
2025-01-03 15:09:54 -05:00
if ( sptReleaseInfo = = null | | patchMirrorInfo = = null )
2024-04-27 14:51:54 -04:00
{
2025-01-03 15:09:54 -05:00
return Result . FromError (
"Release or mirror info was null. If you are seeing this report it. This should never be hit" ) ;
2024-04-27 14:51:54 -04:00
}
2025-01-03 15:09:54 -05:00
_data . ReleaseInfo = sptReleaseInfo ;
2024-04-27 14:51:54 -04:00
_data . PatchInfo = patchMirrorInfo ;
2025-01-03 15:09:54 -05:00
int intSPTVersion = int . Parse ( sptReleaseInfo . ClientVersion ) ;
2024-04-27 14:51:54 -04:00
int intGameVersion = int . Parse ( _data . OriginalGameVersion ) ;
2024-05-21 23:07:51 +01:00
// note: it's possible the game version could be lower than the SPT version and still need a patch if the major version numbers change
2024-04-27 14:51:54 -04:00
// : it's probably a low chance though
2024-05-21 23:07:51 +01:00
bool patchNeedCheck = intGameVersion > intSPTVersion ;
2024-05-01 10:31:55 -04:00
2024-05-21 23:07:51 +01:00
if ( intGameVersion < intSPTVersion )
2023-07-12 09:19:33 +02:00
{
2024-07-18 13:57:19 +01:00
return Result . FromError ( "Your live EFT is out of date. Please update it using the Battlestate Games Launcher and try runing the SPT Installer again" ) ;
2023-07-12 09:19:33 +02:00
}
2024-05-01 10:31:55 -04:00
2024-05-21 23:07:51 +01:00
if ( intGameVersion = = intSPTVersion )
2023-07-12 09:19:33 +02:00
{
patchNeedCheck = false ;
}
2024-10-04 09:55:04 -04:00
/ *
An example of the logic going on here because holy shit I can ' t keep track of why we do it this way - waffle . lazy
- - - - Example data - - - -
gameVersion : 32738
sptVersion : 30626
SourceClientVersion : 32678
TargetClientVersion : 30626
patchNeeded : true
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
* spt client is ' outdated ' if the game and target versions don ' t match
* or
* the game version is behind the mirror ' s source client version
sptClientIsOutdated = ( 30626 ! = 30626 | | 32738 > 32678 ) & & true
* otherwise , if the game version doesn ' t match the mirror ' s source version , we assume live is outdated
liveClientIsOutdated = 32738 ! = 32678 & & true
* /
2024-06-09 17:15:47 +01:00
2024-10-04 09:55:04 -04:00
bool sptClientIsOutdated = ( intSPTVersion ! = patchMirrorInfo . TargetClientVersion | | intGameVersion > patchMirrorInfo . SourceClientVersion ) & & patchNeedCheck ;
2024-06-09 17:15:47 +01:00
bool liveClientIsOutdated = intGameVersion ! = patchMirrorInfo . SourceClientVersion & & patchNeedCheck ;
2024-07-10 13:00:52 -04:00
2024-06-09 17:15:47 +01:00
if ( sptClientIsOutdated )
2023-07-12 09:19:33 +02:00
{
2024-05-01 10:31:55 -04:00
return Result . FromError (
2025-01-03 15:09:54 -05:00
"Live EFT has recently updated. The SPT team needs to make a new patcher." +
"\n* It's usually made within 24 hours." +
"\n* The patcher is only for turning your EFT files into an older version for SPT to use." +
"\n* This does not mean SPT is being updated to a newer version." ) ;
2023-07-12 09:19:33 +02:00
}
2024-07-10 13:00:52 -04:00
if ( liveClientIsOutdated )
{
2024-07-18 13:57:19 +01:00
return Result . FromError ( "Your live EFT is out of date. Please update it using your Battlestate Games Launcher then run the SPT Installer again" ) ;
2024-07-10 13:00:52 -04:00
}
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02:00
_data . PatchNeeded = patchNeedCheck ;
2024-05-01 10:31:55 -04:00
string status =
2025-01-03 15:09:54 -05:00
$"Current Release: {sptReleaseInfo.ClientVersion} - {(_data.PatchNeeded ? " Patch Available " : " No Patch Needed ")}" ;
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02:00
SetStatus ( null , status ) ;
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02: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
}
}
2023-07-12 09:19:33 +02:00
}