Skip to content
This repository was archived by the owner on May 19, 2021. It is now read-only.

Commit b813f00

Browse files
committed
start unity, check exact hit
1 parent 0907942 commit b813f00

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

UnityLauncher/Form1.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ public partial class Form1 : Form
1616
{
1717
private string pathArg = "";
1818

19+
Dictionary<string, string> unityList = new Dictionary<string, string>();
20+
21+
1922
public Form1()
2023
{
2124
InitializeComponent();
2225
}
2326

2427
private void Form1_Load(object sender, EventArgs e)
2528
{
29+
// force scan
30+
btn_setinstallfolder_Click(null, null);
31+
2632
// TODO: setup window to scan for unity installations (give parent folder)
2733

2834
// check if any arguments (that means, it should parse something)
@@ -61,11 +67,30 @@ private void Form1_Load(object sender, EventArgs e)
6167
var version = GetProjectVersion(versionPath);
6268
Console.WriteLine("Detected project version: " + version);
6369

64-
bool installed = CheckInstalled("Unity " + version);
70+
bool installed = OpenWithSuitableVersion(version);
6571
if (installed == true)
6672
{
6773
// TODO: open?
6874
Console.WriteLine("Opening unity version " + version);
75+
76+
try
77+
{
78+
Process myProcess = new Process();
79+
80+
var cmd = "\"" + unityList[version] + "\"";
81+
var pars = " -projectPath " + "\"" + pathArg + "\"";
82+
83+
Console.WriteLine("execute: " + cmd);
84+
85+
myProcess.StartInfo.FileName = cmd;
86+
myProcess.StartInfo.Arguments = pars;
87+
myProcess.Start();
88+
}
89+
catch (Exception ex)
90+
{
91+
Console.WriteLine(ex);
92+
}
93+
6994
}
7095
else
7196
{
@@ -99,6 +124,17 @@ private void Form1_Load(object sender, EventArgs e)
99124

100125
}
101126

127+
bool OpenWithSuitableVersion(string version)
128+
{
129+
// check if got exact hit
130+
Console.WriteLine("checking: '" + version + "'");
131+
132+
var installedExact = unityList.ContainsKey(version);
133+
Console.WriteLine("have exact:" + installedExact);
134+
135+
return installedExact;
136+
}
137+
102138

103139
// read and parse project settings file
104140
string GetProjectVersion(string path)
@@ -134,51 +170,14 @@ string GetProjectVersion(string path)
134170
}
135171

136172

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-
175173
// set basefolder of all unity installations
176174
private void btn_setinstallfolder_Click(object sender, EventArgs e)
177175
{
178-
Dictionary<string, string> unityList = new Dictionary<string, string>();
176+
//var d = folderBrowserDialog1.ShowDialog();
177+
//var root = folderBrowserDialog1.SelectedPath;
179178

180-
var d = folderBrowserDialog1.ShowDialog();
181-
var root = folderBrowserDialog1.SelectedPath;
179+
// override
180+
var root = "D:/Program Files/";
182181

183182
if (String.IsNullOrWhiteSpace(root) == false)
184183
{
@@ -190,10 +189,11 @@ private void btn_setinstallfolder_Click(object sender, EventArgs e)
190189
if (File.Exists(uninstallExe))
191190
{
192191
var unityExe = Path.Combine(directories[i], "Editor", "Unity.exe");
193-
var unityVersion = GetFileVersion(uninstallExe);
192+
var unityVersion = GetFileVersion(uninstallExe).Replace("Unity", "").Trim();
194193
// TODO: check if exists, warn
195194
unityList.Add(unityVersion, unityExe);
196195
lst_unitys.Items.Add(unityVersion + " (" + unityExe + ")");
196+
//Console.WriteLine(unityVersion);
197197
} // have uninstaller
198198
} // got folders
199199
} // didnt select anything

0 commit comments

Comments
 (0)