Skip to content

Commit

Permalink
Try to run as admin if we don't have access to update CM
Browse files Browse the repository at this point in the history
  • Loading branch information
Top-Cat committed Jul 23, 2024
1 parent 073a979 commit f68f6fd
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
2 changes: 1 addition & 1 deletion OSX/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<key>CFBundleIdentifier</key>
<string>at.topc.cm.launcher</string>
<key>CFBundleShortVersionString</key>
<string>1.13</string>
<string>1.14</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Shared/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public static class Config
{
public const int APP_VERSION = 13;
public const int APP_VERSION = 14;
public const int PATCH_SKIP_LIMIT = 12;
public const string CDN_URL = "https://cm.topc.at";
}
4 changes: 2 additions & 2 deletions Windows/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.13.0.0")]
[assembly: AssemblyFileVersion("1.13.0.0")]
[assembly: AssemblyVersion("1.14.0.0")]
[assembly: AssemblyFileVersion("1.14.0.0")]
42 changes: 28 additions & 14 deletions Windows/src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
Expand Down Expand Up @@ -68,27 +69,40 @@ private static void Main(string[] args)
{
using (SentrySdk.Init("https://76dcf1f2484f4839a78b3713420b5147@o462013.ingest.sentry.io/5556322"))
{
using (new Mutex(true, "CMLauncher", out var createdNew))
var identity = WindowsIdentity.GetCurrent();
SentrySdk.ConfigureScope(scope =>
{
if (!createdNew) return;
scope.User = new User
{
Username = identity.Name
};
});

var identity = WindowsIdentity.GetCurrent();
SentrySdk.ConfigureScope(scope =>
var isAdmin = WindowsSpecific.IsAdministrator();
if (isAdmin)
{
LaunchApp(args, true);
}
else
{
using (new Mutex(true, "CMLauncher", out var createdNew))
{
scope.User = new User
{
Username = identity.Name
};
});
if (!createdNew) return;

SetCurrentProcessExplicitAppUserModelID(CmExeName);
UpdatePinnedTaskBarElements();
SetCurrentProcessExplicitAppUserModelID(CmExeName);
UpdatePinnedTaskBarElements();

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Updater(args));
LaunchApp(args, false);
}
}
}
}

private static void LaunchApp(string[] args, bool isAdmin)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Updater(args, isAdmin));
}
}
}
32 changes: 20 additions & 12 deletions Windows/src/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ public partial class Updater : Form
private readonly SynchronizationContext synchronizationContext;
public static FormWindowState OriginalWindowState;

private static bool FilesLocked(IPlatformSpecific specific)
private static bool FilesLocked(IPlatformSpecific specific, bool isAdmin)
{
var file = Path.Combine(specific.GetDownloadFolder(), "chromapper", "ChroMapper.exe");
var folder = Path.Combine(specific.GetDownloadFolder(), "chromapper");
var file = Path.Combine(folder, "ChroMapper.exe");
try
{
if (!File.Exists(folder))
{
Directory.CreateDirectory(folder);
}

if (File.Exists(file))
{
using (var stream = new FileStream(file, FileMode.Open, FileAccess.Write, FileShare.None))
Expand All @@ -23,8 +29,9 @@ private static bool FilesLocked(IPlatformSpecific specific)
}
}
}
catch (Exception)
catch (Exception e)
{
if (isAdmin) MessageBox.Show(e.Message);
return true;
}

Expand All @@ -38,21 +45,22 @@ protected override void OnLoad(EventArgs e)
base.OnLoad(e);
}

public Updater(string[] args)
public Updater(string[] args, bool isAdmin)
{
InitializeComponent();
synchronizationContext = SynchronizationContext.Current;

var specific = new WindowsSpecific(this, args);
int tries = 0;
while (tries++ < 3)

if (!FilesLocked(specific, isAdmin))
{
if (!FilesLocked(specific))
{
new Main(specific);
return;
}
Thread.Sleep(1000 * tries);
new Main(specific);
return;
}

if (!isAdmin)
{
specific.StartAsAdmin();
}

new Thread(() =>
Expand Down
32 changes: 32 additions & 0 deletions Windows/src/WindowsSpecific.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
using CM_Launcher;
using SimpleJSON;
Expand Down Expand Up @@ -75,6 +76,13 @@ public void UpdateProgress(float progress)

public void Exit()
{
// Don't run CM as admin, return to unprivileged
if (IsAdministrator())
{
Application.Exit();
return;
}

var cmWindowStyle = ProcessWindowStyle.Normal;
if (Updater.OriginalWindowState == FormWindowState.Maximized)
{
Expand Down Expand Up @@ -110,6 +118,7 @@ public void Restart(string tmpFile)
// Run us
var startInfo = new ProcessStartInfo(AppDomain.CurrentDomain.FriendlyName)
{
Arguments = string.Join(" ", _args),
WorkingDirectory = GetDownloadFolder()
};

Expand All @@ -118,6 +127,29 @@ public void Restart(string tmpFile)
Application.Exit();
}

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

public void StartAsAdmin()
{
var startInfo = new ProcessStartInfo(AppDomain.CurrentDomain.FriendlyName)
{
WorkingDirectory = GetDownloadFolder(),
UseShellExecute = true,
Verb = "runas"
};

var process = Process.Start(startInfo);
if (process == null) return;

process.WaitForExit();
process.Close();
}

public void CleanupUpdate()
{
try
Expand Down

0 comments on commit f68f6fd

Please sign in to comment.