-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
171 lines (159 loc) · 5.9 KB
/
Utils.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Updater
{
public static class Utils
{
private static string _softwareName;
public static string SoftwareName
{
set => _softwareName = value;
get
{
if (_softwareName == null)
{
throw new NullReferenceException($"You must set {nameof(SoftwareName)} before using it");
}
return _softwareName;
}
}
private static string _repoName;
public static string RepoName
{
set => _repoName = value;
get
{
if (_repoName == null)
{
throw new NullReferenceException($"You must set {nameof(RepoName)} before using it");
}
return _repoName;
}
}
private static Version _currentVersion;
public static Version CurrentVersion
{
set => _currentVersion = value;
get
{
if (_currentVersion == null)
{
throw new NullReferenceException($"You must set {nameof(SoftwareName)} before using it");
}
return _currentVersion;
}
}
public static async void CheckUpdate(bool showMessage = false)
{
if (!IsConnectInternet()) return;
try
{
var httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10)
};
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(SoftwareName, CurrentVersion.ToString()));
var httpResponseMessage =
await httpClient.GetAsync(new Uri($"https://api.github.com/repos/{RepoName}/releases/latest"));
if (httpResponseMessage.StatusCode != HttpStatusCode.OK)
{
var body = await httpResponseMessage.Content.ReadAsStringAsync();
Show($"[{httpResponseMessage.StatusCode}]请求发送失败,错误信息:\n{body}");
return;
}
var result =
Jil.JSON.Deserialize<GithubRelease>(
new StreamReader(await httpResponseMessage.Content.ReadAsStreamAsync()));
var remoteVersion = Version.Parse(Convert.ToString(result.tag_name));
if (remoteVersion > CurrentVersion)
{
foreach (var asset in result.assets)
{
if (asset.content_type == "application/x-msdownload")
{
var dialogResult = MessageBox.Show(caption: @"Wow! Such Impressive",
text: $"新车已发车 v{remoteVersion},上车!",
buttons: MessageBoxButtons.YesNo, icon: MessageBoxIcon.Asterisk);
if (dialogResult != DialogResult.Yes) return;
var formUpdater = new FormUpdater(Application.ExecutablePath, remoteVersion,
Convert.ToString(asset.browser_download_url));
formUpdater.ShowDialog();
return;
}
}
}
else
{
Show($"{CurrentVersion}已为最新版");
return;
}
Show("无可用的资源,请向项目维护人咨询具体情况");
}
catch (TaskCanceledException)
{
Show("请求超时");
}
catch (Exception e)
{
Show($"[{e.GetType()}]请求失败: {e.Message}");
}
void Show(string message)
{
if (showMessage)
MessageBox.Show(message, "程序更新");
}
}
public static bool CheckUpdateWeekly(string program)
{
var reg = RegistryStorage.Load(@"Software\" + program, "LastCheck");
if (string.IsNullOrEmpty(reg))
{
RegistryStorage.Save(DateTime.Now.ToString(CultureInfo.InvariantCulture), @"Software\" + program, "LastCheck");
return false;
}
var lastCheckTime = DateTime.Parse(reg);
if (DateTime.Now - lastCheckTime > new TimeSpan(7, 0, 0, 0))
{
CheckUpdate();
RegistryStorage.Save(DateTime.Now.ToString(CultureInfo.InvariantCulture), @"Software\" + program, "LastCheck");
return true;
}
return false;
}
private static bool IsConnectInternet()
{
return InternetGetConnectedState(0, 0);
}
[DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(int description, int reservedValue);
}
public class GithubAsset
{
public string url;
public string name;
public string content_type;
public long size;
public string created_at;
public string updated_at;
public string browser_download_url;
}
public class GithubRelease
{
public string url;
public string assets_url;
public string tag_name;
public string name;
public List<GithubAsset> assets;
public string zipball_url;
public string body;
}
}