-
Notifications
You must be signed in to change notification settings - Fork 16
/
UpdateChecker.cs
90 lines (81 loc) · 3.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 RealTimePPDisplayer
{
static class UpdateChecker
{
private static readonly Regex NAME_REGEX = new Regex(@"""name"":\s*""v(\d+\.\d+\.\d+)(?:\((\d+\.\d+\.\d+)\))?""");
private const string LATEST_RELEASE_URL = "https://api.github.com/repos/OsuSync/RealTimePPDisplayer/releases/latest";
[DllImport("oppai.dll",EntryPoint = "oppai_version")]
static extern void GetOppaiVersion(out int major, out int minor, out int patch);
public static void CheckUpdate()
{
try
{
string data = GetHttpData(LATEST_RELEASE_URL);
var groups = NAME_REGEX.Match(data).Groups;
string rtpp_version = groups[1].Value;
bool has_update = CheckRtppUpdate(rtpp_version);
if (!string.IsNullOrEmpty(groups[2].Value))
{
string oppai_version = groups[2].Value;
has_update = has_update || CheckOppaiUpdate(oppai_version);
}
if(has_update)
{
Sync.Tools.IO.DefaultIO.WriteColor(DefaultLanguage.CHECK_GOTO_RELEASE_PAGE_HINT,ConsoleColor.Yellow);
}
}
catch (Exception e)
{
Sync.Tools.IO.DefaultIO.WriteColor(e.ToString(), ConsoleColor.Red);
}
}
private static bool CheckOppaiUpdate(string version)
{
Version ver = Version.Parse(version);
GetOppaiVersion(out int major, out int minor, out int patch);
Version selfVer = new Version(major, minor, patch);
if (ver > selfVer)
{
Sync.Tools.IO.DefaultIO.WriteColor(
string.Format(DefaultLanguage.CHECK_OPPAI_UPDATE, ver),
ConsoleColor.Yellow);
return true;
}
return false;
}
private static bool CheckRtppUpdate(string tag)
{
Version ver = Version.Parse(tag);
Version selfVer = Version.Parse(RealTimePPDisplayerPlugin.VERSION);
if (ver > selfVer)
{
Sync.Tools.IO.DefaultIO.WriteColor(
string.Format(DefaultLanguage.CHECK_RTPPD_UPDATE, ver),
ConsoleColor.Yellow);
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();
}
}
}
}