private async Task GameBananaUpdate(GameBananaItem item, DisplayedMetadata row, string game, Progress <DownloadProgress> progress, CancellationTokenSource cancellationToken) { if (item.HasUpdates) { GameBananaItemUpdate[] updates = item.Updates; string updateTitle = updates[0].Title; int updateIndex = 0; Match onlineVersionMatch = Regex.Match(updateTitle, @"(?<version>([0-9]+\.?)+)[^a-zA-Z]*"); string onlineVersion = null; if (onlineVersionMatch.Success) { onlineVersion = onlineVersionMatch.Groups["version"].Value; } // GB Api only returns two latest updates, so if the first doesn't have a version try the second else if (updates.Length > 1) { updateTitle = updates[1].Title; onlineVersionMatch = Regex.Match(updateTitle, @"(?<version>([0-9]+\.?)+)[^a-zA-Z]*"); updateIndex = 1; if (onlineVersionMatch.Success) { onlineVersion = onlineVersionMatch.Groups["version"].Value; } } Match localVersionMatch = Regex.Match(row.version, @"(?<version>([0-9]+\.?)+)[^a-zA-Z]*"); string localVersion = null; if (localVersionMatch.Success) { localVersion = localVersionMatch.Groups["version"].Value; } if (row.skippedVersion != null) { if (row.skippedVersion == "all" || !UpdateAvailable(onlineVersion, row.skippedVersion)) { Console.WriteLine($"[INFO] No updates available for {row.name}"); return; } } if (UpdateAvailable(onlineVersion, localVersion)) { Console.WriteLine($"[INFO] An update is available for {row.name} ({onlineVersion})"); // Display the changelog and confirm they want to update ChangelogBox changelogBox = new ChangelogBox(updates[updateIndex], row.name, $"Would you like to update {row.name} to version {onlineVersion}?", row, onlineVersion, $@"{assemblyLocation}\Packages\{game}\{row.path}\Package.xml", false); changelogBox.Activate(); changelogBox.ShowDialog(); if (!changelogBox.YesNo) { Console.WriteLine($"[INFO] Cancelled update for {row.name}"); return; } // Download the update Dictionary <String, GameBananaItemFile> files = item.Files; string downloadUrl, fileName; // Work out which are Aemulus comptaible by examining the file tree Dictionary <String, GameBananaItemFile> aemulusCompatibleFiles = new Dictionary <string, GameBananaItemFile>(); foreach (KeyValuePair <string, GameBananaItemFile> file in files) { if (file.Value.FileMetadata.Values.Count > 2) { string fileTree = file.Value.FileMetadata.Values.ElementAt(3).ToString(); if (fileTree.ToLower().Contains("package.xml") || fileTree.ToLower().Contains("mod.xml") || fileTree == "[]") { aemulusCompatibleFiles.Add(file.Key, file.Value); } } } if (aemulusCompatibleFiles.Count > 1) { UpdateFileBox fileBox = new UpdateFileBox(aemulusCompatibleFiles, row.name); fileBox.Activate(); fileBox.ShowDialog(); downloadUrl = fileBox.chosenFileUrl; fileName = fileBox.chosenFileName; } else if (aemulusCompatibleFiles.Count == 1) { downloadUrl = aemulusCompatibleFiles.ElementAt(0).Value.DownloadUrl; fileName = aemulusCompatibleFiles.ElementAt(0).Value.FileName; } else { Console.WriteLine($"[INFO] An update is available for {row.name} ({onlineVersion}) but no downloadable files are available."); NotificationBox notification = new NotificationBox($"{row.name} has an update ({onlineVersion}) but no downloadable files.\nWould you like to go to the page to manually download the update?", false); notification.ShowDialog(); notification.Activate(); if (notification.YesNo) { Process.Start(row.link); } return; } if (downloadUrl != null && fileName != null) { await DownloadFile(downloadUrl, fileName, game, row, onlineVersion, progress, cancellationToken, updates[updateIndex]); } else { Console.WriteLine($"[INFO] Cancelled update for {row.name}"); } } else { Console.WriteLine($"[INFO] No updates available for {row.name}"); } // TODO Check if there was no version number } else { Console.WriteLine($"[INFO] No updates available for {row.name}"); } }
private async Task GitHubUpdate(Release release, DisplayedMetadata row, string game, Progress <DownloadProgress> progress, CancellationTokenSource cancellationToken) { Match onlineVersionMatch = Regex.Match(release.TagName, @"(?<version>([0-9]+\.?)+)[^a-zA-Z]*"); string onlineVersion = null; if (onlineVersionMatch.Success) { onlineVersion = onlineVersionMatch.Groups["version"].Value; } Match localVersionMatch = Regex.Match(row.version, @"(?<version>([0-9]+\.?)+)[^a-zA-Z]*"); string localVersion = null; if (localVersionMatch.Success) { localVersion = localVersionMatch.Groups["version"].Value; } if (row.skippedVersion != null) { if (row.skippedVersion == "all" || !UpdateAvailable(onlineVersion, row.skippedVersion)) { Console.WriteLine($"[INFO] No updates available for {row.name}"); return; } } if (UpdateAvailable(onlineVersion, localVersion)) { Console.WriteLine($"[INFO] An update is available for {row.name} ({release.TagName})"); NotificationBox notification = new NotificationBox($"{row.name} has an update ({release.TagName}):\n{release.Body}\n\nWould you like to update?", false); notification.ShowDialog(); notification.Activate(); if (!notification.YesNo) { return; } string downloadUrl, fileName; if (release.Assets.Count > 1) { UpdateFileBox fileBox = new UpdateFileBox(release.Assets, row.name); fileBox.Activate(); fileBox.ShowDialog(); downloadUrl = fileBox.chosenFileUrl; fileName = fileBox.chosenFileName; } else if (release.Assets.Count == 1) { downloadUrl = release.Assets.First().BrowserDownloadUrl; fileName = release.Assets.First().Name; } else { Console.WriteLine($"[INFO] An update is available for {row.name} ({release.TagName}) but no downloadable files are available."); notification = new NotificationBox($"{row.name} has an update ({release.TagName}) but no downloadable files.\nWould you like to go to the page to manually download the update?", false); notification.ShowDialog(); notification.Activate(); if (notification.YesNo) { Process.Start(row.link); } return; } if (downloadUrl != null && fileName != null) { await DownloadFile(downloadUrl, fileName, game, row, release.TagName, progress, CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Token)); } else { Console.WriteLine($"[INFO] Cancelled update for {row.name}"); } } else { Console.WriteLine($"[INFO] No updates available for {row.name}"); } }
public async Task <bool> CheckForAemulusUpdate(string aemulusVersion, CancellationTokenSource cancellationToken) { try { Uri uri = CreateUri($"https://github.com/TekkaGB/AemulusModManager"); Release release = await gitHubClient.Repository.Release.GetLatest(uri.Segments[1].Replace("/", ""), uri.Segments[2].Replace("/", "")); Match onlineVersionMatch = Regex.Match(release.TagName, @"(?<version>([0-9]+\.?)+)[^a-zA-Z]*"); string onlineVersion = null; if (onlineVersionMatch.Success) { onlineVersion = onlineVersionMatch.Groups["version"].Value; } if (UpdateAvailable(onlineVersion, aemulusVersion)) { Console.WriteLine($"[INFO] An update is available for Aemulus ({onlineVersion})"); NotificationBox notification = new NotificationBox($"Aemulus has a new update ({release.TagName}):\n{release.Body}\n\nWould you like to update?", false); notification.ShowDialog(); notification.Activate(); if (!notification.YesNo) { return(false); } string downloadUrl, fileName; downloadUrl = release.Assets.First().BrowserDownloadUrl; fileName = release.Assets.First().Name; if (downloadUrl != null && fileName != null) { await DownloadAemulus(downloadUrl, fileName, onlineVersion, new Progress <DownloadProgress>(ReportUpdateProgress), cancellationToken); // Notify that the update is about to happen NotificationBox finishedNotification = new NotificationBox($"Finished downloading {fileName}!\nAemulus will now restart.", true); finishedNotification.ShowDialog(); finishedNotification.Activate(); // Update Aemulus UpdateManager updateManager = new UpdateManager(new LocalPackageResolver(@$ "{assemblyLocation}\Downloads\AemulusUpdate"), new Zip7Extractor()); if (!Version.TryParse(onlineVersion, out Version version)) { Console.WriteLine("[ERROR] Error parsing Aemulus version, cancelling update"); // TODO Delete the downloaded stuff return(false); } // Updates and restarts Aemulus await updateManager.PrepareUpdateAsync(version); // Clean up the downloaded files if (Directory.Exists($@"{assemblyLocation}\Downloads\")) { Directory.Delete($@"{assemblyLocation}\Downloads\", true); } updateManager.LaunchUpdater(version); return(true); } } else { Console.WriteLine($"[INFO] No updates available for Aemulus"); } } catch (Exception e) { Console.WriteLine($"[ERROR] Error whilst checking for updates: {e.Message}"); } return(false); }