Skip to content

Commit

Permalink
Prompt to restart the app with elevated access. Auto-save the directo…
Browse files Browse the repository at this point in the history
…ry to config.
  • Loading branch information
SandeMC committed Mar 29, 2024
1 parent ba1d077 commit 9f1090a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions GTAIVDowngradeUtility/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<appSettings>
<add key="ultimate-asi-loader" value=""/>
<add key="fusionfix" value=""/>
<add key="directory" value=""/>
</appSettings>
</configuration>
94 changes: 91 additions & 3 deletions GTAIVDowngradeUtility/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GTAIVDowngradeUtilityWPF.Common;
using GTAIVDowngradeUtilityWPF.Functions;
using GTAIVDowngradeUtilityWPF.Functions;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using NLog;
Expand All @@ -11,14 +10,14 @@
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

// hi here, i'm an awful coder, so please clean up for me if it really bothers you

Expand Down Expand Up @@ -48,6 +47,56 @@ public MainWindow()
Logger.Info(" Initializing the main window...");
InitializeComponent();
Logger.Info(" Main window initialized!");

var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
directory = settings["directory"].Value;

if (directory != "")
{
if (AppVersionGrabber.GetFileVersion($"{directory}\\GTAIV.exe").StartsWith("1, 0"))
{
Logger.Debug(" Folder contains a retail exe already.");
MessageBox.Show("Your game exe is already downgraded, proceeding to use the tool may produce unexpected results and corrupt the game.");
}
else { Logger.Debug(" Folder contains an exe of Steam Version."); }

if (Directory.Exists($"{directory}\\backup")) { backupexists = true; }

if (directory.Contains("steamapps")) { achievementscheckbox.IsEnabled = true; }
else if (directory.Contains("Program Files"))
{
if (IsRunningAsAdministrator() == false)
{
MessageBox.Show("Your game is located in Program Files, which requires elevated permissions to be modified.\n\nPressing 'Ok' will restart the app with elevated permissions.");
try
{
var proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Path.Combine(proc.WorkingDirectory, "GTAIVDowngradeUtilityWPF.exe");
proc.Verb = "runas";
Process.Start(proc);
}
catch (Exception error)
{
Logger.Error(error, "Failed to elevate.");
throw;
}

Application.Current.Shutdown();
}
}

directorytxt.Text = "Game Directory:";
directorytxt.FontWeight = FontWeights.Normal;
directorytxt.TextDecorations = null;
tipsnote.TextDecorations = TextDecorations.Underline;
gamedirectory.Text = directory;
options.IsEnabled = true;
version.IsEnabled = true;
buttons.IsEnabled = true;
}
}

private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
Expand Down Expand Up @@ -133,6 +182,15 @@ public bool IsGFWLInstalled()
return false;
}

public static bool IsRunningAsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}

private void full_Click(object sender, RoutedEventArgs e)
{
if (fullcheckbox.IsChecked == true)
Expand Down Expand Up @@ -311,9 +369,39 @@ private void Button_Click(object sender, RoutedEventArgs e)
}
else { Logger.Debug(" Folder contains an exe of Steam Version."); }

var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;

settings["directory"].Value = dialog.FileName;
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);

if (Directory.Exists($"{dialog.FileName}\\backup")) { backupexists = true; }

if (dialog.FileName.Contains("steamapps")) { achievementscheckbox.IsEnabled = true; }
else if (dialog.FileName.Contains("Program Files"))
{
if (IsRunningAsAdministrator() == false)
{
MessageBox.Show("Your game is located in Program Files, which requires elevated permissions to be modified.\n\nPressing 'Ok' will restart the app with elevated permissions.");
try
{
var proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Path.Combine(proc.WorkingDirectory, "GTAIVDowngradeUtilityWPF.exe");
proc.Verb = "runas";
Process.Start(proc);
}
catch (Exception error)
{
Logger.Error(error, "Failed to elevate.");
throw;
}

Application.Current.Shutdown();
}
}

directorytxt.Text = "Game Directory:";
directorytxt.FontWeight = FontWeights.Normal;
Expand Down

0 comments on commit 9f1090a

Please sign in to comment.