|
1 | 1 | using CSharpRegexTools4Npp.PluginInfrastructure; |
| 2 | +using Newtonsoft.Json.Linq; |
2 | 3 | using RegexDialog; |
3 | 4 | using System; |
| 5 | +using System.Diagnostics; |
4 | 6 | using System.Drawing; |
5 | 7 | using System.Linq; |
| 8 | +using System.Net; |
| 9 | +using System.Net.Http; |
6 | 10 | using System.Reflection; |
7 | 11 | using System.Runtime.InteropServices; |
8 | 12 | using System.Windows; |
@@ -84,21 +88,71 @@ internal static void SetToolBarIcon() |
84 | 88 | Marshal.FreeHGlobal(pTbIcons); |
85 | 89 | } |
86 | 90 |
|
| 91 | + private static async void CheckUpdates(RegExToolDialog dialog) |
| 92 | + { |
| 93 | + double hoursFromLastCheck = Math.Abs((DateTime.Now - Config.Instance.LastUpdateCheck).TotalHours); |
| 94 | + |
| 95 | + //if (hoursFromLastCheck > 8) |
| 96 | + if (true) |
| 97 | + { |
| 98 | + ServicePointManager.ServerCertificateValidationCallback += (_, __, ___, ____) => true; |
| 99 | + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; |
| 100 | + |
| 101 | + try |
| 102 | + { |
| 103 | + HttpClient client = new HttpClient(); |
| 104 | + client.DefaultRequestHeaders.Add("Accept-Language", "en-US;q=0.5,en;q=0.3"); |
| 105 | + client.DefaultRequestHeaders.Add("User-Agent", "C# App"); |
| 106 | + |
| 107 | + var response = await client.GetAsync("https://api.github.com/repos/codingseb/CSharpRegexTools4Npp/releases/latest").ConfigureAwait(true); |
| 108 | + |
| 109 | + string responseText = await response.Content.ReadAsStringAsync(); |
| 110 | + |
| 111 | + var jsonResult = JObject.Parse(responseText); |
| 112 | + |
| 113 | + int[] latestVersion = jsonResult["name"].ToString().Split('.').Select(digit => int.Parse(digit.Trim())).ToArray(); |
| 114 | + int[] currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.').Select(digit => int.Parse(digit.Trim())).ToArray(); |
| 115 | + |
| 116 | + Debug.WriteLine($"{latestVersion} - {currentVersion}"); |
| 117 | + |
| 118 | + for(int i = 0; i < latestVersion.Length && i < currentVersion.Length;i++) |
| 119 | + { |
| 120 | + if(latestVersion[i] > currentVersion[i]) |
| 121 | + { |
| 122 | + Config.Instance.UpdateAvailable = true; |
| 123 | + Config.Instance.UpdateURL = "https://github.com/codingseb/CSharpRegexTools4Npp/releases"; |
| 124 | + break; |
| 125 | + } |
| 126 | + else if(latestVersion[i] < currentVersion[i]) |
| 127 | + { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + catch |
| 133 | + { } |
| 134 | + } |
| 135 | + } |
| 136 | + |
87 | 137 | public static void ShowTheDialog() |
88 | 138 | { |
89 | 139 | try |
90 | 140 | { |
91 | 141 | AppDomain.CurrentDomain.SetupInformation.PrivateBinPath = @"plugins\CSharpRegexTools4Npp"; |
92 | 142 |
|
93 | | - IntPtr hWnd = FindWindow(null, "C# Regex Tools - " + Assembly.GetCallingAssembly().GetName().Version.ToString()); |
| 143 | + IntPtr hWnd = FindWindow(null, "C# Regex Tools - " + Assembly.GetExecutingAssembly().GetName().Version.ToString()); |
94 | 144 |
|
95 | 145 | if (hWnd.ToInt64() > 0) |
96 | 146 | { |
97 | 147 | SetForegroundWindow(hWnd); |
98 | 148 | } |
99 | 149 | else |
100 | 150 | { |
101 | | - var dialog = new RegExToolDialog |
| 151 | + RegExToolDialog dialog = null; |
| 152 | + |
| 153 | + RegExToolDialog.InitIsOK = () => CheckUpdates(dialog); |
| 154 | + |
| 155 | + dialog = new RegExToolDialog |
102 | 156 | { |
103 | 157 | GetText = () => BNpp.Text, |
104 | 158 |
|
@@ -157,7 +211,7 @@ public static void ShowTheDialog() |
157 | 211 | result = false; |
158 | 212 | } |
159 | 213 |
|
160 | | - hWnd = FindWindow(null, "C# Regex Tool - " + Assembly.GetCallingAssembly().GetName().Version.ToString()); |
| 214 | + hWnd = FindWindow(null, "C# Regex Tool - " + Assembly.GetExecutingAssembly().GetName().Version.ToString()); |
161 | 215 | if (hWnd.ToInt64() > 0) |
162 | 216 | { |
163 | 217 | SetForegroundWindow(hWnd); |
|
0 commit comments