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

Commit 37c03f1

Browse files
committed
fix download assistant url parsing, add setting: close after explorer launch
1 parent 3b4e88f commit 37c03f1

File tree

6 files changed

+150
-84
lines changed

6 files changed

+150
-84
lines changed

UnityLauncher/App.config

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
2828
</value>
2929
</setting>
30+
<setting name="closeAfterExplorer" serializeAs="String">
31+
<value>True</value>
32+
</setting>
3033
</UnityLauncher.Properties.Settings>
3134
</userSettings>
3235
</configuration>

UnityLauncher/Form1.Designer.cs

+45-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityLauncher/Form1.cs

+73-52
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public Form1()
2323
}
2424

2525
private void Form1_Load(object sender, EventArgs e)
26+
{
27+
Start();
28+
}
29+
30+
void Start()
2631
{
2732
SetStatus("Initializing..");
2833

@@ -35,13 +40,7 @@ private void Form1_Load(object sender, EventArgs e)
3540
SetStatus("Ready");
3641
}
3742

38-
// update settings window
39-
chkMinimizeToTaskbar.Checked = Properties.Settings.Default.minimizeToTaskbar;
40-
41-
// update installations folder listbox
42-
lstRootFolders.Items.AddRange(Properties.Settings.Default.rootFolders.Cast<string>().ToArray());
43-
// update packages folder listbox
44-
lstPackageFolders.Items.AddRange(Properties.Settings.Default.packageFolders.Cast<string>().ToArray());
43+
LoadSettings();
4544

4645
// scan installed unitys, TODO: could cache results, at least fileinfo's
4746
bool foundedUnitys = ScanUnityInstallations();
@@ -63,13 +62,17 @@ private void Form1_Load(object sender, EventArgs e)
6362
SetStatus("Launching from commandline..");
6463

6564
var pathArg = args[2];
66-
// Console.WriteLine("\nPATH: " + pathArg);
6765
LaunchProject(pathArg);
6866
SetStatus("Ready");
67+
68+
// quit after launch if enabled in settings
69+
if (Properties.Settings.Default.closeAfterExplorer == true)
70+
{
71+
Application.Exit();
72+
}
6973
}
7074
else
7175
{
72-
// Console.WriteLine("Invalid arguments:" + args[1]);
7376
SetStatus("Error> Invalid arguments:" + args[1]);
7477
}
7578

@@ -81,6 +84,18 @@ private void Form1_Load(object sender, EventArgs e)
8184
gridRecent.Select();
8285
}
8386

87+
void LoadSettings()
88+
{
89+
// update settings window
90+
chkMinimizeToTaskbar.Checked = Properties.Settings.Default.minimizeToTaskbar;
91+
chkQuitAfterCommandline.Checked = Properties.Settings.Default.closeAfterExplorer;
92+
93+
// update installations folder listbox
94+
lstRootFolders.Items.AddRange(Properties.Settings.Default.rootFolders.Cast<string>().ToArray());
95+
// update packages folder listbox
96+
lstPackageFolders.Items.AddRange(Properties.Settings.Default.packageFolders.Cast<string>().ToArray());
97+
}
98+
8499
/// <summary>
85100
/// returns true if we have exact version installed
86101
/// </summary>
@@ -339,64 +354,64 @@ void LaunchProject(string pathArg = null)
339354
}
340355
}
341356

