From cae379fbb703a11d738a39e2a44664385478876d Mon Sep 17 00:00:00 2001 From: akarin Date: Sun, 22 Jan 2023 16:26:35 +0900 Subject: [PATCH] Remove CheckUpdate feature Signed-off-by: akarin --- OKEGui/OKEGui/App.xaml.cs | 1 - OKEGui/OKEGui/Gui/MainWindow.xaml.cs | 14 --- OKEGui/OKEGui/OKEGui.csproj | 1 - OKEGui/OKEGui/Utils/Initializer.cs | 28 ----- OKEGui/OKEGui/Utils/Updater.cs | 172 --------------------------- 5 files changed, 216 deletions(-) delete mode 100644 OKEGui/OKEGui/Utils/Updater.cs diff --git a/OKEGui/OKEGui/App.xaml.cs b/OKEGui/OKEGui/App.xaml.cs index 57178579..d88c1265 100644 --- a/OKEGui/OKEGui/App.xaml.cs +++ b/OKEGui/OKEGui/App.xaml.cs @@ -19,7 +19,6 @@ private void AppStartup(object sender, StartupEventArgs e) Initializer.ConfigLogger(); Initializer.WriteConfig(); Initializer.ClearOldLogs(); - Initializer.CheckUpdate(); Logger.Info("程序正常启动"); } else diff --git a/OKEGui/OKEGui/Gui/MainWindow.xaml.cs b/OKEGui/OKEGui/Gui/MainWindow.xaml.cs index 8580ea22..980630c4 100644 --- a/OKEGui/OKEGui/Gui/MainWindow.xaml.cs +++ b/OKEGui/OKEGui/Gui/MainWindow.xaml.cs @@ -25,7 +25,6 @@ namespace OKEGui public partial class MainWindow : Window { private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); - private SystemMenu _systemMenu; public int WorkerCount = 0; public TaskManager tm = new TaskManager(); @@ -65,25 +64,12 @@ public MainWindow() } WorkerNumber.Text = "工作单元:" + WorkerCount.ToString(); - // 初始化更新菜单 - _systemMenu = new SystemMenu(this); - _systemMenu.AddCommand("检查更新(&U)", () => { Updater.CheckUpdate(true); }, true); - if (Initializer.Config.memoryTotal == WmiUtils.GetTotalPhysicalMemory()) { TxtFreeMemory.Text = Initializer.Config.memoryLimit.ToString(); } } - protected override void OnSourceInitialized(EventArgs e) - { - base.OnSourceInitialized(e); - if (PresentationSource.FromVisual(this) is HwndSource hwndSource) - { - hwndSource.AddHook(_systemMenu.HandleMessage); - } - } - private void Checkbox_Changed(object sender, RoutedEventArgs e) { if (!wm.IsRunning) diff --git a/OKEGui/OKEGui/OKEGui.csproj b/OKEGui/OKEGui/OKEGui.csproj index 8a262d7d..308ff94e 100644 --- a/OKEGui/OKEGui/OKEGui.csproj +++ b/OKEGui/OKEGui/OKEGui.csproj @@ -161,7 +161,6 @@ - diff --git a/OKEGui/OKEGui/Utils/Initializer.cs b/OKEGui/OKEGui/Utils/Initializer.cs index 9c8cace4..12d21084 100644 --- a/OKEGui/OKEGui/Utils/Initializer.cs +++ b/OKEGui/OKEGui/Utils/Initializer.cs @@ -206,33 +206,5 @@ public static void ClearOldLogs() .ForEach(f => f.Delete()); } } - - public static void CheckUpdate() - { - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; - Updater.SoftwareName = "OKEGui"; - Updater.CurrentVersion = Assembly.GetExecutingAssembly().GetName().Version; - Updater.RepoName = "AmusementClub/OKEGui"; - try - { - var reg = RegistryStorage.Load(null, "LastCheck"); - if (string.IsNullOrEmpty(reg)) - { - RegistryStorage.Save(DateTime.Now.ToString(CultureInfo.InvariantCulture), null, "LastCheck"); - return; - } - - var lastCheckTime = DateTime.Parse(reg); - if (DateTime.Now - lastCheckTime > new TimeSpan(7, 0, 0, 0)) - { - Updater.CheckUpdate(); - RegistryStorage.Save(DateTime.Now.ToString(CultureInfo.InvariantCulture), null, "LastCheck"); - } - } - catch (Exception ex) - { - Logger.Error(ex, "Failed to check update"); - } - } } } diff --git a/OKEGui/OKEGui/Utils/Updater.cs b/OKEGui/OKEGui/Utils/Updater.cs deleted file mode 100644 index e3dbf56e..00000000 --- a/OKEGui/OKEGui/Utils/Updater.cs +++ /dev/null @@ -1,172 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Runtime.InteropServices; -using System.Threading.Tasks; -using System.Windows.Forms; -using Newtonsoft.Json; -// ReSharper disable UnusedMember.Global - -namespace OKEGui.Utils -{ - static class Updater - { - private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); - private static readonly string Title = "OKEGui Updater"; - - private static string _softwareName; - public static string SoftwareName - { - set => _softwareName = value; - get - { - if (_softwareName == null) - { - throw new NullReferenceException($"You must set {nameof(SoftwareName)} before using it"); - } - return _softwareName; - } - } - - private static string _repoName; - public static string RepoName - { - set => _repoName = value; - get - { - if (_repoName == null) - { - throw new NullReferenceException($"You must set {nameof(RepoName)} before using it"); - } - return _repoName; - } - } - - private static Version _currentVersion; - public static Version CurrentVersion - { - set => _currentVersion = value; - get - { - if (_currentVersion == null) - { - throw new NullReferenceException($"You must set {nameof(CurrentVersion)} before using it"); - } - return _currentVersion; - } - } - - - public static async void CheckUpdate(bool interactive=false) - { - if (!IsConnectInternet()) return; - try - { - var httpClient = new HttpClient - { - Timeout = TimeSpan.FromSeconds(10) - }; - httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(SoftwareName, CurrentVersion.ToString())); - var httpResponseMessage = - await httpClient.GetAsync(new Uri($"https://api.github.com/repos/{RepoName}/releases/latest")); - if (httpResponseMessage.StatusCode != HttpStatusCode.OK) - { - var body = await httpResponseMessage.Content.ReadAsStringAsync(); - Show($"[{httpResponseMessage.StatusCode}]请求发送失败,错误信息:\n{body}", Logger.Error); - return; - } - - GithubRelease result; - using (var streamReader = new StreamReader(await httpResponseMessage.Content.ReadAsStreamAsync())) - { - result = JsonConvert.DeserializeObject(streamReader.ReadToEnd()); - } - - var remoteVersion = Version.Parse(Convert.ToString(result.tag_name)); - Logger.Info($"本地版本: v{CurrentVersion}, 远端版本: v{remoteVersion}"); - if (remoteVersion <= CurrentVersion) - { - Show($"{CurrentVersion}已为最新版", Logger.Info); - return; - } - - var validAsset = result.assets.FirstOrDefault(asset => - asset.content_type == "application/x-msdownload" || - asset.content_type == "application/x-zip-compressed"); - if (validAsset != null) - { - Logger.Info($"发现新版本v{remoteVersion}, 下载地址: {validAsset.browser_download_url}"); - var dialogResult = MessageBox.Show(caption: Title, - text: $"发现新版本v{remoteVersion},是否现在下载", - buttons: MessageBoxButtons.YesNo, icon: MessageBoxIcon.Asterisk); - if (dialogResult != DialogResult.Yes) return; - var proc = new Process - { - StartInfo = { UseShellExecute = true, FileName = validAsset.browser_download_url } - }; - proc.Start(); - } - else - { - Show($"发现新版本v{remoteVersion},但无可用的资源", Logger.Error); - } - } - catch (TaskCanceledException) - { - Show("请求超时", Logger.Error); - } - catch (Exception e) - { - Show($"[{e.GetType()}]请求失败: {e.Message},${e.InnerException}", Logger.Error); - } - - void Show(string message, Action logger) - { - if (interactive) - { - MessageBox.Show(message, Title); - } - else - { - logger(message); - } - } - } - - private static bool IsConnectInternet() - { - return InternetGetConnectedState(0, 0); - } - - [DllImport("wininet.dll")] - private static extern bool InternetGetConnectedState(int description, int reservedValue); - - public class GithubAsset - { - public string url; - public string name; - public string content_type; - public long size; - public string created_at; - public string updated_at; - public string browser_download_url; - } - - public class GithubRelease - { - public string url; - public string assets_url; - public string tag_name; - public string name; - public List assets; - public string zipball_url; - public string body; - } - } -}