2024-03-23 18:44:27 -04:00
using Serilog ;
using SPTInstaller.Models ;
using System.Diagnostics ;
using System.Text.RegularExpressions ;
using System.Threading.Tasks ;
using SPTInstaller.Helpers ;
namespace SPTInstaller.Installer_Tasks.PreChecks ;
2024-05-01 10:31:55 -04:00
[ Obsolete (
"No longer needed, but keeping around for now just in case. Can be removed from code after 7/1/2024 if no issues are found" ) ]
2024-03-23 18:44:27 -04:00
public class NetCore6PreCheck : PreCheckBase
{
public NetCore6PreCheck ( ) : base ( ".Net Core 6 Desktop Runtime" , true )
{
}
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
public override async Task < PreCheckResult > CheckOperation ( )
{
var minRequiredVersion = new Version ( "6.0.0" ) ;
string [ ] output ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
var failedButtonText = "Download .Net Core 6 Desktop Runtime" ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -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/en-us/download/dotnet/thank-you/runtime-desktop-6.0.4-windows-x64-installer"
}
2024-03-23 18:44:27 -04:00
} ) ;
} ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
try
{
2024-04-04 19:45:53 -07:00
var programFiles = Environment . ExpandEnvironmentVariables ( "%ProgramW6432%" ) ;
2024-05-01 10:31:55 -04:00
var result =
ProcessHelper . RunAndReadProcessOutputs ( $@"{programFiles}\dotnet\dotnet.exe" , "--list-runtimes" ) ;
2024-03-23 18:44:27 -04:00
if ( ! result . Succeeded )
{
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromError ( result . Message + "\n\nYou most likely don't have .net 6 installed" ,
failedButtonText , failedButtonAction ) ;
2024-03-23 18:44:27 -04:00
}
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
output = result . StdOut . Split ( "\r\n" ) ;
}
catch ( Exception ex )
{
Log . Error ( ex , $"PreCheck::{Name}::Exception" ) ;
return PreCheckResult . FromException ( ex ) ;
}
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
var highestFoundVersion = new Version ( "0.0.0" ) ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
foreach ( var lineVersion in output )
{
var regex = Regex . Match ( lineVersion , @"Microsoft.WindowsDesktop.App (\d\.\d\.\d)" ) ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
if ( ! regex . Success | | regex . Groups . Count < 1 )
continue ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
var stringVersion = regex . Groups [ 1 ] . Value ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
var foundVersion = new Version ( stringVersion ) ;
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
if ( foundVersion > = minRequiredVersion )
{
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromSuccess (
$".Net Core {minRequiredVersion} Desktop Runtime or higher is installed.\n\nInstalled Version: {foundVersion}" ) ;
2024-03-23 18:44:27 -04:00
}
2024-05-01 10:31:55 -04:00
2024-03-23 18:44:27 -04:00
highestFoundVersion = foundVersion > highestFoundVersion ? foundVersion : highestFoundVersion ;
}
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromError (
$".Net Core Desktop Runtime version {minRequiredVersion} or higher is required.\n\nHighest Version Found: {(highestFoundVersion > new Version(" 0.0 . 0 ") ? highestFoundVersion : " Not Found ")}\n\nThis is required to play SPT, but you can install it later if and shouldn't affect the SPT install process." ,
failedButtonText , failedButtonAction ) ;
2024-03-23 18:44:27 -04:00
}
}