-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateManager.cs
More file actions
70 lines (55 loc) · 2.15 KB
/
UpdateManager.cs
File metadata and controls
70 lines (55 loc) · 2.15 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
using Newtonsoft.Json;
using TerrariaApi.Server;
using TShockAPI;
namespace BanGuard;
public static class UpdateManager
{
public static async Task<Version?> RequestLatestVersion()
{
string url = "https://api.github.com/repos/zyrafaq/BanGuard-plugin/releases/latest";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.TryParseAdd("request"); // Set a user agent header
try
{
var response = await client.GetStringAsync(url);
dynamic? latestRelease = JsonConvert.DeserializeObject<dynamic>(response);
if (latestRelease == null) return null;
string tag = latestRelease.tag_name;
tag = tag.Trim('v');
string[] nums = tag.Split('.');
Version version = new Version(int.Parse(nums[0]),
int.Parse(nums[1]),
int.Parse(nums[2])
);
return version;
}
catch
{
TShock.Log.ConsoleError("[BanGuard] An error occured while checking for updates.");
}
}
return null;
}
public static async Task<bool> IsUpToDate(TerrariaPlugin plugin)
{
Version? latestVersion = await RequestLatestVersion();
Version curVersion = plugin.Version;
return latestVersion != null && curVersion >= latestVersion;
}
public static async void CheckUpdateVerbose(TerrariaPlugin? plugin)
{
if (plugin == null) return;
TShock.Log.ConsoleInfo("[BanGuard] Checking for updates...");
bool isUpToDate = await IsUpToDate(plugin);
if (isUpToDate)
{
TShock.Log.ConsoleInfo("[BanGuard] The plugin is up to date!");
}
else
{
TShock.Log.ConsoleError("[BanGuard] The plugin is not up to date.\n" +
"Please visit https://github.com/zyrafaq/BanGuard-plugin/releases/latest to download the latest version.");
}
}
}