forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdater.cs
More file actions
85 lines (76 loc) · 2.79 KB
/
Updater.cs
File metadata and controls
85 lines (76 loc) · 2.79 KB
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
using System;
using System.Threading.Tasks;
using Octokit;
namespace NETworkManager.Update
{
/// <summary>
/// Updater to check if a new program version is available.
/// </summary>
public class Updater
{
#region Events
/// <summary>
/// Is triggered when update check is complete and an update is available.
/// </summary>
public event EventHandler<UpdateAvailableArgs> UpdateAvailable;
/// <summary>
/// Triggers the <see cref="UpdateAvailable"/> event.
/// </summary>
/// <param name="e">Passes <see cref="UpdateAvailableArgs"/> to the event.</param>
protected virtual void OnUpdateAvailable(UpdateAvailableArgs e)
{
UpdateAvailable?.Invoke(this, e);
}
/// <summary>
/// Is triggered when update check is complete and no update is available.
/// </summary>
public event EventHandler NoUpdateAvailable;
/// <summary>
/// Triggers the <see cref="NoUpdateAvailable"/> event.
/// </summary>
protected virtual void OnNoUpdateAvailable()
{
NoUpdateAvailable?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Is triggered when an error occurs during the update check.
/// </summary>
public event EventHandler Error;
/// <summary>
/// Triggers the <see cref="Error"/> event.
/// </summary>
protected virtual void OnError()
{
Error?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Methods
/// <summary>
/// Checks on GitHub whether a new version of the program is available
/// </summary>
/// <param name="userName">GitHub username like "BornToBeRoot".</param>
/// <param name="projectName">GitHub repository like "NETworkManager".</param>
/// <param name="currentVersion">Version like 1.2.0.0.</param>
public void CheckOnGitHub(string userName, string projectName, Version currentVersion)
{
Task.Run(() =>
{
try
{
var client = new GitHubClient(new ProductHeaderValue(userName + "_" + projectName));
var latestVersion = new Version(client.Repository.Release.GetLatest(userName, projectName).Result.TagName);
// Compare versions (tag=2021.2.15.0, version=2021.2.15.0)
if (latestVersion > currentVersion)
OnUpdateAvailable(new UpdateAvailableArgs(latestVersion));
else
OnNoUpdateAvailable();
}
catch
{
OnError();
}
});
}
#endregion
}
}