|
1 | 1 | // NPP plugin platform for .Net v0.91.57 by Kasper B. Graversen etc.
|
2 | 2 | using CSharpRegexTools4Npp.PluginInfrastructure;
|
3 | 3 | using CSharpRegexTools4Npp.Utils;
|
| 4 | +using Newtonsoft.Json.Linq; |
4 | 5 | using RegexDialog;
|
5 | 6 | using System;
|
6 | 7 | using System.Diagnostics;
|
7 | 8 | using System.Drawing;
|
8 | 9 | using System.IO;
|
9 | 10 | using System.Linq;
|
| 11 | +using System.Net.Http; |
| 12 | +using System.Net; |
10 | 13 | using System.Reflection;
|
11 | 14 | using System.Runtime.InteropServices;
|
12 | 15 | using System.Windows;
|
@@ -90,7 +93,7 @@ public static void ShowTheDialog()
|
90 | 93 | {
|
91 | 94 | RegExToolDialog dialog = null;
|
92 | 95 |
|
93 |
| - //RegExToolDialog.InitIsOK = () => CheckUpdates(dialog); |
| 96 | + RegExToolDialog.InitIsOK = () => CheckUpdates(dialog); |
94 | 97 |
|
95 | 98 | dialog = new RegExToolDialog
|
96 | 99 | {
|
@@ -215,6 +218,52 @@ internal static void SetToolBarIcon()
|
215 | 218 | }
|
216 | 219 | }
|
217 | 220 |
|
| 221 | + private static async void CheckUpdates(RegExToolDialog dialog) |
| 222 | + { |
| 223 | + double hoursFromLastCheck = Math.Abs((DateTime.Now - Config.Instance.LastUpdateCheck).TotalHours); |
| 224 | + |
| 225 | + //if (hoursFromLastCheck > 8) |
| 226 | + if (true) |
| 227 | + { |
| 228 | + ServicePointManager.ServerCertificateValidationCallback += (_, __, ___, ____) => true; |
| 229 | + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; |
| 230 | + |
| 231 | + try |
| 232 | + { |
| 233 | + HttpClient client = new(); |
| 234 | + client.DefaultRequestHeaders.Add("Accept-Language", "en-US;q=0.5,en;q=0.3"); |
| 235 | + client.DefaultRequestHeaders.Add("User-Agent", "C# App"); |
| 236 | + |
| 237 | + var response = await client.GetAsync("https://api.github.com/repos/codingseb/CSharpRegexTools4Npp/releases/latest").ConfigureAwait(true); |
| 238 | + |
| 239 | + string responseText = await response.Content.ReadAsStringAsync(); |
| 240 | + |
| 241 | + var jsonResult = JObject.Parse(responseText); |
| 242 | + |
| 243 | + int[] latestVersion = jsonResult["name"].ToString().Split('.').Select(digit => int.Parse(digit.Trim())).ToArray(); |
| 244 | + int[] currentVersion = typeof(RegExToolDialog).Assembly.GetName().Version.ToString().Split('.').Select(digit => int.Parse(digit.Trim())).ToArray(); |
| 245 | + |
| 246 | + Debug.WriteLine($"{string.Join(".", latestVersion)} - {string.Join(".", currentVersion)}"); |
| 247 | + |
| 248 | + for (int i = 0; i < latestVersion.Length && i < currentVersion.Length; i++) |
| 249 | + { |
| 250 | + if (latestVersion[i] > currentVersion[i]) |
| 251 | + { |
| 252 | + Config.Instance.UpdateAvailable = true; |
| 253 | + Config.Instance.UpdateURL = "https://github.com/codingseb/CSharpRegexTools4Npp/releases"; |
| 254 | + break; |
| 255 | + } |
| 256 | + else if (latestVersion[i] < currentVersion[i]) |
| 257 | + { |
| 258 | + break; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + catch |
| 263 | + { } |
| 264 | + } |
| 265 | + } |
| 266 | + |
218 | 267 | /// <summary>
|
219 | 268 | /// open GitHub repo with the web browser
|
220 | 269 | /// </summary>
|
|
0 commit comments