From 123307aed78b42f5dc72a1b8bea0775eb97df0eb Mon Sep 17 00:00:00 2001 From: kitbyte Date: Sun, 23 Mar 2025 11:37:00 +0200 Subject: [PATCH] added updater --- WeModPatcher/App.xaml.cs | 5 + WeModPatcher/Constants.cs | 15 +- WeModPatcher/Utils/Patcher.cs | 2 +- WeModPatcher/Utils/Updater.cs | 130 ++++++++++++++++++ WeModPatcher/View/MainWindow/MainWindow.xaml | 61 +++++--- .../View/MainWindow/MainWindow.xaml.cs | 1 + WeModPatcher/View/MainWindow/MainWindowVm.cs | 38 ++++- WeModPatcher/WeModPatcher.csproj | 6 + 8 files changed, 228 insertions(+), 30 deletions(-) create mode 100644 WeModPatcher/Utils/Updater.cs diff --git a/WeModPatcher/App.xaml.cs b/WeModPatcher/App.xaml.cs index 9e6a6af..0d9be3e 100644 --- a/WeModPatcher/App.xaml.cs +++ b/WeModPatcher/App.xaml.cs @@ -28,5 +28,10 @@ namespace WeModPatcher MessageBox.Show(e.ToString()); Shutdown(); } + + public new static void Shutdown() + { + Current.Dispatcher.Invoke(() => Current.Shutdown()); + } } } \ No newline at end of file diff --git a/WeModPatcher/Constants.cs b/WeModPatcher/Constants.cs index d8fabbe..8c2d591 100644 --- a/WeModPatcher/Constants.cs +++ b/WeModPatcher/Constants.cs @@ -1,7 +1,18 @@ -namespace WeModPatcher +using System; +using System.Reflection; + +namespace WeModPatcher { public static class Constants { - public const string RepositoryUrl = "https://github.com/k1tbyte/Wemod-Patcher"; + public const string RepoName = "Wemod-Patcher"; + public const string Owner = "k1tbyte"; + public static readonly string RepositoryUrl = $"https://github.com/{Owner}/{RepoName}"; + public static readonly Version Version; + + static Constants() + { + Version = Assembly.GetExecutingAssembly().GetName().Version; + } } } \ No newline at end of file diff --git a/WeModPatcher/Utils/Patcher.cs b/WeModPatcher/Utils/Patcher.cs index 43fec64..4400264 100644 --- a/WeModPatcher/Utils/Patcher.cs +++ b/WeModPatcher/Utils/Patcher.cs @@ -200,7 +200,7 @@ namespace WeModPatcher.Utils return; } - await PatchPE(); + // await PatchPE(); _logger("[PATCHER] Done!", ELogType.Success); } diff --git a/WeModPatcher/Utils/Updater.cs b/WeModPatcher/Utils/Updater.cs new file mode 100644 index 0000000..a1f286c --- /dev/null +++ b/WeModPatcher/Utils/Updater.cs @@ -0,0 +1,130 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using System.Net.Http; +using System.Windows; +using Newtonsoft.Json; + +namespace WeModPatcher.Utils +{ + public class GitHubRelease + { + public class AssetsType + { + public string Name { get; set; } + + [JsonProperty("browser_download_url")] + public string Url { get; set; } + } + + [JsonProperty("tag_name")] + public string TagName { get; set; } + + [JsonProperty("assets")] + public AssetsType[] Assets { get; set; } + + } + + public class Updater + { + private GitHubRelease _release = null; + private static readonly HttpClient _httpClient = new HttpClient() + { + DefaultRequestHeaders = + { + { "User-Agent", "GitHub-Updater" } + } + }; + + private static readonly string ApiUrl = $"https://api.github.com/repos/{Constants.Owner}/{Constants.RepoName}/releases/latest"; + public async Task CheckForUpdates() + { + try + { + var currentVersion = Assembly.GetExecutingAssembly().GetName().Version; + var response = await _httpClient.GetAsync(ApiUrl); + response.EnsureSuccessStatusCode(); + _release = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + + if (_release == null) + { + return false; + } + + var latestVersion = new Version(_release.TagName); + + return latestVersion > currentVersion; + } + catch (Exception e) + { + return false; + } + } + + public async Task Update() + { + if (_release == null) + { + throw new Exception("No release found"); + } + + var asset = _release.Assets.FirstOrDefault(o => o.Name.EndsWith(".exe")); + if(asset == null) + { + throw new Exception("No asset found"); + } + + // download to temp + var downloadPath = Path.Combine(Path.GetTempPath(), asset.Name); + + using(var response = await _httpClient.GetAsync(asset.Url)) + using(var fileStream = File.Create(downloadPath)) + { + response.EnsureSuccessStatusCode(); + await response.Content.CopyToAsync(fileStream); + } + + ApplyUpdate(downloadPath); + } + + + + private static void ApplyUpdate(string filePath) + { + try + { + var currentExecutable = Assembly.GetExecutingAssembly().Location; + + var psCommand = $"Start-Sleep -Seconds 2; " + + $"Copy-Item -Path '{filePath}' -Destination '{currentExecutable}' -Force; " + + $"Remove-Item -Path '{filePath}' -Force; " + + $"Start-Sleep -Seconds 1; " + + $"Start-Process -FilePath '{currentExecutable}';"; + + var startInfo = new ProcessStartInfo + { + FileName = "powershell.exe", + Arguments = $"-WindowStyle Hidden -ExecutionPolicy Bypass -Command \"{psCommand}\"", + UseShellExecute = true, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden + }; + + Process.Start(startInfo); + + Task.Delay(500).ContinueWith(_ => + { + App.Shutdown(); + }); + } + catch (Exception ex) + { + throw new Exception($"Update failed: {ex.Message}"); + } + } + + } +} \ No newline at end of file diff --git a/WeModPatcher/View/MainWindow/MainWindow.xaml b/WeModPatcher/View/MainWindow/MainWindow.xaml index 252800c..15f95dc 100644 --- a/WeModPatcher/View/MainWindow/MainWindow.xaml +++ b/WeModPatcher/View/MainWindow/MainWindow.xaml @@ -39,6 +39,18 @@ WeMod Patcher + + v 1.0.0 + + + - - + +