[#8] Add auto update checker

This commit is contained in:
2024-03-04 21:19:08 +01:00
parent ac4026571e
commit 7e1843a1b3
11 changed files with 231 additions and 4 deletions

View File

@ -12,6 +12,10 @@ using System.Text;
using System.Numerics;
using Elwig.Models.Entities;
using Elwig.Helpers.Billing;
using System.Runtime.InteropServices;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.IO;
namespace Elwig.Helpers {
public static partial class Utils {
@ -61,6 +65,8 @@ namespace Elwig.Helpers {
return PhoneNrTypes.Where(t => t.Key == type).Select(t => t.Value).FirstOrDefault(type);
}
private static readonly string[] TempWildcards = ["*.html", "*.pdf", "*.exe"];
private static readonly ushort[] Crc16ModbusTable = [
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
@ -302,7 +308,7 @@ namespace Elwig.Helpers {
public static string GetSign<T>(T number) where T : INumber<T>
=> T.Sign(number) switch {
< 0 => "\u2212", // minus
0 => "\u00b1", // plus minus
0 => "\u00b1", // plus minus
> 0 => "+",
};
@ -378,5 +384,33 @@ namespace Elwig.Helpers {
.Select(s => new Varibute(s, varieties, attributes, cultivations))
.ToList();
}
[LibraryImport("wininet.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool InternetGetConnectedState(out int description, int reservedValue);
public static bool HasInternetConnectivity() {
return InternetGetConnectedState(out var _, 0);
}
public static async Task<(string Version, string Url, long Size)?> GetLatestInstallerUrl(string url) {
try {
using var client = new HttpClient() {
Timeout = TimeSpan.FromSeconds(5),
};
var res = JsonNode.Parse(await client.GetStringAsync(url));
var data = res!["data"]![0]!;
return ((string)data["version"]!, (string)data["url"]!, (int)data["size"]!);
} catch {
return null;
}
}
public static void CleanupTempFiles() {
var dir = new DirectoryInfo(App.TempPath);
foreach (var file in TempWildcards.SelectMany(dir.EnumerateFiles)) {
file.Delete();
}
}
}
}