From 284987daf8cd8530870f69cf02bff3af06e763b8 Mon Sep 17 00:00:00 2001 From: SirMangler Date: Thu, 7 Nov 2019 17:19:02 +0000 Subject: [PATCH] Requests Admin when necessary. No longer shows console when unnecessary. --- PrimeHack Updater/CfgManager.cs | 6 +++ PrimeHack Updater/PrimeHack Updater.csproj | 2 +- PrimeHack Updater/Properties/app.manifest | 4 +- PrimeHack Updater/Updater.cs | 43 +++++++++++++++++++--- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/PrimeHack Updater/CfgManager.cs b/PrimeHack Updater/CfgManager.cs index 782f4f3..31d7f8c 100644 --- a/PrimeHack Updater/CfgManager.cs +++ b/PrimeHack Updater/CfgManager.cs @@ -44,6 +44,9 @@ public void loadCfg() } } else { + if (!Updater.WriteAccess(".\\")) + Updater.restartAsAdmin(); + File.Create("./updater.cfg"); } } @@ -74,6 +77,9 @@ public bool isMainBranch() public void saveCfg() { + if (!Updater.WriteAccess(".\\")) + Updater.restartAsAdmin(); + if (!File.Exists("./updater.cfg")) loadCfg(); diff --git a/PrimeHack Updater/PrimeHack Updater.csproj b/PrimeHack Updater/PrimeHack Updater.csproj index a5320cd..adda939 100644 --- a/PrimeHack Updater/PrimeHack Updater.csproj +++ b/PrimeHack Updater/PrimeHack Updater.csproj @@ -7,7 +7,7 @@ Debug AnyCPU {BC203460-914A-46A0-90E3-97897438CE4E} - Exe + WinExe PrimeHack_Updater PrimeHack Updater v4.5.2 diff --git a/PrimeHack Updater/Properties/app.manifest b/PrimeHack Updater/Properties/app.manifest index 13ea300..2f8605f 100644 --- a/PrimeHack Updater/Properties/app.manifest +++ b/PrimeHack Updater/Properties/app.manifest @@ -8,7 +8,7 @@ If you want to change the Windows User Account Control level replace the requestedExecutionLevel node with one of the following. - + @@ -16,7 +16,7 @@ Remove this element if your application requires this virtualization for backwards compatibility. --> - + diff --git a/PrimeHack Updater/Updater.cs b/PrimeHack Updater/Updater.cs index 4e9fa08..d3dae5a 100644 --- a/PrimeHack Updater/Updater.cs +++ b/PrimeHack Updater/Updater.cs @@ -6,24 +6,28 @@ using System.Net; using System.Linq; using System.Windows.Forms; -using System.Text; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Security.AccessControl; using System.Security.Principal; +using System.Reflection; +using System.ComponentModel; +using System.Runtime.InteropServices; namespace PrimeHack_Updater { class Updater { - static string sysversion = "1.5.4"; + static string sysversion = "1.5.5"; static CfgManager cfg = new CfgManager(); + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool AllocConsole(); + [STAThread] static void Main(string[] args) { - Console.WriteLine("Checking for latest updates."); - string html = VersionCheck.getJSONInfo(@"https://api.github.com/repos/SirMangler/PrimeHack-Updater/releases/latest"); string remoteversion = VersionCheck.getVersion(html); @@ -52,6 +56,8 @@ static void Main(string[] args) } else { + AllocConsole(); + while (true) { Process[] runningProcesses = Process.GetProcessesByName("Dolphin"); @@ -67,6 +73,9 @@ static void Main(string[] args) } } + if (!WriteAccess(".\\")) + restartAsAdmin(); + dynamic j = JObject.Parse(html); JArray ja = j.assets; dynamic assets = ja[0]; @@ -80,6 +89,30 @@ static void Main(string[] args) runPrimeHack(args); } + public static void restartAsAdmin() + { + WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); + bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator); + if (!hasAdministrativeRight) + { + string fileName = Assembly.GetExecutingAssembly().Location; + ProcessStartInfo processInfo = new ProcessStartInfo(); + processInfo.Verb = "runas"; + processInfo.FileName = fileName; + + try + { + Process.Start(processInfo); + } + catch (Win32Exception) + { + + } + + System.Environment.Exit(1); + } + } + static string quickpath = null; public static void isoSelection() { @@ -342,7 +375,7 @@ public static bool WriteAccess(string folderName) public static void downloadLatest(string url) { - Console.WriteLine("Downloading: " + url); + Console.WriteLine("New Update!\nDownloading: " + url); using (var client = new WebClient()) { client.DownloadFile(url, Path.GetTempPath() + "\\PrimeHackRelease.zip");