@@ -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}
0 commit comments