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

Commit

Permalink
fix download assistant url parsing, add setting: close after explorer…
Browse files Browse the repository at this point in the history
… launch
  • Loading branch information
unitycoder committed Sep 4, 2017
1 parent 3b4e88f commit 37c03f1
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 84 deletions.
3 changes: 3 additions & 0 deletions UnityLauncher/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
</value>
</setting>
<setting name="closeAfterExplorer" serializeAs="String">
<value>True</value>
</setting>
</UnityLauncher.Properties.Settings>
</userSettings>
</configuration>
76 changes: 45 additions & 31 deletions UnityLauncher/Form1.Designer.cs

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

125 changes: 73 additions & 52 deletions UnityLauncher/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public Form1()
}

private void Form1_Load(object sender, EventArgs e)
{
Start();
}

void Start()
{
SetStatus("Initializing..");

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

// update settings window
chkMinimizeToTaskbar.Checked = Properties.Settings.Default.minimizeToTaskbar;

// update installations folder listbox
lstRootFolders.Items.AddRange(Properties.Settings.Default.rootFolders.Cast<string>().ToArray());
// update packages folder listbox
lstPackageFolders.Items.AddRange(Properties.Settings.Default.packageFolders.Cast<string>().ToArray());
LoadSettings();

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

var pathArg = args[2];
// Console.WriteLine("\nPATH: " + pathArg);
LaunchProject(pathArg);
SetStatus("Ready");

// quit after launch if enabled in settings
if (Properties.Settings.Default.closeAfterExplorer == true)
{
Application.Exit();
}
}
else
{
// Console.WriteLine("Invalid arguments:" + args[1]);
SetStatus("Error> Invalid arguments:" + args[1]);
}

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

void LoadSettings()
{
// update settings window
chkMinimizeToTaskbar.Checked = Properties.Settings.Default.minimizeToTaskbar;
chkQuitAfterCommandline.Checked = Properties.Settings.Default.closeAfterExplorer;

// update installations folder listbox
lstRootFolders.Items.AddRange(Properties.Settings.Default.rootFolders.Cast<string>().ToArray());
// update packages folder listbox
lstPackageFolders.Items.AddRange(Properties.Settings.Default.packageFolders.Cast<string>().ToArray());
}

/// <summary>
/// returns true if we have exact version installed
/// </summary>
Expand Down Expand Up @@ -339,64 +354,64 @@ void LaunchProject(string pathArg = null)
}
}

// parse unity installer exe from release page
// thanks to https://github.com/softfruit
string GetDownloadUrlForUnityVersion(string releaseUrl)
{
string url = "";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (WebClient client = new WebClient())
{
string html = client.DownloadString(releaseUrl);
Regex regex = new Regex(@"(http).+(UnityDownloadAssistant)+[^\s*]*(.exe)");
Match match = regex.Match(html);
url = match.Groups[0].Captures[0].Value;
Console.WriteLine(url);
}
return url;
}

/// <summary>
/// downloads unity installer and launches it
/// </summary>
/// <param name="url"></param>
void DownloadAndRun(string url)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (WebClient client = new WebClient())
{
string html = client.DownloadString(url);
string exeURL = GetDownloadUrlForUnityVersion(url);

string foundedURL = "";
var allLines = html.Split('\n');
for (int i = 0, length = allLines.Length; i < length; i++)
if (string.IsNullOrEmpty(exeURL) == false)
{
SetStatus("Download installer: " + exeURL);
// download temp file
using (WebClient downloader = new WebClient())
{
if (allLines[i].Contains("UnityDownloadAssistant") && allLines[i].Contains(".exe"))
var f = GetFileNameFromUrl(exeURL);
FileInfo fileInfo = new FileInfo(f);
downloader.DownloadFile(exeURL, f);
if (File.Exists(fileInfo.FullName))
{
var dlURL = allLines[i].Split('\"');
if (dlURL.Length > 1)
SetStatus("Running installer");
try
{
Console.WriteLine(dlURL[1]);
foundedURL = dlURL[1];
break;
Process myProcess = new Process();
myProcess.StartInfo.FileName = fileInfo.FullName;
myProcess.Start();
myProcess.WaitForExit();
}
break;
}
}

if (string.IsNullOrEmpty(foundedURL) == false)
{
// download temp file
using (WebClient downloader = new WebClient())
{
var f = GetFileNameFromUrl(foundedURL);
FileInfo fileInfo = new FileInfo(f);
downloader.DownloadFile(foundedURL, f);
if (File.Exists(fileInfo.FullName))
catch (Exception ex)
{
try
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = fileInfo.FullName;
myProcess.Start();
myProcess.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

Console.WriteLine(ex);
SetStatus("Failed running installer");
}

}
}
else // not found
{
Console.WriteLine("Cannot parse exe.. opening website instead");
Process.Start(url);
}
SetStatus("Finished Running installer");
}
else // not found
{
SetStatus("Error> Cannot find installer exe.. opening website instead");
Process.Start(url);
}
}

Expand All @@ -407,6 +422,7 @@ void DownloadAndRun(string url)
/// <returns></returns>
string GetFileNameFromUrl(string url)
{
Console.WriteLine(url);
var uri = new Uri(url);
var filename = uri.Segments.Last();
return filename;
Expand Down Expand Up @@ -786,5 +802,10 @@ private void btnAddRegister_Click(object sender, EventArgs e)
}
#endregion

private void chkQuitAfterCommandline_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.closeAfterExplorer = chkQuitAfterCommandline.Checked;
Properties.Settings.Default.Save();
}
}
}
15 changes: 14 additions & 1 deletion UnityLauncher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -14,9 +13,23 @@ static class Program
[STAThread]
static void Main()
{
/*
// TODO allow only single instance https://stackoverflow.com/a/6486341/5452781
bool result = false;
var mutex = new System.Threading.Mutex(true, "UniqueAppId", out result);
if (result == false)
{
// another instance already running, decide what to do
MessageBox.Show("Another instance is already running.");
return;
}*/

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

//GC.KeepAlive(mutex);
}
}
}
12 changes: 12 additions & 0 deletions UnityLauncher/Properties/Settings.Designer.cs

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

3 changes: 3 additions & 0 deletions UnityLauncher/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
<Value Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;</Value>
</Setting>
<Setting Name="closeAfterExplorer" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>

0 comments on commit 37c03f1

Please sign in to comment.