2023-05-11 23:11:39 -04:00
using Microsoft.Win32 ;
2023-08-28 11:05:00 -04:00
using Serilog ;
2023-05-11 23:11:39 -04:00
using SPTInstaller.Models ;
2023-07-29 14:26:55 -04:00
using System.Diagnostics ;
2023-05-11 23:11:39 -04:00
using System.Threading.Tasks ;
2023-07-12 09:19:33 +02:00
namespace SPTInstaller.Installer_Tasks.PreChecks ;
public class NetFramework472PreCheck : PreCheckBase
2023-05-11 23:11:39 -04:00
{
2024-02-05 10:09:23 -05:00
public NetFramework472PreCheck ( ) : base ( ".Net Framework 4.7.2" , true )
2023-05-11 23:11:39 -04:00
{
2023-07-12 09:19:33 +02:00
}
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
public override async Task < PreCheckResult > CheckOperation ( )
2023-07-12 09:19:33 +02:00
{
try
2023-05-11 23:11:39 -04:00
{
2023-07-12 09:19:33 +02:00
var minRequiredVersion = new Version ( "4.7.2" ) ;
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02:00
var key = Registry . LocalMachine . OpenSubKey ( "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full" ) ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
var failedButtonText = "Download .Net Framework 4.7.2" ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
var failedButtonAction = ( ) = >
{
Process . Start ( new ProcessStartInfo
{
FileName = "cmd.exe" ,
UseShellExecute = true ,
WindowStyle = ProcessWindowStyle . Hidden ,
2024-05-01 10:31:55 -04:00
ArgumentList =
{
"/C" , "start" ,
"https://dotnet.microsoft.com/download/dotnet-framework/thank-you/net472-developer-pack-offline-installer"
}
2023-07-29 14:26:55 -04:00
} ) ;
} ;
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02:00
if ( key = = null )
{
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromError (
"Could not find .Net Framework on system.\n\nThis is required to play SPT, but you can install it later and shouldn't affect the SPT install process." ,
failedButtonText , failedButtonAction ) ;
2023-05-11 23:11:39 -04:00
}
2024-05-01 10:31:55 -04:00
2023-07-12 09:19:33 +02:00
var value = key . GetValue ( "Version" ) ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
if ( value = = null | | value is not string versionString )
2023-05-11 23:11:39 -04:00
{
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromError (
"Something went wrong. This precheck failed for an unknown reason. :(" ) ;
2023-07-29 14:26:55 -04:00
}
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
var installedVersion = new Version ( versionString ) ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
if ( installedVersion < minRequiredVersion )
{
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromError (
$".Net Framework {versionString} is installed, but {minRequiredVersion} or higher is required.\n\nYou can install it later and shouldn't affect the SPT install process." ,
failedButtonText , failedButtonAction ) ;
2023-05-11 23:11:39 -04:00
}
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromSuccess (
$".Net Framework {minRequiredVersion} or higher is installed.\n\nInstalled Version: {installedVersion}" ) ;
2023-07-12 09:19:33 +02:00
}
catch ( Exception ex )
{
2023-08-28 11:05:00 -04:00
Log . Error ( ex , $"PreCheck::{Name}::Exception" ) ;
2023-07-29 14:26:55 -04:00
return PreCheckResult . FromException ( ex ) ;
2023-05-11 23:11:39 -04:00
}
}
2023-07-12 09:19:33 +02:00
}