Skip to content

Commit

Permalink
Add .NET 8 installation feature with admin privileges
Browse files Browse the repository at this point in the history
Added a new command line argument `-installDotNetAdmin` to handle the installation of .NET 8 with administrative privileges. Introduced the `InstallDotNet` method in `UpdateApp.cs` to manage the installation process, including checking for admin rights and prompting for elevation. Created a new WPF window `DotNetInstallerWindow` in `DotNetInstallerWindow.xaml` and its code-behind file to provide a UI for the installation process, including multiple pages for user guidance. Updated `App.xaml.cs` to call `InstallDotNet` during initialization and handle the new argument. Added necessary using directives for security, process management, and UI components.
  • Loading branch information
BoiHanny committed Aug 22, 2024
1 parent aa9d7d7 commit 54a656b
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vrcosc-magicchatbox/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ protected override async void OnStartup(StartupEventArgs e)
loadingWindow.UpdateProgress("Rolling back and clearing the slate. Fresh start!", 50);
await Task.Run(() => updater.ClearBackUp());
break;
case "-installDotNetAdmin":
await updater.InstallDotNet(requiresAdmin: true);
Shutdown();
return;
default:
loadingWindow.Hide();
Logging.WriteException(new Exception($"Invalid command line argument '{e.Args[0]}'"), MSGBox: true, exitapp: true);
Expand All @@ -77,6 +81,8 @@ protected override async void OnStartup(StartupEventArgs e)
// Close the loading window once initialization is complete
loadingWindow.UpdateProgress("Rolling out the red carpet... Here comes the UI!", 100);
loadingWindow.Close();

await updater.InstallDotNet();
}

// Handle first-chance exceptions (before they are thrown)
Expand Down
59 changes: 59 additions & 0 deletions vrcosc-magicchatbox/Classes/DataAndSecurity/UpdateApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using vrcosc_magicchatbox.UI.Dialogs;
using vrcosc_magicchatbox.ViewModels;

namespace vrcosc_magicchatbox.Classes.DataAndSecurity
Expand Down Expand Up @@ -336,7 +338,64 @@ private void CopyDirectory(DirectoryInfo source, DirectoryInfo target)
}
}

public async Task InstallDotNet(bool requiresAdmin = false)
{
var installerWindow = new DotNetInstallerWindow();
installerWindow.Show();

if (requiresAdmin && !IsAdministrator())
{
ElevateAndRestartDotNetInstaller();
return;
}

if (installerWindow.IsDotNet8Installed())
{
installerWindow.ShowConfirmationPage();
}
else
{
installerWindow.ShowAskInstallPage();
var userDecision = await installerWindow.WaitForUserDecisionAsync();

if (userDecision == DotNetInstallerWindow.UserDecision.Install)
{
await installerWindow.InstallDotNet8Async();
}
else
{
installerWindow.Close();
}
}
}

private bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

