2023-08-28 11:05:00 -04:00
using Serilog ;
using SPTInstaller.Models ;
2023-05-11 23:11:39 -04:00
using System.Diagnostics ;
2023-08-28 11:05:00 -04:00
using System.Text.RegularExpressions ;
2023-05-11 23:11:39 -04:00
using System.Threading.Tasks ;
2023-11-22 09:32:18 -05:00
using SPTInstaller.Helpers ;
2023-05-11 23:11:39 -04:00
2023-07-12 09:19:33 +02:00
namespace SPTInstaller.Installer_Tasks.PreChecks ;
public class NetCore6PreCheck : PreCheckBase
2023-05-11 23:11:39 -04:00
{
2023-07-12 09:19:33 +02:00
public NetCore6PreCheck ( ) : base ( ".Net Core 6 Desktop Runtime" , false )
2023-05-11 23:11:39 -04:00
{
2023-07-12 09:19:33 +02:00
}
2023-05-11 23:11:39 -04:00
2023-07-29 14:26:55 -04:00
public override async Task < PreCheckResult > CheckOperation ( )
2023-07-12 09:19:33 +02:00
{
var minRequiredVersion = new Version ( "6.0.0" ) ;
string [ ] output ;
2023-05-11 23:11:39 -04:00
2023-07-29 14:26:55 -04:00
var failedButtonText = "Download .Net Core 6 Desktop Runtime" ;
var failedButtonAction = ( ) = >
{
Process . Start ( new ProcessStartInfo
{
FileName = "cmd.exe" ,
UseShellExecute = true ,
WindowStyle = ProcessWindowStyle . Hidden ,
ArgumentList = { "/C" , "start" , "https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-desktop-6.0.4-windows-x64-installer" }
} ) ;
} ;
2023-07-12 09:19:33 +02:00
try
{
2023-11-22 09:32:18 -05:00
var result = ProcessHelper . RunAndReadProcessOutputs ( "dotnet" , "--list-runtimes" ) ;
2023-05-11 23:11:39 -04:00
2023-11-22 09:32:18 -05:00
if ( ! result . Succeeded )
{
2024-01-08 21:29:59 -05:00
return PreCheckResult . FromError ( result . Message + "\n\nYou most likely don't have .net 6 installed" , failedButtonText , failedButtonAction ) ;
2023-11-22 09:32:18 -05:00
}
2023-05-11 23:11:39 -04:00
2023-11-22 09:32:18 -05:00
output = result . StdOut . Split ( "\r\n" ) ;
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-07-12 09:19:33 +02:00
}
2023-05-11 23:11:39 -04:00
2023-07-29 14:26:55 -04:00
var highestFoundVersion = new Version ( "0.0.0" ) ;
2023-07-12 09:19:33 +02:00
foreach ( var lineVersion in output )
{
2023-08-28 11:05:00 -04:00
var regex = Regex . Match ( lineVersion , @"Microsoft.WindowsDesktop.App (\d\.\d\.\d)" ) ;
if ( ! regex . Success | | regex . Groups . Count < 1 )
continue ;
2023-05-11 23:11:39 -04:00
2023-08-28 11:05:00 -04:00
var stringVersion = regex . Groups [ 1 ] . Value ;
2023-05-11 23:11:39 -04:00
2023-08-28 11:05:00 -04:00
var foundVersion = new Version ( stringVersion ) ;
2023-07-29 14:26:55 -04:00
2023-08-28 11:05:00 -04:00
if ( foundVersion > = minRequiredVersion )
{
return PreCheckResult . FromSuccess ( $".Net Core {minRequiredVersion} Desktop Runtime or higher is installed.\n\nInstalled Version: {foundVersion}" ) ;
2023-05-11 23:11:39 -04:00
}
2023-08-28 11:05:00 -04:00
highestFoundVersion = foundVersion > highestFoundVersion ? foundVersion : highestFoundVersion ;
2023-05-11 23:11:39 -04:00
}
2023-07-12 09:19:33 +02:00
2023-07-29 14:26: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 ) ;
2023-05-11 23:11:39 -04:00
}
2023-07-12 09:19:33 +02:00
}