-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUpdateChecker.cs
65 lines (58 loc) · 2.11 KB
/
UpdateChecker.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace OsuRTDataProvider
{
static class UpdateChecker
{
private static readonly Regex NAME_REGEX = new Regex(@"""name"":\s*""v(\d+\.\d+\.\d+)""");
private const string LATEST_RELEASE_URL = "https://api.github.com/repos/OsuSync/OsuRTDataProvider/releases/latest";
public static void CheckUpdate()
{
try
{
string data = GetHttpData(LATEST_RELEASE_URL);
var groups = NAME_REGEX.Match(data).Groups;
string ortdp_version = groups[1].Value;
bool has_update = CheckRtppUpdate(ortdp_version);
if(has_update)
{
Logger.Warn(DefaultLanguage.CHECK_GOTO_RELEASE_PAGE_HINT);
}
}
catch (Exception e)
{
Logger.Error(e.ToString());
}
}
private static bool CheckRtppUpdate(string tag)
{
Version ver = Version.Parse(tag);
Version selfVer = Version.Parse(OsuRTDataProviderPlugin.VERSION);
if (ver > selfVer)
{
Logger.Warn(string.Format(DefaultLanguage.LANG_CHECK_ORTDP_UPDATE, ver));
return true;
}
return false;
}
private static string GetHttpData(string url)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
wReq.UserAgent = "OsuSync";
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
using (StreamReader reader = new StreamReader(respStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}