private void ElevateAndRestartDotNetInstaller()
{
var startInfo = new ProcessStartInfo
{
FileName = Assembly.GetExecutingAssembly().Location,
Arguments = "-installDotNetAdmin",
Verb = "runas", // Run with elevated permissions
UseShellExecute = true
};

try
{
Process.Start(startInfo);
Application.Current.Shutdown();
}
catch (Exception ex)
{
MessageBox.Show($"Failed to restart as admin: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private async Task DownloadAndExtractUpdate(string zipPath)
{
Expand Down
221 changes: 221 additions & 0 deletions vrcosc-magicchatbox/UI/Dialogs/DotNetInstallerWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<Window
x:Class="vrcosc_magicchatbox.UI.Dialogs.DotNetInstallerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:vrcosc_magicchatbox.UI.Dialogs"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title=".NET 8 Installation"
Width="700"
Height="300"
Background="#41375B"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">

<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="55"
CornerRadius="0"
GlassFrameThickness="1"
ResizeBorderThickness="8"
UseAeroCaptionButtons="False" />
</WindowChrome.WindowChrome>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>

<!-- Header Area -->
<Grid x:Name="Drag_area" Grid.Row="0">
<Grid.Background>
<LinearGradientBrush StartPoint="0,1" EndPoint="0.5,1">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5" />
<SkewTransform AngleX="-10" CenterX="0.5" CenterY="0.5" />
<RotateTransform CenterX="0.5" CenterY="0.5" />
<TranslateTransform />
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Offset="1" Color="#FF1A0842" />
<GradientStop Color="#FF240E54" />
<GradientStop Offset="0.539" Color="#FF630FA0" />
</LinearGradientBrush>
</Grid.Background>
<StackPanel Orientation="Horizontal">
<Image Margin="2" Source="/Img/MagicOSC_icon.png" />
<TextBlock
Margin="6,1,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Albert Sans Thin"
FontSize="14"
Foreground="White"
Text=".NET 8 Setup">
<TextBlock.Effect>
<DropShadowEffect
BlurRadius="15"
Direction="315"
Opacity="1"
ShadowDepth="0"
Color="Black" />
</TextBlock.Effect>
</TextBlock>
</StackPanel>
</Grid>

<!-- Main Content Area -->
<Grid Grid.Row="1">
<!-- First Page: Checking for .NET 8 -->
<Grid x:Name="CheckDotNetPage">
<TextBlock
Margin="25"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontFamily="Comfortaa Light"
FontSize="20"
Foreground="White"
Text="Checking if .NET 8 is installed..."
TextAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="1"
ShadowDepth="0"
Color="#251153" />
</TextBlock.Effect>
</TextBlock>
</Grid>

<!-- Second Page: Ask to Install .NET 8 -->
<Grid x:Name="AskInstallPage" Visibility="Hidden">
<TextBlock
Margin="25"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontFamily="Comfortaa Light"
FontSize="20"
Foreground="White"
Text=".NET 8 is not installed on your system."
TextAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="1"
ShadowDepth="0"
Color="#251153" />
</TextBlock.Effect>
</TextBlock>
<TextBlock
Margin="25,60,25,0"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontFamily="Comfortaa Light"
FontSize="16"
Foreground="LightYellow"
Text="Would you like to install .NET 8 now?"
TextAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="1"
ShadowDepth="0"
Color="#251153" />
</TextBlock.Effect>
</TextBlock>
</Grid>

<!-- Third Page: Installing .NET 8 -->
<Grid x:Name="InstallDotNetPage" Visibility="Hidden">
<TextBlock
Margin="25"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontFamily="Comfortaa Light"
FontSize="20"
Foreground="White"
Text="Installing .NET 8 in the background..."
TextAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="1"
ShadowDepth="0"
Color="#251153" />
</TextBlock.Effect>
</TextBlock>

<ProgressBar
Width="600"
Height="30"
Margin="0,60,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Top"
IsIndeterminate="True"
Foreground="YellowGreen" />
</Grid>

<!-- Fourth Page: Confirmation -->
<Grid x:Name="ConfirmDotNetPage" Visibility="Hidden">
<TextBlock
Margin="25"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontFamily="Comfortaa Light"
FontSize="20"
Foreground="White"
Text=".NET 8 is installed and ready for use!"
TextAlignment="Center">
<TextBlock.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="1"
ShadowDepth="0"
Color="#251153" />
</TextBlock.Effect>
</TextBlock>

<Button
Width="250"
Height="50"
Margin="0,140,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Click="CloseWindow_Click"
Style="{StaticResource Status_Button_style}">
<TextBlock FontSize="20" Text="Close" />
</Button>
</Grid>
</Grid>

<!-- Footer with Navigation Buttons -->
<Grid Grid.Row="2" HorizontalAlignment="Center" Margin="10,0">
<StackPanel Orientation="Horizontal">
<Button
x:Name="CancelButton"
Width="100"
Height="40"
Margin="10,0"
Click="CancelButton_Click"
Visibility="Hidden"
Style="{StaticResource Status_Button_style}">
<TextBlock FontSize="16" Text="Cancel" />
</Button>
<Button
x:Name="NextButton"
Width="100"
Height="40"
Margin="10,0"
Click="NextButton_Click"
Visibility="Hidden"
Style="{StaticResource Status_Button_style}">
<TextBlock FontSize="16" Text="Next" />
</Button>
</StackPanel>
</Grid>
</Grid>
</Window>
Loading

0 comments on commit 54a656b

Please sign in to comment.