diff --git a/ZuneModdingHelper/App.xaml.cs b/ZuneModdingHelper/App.xaml.cs index 77a6a3f..4792455 100644 --- a/ZuneModdingHelper/App.xaml.cs +++ b/ZuneModdingHelper/App.xaml.cs @@ -1,9 +1,4 @@ using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; using System.Windows; namespace ZuneModdingHelper @@ -13,5 +8,23 @@ namespace ZuneModdingHelper /// public partial class App : Application { + public static string Title => "Zune Modding Helper"; + + public static Version VersionNum => new(2021, 4, 26, 1); + public static string VersionStatus => "alpha"; + public static string Version => VersionNum.ToString() + (VersionStatus != string.Empty ? "-" + VersionStatus : string.Empty); + + public static bool CheckIfNewerVersion(string otherStr) + { + int idxSplit = otherStr.IndexOf('-'); + Version otherNum = new(otherStr[..idxSplit]); + //string otherStatus = otherStr[(idxSplit + 1)..]; + + // TODO: This assumes that the VersionStatus is "alpha" + //bool isNotAlpha = otherStatus != VersionStatus; + bool isNotAlpha = !otherStr.EndsWith(VersionStatus); + bool isNewer = otherNum > VersionNum; + return isNotAlpha || isNewer; + } } } diff --git a/ZuneModdingHelper/MainWindow.xaml b/ZuneModdingHelper/MainWindow.xaml index caf5390..7b0ed32 100644 --- a/ZuneModdingHelper/MainWindow.xaml +++ b/ZuneModdingHelper/MainWindow.xaml @@ -7,11 +7,14 @@ xmlns:core="clr-namespace:ZuneModCore;assembly=ZuneModCore" xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" mc:Ignorable="d" - Title="Zune Modding Helper" Height="450" Width="800" + Title="{x:Static local:App.Title}" Height="450" Width="800" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"> + @@ -20,10 +23,10 @@ - + - - + + diff --git a/ZuneModdingHelper/MainWindow.xaml.cs b/ZuneModdingHelper/MainWindow.xaml.cs index b3d2242..2b63866 100644 --- a/ZuneModdingHelper/MainWindow.xaml.cs +++ b/ZuneModdingHelper/MainWindow.xaml.cs @@ -1,22 +1,17 @@ using ControlzEx.Theming; +using Flurl.Http; using MahApps.Metro.Controls; using MahApps.Metro.Controls.Dialogs; +using Newtonsoft.Json.Linq; using OwlCore.AbstractUI.ViewModels; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; using System.Windows.Navigation; -using System.Windows.Shapes; using ZuneModCore; namespace ZuneModdingHelper @@ -139,5 +134,65 @@ namespace ZuneModdingHelper }); e.Handled = true; } + + private async void UpdatesButton_Click(object sender, RoutedEventArgs e) + { + var checkDialog = await this.ShowProgressAsync("Checking for updates...", "Please wait.", settings: defaultMetroDialogSettings); + checkDialog.SetIndeterminate(); + + // Get releases list from GitHub + List releases = await "https://api.github.com/repos/yoshiask/ZuneModdingHelper/releases" + .WithHeader("User-Agent", App.Title).GetJsonAsync>(); + JObject latest = releases[0]; + if (!App.CheckIfNewerVersion(latest["tag_name"].Value())) + { + // Already up-to-date + await checkDialog.CloseAsync(); + await this.ShowMessageAsync("No updates available", "You're already using the latest version.", settings: defaultMetroDialogSettings); + return; + } + + // Newer version available, prompt user to download + MetroDialogSettings promptSettings = new() + { + ColorScheme = MetroDialogColorScheme.Accented, + AnimateShow = true, + AnimateHide = true, + AffirmativeButtonText = "Download", + NegativeButtonText = "Later" + }; + await checkDialog.CloseAsync(); + var promptResult = await this.ShowMessageAsync("Update available", $"Relase {latest["name"]} is available. Would you like to download it now?", + MessageDialogStyle.AffirmativeAndNegative, promptSettings); + + if (promptResult == MessageDialogResult.Affirmative) + { + var progDialog = await this.ShowProgressAsync("Downloading update...", "This may take a few minutes.", settings: defaultMetroDialogSettings); + progDialog.SetIndeterminate(); + + // Download new version to AppData + JObject asset = latest["assets"].ToObject>()[0]; + string assetName = asset["name"].Value(); + string downloadedFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), assetName); + if (File.Exists(downloadedFile)) File.Delete(downloadedFile); + using (var client = new System.Net.WebClient()) + { + await client.DownloadFileTaskAsync(new Uri(asset["browser_download_url"].Value()), downloadedFile); + } + + // Ask user to save file + Microsoft.Win32.SaveFileDialog saveFileDialog = new() + { + FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", assetName) + }; + if (saveFileDialog.ShowDialog() == true) + { + File.Copy(downloadedFile, saveFileDialog.FileName, true); + + await progDialog.CloseAsync(); + await this.ShowMessageAsync("Update complete", "You may now exit this program and open the new version.", settings: defaultMetroDialogSettings); + } + } + } } } diff --git a/ZuneModdingHelper/ZuneModdingHelper.csproj b/ZuneModdingHelper/ZuneModdingHelper.csproj index 08ecde3..9871c07 100644 --- a/ZuneModdingHelper/ZuneModdingHelper.csproj +++ b/ZuneModdingHelper/ZuneModdingHelper.csproj @@ -12,6 +12,7 @@ +