public DownloadWindow(GameBananaItem item) { InitializeComponent(); DownloadText.Text = $"{item.Name}\nSubmitted by {item.Owner}"; var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = item.EmbedImage; bitmap.EndInit(); Preview.Source = bitmap; }
private async Task <bool> GetData() { try { string responseString = await client.GetStringAsync(URL); response = JsonSerializer.Deserialize <GameBananaItem>(responseString); fileName = response.Files[DL_ID].FileName; return(true); } catch (Exception e) { MessageBox.Show($"Error while fetching data: {e.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); return(false); } }
private static async Task ModUpdate(GameBananaItem item, string mod, Metadata metadata, Progress <DownloadProgress> progress, CancellationTokenSource cancellationToken) { // If lastupdate doesn't exist, add one if (metadata.lastupdate == null) { if (item.HasUpdates) { metadata.lastupdate = item.Updates[0].DateAdded; } else { metadata.lastupdate = new DateTime(1970, 1, 1); } string metadataString = JsonSerializer.Serialize(metadata, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText($@"{mod}/mod.json", metadataString); return; } if (item.HasUpdates) { var update = item.Updates[0]; // Compares dates of last update to current if (DateTime.Compare((DateTime)metadata.lastupdate, update.DateAdded) < 0) { ++updateCounter; // Display the changelog and confirm they want to update _logger.WriteLine($"An update is available for {Path.GetFileName(mod)}!", LoggerType.Info); ChangelogBox changelogBox = new ChangelogBox(update, Path.GetFileName(mod), $"A new update is available for {Path.GetFileName(mod)}", item.EmbedImage, true); changelogBox.Activate(); changelogBox.ShowDialog(); if (changelogBox.Skip) { if (File.Exists($@"{mod}/mod.json")) { _logger.WriteLine($"Skipped update for {Path.GetFileName(mod)}...", LoggerType.Info); metadata.lastupdate = update.DateAdded; string metadataString = JsonSerializer.Serialize(metadata, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText($@"{mod}/mod.json", metadataString); } return; } if (!changelogBox.YesNo) { _logger.WriteLine($"Declined update for {Path.GetFileName(mod)}...", LoggerType.Info); return; } // Download the update var files = item.Files; string downloadUrl, fileName; if (files.Count > 1) { UpdateFileBox fileBox = new UpdateFileBox(files.Values.ToList(), Path.GetFileName(mod)); fileBox.Activate(); fileBox.ShowDialog(); downloadUrl = fileBox.chosenFileUrl; fileName = fileBox.chosenFileName; } else if (files.Count == 1) { downloadUrl = files.ElementAt(0).Value.DownloadUrl; fileName = files.ElementAt(0).Value.FileName; } else { _logger.WriteLine($"An update is available for {Path.GetFileName(mod)} but no downloadable files are available.", LoggerType.Warning); return; } if (downloadUrl != null && fileName != null) { await DownloadFile(downloadUrl, fileName, mod, update.DateAdded, progress, cancellationToken); } else { _logger.WriteLine($"Cancelled update for {Path.GetFileName(mod)}", LoggerType.Info); } } } }
public static async Task <bool> CheckForFileDaddyUpdate(CancellationTokenSource cancellationToken) { // Get Version Number var localVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; try { var requestUrl = $"https://api.gamebanana.com/Core/Item/Data?itemtype=Tool&itemid=7015&fields=Updates().bSubmissionHasUpdates()," + $"Updates().aGetLatestUpdates(),Files().aFiles()&return_keys=1"; GameBananaItem response = JsonSerializer.Deserialize <GameBananaItem>(await client.GetStringAsync(requestUrl)); if (response == null) { MessageBox.Show("Error whilst checking for FileDaddy update: No response from GameBanana API"); return(false); } if (response.HasUpdates) { GameBananaItemUpdate[] updates = response.Updates; string updateTitle = updates[0].Title; Match onlineVersionMatch = Regex.Match(updateTitle, @"(?<version>([1-9]+\.?)+)[^a-zA-Z]"); string onlineVersion = null; if (onlineVersionMatch.Success) { onlineVersion = onlineVersionMatch.Value; } if (UpdateAvailable(onlineVersion, localVersion)) { ChangelogBox notification = new ChangelogBox(updates[0], "FileDaddy", $"A new version of FileDaddy is available (v{onlineVersion})!", null); notification.ShowDialog(); notification.Activate(); if (notification.YesNo) { Dictionary <String, GameBananaItemFile> files = response.Files; string downloadUrl = files.ElementAt(0).Value.DownloadUrl; string fileName = files.ElementAt(0).Value.FileName; // Download the update await DownloadFileDaddy(downloadUrl, fileName, onlineVersion, new Progress <DownloadProgress>(ReportUpdateProgress), cancellationToken); // Notify that the update is about to happen MessageBox.Show($"Finished downloading {fileName}!\nFileDaddy will now restart.", "Notification", MessageBoxButton.OK); // Update FileDaddy UpdateManager updateManager = new UpdateManager(new LocalPackageResolver($"{assemblyLocation}/Downloads/FileDaddyUpdate"), new ZipExtractor()); if (!Version.TryParse(onlineVersion, out Version version)) { MessageBox.Show($"Error parsing {onlineVersion}!\nCancelling update.", "Notification", MessageBoxButton.OK); return(false); } // Updates and restarts FileDaddy await updateManager.PrepareUpdateAsync(version); updateManager.LaunchUpdater(version); return(true); } } } } catch (Exception e) { } return(false); }