forked from nomi-san/parsec-vdd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.cs
80 lines (68 loc) · 2.44 KB
/
Updater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ParsecVDisplay
{
internal static class Updater
{
public static string DOWNLOAD_URL => $"https://github.com/{Program.GitHubRepo}/releases/latest";
static string GH_API_URL => $"https://api.github.com/repos/{Program.GitHubRepo}/releases/latest";
static Updater()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12;
}
public static Task<string> CheckUpdate()
{
return Task.Run(async () =>
{
var localVersion = new Version(Program.AppVersion);
var remoteVersion = await FetchLatestVersion();
if (remoteVersion.CompareTo(localVersion) > 0)
{
return remoteVersion.ToString();
}
return string.Empty;
});
}
static async Task<Version> FetchLatestVersion()
{
try
{
var json = await DownloadString(GH_API_URL);
if (!string.IsNullOrEmpty(json))
{
var tagNameRegex = new Regex("\"tag_name\":\\s+\"(.*)\"");
var match = tagNameRegex.Match(json);
if (match.Success && match.Groups.Count > 1)
{
var vtag = match.Groups[1].Value.ToLower();
if (vtag.StartsWith("v"))
vtag = vtag.Substring(1);
return new Version(vtag);
}
}
}
catch
{
}
return new Version(Program.AppVersion);
}
static async Task<string> DownloadString(string url)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36");
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true
};
return await client.GetStringAsync(url);
}
}
}
}