private async Task CheckForUpdates() { try { SetControlsEnabled(false); var response = await httpClient.GetAsync("https://pastebin.com/raw/tc6Pk7rz"); if (response.IsSuccessStatusCode) { try { var stringResponse = await response.Content.ReadAsStringAsync(); string[] lines = stringResponse.Split(new[] { Environment.NewLine }, StringSplitOptions.None); if (lines.Length > 1) { SetStatusAndProgress("Retrieved Database Info", 25); string version = lines[0]; string nextURL = lines[2]; int latestArchivedVersion = 0; int versionAsInt = 0; int currentVersion = 0; try { latestArchivedVersion = int.Parse(VersionDropdown.Items[1].ToString()); versionAsInt = int.Parse(version); currentVersion = int.Parse(settings.Version); }catch (Exception) { // Ignore } if (latestArchivedVersion > versionAsInt) { if (latestArchivedVersion > currentVersion) { if (settings.BackupSaveFiles) { BackupSaveFiles(); } await DownloadVersion(VersionDropdown.Items[1].ToString(), true); } else { SetStatusAndProgress("You have the latest version!", 100); FinishUp(); } } else { if (settings.Version.Equals(version)) { SetStatusAndProgress("You have the latest version!", 100); FinishUp(); } else { if (settings.BackupSaveFiles) { BackupSaveFiles(); } if (nextURL.Length > 0) { response = await httpClient.GetAsync(nextURL); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); content = content.Substring(content.IndexOf("https://download")); content = content.Substring(0, content.IndexOf("\"")); nextURL = content; String fileName = "YuzuEA-" + version + ".7z"; SetStatusAndProgress("Downloading Update: " + version, 35); String address = nextURL; if (settings.AcceleratedDownloads) { DownloadResult downloadResult = await DownloadManager.Download(address, Directory.GetCurrentDirectory(), Math.Min(settings.MaxConnections, 16), DownloadProgressChanged); SetStatus($"Download Took: {downloadResult.TimeTaken}"); DownloadCompleted(new FileDownloadInfo(downloadResult.FilePath, version, false)); } else { using (WebClient webClient = new WebClient()) { webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged; webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted; stopwatch.Start(); webClient.DownloadFileAsync(new Uri(address), Directory.GetCurrentDirectory() + "\\" + fileName, new FileDownloadInfo(fileName, version, false)); } } } } else { SetStatusAndProgress("Initial attempt failed", 0); logger.Log(LogLevel.WARNING, "Couldn't parse URL for download"); SetControlsEnabled(true); } } } } else { SetStatusAndProgress("Initial attempt failed", 0); logger.Log(LogLevel.WARNING, "Couldn't parse database for URL fetch."); if (settings.SinMode) { SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0); } else { await AttemptFallbackDownload(); } } } catch (Exception e) { if (!e.GetType().IsAssignableFrom(typeof(ObjectDisposedException))) { logger.Log(LogLevel.WARNING, e.Message); if (settings.SinMode) { SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0); } else { await AttemptFallbackDownload(); } } } } else { if (settings.SinMode) { SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0); } else { await AttemptFallbackDownload(); } } } catch (Exception e) { if (e.GetType().IsAssignableFrom(typeof(HttpRequestException)) || e.GetType().IsAssignableFrom(typeof(TaskCanceledException))) { if (settings.SinMode) { SetStatusAndProgress("You have the latest version (Sin hasn't updated yet)!", 0); } else { await AttemptFallbackDownload(); } } else { SetStatusAndProgress("An error occurred, please check the logs for the issue", 0); logger.Log(LogLevel.ERROR, e.Message); logger.Log(LogLevel.ERROR, e.StackTrace); SetControlsEnabled(true); } } }
private async Task DownloadVersion(string version, bool replaceAsLatest) { try { if (archivedVersions.ContainsKey(version)) { SetStatusAndProgress("Downloading version " + version + "\r\nThis may take a while..", 0); SetControlsEnabled(false); var gitUrl = "https://github.com/pineappleEA/pineapple-src/releases/tag/EA-"; var gitLink = gitUrl + version; var anonResponse = await httpClient.GetAsync(archivedVersions[version]); var gitResponse = await httpClient.GetAsync(gitLink); if (gitResponse.IsSuccessStatusCode) { String fileName = "Windows-Yuzu-EA-" + version + ".7z"; String address = gitLink.Replace("tag", "download") + "/Windows-Yuzu-EA-" + version + ".7z"; if (settings.AcceleratedDownloads) { DownloadResult downloadResult = await DownloadManager.Download(address, Directory.GetCurrentDirectory(), settings.MaxConnections, ArchiveOctaneClient_DownloadProgressChanged); SetStatus($"Download Took: {downloadResult.TimeTaken}"); ArchiveDownloadCompleted(new FileDownloadInfo(fileName, version, replaceAsLatest)); } else { using (WebClient archiveWebClient = new WebClient()) { archiveWebClient.DownloadProgressChanged += ArchiveWebClient_DownloadProgressChanged; archiveWebClient.DownloadFileCompleted += ArchiveWebClient_DownloadFileCompleted; stopwatch.Start(); archiveWebClient.DownloadFileAsync(new Uri(address), Directory.GetCurrentDirectory() + "\\" + fileName, new FileDownloadInfo(Directory.GetCurrentDirectory() + "\\" + fileName, version, replaceAsLatest)); } } } if (anonResponse.IsSuccessStatusCode && !gitResponse.IsSuccessStatusCode) { var content = await anonResponse.Content.ReadAsStringAsync(); var pattern = new Regex("https://cdn.*.anonfiles.com/.*/(YuzuEA-.*.7z)"); var match = pattern.Match(content); if (match.Success) { String address = match.Groups[0].Value; String fileName = match.Groups[1].Value; if (settings.AcceleratedDownloads) { DownloadResult downloadResult = await DownloadManager.Download(address, Directory.GetCurrentDirectory(), settings.MaxConnections, ArchiveOctaneClient_DownloadProgressChanged); SetStatus($"Download Took: {downloadResult.TimeTaken}"); ArchiveDownloadCompleted(new FileDownloadInfo(fileName, version, replaceAsLatest)); } else { using (WebClient archiveWebClient = new WebClient()) { archiveWebClient.DownloadProgressChanged += ArchiveWebClient_DownloadProgressChanged; archiveWebClient.DownloadFileCompleted += ArchiveWebClient_DownloadFileCompleted; stopwatch.Start(); archiveWebClient.DownloadFileAsync(new Uri(address), Directory.GetCurrentDirectory() + "\\" + fileName, new FileDownloadInfo(Directory.GetCurrentDirectory() + "\\" + fileName, version, replaceAsLatest)); } } } } } else { SetStatusAndProgress("Please select a valid version to download.", 0); SetControlsEnabled(true); } } catch (Exception e) { SetStatusAndProgress("An error occurred, please check the logs for the issue", 0); logger.Log(LogLevel.ERROR, e.Message); logger.Log(LogLevel.ERROR, e.StackTrace); SetControlsEnabled(true); } }