Skip to content
This repository has been archived by the owner on Dec 17, 2022. It is now read-only.

Commit

Permalink
Requests Admin when necessary. No longer shows console when unnecessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirMangler committed Nov 7, 2019
1 parent 2379d2a commit 284987d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
6 changes: 6 additions & 0 deletions PrimeHack Updater/CfgManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public void loadCfg()
}
} else
{
if (!Updater.WriteAccess(".\\"))
Updater.restartAsAdmin();

File.Create("./updater.cfg");
}
}
Expand Down Expand Up @@ -74,6 +77,9 @@ public bool isMainBranch()

public void saveCfg()
{
if (!Updater.WriteAccess(".\\"))
Updater.restartAsAdmin();

if (!File.Exists("./updater.cfg"))
loadCfg();

Expand Down
2 changes: 1 addition & 1 deletion PrimeHack Updater/PrimeHack Updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BC203460-914A-46A0-90E3-97897438CE4E}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<RootNamespace>PrimeHack_Updater</RootNamespace>
<AssemblyName>PrimeHack Updater</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
Expand Down
4 changes: 2 additions & 2 deletions PrimeHack Updater/Properties/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
Expand Down
43 changes: 38 additions & 5 deletions PrimeHack Updater/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -52,6 +56,8 @@ static void Main(string[] args)
}
else
{
AllocConsole();

while (true)
{
Process[] runningProcesses = Process.GetProcessesByName("Dolphin");
Expand All @@ -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];
Expand All @@ -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()
{
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 284987d

Please sign in to comment.