Skip to content

Commit 5446b0f

Browse files
authored
Merge pull request #146 from Timboy67678/master
Implemented run as admin and randomize window title option
2 parents 7763364 + 3edc3d1 commit 5446b0f

File tree

8 files changed

+94
-27
lines changed

8 files changed

+94
-27
lines changed

ReClass.NET/Forms/MainForm.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,37 @@ public ClassNode CurrentClassNode
5656
}
5757
}
5858

59+
private void UpdateWindowTitle(string extra = null)
60+
{
61+
var title = $"{(Program.Settings.RandomizeWindowTitle ? Utils.RandomString(Program.GlobalRandom.Next(15, 20)) : Constants.ApplicationName)} ({Constants.Platform})";
62+
if (!string.IsNullOrEmpty(extra))
63+
{
64+
title += $" - {extra}";
65+
}
66+
Text = title;
67+
}
68+
5969
public MainForm()
6070
{
6171
Contract.Ensures(pluginManager != null);
6272
Contract.Ensures(currentProject != null);
6373

6474
InitializeComponent();
65-
66-
Text = $"{Constants.ApplicationName} ({Constants.Platform})";
75+
UpdateWindowTitle();
6776

6877
mainMenuStrip.Renderer = new CustomToolStripProfessionalRenderer(true, true);
6978
toolStrip.Renderer = new CustomToolStripProfessionalRenderer(true, false);
7079

7180
Program.RemoteProcess.ProcessAttached += sender =>
7281
{
7382
var text = $"{sender.UnderlayingProcess.Name} (ID: {sender.UnderlayingProcess.Id.ToString()})";
74-
75-
Text = $"{Constants.ApplicationName} ({Constants.Platform}) - {text}";
7683
processInfoToolStripStatusLabel.Text = text;
84+
UpdateWindowTitle(text);
85+
7786
};
7887
Program.RemoteProcess.ProcessClosed += sender =>
7988
{
80-
Text = $"{Constants.ApplicationName} ({Constants.Platform})";
89+
UpdateWindowTitle();
8190
processInfoToolStripStatusLabel.Text = "No process selected";
8291
};
8392

ReClass.NET/Forms/SettingsForm.Designer.cs

Lines changed: 47 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ReClass.NET/Forms/SettingsForm.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Diagnostics.Contracts;
34
using System.Windows.Forms;
45
using ReClassNET.Extensions;
@@ -43,6 +44,7 @@ public SettingsForm(Settings settings, CppTypeMapping typeMapping)
4344
if (NativeMethods.IsUnix())
4445
{
4546
fileAssociationGroupBox.Enabled = false;
47+
runAsAdminCheckBox.Enabled = false;
4648
}
4749
else
4850
{
@@ -105,6 +107,8 @@ private void SetGeneralBindings()
105107
SetBinding(showSymbolsCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.ShowCommentSymbol));
106108
SetBinding(showStringCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.ShowCommentString));
107109
SetBinding(showPluginInfoCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.ShowCommentPluginInfo));
110+
SetBinding(runAsAdminCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.RunAsAdmin));
111+
SetBinding(randomizeWindowTitleCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.RandomizeWindowTitle));
108112
}
109113

110114
private void SetColorBindings()

ReClass.NET/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Drawing;
34
using System.Globalization;
45
using System.Windows.Forms;
@@ -66,6 +67,12 @@ static void Main(string[] args)
6667
Settings = SettingsSerializer.Load();
6768
Logger = new GuiLogger();
6869

70+
if(!NativeMethods.IsUnix() && Settings.RunAsAdmin && !WinUtil.IsAdministrator)
71+
{
72+
WinUtil.RunElevated(Process.GetCurrentProcess().MainModule.FileName, args.Length > 0 ? string.Join(" ", args) : null);
73+
return;
74+
}
75+
6976
#if !DEBUG
7077
try
7178
{

ReClass.NET/Settings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public class Settings
1212

1313
public bool StayOnTop { get; set; } = false;
1414

15+
public bool RunAsAdmin { get; set; } = false;
16+
17+
public bool RandomizeWindowTitle { get; set; } = false;
18+
1519
// Node Drawing Settings
1620

1721
public bool ShowNodeAddress { get; set; } = true;

ReClass.NET/Util/SettingsSerializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static Settings Load()
3535
{
3636
XElementSerializer.TryRead(general, nameof(settings.LastProcess), e => settings.LastProcess = XElementSerializer.ToString(e));
3737
XElementSerializer.TryRead(general, nameof(settings.StayOnTop), e => settings.StayOnTop = XElementSerializer.ToBool(e));
38+
XElementSerializer.TryRead(general, nameof(settings.RunAsAdmin), e => settings.RunAsAdmin = XElementSerializer.ToBool(e));
39+
XElementSerializer.TryRead(general, nameof(settings.RandomizeWindowTitle), e => settings.RandomizeWindowTitle = XElementSerializer.ToBool(e));
3840
}
3941
var display = root?.Element(XmlDisplayElement);
4042
if (display != null)
@@ -105,7 +107,9 @@ public static void Save(Settings settings)
105107
new XElement(
106108
XmlGeneralElement,
107109
XElementSerializer.ToXml(nameof(settings.LastProcess), settings.LastProcess),
108-
XElementSerializer.ToXml(nameof(settings.StayOnTop), settings.StayOnTop)
110+
XElementSerializer.ToXml(nameof(settings.StayOnTop), settings.StayOnTop),
111+
XElementSerializer.ToXml(nameof(settings.RunAsAdmin), settings.RunAsAdmin),
112+
XElementSerializer.ToXml(nameof(settings.RandomizeWindowTitle), settings.RandomizeWindowTitle)
109113
),
110114
new XElement(
111115
XmlDisplayElement,

ReClass.NET/Util/Util.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.Contracts;
4+
using System.Linq;
45

56
namespace ReClassNET.Util
67
{
@@ -50,5 +51,13 @@ public static void Swap<T>(ref T lhs, ref T rhs)
5051
lhs = rhs;
5152
rhs = temp;
5253
}
54+
55+
//thx again stack overflow https://stackoverflow.com/a/1344242
56+
public static string RandomString(int length)
57+
{
58+
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
59+
return new string(Enumerable.Repeat(chars, length)
60+
.Select(s => s[Program.GlobalRandom.Next(s.Length)]).ToArray());
61+
}
5362
}
5463
}

ReClass.NET/Util/WinUtil.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.Diagnostics.Contracts;
4+
using System.Security.Principal;
45
using Microsoft.Win32;
56

67
namespace ReClassNET.Util
@@ -23,6 +24,9 @@ public static class WinUtil
2324

2425
public static bool IsAtLeastWindows10 { get; }
2526

27+
//from https://stackoverflow.com/a/11660205
28+
public static bool IsAdministrator => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
29+
2630
static WinUtil()
2731
{
2832
var os = Environment.OSVersion;

0 commit comments

Comments
 (0)