/// <summary> /// Checks for a new update Async. /// </summary> public static async void CheckForUpdateAsync() { try { await Task.Run(() => { // Use WebClient to download the latest version string using (Web = new WebClient()) { // Simulate some headers, Github throws a fit otherwise Web.Headers["User-Agent"] = "BF2GameSpyRedirector v" + Program.Version; Web.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Web.Headers["Accept-Language"] = "en-US,en;q=0.8"; Web.Proxy = null; // Disable proxy because this can cause slowdown on some machines // Download file string json = Web.DownloadString(Url); // Use our Json.Net library to convert our API string into an object List <GitHubRelease> Releases = JsonConvert.DeserializeObject <List <GitHubRelease> >(json) .Where(x => x.PreRelease == false && x.Draft == false && x.Assets.Count > 0) .OrderByDescending(x => x.Published).ToList(); // Parse version if (Releases?.Count > 0) { Version.TryParse(Releases[0].TagName, out NewVersion); } } }); } catch (Exception e) { TraceLog.TraceWarning("Error occured while trying to fetch the new release version: " + e.Message); NewVersion = Program.Version; } // Fire Check Completed Event CheckCompleted(NewVersion, EventArgs.Empty); }
private async void ProgramUpdater_CheckCompleted(object sender, EventArgs e) { if (ProgramUpdater.UpdateAvailable) { // Show overlay first, which provides the smokey (Modal) background using (ModalOverlay overlay = new ModalOverlay(this, 0.3)) { // Show overlay overlay.Show(this); // Make sure a mod is selected DialogResult r = MetroMessageBox.Show(overlay, "An Update for this program is avaiable for download (" + ProgramUpdater.NewVersion + ")." + Environment.NewLine.Repeat(1) + "Would you like to download and install this update now?", "Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Question, 150 ); // Apply update if (r == DialogResult.Yes) { bool success = false; try { success = await ProgramUpdater.DownloadUpdateAsync(overlay); } catch (Exception ex) { // Create Exception Log TraceLog.TraceWarning("Unable to Download new update archive :: Generating Exception Log"); ExceptionHandler.GenerateExceptionLog(ex); // Alert User MetroMessageBox.Show(overlay, "Failed to download update archive! Reason: " + ex.Message + Environment.NewLine.Repeat(1) + "An exception log has been generated and created inside the My Documents/BF2Statistics folder.", "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } // If the file downloaded successfully if (success) { try { ProgramUpdater.RunUpdate(); } catch (Exception ex) { MetroMessageBox.Show(overlay, "An Occured while trying to install the new update. You will need to manually apply the update." + Environment.NewLine.Repeat(1) + "Error Message: " + ex.Message, "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } } // Close overlay overlay.Close(); // Focus the mod select ModComboBox.Focus(); } } }