357+
// parse unity installer exe from release page
358+
// thanks to https://github.com/softfruit
359+
string GetDownloadUrlForUnityVersion(string releaseUrl)
360+
{
361+
string url = "";
362+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
363+
using (WebClient client = new WebClient())
364+
{
365+
string html = client.DownloadString(releaseUrl);
366+
Regex regex = new Regex(@"(http).+(UnityDownloadAssistant)+[^\s*]*(.exe)");
367+
Match match = regex.Match(html);
368+
url = match.Groups[0].Captures[0].Value;
369+
Console.WriteLine(url);
370+
}
371+
return url;
372+
}
373+
342374
/// <summary>
343375
/// downloads unity installer and launches it
344376
/// </summary>
345377
/// <param name="url"></param>
346378
void DownloadAndRun(string url)
347379
{
348-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
349-
using (WebClient client = new WebClient())
350-
{
351-
string html = client.DownloadString(url);
380+
string exeURL = GetDownloadUrlForUnityVersion(url);
352381

353-
string foundedURL = "";
354-
var allLines = html.Split('\n');
355-
for (int i = 0, length = allLines.Length; i < length; i++)
382+
if (string.IsNullOrEmpty(exeURL) == false)
383+
{
384+
SetStatus("Download installer: " + exeURL);
385+
// download temp file
386+
using (WebClient downloader = new WebClient())
356387
{
357-
if (allLines[i].Contains("UnityDownloadAssistant") && allLines[i].Contains(".exe"))
388+
var f = GetFileNameFromUrl(exeURL);
389+
FileInfo fileInfo = new FileInfo(f);
390+
downloader.DownloadFile(exeURL, f);
391+
if (File.Exists(fileInfo.FullName))
358392
{
359-
var dlURL = allLines[i].Split('\"');
360-
if (dlURL.Length > 1)
393+
SetStatus("Running installer");
394+
try
361395
{
362-
Console.WriteLine(dlURL[1]);
363-
foundedURL = dlURL[1];
364-
break;
396+
Process myProcess = new Process();
397+
myProcess.StartInfo.FileName = fileInfo.FullName;
398+
myProcess.Start();
399+
myProcess.WaitForExit();
365400
}
366-
break;
367-
}
368-
}
369-
370-
if (string.IsNullOrEmpty(foundedURL) == false)
371-
{
372-
// download temp file
373-
using (WebClient downloader = new WebClient())
374-
{
375-
var f = GetFileNameFromUrl(foundedURL);
376-
FileInfo fileInfo = new FileInfo(f);
377-
downloader.DownloadFile(foundedURL, f);
378-
if (File.Exists(fileInfo.FullName))
401+
catch (Exception ex)
379402
{
380-
try
381-
{
382-
Process myProcess = new Process();
383-
myProcess.StartInfo.FileName = fileInfo.FullName;
384-
myProcess.Start();
385-
myProcess.WaitForExit();
386-
}
387-
catch (Exception ex)
388-
{
389-
Console.WriteLine(ex);
390-
}
391-
403+
Console.WriteLine(ex);
404+
SetStatus("Failed running installer");
392405
}
406+
393407
}
394408
}
395-
else // not found
396-
{
397-
Console.WriteLine("Cannot parse exe.. opening website instead");
398-
Process.Start(url);
399-
}
409+
SetStatus("Finished Running installer");
410+
}
411+
else // not found
412+
{
413+
SetStatus("Error> Cannot find installer exe.. opening website instead");
414+
Process.Start(url);
400415
}
401416
}
402417

@@ -407,6 +422,7 @@ void DownloadAndRun(string url)
407422
/// <returns></returns>
408423
string GetFileNameFromUrl(string url)
409424
{
425+
Console.WriteLine(url);
410426
var uri = new Uri(url);
411427
var filename = uri.Segments.Last();
412428
return filename;
@@ -786,5 +802,10 @@ private void btnAddRegister_Click(object sender, EventArgs e)
786802
}
787803
#endregion
788804

805+
private void chkQuitAfterCommandline_CheckedChanged(object sender, EventArgs e)
806+
{
807+
Properties.Settings.Default.closeAfterExplorer = chkQuitAfterCommandline.Checked;
808+
Properties.Settings.Default.Save();
809+
}
789810
}
790811
}

UnityLauncher/Program.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Threading.Tasks;
54
using System.Windows.Forms;
65

@@ -14,9 +13,23 @@ static class Program
1413
[STAThread]
1514
static void Main()
1615
{
16+
/*
17+
// TODO allow only single instance https://stackoverflow.com/a/6486341/5452781
18+
bool result = false;
19+
var mutex = new System.Threading.Mutex(true, "UniqueAppId", out result);
20+
21+
if (result == false)
22+
{
23+
// another instance already running, decide what to do
24+
MessageBox.Show("Another instance is already running.");
25+
return;
26+
}*/
27+
1728
Application.EnableVisualStyles();
1829
Application.SetCompatibleTextRenderingDefault(false);
1930
Application.Run(new Form1());
31+
32+
//GC.KeepAlive(mutex);
2033
}
2134
}
2235
}

UnityLauncher/Properties/Settings.Designer.cs

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityLauncher/Properties/Settings.settings

+3
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
<Value Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
1616
&lt;ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;</Value>
1717
</Setting>
18+
<Setting Name="closeAfterExplorer" Type="System.Boolean" Scope="User">
19+
<Value Profile="(Default)">True</Value>
20+
</Setting>
1821
</Settings>
1922
</SettingsFile>

0 commit comments

Comments
 (0)