|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.IO.Compression; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | + |
| 7 | +namespace BabylonJS_Installer |
| 8 | +{ |
| 9 | + class Downloader |
| 10 | + { |
| 11 | + private readonly string url_github = "github.com"; |
| 12 | + private readonly string url_download = "https://github.com/BabylonJS/Exporters/releases/download"; |
| 13 | + private readonly string url_github_API_releases = "https://api.github.com/repos/BabylonJS/Exporters/releases"; |
| 14 | + private string software = ""; |
| 15 | + private string version = ""; |
| 16 | + private string installDir = ""; |
| 17 | + private string latestRelease = ""; |
| 18 | + |
| 19 | + public MainForm form; |
| 20 | + |
| 21 | + public void init(string software, string version, string installDir) |
| 22 | + { |
| 23 | + this.form.goTab(""); |
| 24 | + this.form.log("\n----- INSTALLING / DOWNLOADING " + software + " v" + version + " EXPORTER -----\n"); |
| 25 | + |
| 26 | + this.software = software; |
| 27 | + this.version = version; |
| 28 | + this.installDir = installDir; |
| 29 | + |
| 30 | + if (this.pingSite(this.url_github)) |
| 31 | + { |
| 32 | + this.form.log("Info : Connection to GitHub OK"); |
| 33 | + if (this.latestRelease == "") |
| 34 | + this.getLatestRelease(); |
| 35 | + else |
| 36 | + this.download(this.latestRelease); |
| 37 | + |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private bool pingSite(string url_toTest) |
| 42 | + { |
| 43 | + |
| 44 | + var ping = new System.Net.NetworkInformation.Ping(); |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + var result = ping.Send(url_toTest); |
| 49 | + |
| 50 | + if (result.Status != System.Net.NetworkInformation.IPStatus.Success) |
| 51 | + { |
| 52 | + this.form.log("Error : Can't reach Github."); |
| 53 | + return false; |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + this.form.log( |
| 63 | + "Can't reach Github.\n" |
| 64 | + + "Error : \n" |
| 65 | + + "\"" + ex.Message + "\"" |
| 66 | + ); |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private async void getLatestRelease() |
| 72 | + { |
| 73 | + this.form.log("Trying to get the last version ..."); |
| 74 | + |
| 75 | + try |
| 76 | + { |
| 77 | + // Old way with Octokit (nuget package) |
| 78 | + //var ghClient = new GitHubClient(new ProductHeaderValue("BJS_Installer")); |
| 79 | + //var lastRelease = await ghClient.Repository.Release.GetAll("BabylonJS", "Exporters"); |
| 80 | + //if (lastRelease[0].Prerelease == true) |
| 81 | + //{ |
| 82 | + //this.latestRelease = lastRelease[0].HtmlUrl.Substring(lastRelease[0].HtmlUrl.LastIndexOf("/")); |
| 83 | + //this.form.log("Last release package is : " + this.latestRelease + "\n"); |
| 84 | + //this.download(this.latestRelease); |
| 85 | + //} |
| 86 | + |
| 87 | + HttpClient client = new HttpClient(); |
| 88 | + client.DefaultRequestHeaders.Add("User-Agent", "BJS_Installer"); |
| 89 | + HttpResponseMessage response = await client.GetAsync(this.url_github_API_releases); |
| 90 | + |
| 91 | + // TO DO - Parse the JSON in a more beautiful way... |
| 92 | + String responseBody = await response.Content.ReadAsStringAsync(); |
| 93 | + String lastestReleaseInfos = responseBody.Substring(responseBody.IndexOf("\"prerelease\":") + "\"prerelease\":".Length); |
| 94 | + //Ensure we are on Rrerelease version |
| 95 | + if (lastestReleaseInfos.StartsWith("true")) |
| 96 | + { |
| 97 | + //We parse the array to find the dowload URL |
| 98 | + this.latestRelease = lastestReleaseInfos.Substring(lastestReleaseInfos.IndexOf("\"browser_download_url\":") + "\"browser_download_url\": ".Length); |
| 99 | + |
| 100 | + // We split, remove & substrings to get only the URL starting with https://github.com and lasting with preRelease version |
| 101 | + this.latestRelease = this.latestRelease.Split('"')[0]; |
| 102 | + this.latestRelease = this.latestRelease.Remove(this.latestRelease.LastIndexOf("/")); |
| 103 | + this.latestRelease = this.latestRelease.Substring(this.latestRelease.LastIndexOf("/")); |
| 104 | + this.download(this.latestRelease); |
| 105 | + } |
| 106 | + else |
| 107 | + this.form.log("Error : Can't find the last release package."); |
| 108 | + } |
| 109 | + catch(Exception ex) |
| 110 | + { |
| 111 | + this.form.log( |
| 112 | + "Can't reach the GitHub API\n" |
| 113 | + + "Please, try in 1 hour. (The API limitation is 60 queries / hour\n" |
| 114 | + + "Error message : \n" |
| 115 | + + "\"" + ex.Message + "\"" |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void download(string releaseName) |
| 121 | + { |
| 122 | + this.form.log( |
| 123 | + "Downloading files : \n" |
| 124 | + + this.url_download + releaseName + "/" + this.software + "_" + this.version + ".zip" |
| 125 | + ); |
| 126 | + |
| 127 | + // Download the zip |
| 128 | + try |
| 129 | + { |
| 130 | + using (var client = new WebClient()) |
| 131 | + { |
| 132 | + client.DownloadFile( |
| 133 | + this.url_download + releaseName + "/" + this.software + "_" + this.version + ".zip", |
| 134 | + this.software + "_" + this.version + ".zip" |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + catch (Exception ex) |
| 139 | + { |
| 140 | + this.form.log( |
| 141 | + "Can't download the files.\n" |
| 142 | + + "Error message : \n" |
| 143 | + + "\"" + ex.Message + "\"" |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + this.downloadComplete(); |
| 148 | + } |
| 149 | + |
| 150 | + private void downloadComplete() |
| 151 | + { |
| 152 | + this.form.log( |
| 153 | + "Download complete.\n" |
| 154 | + + "Extracting files ..." |
| 155 | + ); |
| 156 | + |
| 157 | + try |
| 158 | + { |
| 159 | + // This way can't rewrite files |
| 160 | + //ZipFile.ExtractToDirectory( |
| 161 | + // this.software + "_" + this.version + ".zip", |
| 162 | + // this.installDir |
| 163 | + // ); |
| 164 | + |
| 165 | + String zipFileName = this.software + "_" + this.version + ".zip"; |
| 166 | + using (ZipArchive myZip = ZipFile.OpenRead(zipFileName)) |
| 167 | + { |
| 168 | + foreach (ZipArchiveEntry entry in myZip.Entries) |
| 169 | + { |
| 170 | + entry.ExtractToFile(this.installDir + "/" + entry.Name, true); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + catch(Exception ex) |
| 175 | + { |
| 176 | + this.form.log( |
| 177 | + "Can't extract the files.\n" |
| 178 | + + "If you're not, please try to run this tool in ADMINISTRATOR MODE. It's necessary to extract the files in \"Program Files\" folder (or other protected folders).\n" |
| 179 | + + "Error message : \n" |
| 180 | + + "\"" + ex.Message + "\"" |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + this.form.log( |
| 185 | + "Extraction complete.\n" |
| 186 | + + "Deleting temporary files ..." |
| 187 | + ); |
| 188 | + |
| 189 | + try |
| 190 | + { |
| 191 | + File.Delete(this.software + "_" + this.version + ".zip"); |
| 192 | + } |
| 193 | + catch (Exception ex) |
| 194 | + { |
| 195 | + this.form.log( |
| 196 | + "Can't delete temporary files.\n" |
| 197 | + + "Error message : \n" |
| 198 | + + "\"" + ex.Message + "\"" |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + this.form.log("\n----- " + this.software + " " + this.version + " EXPORTER UP TO DATE ----- \n"); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments