|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Data; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Windows.Forms; |
| 12 | + |
| 13 | +namespace UnityLauncher |
| 14 | +{ |
| 15 | + public partial class Form1 : Form |
| 16 | + { |
| 17 | + private string pathArg = ""; |
| 18 | + |
| 19 | + public Form1() |
| 20 | + { |
| 21 | + InitializeComponent(); |
| 22 | + } |
| 23 | + |
| 24 | + private void Form1_Load(object sender, EventArgs e) |
| 25 | + { |
| 26 | + // TODO: setup window to scan for unity installations (give parent folder) |
| 27 | + |
| 28 | + // check if any arguments (that means, it should parse something) |
| 29 | + string[] args = Environment.GetCommandLineArgs(); |
| 30 | + |
| 31 | + // sample path |
| 32 | + // "D:\download\Swarm-master(1)\Swarm-master" |
| 33 | + |
| 34 | + |
| 35 | + if (args != null && args.Length > 1) |
| 36 | + { |
| 37 | + pathArg = args[1]; |
| 38 | + |
| 39 | + Console.WriteLine("\nPATH: " + pathArg); |
| 40 | + Console.WriteLine(""); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + |
| 45 | + Console.WriteLine("Running in setup-mode"); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // check if path is unity project folder |
| 50 | + if (Directory.Exists(pathArg) == true) |
| 51 | + { |
| 52 | + // validate folder |
| 53 | + if (Directory.Exists(Path.Combine(pathArg, "Assets"))) |
| 54 | + { |
| 55 | + // this looks like unity project folder, check version |
| 56 | + if (Directory.Exists(Path.Combine(pathArg, "ProjectSettings"))) |
| 57 | + { |
| 58 | + var versionPath = Path.Combine(pathArg, "ProjectSettings", "ProjectVersion.txt"); |
| 59 | + if (File.Exists(versionPath)) |
| 60 | + { |
| 61 | + var version = GetProjectVersion(versionPath); |
| 62 | + Console.WriteLine("Detected project version: " + version); |
| 63 | + |
| 64 | + bool installed = CheckInstalled("Unity " + version); |
| 65 | + if (installed == true) |
| 66 | + { |
| 67 | + // TODO: open? |
| 68 | + Console.WriteLine("Opening unity version " + version); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + throw new Exception("Unity version " + version + " is not installed!"); |
| 73 | + } |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + throw new FileNotFoundException("Cannot find ProjectVersion.txt for :" + pathArg); |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + throw new DirectoryNotFoundException("Cannot find ProjectSettings/ at :" + pathArg); |
| 83 | + } |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + throw new DirectoryNotFoundException("No Assets folder founded in: " + pathArg); |
| 88 | + } |
| 89 | + } |
| 90 | + else // given path doesnt exists, strange |
| 91 | + { |
| 92 | + throw new DirectoryNotFoundException("Invalid Path:" + pathArg); |
| 93 | + } |
| 94 | + |
| 95 | + // check if unity with that version is installed |
| 96 | + |
| 97 | + |
| 98 | + // if not installed, offer download |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + // read and parse project settings file |
| 104 | + string GetProjectVersion(string path) |
| 105 | + { |
| 106 | + var v = "unknown"; |
| 107 | + var d = File.ReadAllLines(path); |
| 108 | + if (d != null && d.Length > 0) |
| 109 | + { |
| 110 | + var dd = d[0]; |
| 111 | + // check first line |
| 112 | + if (dd.Contains("m_EditorVersion")) |
| 113 | + { |
| 114 | + var t = dd.Split(new string[] { "m_EditorVersion: " }, StringSplitOptions.None); |
| 115 | + if (t != null && t.Length > 0) |
| 116 | + { |
| 117 | + v = t[1].Trim(); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + throw new InvalidDataException("invalid version data:" + d); |
| 122 | + } |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + throw new InvalidDataException("Cannot find m_EditorVersion:" + dd); |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + throw new InvalidDataException("invalid projectversion data:" + d.ToString()); |
| 132 | + } |
| 133 | + return v; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + // check installed apps from uninstall list in registry https://stackoverflow.com/a/16392220/5452781 |
| 138 | + // but unity doesnt write there |
| 139 | + public static bool CheckInstalled(string appName) |
| 140 | + { |
| 141 | + string displayName; |
| 142 | + |
| 143 | + string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; |
| 144 | + RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey); |
| 145 | + if (key != null) |
| 146 | + { |
| 147 | + foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName))) |
| 148 | + { |
| 149 | + displayName = subkey.GetValue("DisplayName") as string; |
| 150 | + if (displayName != null && displayName.Contains(appName)) |
| 151 | + { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + key.Close(); |
| 156 | + } |
| 157 | + |
| 158 | + registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; |
| 159 | + key = Registry.LocalMachine.OpenSubKey(registryKey); |
| 160 | + if (key != null) |
| 161 | + { |
| 162 | + foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName))) |
| 163 | + { |
| 164 | + displayName = subkey.GetValue("DisplayName") as string; |
| 165 | + if (displayName != null && displayName.Contains(appName)) |
| 166 | + { |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | + key.Close(); |
| 171 | + } |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + // set basefolder of all unity installations |
| 176 | + private void btn_setinstallfolder_Click(object sender, EventArgs e) |
| 177 | + { |
| 178 | + Dictionary<string, string> unityList = new Dictionary<string, string>(); |
| 179 | + |
| 180 | + var d = folderBrowserDialog1.ShowDialog(); |
| 181 | + var root = folderBrowserDialog1.SelectedPath; |
| 182 | + |
| 183 | + if (String.IsNullOrWhiteSpace(root) == false) |
| 184 | + { |
| 185 | + // parse all folders here, and search for unity editor files |
| 186 | + var directories = Directory.GetDirectories(root); |
| 187 | + for (int i = 0, length = directories.Length; i < length; i++) |
| 188 | + { |
| 189 | + var uninstallExe = Path.Combine(directories[i], "Editor", "Uninstall.exe"); |
| 190 | + if (File.Exists(uninstallExe)) |
| 191 | + { |
| 192 | + var unityExe = Path.Combine(directories[i], "Editor", "Unity.exe"); |
| 193 | + var unityVersion = GetFileVersion(uninstallExe); |
| 194 | + // TODO: check if exists, warn |
| 195 | + unityList.Add(unityVersion, unityExe); |
| 196 | + lst_unitys.Items.Add(unityVersion + " (" + unityExe + ")"); |
| 197 | + } // have uninstaller |
| 198 | + } // got folders |
| 199 | + } // didnt select anything |
| 200 | + |
| 201 | + lst_unitys.Sorted = true; |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + private string GetFileVersion(string path) |
| 206 | + { |
| 207 | + // todo check path |
| 208 | + FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(path); |
| 209 | + return fvi.ProductName.Replace("(64-bit)", "").Trim(); |
| 210 | + } |
| 211 | + |
| 212 | + } |
| 213 | +} |
0 commit comments