@@ -9,9 +9,11 @@ namespace BabylonJS_Installer
99{
1010 class Downloader
1111 {
12- private readonly string url_github = "github.com" ;
13- private readonly string url_download = "https://github.com/BabylonJS/Exporters/releases/download" ;
14- private readonly string url_github_API_releases = "https://api.github.com/repos/BabylonJS/Exporters/releases" ;
12+ private static readonly string Url_github = "github.com" ;
13+ private static readonly string Api_url_github = $ "api.{ Url_github } ";
14+ private static readonly string Url_download = $ "https://{ Url_github } /BabylonJS/Exporters/releases/download";
15+ private static readonly string Url_github_API_releases = $ "https://{ Api_url_github } /repos/BabylonJS/Exporters/releases";
16+
1517 private string software = "" ;
1618 private string version = "" ;
1719 private string installDir = "" ;
@@ -20,7 +22,7 @@ class Downloader
2022
2123 public MainForm form ;
2224
23- public void init ( string software , string version , string installDir , string installLibSubDir )
25+ public async Task UpdateAsync ( string software , string version , string installDir , string installLibSubDir )
2426 {
2527 this . form . goTab ( "" ) ;
2628 this . form . log ( "\n ----- INSTALLING / DOWNLOADING " + software + " v" + version + " EXPORTER -----\n " ) ;
@@ -42,137 +44,104 @@ public void init(string software, string version, string installDir, string inst
4244 }
4345 } ;
4446
45- if ( this . pingSite ( this . url_github ) )
46- {
47- this . form . log ( "Info : Connection to GitHub OK" ) ;
48- if ( this . latestRelease == "" )
49- this . getLatestRelease ( logPostInstall ) ;
50- else
51- if ( this . tryDownload ( this . latestRelease ) ) logPostInstall ( ) ;
52- }
53- }
54-
55- private bool pingSite ( string url_toTest )
56- {
57-
58- var ping = new System . Net . NetworkInformation . Ping ( ) ;
47+ string downloadedFileName = null ;
5948
6049 try
6150 {
62- var result = ping . Send ( url_toTest ) ;
63-
64- if ( result . Status != System . Net . NetworkInformation . IPStatus . Success )
65- {
66- this . form . error ( "Error : Can't reach Github." ) ;
67- return false ;
68- }
69- else
51+ if ( this . latestRelease == "" )
7052 {
71- return true ;
53+ this . form . log ( "Trying to get the last version." ) ;
54+ try
55+ {
56+ if ( ! await TryRetreiveLatestReleaseAsync ( ) )
57+ {
58+ this . form . error ( "Error : Can't find the last release package." ) ;
59+ return ;
60+ }
61+ }
62+ catch
63+ {
64+ this . form . warn ( "Unable to retreive the last version.\n "
65+ + "Please, try in 1 hour. (The API limitation is 60 queries / hour)" ) ;
66+ throw ;
67+ }
7268 }
69+
70+ this . form . log ( "Downloading files : \n "
71+ + Url_download + this . latestRelease ) ;
72+ downloadedFileName = this . DownloadFile ( this . latestRelease ) ;
7373 }
7474 catch ( Exception ex )
7575 {
76- this . form . error (
77- "Can't reach Github.\n "
78- + "Error : \n "
79- + "\" " + ex . Message + "\" "
80- ) ;
81- return false ;
76+ this . form . warn ( "Unable to download the files.\n "
77+ + "Error message : \n "
78+ + "\" " + ex . Message + "\" " ) ;
79+ return ;
8280 }
83- }
8481
85- private async void getLatestRelease ( Action OnSuccess )
86- {
87- this . form . log ( "Trying to get the last version ..." ) ;
82+ this . form . log ( "Download complete.\n "
83+ + "Extracting files ..." ) ;
8884
89- try
85+ if ( ! tryInstallDownloaded ( downloadedFileName ) )
9086 {
91- // TO DO - Parse the JSON in a more beautiful way...
87+ // catch and log are processed into the function.
88+ return ;
89+ }
9290
93- String responseBody = await this . GetJSONBodyRequest ( this . url_github_API_releases ) ;
91+ this . form . log ( " \n ----- " + this . software + " " + downloadedFileName + " EXPORTER UP TO DATE ----- \n " ) ;
9492
95- String lastestReleaseInfos = responseBody . Substring ( responseBody . IndexOf ( "\" prerelease\" :" ) + "\" prerelease\" :" . Length ) ;
96- //Ensure we are on release version
97- if ( lastestReleaseInfos . StartsWith ( "false" ) )
98- {
99- //We parse the array to find the dowload URL
100- this . latestRelease = lastestReleaseInfos . Substring ( lastestReleaseInfos . IndexOf ( "\" browser_download_url\" :" ) + "\" browser_download_url\" : " . Length ) ;
101-
102- // We split, remove & substrings to get only the URL starting with https://github.com and lasting with preRelease version
103- this . latestRelease = this . latestRelease . Split ( '"' ) [ 0 ] ;
104- this . latestRelease = this . latestRelease . Remove ( this . latestRelease . LastIndexOf ( "/" ) ) ;
105- this . latestRelease = this . latestRelease . Substring ( this . latestRelease . LastIndexOf ( "/" ) ) ;
106- this . tryDownload ( this . latestRelease ) ;
107- }
108- else
109- {
110- this . form . error ( "Error : Can't find the last release package." ) ;
111- return ;
112- }
113- }
114- catch ( Exception ex )
93+ this . form . displayInstall ( this . software , this . version ) ;
94+
95+ logPostInstall ( ) ;
96+ }
97+
98+ private async Task < bool > TryRetreiveLatestReleaseAsync ( )
99+ {
100+ this . form . log ( "Trying to get the last version ..." ) ;
101+
102+ // TO DO - Parse the JSON in a more beautiful way...
103+ String responseBody = await this . GetJSONBodyRequest ( Url_github_API_releases ) ;
104+ String lastestReleaseInfos = responseBody . Substring ( responseBody . IndexOf ( "\" prerelease\" :" ) + "\" prerelease\" :" . Length ) ;
105+ //Ensure we are on release version
106+ if ( lastestReleaseInfos . StartsWith ( "false" ) )
115107 {
116- this . form . error (
117- "Can't reach the GitHub API\n "
118- + "Please, try in 1 hour. (The API limitation is 60 queries / hour)\n "
119- + "Error message : \n "
120- + "\" " + ex . Message + "\" "
121- ) ;
122- return ;
108+ //We parse the array to find the dowload URL
109+ this . latestRelease = lastestReleaseInfos . Substring ( lastestReleaseInfos . IndexOf ( "\" browser_download_url\" :" ) + "\" browser_download_url\" : " . Length ) ;
110+
111+ // We split, remove & substrings to get only the URL starting with https://github.com and lasting with preRelease version
112+ this . latestRelease = this . latestRelease . Split ( '"' ) [ 0 ] ;
113+ this . latestRelease = this . latestRelease . Remove ( this . latestRelease . LastIndexOf ( "/" ) ) ;
114+ this . latestRelease = this . latestRelease . Substring ( this . latestRelease . LastIndexOf ( "/" ) ) ;
115+ return true ;
123116 }
124-
125- OnSuccess ( ) ;
117+ return false ;
126118 }
127119
128- private bool tryDownload ( string releaseName )
120+ private string DownloadFile ( string releaseName )
129121 {
130122 var downloadVersion = this . version ;
131123 if ( this . software . Equals ( "Maya" ) && ( this . version . Equals ( "2017" ) || this . version . Equals ( "2018" ) ) )
132124 {
133125 this . form . warn ( "Maya 2017 and 2018 have the same archive, changing version for proper download" ) ;
134126 downloadVersion = "2017-2018" ;
135127 }
136- this . form . log (
137- "Downloading files : \n "
138- + this . url_download + releaseName + "/" + this . software + "_" + downloadVersion + ".zip"
139- ) ;
140128
141129 // Download the zip
142- try
143- {
144- using ( var client = new WebClient ( ) )
145- {
146- client . DownloadFile (
147- this . url_download + releaseName + "/" + this . software + "_" + downloadVersion + ".zip" ,
148- this . software + "_" + downloadVersion + ".zip"
149- ) ;
150- }
151- }
152- catch ( Exception ex )
130+ var srcUrl = Url_download + releaseName + "/" + this . software + "_" + downloadVersion + ".zip" ;
131+ var targetFileName = this . software + "_" + downloadVersion + ".zip" ;
132+ using ( var client = new WebClient ( ) )
153133 {
154- this . form . warn (
155- "Can't download the files.\n "
156- + "Error message : \n "
157- + "\" " + ex . Message + "\" "
158- ) ;
159- return false ;
134+ client . DownloadFile ( srcUrl , targetFileName ) ;
160135 }
136+ return targetFileName ;
137+ }
161138
162- return this . tryInstallDownload ( downloadVersion ) ;
163- }
164-
165- private bool tryInstallDownload ( string downloadVersion )
139+ private bool tryInstallDownloaded ( string downloadVersion )
166140 {
167- this . form . log (
168- "Download complete.\n "
169- + "Extracting files ..."
170- ) ;
171141
172142 try
173143 {
174- String zipFileName = this . software + "_" + downloadVersion + ".zip" ;
175- using ( ZipArchive myZip = ZipFile . OpenRead ( zipFileName ) )
144+ using ( ZipArchive myZip = ZipFile . OpenRead ( downloadVersion ) )
176145 {
177146 foreach ( ZipArchiveEntry entry in myZip . Entries )
178147 {
@@ -202,7 +171,6 @@ private bool tryInstallDownload(string downloadVersion)
202171 try
203172 {
204173 File . Delete ( this . software + "_" + downloadVersion + ".zip" ) ;
205- this . form . log ( "\n ----- " + this . software + " " + downloadVersion + " EXPORTER UP TO DATE ----- \n " ) ;
206174 }
207175 catch ( Exception ex )
208176 {
@@ -217,15 +185,18 @@ private bool tryInstallDownload(string downloadVersion)
217185 try
218186 {
219187 string uninstallScriptPath = this . installDir + "scripts\\ Startup\\ BabylonCleanUp.ms" ;
220- File . Delete ( uninstallScriptPath ) ;
221188 this . form . log ( "\n Removing " + uninstallScriptPath + ".\n " ) ;
189+ File . Delete ( uninstallScriptPath ) ;
222190 }
223191 catch ( Exception ex )
224192 {
225-
193+ this . form . warn (
194+ "Can't delete temporary script.\n "
195+ + "Error message : \n "
196+ + "\" " + ex . Message + "\" "
197+ ) ;
226198 }
227199
228- this . form . displayInstall ( this . software , this . version ) ;
229200 return true ;
230201 }
231202
@@ -234,13 +205,12 @@ public async Task<string> GetJSONBodyRequest(string requestURI)
234205 HttpClient client = new HttpClient ( ) ;
235206 client . DefaultRequestHeaders . Add ( "User-Agent" , "BJS_Installer" ) ;
236207 HttpResponseMessage response = await client . GetAsync ( requestURI ) ;
237-
238208 return await response . Content . ReadAsStringAsync ( ) ;
239209 }
240210
241211 public string GetURLGitHubAPI ( )
242212 {
243- return this . url_github_API_releases ;
213+ return Url_github_API_releases ;
244214 }
245215 }
246216
0 commit comments