Skip to content

Implemented run as admin and randomize window title option #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions ReClass.NET/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,37 @@ public ClassNode CurrentClassNode
}
}

private void UpdateWindowTitle(string extra = null)
{
var title = $"{(Program.Settings.RandomizeWindowTitle ? Utils.RandomString(Program.GlobalRandom.Next(15, 20)) : Constants.ApplicationName)} ({Constants.Platform})";
if (!string.IsNullOrEmpty(extra))
{
title += $" - {extra}";
}
Text = title;
}

public MainForm()
{
Contract.Ensures(pluginManager != null);
Contract.Ensures(currentProject != null);

InitializeComponent();

Text = $"{Constants.ApplicationName} ({Constants.Platform})";
UpdateWindowTitle();

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

Program.RemoteProcess.ProcessAttached += sender =>
{
var text = $"{sender.UnderlayingProcess.Name} (ID: {sender.UnderlayingProcess.Id.ToString()})";

Text = $"{Constants.ApplicationName} ({Constants.Platform}) - {text}";
processInfoToolStripStatusLabel.Text = text;
UpdateWindowTitle(text);

};
Program.RemoteProcess.ProcessClosed += sender =>
{
Text = $"{Constants.ApplicationName} ({Constants.Platform})";
UpdateWindowTitle();
processInfoToolStripStatusLabel.Text = "No process selected";
};

Expand Down
68 changes: 47 additions & 21 deletions ReClass.NET/Forms/SettingsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ReClass.NET/Forms/SettingsForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Windows.Forms;
using ReClassNET.Extensions;
Expand Down Expand Up @@ -43,6 +44,7 @@ public SettingsForm(Settings settings, CppTypeMapping typeMapping)
if (NativeMethods.IsUnix())
{
fileAssociationGroupBox.Enabled = false;
runAsAdminCheckBox.Enabled = false;
}
else
{
Expand Down Expand Up @@ -105,6 +107,8 @@ private void SetGeneralBindings()
SetBinding(showSymbolsCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.ShowCommentSymbol));
SetBinding(showStringCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.ShowCommentString));
SetBinding(showPluginInfoCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.ShowCommentPluginInfo));
SetBinding(runAsAdminCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.RunAsAdmin));
SetBinding(randomizeWindowTitleCheckBox, nameof(CheckBox.Checked), settings, nameof(Settings.RandomizeWindowTitle));
}

private void SetColorBindings()
Expand Down
7 changes: 7 additions & 0 deletions ReClass.NET/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
Expand Down Expand Up @@ -66,6 +67,12 @@ static void Main(string[] args)
Settings = SettingsSerializer.Load();
Logger = new GuiLogger();

if(!NativeMethods.IsUnix() && Settings.RunAsAdmin && !WinUtil.IsAdministrator)
{
WinUtil.RunElevated(Process.GetCurrentProcess().MainModule.FileName, args.Length > 0 ? string.Join(" ", args) : null);
return;
}

#if !DEBUG
try
{
Expand Down
4 changes: 4 additions & 0 deletions ReClass.NET/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class Settings

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

public bool RunAsAdmin { get; set; } = false;

public bool RandomizeWindowTitle { get; set; } = false;

// Node Drawing Settings

public bool ShowNodeAddress { get; set; } = true;
Expand Down
6 changes: 5 additions & 1 deletion ReClass.NET/Util/SettingsSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static Settings Load()
{
XElementSerializer.TryRead(general, nameof(settings.LastProcess), e => settings.LastProcess = XElementSerializer.ToString(e));
XElementSerializer.TryRead(general, nameof(settings.StayOnTop), e => settings.StayOnTop = XElementSerializer.ToBool(e));
XElementSerializer.TryRead(general, nameof(settings.RunAsAdmin), e => settings.RunAsAdmin = XElementSerializer.ToBool(e));
XElementSerializer.TryRead(general, nameof(settings.RandomizeWindowTitle), e => settings.RandomizeWindowTitle = XElementSerializer.ToBool(e));
}
var display = root?.Element(XmlDisplayElement);
if (display != null)
Expand Down Expand Up @@ -105,7 +107,9 @@ public static void Save(Settings settings)
new XElement(
XmlGeneralElement,
XElementSerializer.ToXml(nameof(settings.LastProcess), settings.LastProcess),
XElementSerializer.ToXml(nameof(settings.StayOnTop), settings.StayOnTop)
XElementSerializer.ToXml(nameof(settings.StayOnTop), settings.StayOnTop),
XElementSerializer.ToXml(nameof(settings.RunAsAdmin), settings.RunAsAdmin),
XElementSerializer.ToXml(nameof(settings.RandomizeWindowTitle), settings.RandomizeWindowTitle)
),
new XElement(
XmlDisplayElement,
Expand Down
9 changes: 9 additions & 0 deletions ReClass.NET/Util/Util.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace ReClassNET.Util
{
Expand Down Expand Up @@ -50,5 +51,13 @@ public static void Swap<T>(ref T lhs, ref T rhs)
lhs = rhs;
rhs = temp;
}

//thx again stack overflow https://stackoverflow.com/a/1344242
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[Program.GlobalRandom.Next(s.Length)]).ToArray());
}
}
}
4 changes: 4 additions & 0 deletions ReClass.NET/Util/WinUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Security.Principal;
using Microsoft.Win32;

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

public static bool IsAtLeastWindows10 { get; }

//from https://stackoverflow.com/a/11660205
public static bool IsAdministrator => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);

static WinUtil()
{
var os = Environment.OSVersion;
Expand Down