|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 |
| -using System.ComponentModel; |
4 |
| -using System.Data; |
5 |
| -using System.Drawing; |
6 |
| -using System.Linq; |
7 |
| -using System.Text; |
8 |
| -using System.Threading.Tasks; |
9 | 3 | using System.Windows.Forms;
|
| 4 | +using Microsoft.Win32; |
| 5 | +using System.IO; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Security.Principal; |
10 | 8 |
|
11 |
| -namespace UnityVersionLauncher |
| 9 | +namespace Invertex.UnityVersionLauncher |
12 | 10 | {
|
13 |
| - public partial class Form1 : Form |
| 11 | + public partial class MainWindow : Form |
14 | 12 | {
|
15 |
| - public Form1() |
| 13 | + public MainWindow() |
16 | 14 | {
|
17 | 15 | InitializeComponent();
|
| 16 | + GetUnityVersions(); |
| 17 | + } |
| 18 | + |
| 19 | + public List<UnityInstallObject> UnityInstalls |
| 20 | + { |
| 21 | + get { return unityInstalls; } |
| 22 | + set { unityInstalls = value; } |
| 23 | + } |
| 24 | + |
| 25 | + public List<UnityInstallObject> unityInstalls = new List<UnityInstallObject>(); |
| 26 | + |
| 27 | + public class UnityInstallObject |
| 28 | + { |
| 29 | + public string version; |
| 30 | + public string displayVersion; |
| 31 | + |
| 32 | + public string DisplayVersion |
| 33 | + { |
| 34 | + get { return displayVersion; } |
| 35 | + } |
| 36 | + |
| 37 | + public string keyPath; |
| 38 | + public string installPath; |
| 39 | + }; |
| 40 | + |
| 41 | + public void GetUnityVersions() |
| 42 | + { |
| 43 | + string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; |
| 44 | + using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey)) |
| 45 | + { |
| 46 | + foreach (string skName in rk.GetSubKeyNames()) |
| 47 | + { |
| 48 | + using (RegistryKey sk = rk.OpenSubKey(skName)) |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + if (sk.Name.Contains("Unity")) |
| 53 | + { |
| 54 | + string installLoc = sk.GetValue("DisplayIcon").ToString(); |
| 55 | + string dispVer = sk.GetValue("DisplayVersion").ToString(); |
| 56 | + string keyPath = sk.Name.Replace(@"HKEY_LOCAL_MACHINE\", ""); |
| 57 | + if (File.Exists(installLoc)) |
| 58 | + { |
| 59 | + UnityInstallObject unityInstall = new UnityInstallObject(); |
| 60 | + unityInstall.installPath = installLoc; |
| 61 | + unityInstall.version = FileVersionInfo.GetVersionInfo(installLoc).ProductVersion; |
| 62 | + unityInstall.displayVersion = dispVer; |
| 63 | + unityInstall.keyPath = keyPath; |
| 64 | + |
| 65 | + CheckIfExistingInstall(unityInstalls, unityInstall); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + DialogResult dialogResult = MessageBox.Show("Registry key for " + dispVer + " has empty install location, registry entry is useless, Delete?", |
| 70 | + "Empty Registry Key Detected", |
| 71 | + MessageBoxButtons.YesNo); |
| 72 | + if (dialogResult == DialogResult.Yes) |
| 73 | + { |
| 74 | + RemoveKey(keyPath); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { } |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + this.UnityInstallList.DataSource = this.UnityInstalls; |
| 86 | + } |
| 87 | + |
| 88 | + bool CheckIfExistingInstall(List<UnityInstallObject> installList, UnityInstallObject nextInstall) |
| 89 | + { |
| 90 | + for(int i = 0; i < installList.Count; i++) |
| 91 | + { |
| 92 | + if(installList[i].installPath.Equals(nextInstall.installPath)) |
| 93 | + { |
| 94 | + int displayVerDiff = String.Compare(installList[i].displayVersion, nextInstall.displayVersion); |
| 95 | + |
| 96 | + if(displayVerDiff < 0) |
| 97 | + { |
| 98 | + RemoveDuplicateVersionPrompt(installList[i], nextInstall); |
| 99 | + installList[i] = nextInstall; |
| 100 | + } |
| 101 | + else if(displayVerDiff > 0) |
| 102 | + { |
| 103 | + RemoveDuplicateVersionPrompt(nextInstall, installList[i]); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + Console.WriteLine(nextInstall.displayVersion + " is duplicate of: " + installList[i].displayVersion); |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + unityInstalls.Add(nextInstall); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + void RemoveDuplicateVersionPrompt(UnityInstallObject older, UnityInstallObject newer) |
| 117 | + { |
| 118 | + DialogResult dialogResult = MessageBox.Show( |
| 119 | + "Registry keys for " + older.displayVersion + " & " + newer.displayVersion + " have matching install locations." + Environment.NewLine + |
| 120 | + "Remove useless " + older.displayVersion + " Registry key?", "Matching install paths detected!", |
| 121 | + MessageBoxButtons.YesNo); |
| 122 | + |
| 123 | + if (dialogResult == DialogResult.Yes) |
| 124 | + { |
| 125 | + RemoveKey(older.keyPath); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public static void RemoveKey(string key) |
| 130 | + { |
| 131 | + if (IsAdministrator() == false) |
| 132 | + { |
| 133 | + // Launch new instance of this program as Admin so we can delete registry key, instace kills itself right after. |
| 134 | + var exeName = Process.GetCurrentProcess().MainModule.FileName; |
| 135 | + ProcessStartInfo startInfo = new ProcessStartInfo(exeName); |
| 136 | + startInfo.Verb = "runas"; |
| 137 | + string argers = " \"" + key + "\"" + "^del"; |
| 138 | + startInfo.Arguments = argers; |
| 139 | + Process.Start(startInfo); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + Registry.LocalMachine.DeleteSubKeyTree(key); |
| 144 | + MessageBox.Show(Environment.NewLine + key, "Deleted Registry Key", MessageBoxButtons.OK); |
| 145 | + } |
| 146 | + |
| 147 | + private static bool IsAdministrator() |
| 148 | + { |
| 149 | + WindowsIdentity identity = WindowsIdentity.GetCurrent(); |
| 150 | + WindowsPrincipal principal = new WindowsPrincipal(identity); |
| 151 | + return principal.IsInRole(WindowsBuiltInRole.Administrator); |
| 152 | + } |
| 153 | + |
| 154 | + private void OptionChosen(object sender, MouseEventArgs e) |
| 155 | + { |
| 156 | + UnityInstallObject unityInstall = (UnityInstallObject)UnityInstallList.SelectedItem; |
| 157 | + Process.Start(unityInstall.installPath); |
| 158 | + Application.Exit(); |
| 159 | + } |
| 160 | + |
| 161 | + private void AuthorLinkClicked(object sender, LinkLabelLinkClickedEventArgs e) |
| 162 | + { |
| 163 | + Process.Start("http://invertex.xyz"); |
18 | 164 | }
|
19 | 165 | }
|
20 | 166 | }
|
0 commit comments