75 lines
2.7 KiB
C#
Raw Normal View History

using Serilog;
using SPTInstaller.Models;
2023-05-11 23:11:39 -04:00
using System.Diagnostics;
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
namespace SPTInstaller.Installer_Tasks.PreChecks;
2024-03-07 15:28:09 -05:00
public class Net8PreCheck : PreCheckBase
2023-05-11 23:11:39 -04:00
{
2024-03-07 15:28:09 -05:00
public Net8PreCheck() : base(".Net 8 Desktop Runtime", true)
2023-05-11 23:11:39 -04:00
{
}
2023-05-11 23:11:39 -04:00
2023-07-29 14:26:55 -04:00
public override async Task<PreCheckResult> CheckOperation()
{
2024-03-07 15:28:09 -05:00
var minRequiredVersion = new Version("8.0.0");
string[] output;
2023-05-11 23:11:39 -04:00
2024-03-07 15:28:09 -05:00
var failedButtonText = "Download .Net 8 Desktop Runtime";
2023-07-29 14:26:55 -04:00
var failedButtonAction = () =>
{
Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden,
2024-03-07 15:28:09 -05:00
ArgumentList = { "/C", "start", "https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-desktop-8.0.2-windows-x64-installer" }
2023-07-29 14:26:55 -04:00
});
};
try
{
var programFiles = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
var result = ProcessHelper.RunAndReadProcessOutputs($@"{programFiles}\dotnet\dotnet.exe", "--list-runtimes");
2023-05-11 23:11:39 -04:00
2023-11-22 09:32:18 -05:00
if (!result.Succeeded)
{
2024-03-07 15:28:09 -05:00
return PreCheckResult.FromError(result.Message + "\n\nYou most likely don't have .net 8 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");
}
catch (Exception ex)
{
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-29 14:26:55 -04:00
var highestFoundVersion = new Version("0.0.0");
foreach (var lineVersion in output)
{
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
var stringVersion = regex.Groups[1].Value;
2023-05-11 23:11:39 -04:00
var foundVersion = new Version(stringVersion);
2023-07-29 14:26:55 -04:00
if (foundVersion >= minRequiredVersion)
{
2024-03-07 15:28:09 -05:00
return PreCheckResult.FromSuccess($".Net {minRequiredVersion} Desktop Runtime or higher is installed.\n\nInstalled Version: {foundVersion}");
2023-05-11 23:11:39 -04:00
}
highestFoundVersion = foundVersion > highestFoundVersion ? foundVersion : highestFoundVersion;
2023-05-11 23:11:39 -04:00
}
2024-03-07 15:28:09 -05:00
return PreCheckResult.FromError($".Net 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", failedButtonText, failedButtonAction);
2023-05-11 23:11:39 -04:00
}
}