From 99b5803d8f1cea62cfdaa0f896f38e7a959d9060 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Fri, 10 May 2024 00:37:40 -0500 Subject: [PATCH] Switch to Octokit --- ZuneModdingHelper/App.xaml.cs | 4 ++ ZuneModdingHelper/MainWindow.xaml.cs | 40 ++++++++------ ZuneModdingHelper/ZuneModdingHelper.csproj | 62 +++++++++++----------- 3 files changed, 59 insertions(+), 47 deletions(-) diff --git a/ZuneModdingHelper/App.xaml.cs b/ZuneModdingHelper/App.xaml.cs index cb62e11..a9c9492 100644 --- a/ZuneModdingHelper/App.xaml.cs +++ b/ZuneModdingHelper/App.xaml.cs @@ -46,6 +46,10 @@ namespace ZuneModdingHelper private static void ConfigureServices() { ServiceCollection services = new(); + + Octokit.IGitHubClient github = new Octokit.GitHubClient(new Octokit.ProductHeaderValue(Title.Replace(" ", ""), VersionStr)); + services.AddSingleton(github); + Ioc.Default.ConfigureServices(services.BuildServiceProvider()); } } diff --git a/ZuneModdingHelper/MainWindow.xaml.cs b/ZuneModdingHelper/MainWindow.xaml.cs index 800b929..e071ccc 100644 --- a/ZuneModdingHelper/MainWindow.xaml.cs +++ b/ZuneModdingHelper/MainWindow.xaml.cs @@ -1,10 +1,10 @@ -using ControlzEx.Theming; -using Flurl.Http; +using CommunityToolkit.Mvvm.DependencyInjection; +using ControlzEx.Theming; using MahApps.Metro.Controls; using MahApps.Metro.Controls.Dialogs; using Microsoft.AppCenter.Analytics; using Microsoft.WindowsAPICodePack.Dialogs; -using Newtonsoft.Json.Linq; +using Octokit; using OwlCore.AbstractUI.Models; using Syroot.Windows.IO; using System; @@ -31,6 +31,8 @@ namespace ZuneModdingHelper AffirmativeButtonText = "OK" }; + private readonly IGitHubClient? _gitHub = Ioc.Default.GetService(); + public MainWindow() { InitializeComponent(); @@ -179,21 +181,26 @@ namespace ZuneModdingHelper private async void UpdatesButton_Click(object sender, RoutedEventArgs e) { + if (_gitHub is null) + return; + var checkDialog = await this.ShowProgressAsync("Checking for updates...", "Please wait.", settings: defaultMetroDialogSettings); checkDialog.SetIndeterminate(); try { // Get releases list from GitHub - List releases = await "https://api.github.com/repos/ZuneDev/ZuneModdingHelper/releases" - .WithHeader("User-Agent", App.Title.Replace(" ", "") + "/" + App.VersionStr) - .GetJsonAsync>(); - JObject latest = releases[0]; - string latestVerStr = latest["tag_name"].Value(); - if (!ReleaseVersion.TryParse(latestVerStr, out var latestVer) || App.Version >= latestVer) + // Why not use the `releases/latest` endpoint? Good question: https://github.com/octokit/octokit.net/issues/2916 + var releases = await _gitHub.Repository.Release.GetAll("ZuneDev", "ZuneModdingHelper"); + var latest = releases + .Select(r => new { Release = r, Version = ReleaseVersion.Parse(r.TagName) }) + .OrderByDescending(t => t.Version) + .ThenBy(t => t.Release.Prerelease) + .First(); + + if (!ReleaseVersion.TryParse(latest.Release.TagName, out var latestVer) || App.Version >= latestVer) { // Already up-to-date - Analytics.TrackEvent("Checked for updates", new Dictionary { { "UpdatesFound", bool.FalseString }, }); @@ -213,7 +220,7 @@ namespace ZuneModdingHelper NegativeButtonText = "Later" }; await checkDialog.CloseAsync(); - var promptResult = await this.ShowMessageAsync("Update available", $"Release {latest["name"]} is available. Would you like to download it now?", + var promptResult = await this.ShowMessageAsync("Update available", $"Release {latest.Release.Name} is available. Would you like to download it now?", MessageDialogStyle.AffirmativeAndNegative, promptSettings); bool acceptedUpdate = promptResult == MessageDialogResult.Affirmative; @@ -224,9 +231,8 @@ namespace ZuneModdingHelper 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); + var asset = latest.Release.Assets[0]; + string downloadedFile = Path.Combine(Path.GetTempPath(), asset.Name); if (File.Exists(downloadedFile)) File.Delete(downloadedFile); using (var client = new System.Net.WebClient()) { @@ -236,13 +242,13 @@ namespace ZuneModdingHelper }; progDialog.SetProgress(0); - await client.DownloadFileTaskAsync(new Uri(asset["browser_download_url"].Value()), downloadedFile); + await client.DownloadFileTaskAsync(new Uri(asset.BrowserDownloadUrl), downloadedFile); } // Ask user to save file Microsoft.Win32.SaveFileDialog saveFileDialog = new() { - FileName = assetName, + FileName = asset.Name, InitialDirectory = new KnownFolder(KnownFolderType.Downloads).Path }; bool dialogResult = saveFileDialog.ShowDialog() ?? false; @@ -252,6 +258,8 @@ namespace ZuneModdingHelper File.Copy(downloadedFile, saveFileDialog.FileName, true); await this.ShowMessageAsync("Update complete", "You may now exit this program and open the new version.", settings: defaultMetroDialogSettings); } + + File.Delete(downloadedFile); } Analytics.TrackEvent("Checked for updates", new Dictionary { diff --git a/ZuneModdingHelper/ZuneModdingHelper.csproj b/ZuneModdingHelper/ZuneModdingHelper.csproj index 25ee910..76d311a 100644 --- a/ZuneModdingHelper/ZuneModdingHelper.csproj +++ b/ZuneModdingHelper/ZuneModdingHelper.csproj @@ -1,39 +1,39 @@  - - WinExe + + WinExe net8.0-windows - true - app.manifest - 2021.12.30.0 - Joshua "Yoshi" Askharoun - https://github.com/ZuneDev/ZuneModdingHelper - git - x64;x86 - latest - Assets\ZMH_Icon.ico - + true + app.manifest + 2021.12.30.0 + Joshua "Yoshi" Askharoun + https://github.com/ZuneDev/ZuneModdingHelper + git + x64;x86 + latest + Assets\ZMH_Icon.ico + - - - - + + + + - - - - - - - - - - + + + + + + + + + + - - - PreserveNewest - - + + + PreserveNewest + +