From fe1eb6cf3dd0a9fce127acfd5a3c9dd6d59d9031 Mon Sep 17 00:00:00 2001 From: 404Kurama Date: Sun, 23 Apr 2023 22:23:27 +0700 Subject: [PATCH 1/4] feat(helpers/updater): re-write updater --- Bloxstrap/App.xaml.cs | 3 +- Bloxstrap/Bloxstrap.csproj | 4 +- Bloxstrap/Helpers/Updater.cs | 131 +++++++++++++++++------------------ 3 files changed, 66 insertions(+), 72 deletions(-) diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index 221abeb..92ad79a 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -210,7 +210,8 @@ namespace Bloxstrap #if !DEBUG if (!IsUninstall && !IsFirstRun) - Updater.CheckInstalledVersion(); + Updater.CheckIsSuccessfulyUpdate(); + Updater.CheckForUpdate(); #endif string commandLine = ""; diff --git a/Bloxstrap/Bloxstrap.csproj b/Bloxstrap/Bloxstrap.csproj index c1d35ef..aca7726 100644 --- a/Bloxstrap/Bloxstrap.csproj +++ b/Bloxstrap/Bloxstrap.csproj @@ -7,8 +7,8 @@ true True Bloxstrap.ico - 2.2.0 - 2.2.0.0 + 2.3.0 + 2.3.0.0 app.manifest diff --git a/Bloxstrap/Helpers/Updater.cs b/Bloxstrap/Helpers/Updater.cs index c9c1ebe..8a6dae9 100644 --- a/Bloxstrap/Helpers/Updater.cs +++ b/Bloxstrap/Helpers/Updater.cs @@ -5,40 +5,66 @@ using System.Windows; using System.Windows.Forms; using System.Threading; using System.Threading.Tasks; +using System.Net.Http; using Bloxstrap.Properties; using Bloxstrap.Views; +using System.Text.Json; +using System.Net; namespace Bloxstrap.Helpers { public class Updater { - public static void CheckInstalledVersion() + public static void CheckIsSuccessfulyUpdate() { - if (Environment.ProcessPath is null || !File.Exists(Directories.Application) || Environment.ProcessPath == Directories.Application) + string updatePath = Path.Combine(Directories.Base, "Bloxstrap-Update-Version.exe"); + + if (!File.Exists(updatePath) || !File.Exists(Directories.Application)) return; - // 2.0.0 downloads updates to /Updates so lol - bool isAutoUpgrade = Environment.ProcessPath.StartsWith(Path.Combine(Directories.Base, "Updates")) || Environment.ProcessPath.StartsWith(Path.Combine(Directories.LocalAppData, "Temp")); + var applicationInfo = new FileInfo(Directories.Application); + var updateInfo = new FileInfo(updatePath); - // if downloaded version doesn't match, replace installed version with downloaded version - FileVersionInfo currentVersionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath); - FileVersionInfo installedVersionInfo = FileVersionInfo.GetVersionInfo(Directories.Application); + if (applicationInfo.Length == updateInfo.Length) + { + App.ShowMessageBox("Successfully update Bloxstrap", MessageBoxImage.Information); + } + else + { + App.ShowMessageBox("Failed to update Bloxstrap", MessageBoxImage.Error); + } - if (installedVersionInfo.ProductVersion == currentVersionInfo.ProductVersion) + + File.Delete(updatePath); + } + + public static async void CheckForUpdate() + { + using var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"); + + string url = $"https://api.github.com/repos/{App.ProjectRepository}/releases/latest"; + var response = await httpClient.GetAsync(url); + var responseCOntent = await response.Content.ReadAsStringAsync(); + + var jsonDoc = JsonDocument.Parse(responseCOntent); + + var tagValue = jsonDoc.RootElement.GetProperty("tag_name").GetString().Replace("v", ""); + + if (tagValue == App.Version) return; MessageBoxResult result; - // silently upgrade version if the command line flag is set or if we're launching from an auto update - if (App.IsUpgrade || isAutoUpgrade) + if (App.IsUpgrade) { result = MessageBoxResult.Yes; } else { result = App.ShowMessageBox( - $"The version of {App.ProjectName} you've launched is different to the version you currently have installed.\nWould you like to upgrade your currently installed version?", + "Would you like to update to the latest version of Bloxstrap?", MessageBoxImage.Question, MessageBoxButton.YesNo ); @@ -47,68 +73,35 @@ namespace Bloxstrap.Helpers if (result != MessageBoxResult.Yes) return; - // yes, this is EXTREMELY hacky, but the updater process that launched the - // new version may still be open and so we have to wait for it to close - int attempts = 0; - while (attempts < 10) + string fileName = ""; + + /// Why i didn't do it in short hand if else + if (IntPtr.Size == 8) { - attempts++; - - try - { - File.Delete(Directories.Application); - break; - } - catch (Exception) - { - if (attempts == 1) - App.Logger.WriteLine("[Updater::CheckInstalledVersion] Waiting for write permissions to update version"); - - Thread.Sleep(500); - } + fileName = $"Bloxstrap-v{tagValue}-x64.exe"; + } + else + { + fileName = $"Bloxstrap-v{tagValue}-x86.exe"; } - if (attempts == 10) - { - App.Logger.WriteLine("[Updater::CheckInstalledVersion] Failed to update! (Could not get write permissions after 5 seconds)"); - return; - } + string downloadUrl = $"https://github.com/pizzaboxer/bloxstrap/releases/download/v{tagValue}/{fileName}"; + string downloadPath = Path.Combine(Directories.Base, "Bloxstrap-Update-Version.exe"); - File.Copy(Environment.ProcessPath, Directories.Application); - - Bootstrapper.Register(); - - if (isAutoUpgrade) - { - NotifyIcon notification = new() - { - Icon = Resources.IconBloxstrap, - Text = "Bloxstrap", - Visible = true, - BalloonTipTitle = $"Bloxstrap has been upgraded to v{currentVersionInfo.ProductVersion}", - BalloonTipText = "Click here to see what's new in this version" - }; - - notification.BalloonTipClicked += (_, _) => Utilities.OpenWebsite($"https://github.com/{App.ProjectRepository}/releases/tag/v{currentVersionInfo.ProductVersion}"); - notification.ShowBalloonTip(30); - - Task.Run(() => - { - Task.Delay(30000).Wait(); - notification.Dispose(); - }); - } - else if (!App.IsQuiet) - { - App.ShowMessageBox( - $"{App.ProjectName} has been updated to v{currentVersionInfo.ProductVersion}", - MessageBoxImage.Information, - MessageBoxButton.OK - ); - - new MainWindow().ShowDialog(); - App.Terminate(); - } + WebClient client = new WebClient(); + client.DownloadFile(downloadUrl, downloadPath); + App.Logger.WriteLine("[Updater::CheckForUpdate] Downloaded new bloxstrap version: " + downloadPath); + App.Logger.WriteLine("[Updater::CheckForUpdate] Restarting bloxstrap to update..."); + + /// Use ping command to wait 5 s before replace because it impossible to replace + /// file without closing it process. + ProcessStartInfo startInfo = new ProcessStartInfo(); + startInfo.FileName = "cmd.exe"; + startInfo.Arguments = $"/c ping 127.0.0.1 -n 5 > nul && copy /y {downloadPath} {Directories.Application}"; + ///startInfo.WindowStyle = ProcessWindowStyle.Hidden; + + Process.Start(startInfo); + App.Terminate(); } } } \ No newline at end of file From d1c78bc7db706a62f5fd7a11cec173e30c0a9efb Mon Sep 17 00:00:00 2001 From: 404Kurama Date: Sun, 23 Apr 2023 22:40:39 +0700 Subject: [PATCH 2/4] feat(helper/updater): auto launch bloxstrap after update --- Bloxstrap/Helpers/Updater.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bloxstrap/Helpers/Updater.cs b/Bloxstrap/Helpers/Updater.cs index 8a6dae9..b80cf82 100644 --- a/Bloxstrap/Helpers/Updater.cs +++ b/Bloxstrap/Helpers/Updater.cs @@ -97,7 +97,7 @@ namespace Bloxstrap.Helpers /// file without closing it process. ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; - startInfo.Arguments = $"/c ping 127.0.0.1 -n 5 > nul && copy /y {downloadPath} {Directories.Application}"; + startInfo.Arguments = $"/c ping 127.0.0.1 -n 5 > nul && copy /y {downloadPath} {Directories.Application} && cls && echo Please restart Bloxstrap to complete update bootstrap. && {Directories.Application} -menu"; ///startInfo.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(startInfo); From 6930e008f2d1dac99f36dc298aee724cf2276a85 Mon Sep 17 00:00:00 2001 From: 404Kurama Date: Sun, 23 Apr 2023 22:52:18 +0700 Subject: [PATCH 3/4] feat(aboutpage): add check for update button --- Bloxstrap/ViewModels/AboutViewModel.cs | 9 ++++++++- Bloxstrap/Views/Pages/AboutPage.xaml | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Bloxstrap/ViewModels/AboutViewModel.cs b/Bloxstrap/ViewModels/AboutViewModel.cs index a8751b3..1914a38 100644 --- a/Bloxstrap/ViewModels/AboutViewModel.cs +++ b/Bloxstrap/ViewModels/AboutViewModel.cs @@ -1,11 +1,18 @@ using System.Windows.Input; -using CommunityToolkit.Mvvm.Input; using Bloxstrap.Helpers; +using CommunityToolkit.Mvvm.Input; namespace Bloxstrap.ViewModels { public class AboutViewModel { public string Version => $"Version {App.Version}"; + + public ICommand CFUWindowCommand => new RelayCommand(CheckForUpdate); + + private void CheckForUpdate() + { + Updater.CheckForUpdate(); + } } } diff --git a/Bloxstrap/Views/Pages/AboutPage.xaml b/Bloxstrap/Views/Pages/AboutPage.xaml index 8130c33..d4674da 100644 --- a/Bloxstrap/Views/Pages/AboutPage.xaml +++ b/Bloxstrap/Views/Pages/AboutPage.xaml @@ -38,7 +38,8 @@ - + + From d84e23ac126dbb64deb965c63d4b3faeef1056ca Mon Sep 17 00:00:00 2001 From: 404Kurama Date: Sun, 23 Apr 2023 22:57:33 +0700 Subject: [PATCH 4/4] feat(helper/updater): nvm --- Bloxstrap/Helpers/Updater.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bloxstrap/Helpers/Updater.cs b/Bloxstrap/Helpers/Updater.cs index b80cf82..8a6dae9 100644 --- a/Bloxstrap/Helpers/Updater.cs +++ b/Bloxstrap/Helpers/Updater.cs @@ -97,7 +97,7 @@ namespace Bloxstrap.Helpers /// file without closing it process. ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; - startInfo.Arguments = $"/c ping 127.0.0.1 -n 5 > nul && copy /y {downloadPath} {Directories.Application} && cls && echo Please restart Bloxstrap to complete update bootstrap. && {Directories.Application} -menu"; + startInfo.Arguments = $"/c ping 127.0.0.1 -n 5 > nul && copy /y {downloadPath} {Directories.Application}"; ///startInfo.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(startInfo);