-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateChecker.cs
More file actions
142 lines (121 loc) · 6.02 KB
/
Copy pathUpdateChecker.cs
File metadata and controls
142 lines (121 loc) · 6.02 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
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
using Rage;
using System;
using System.Net.Http;
using System.Text.RegularExpressions;
namespace SSStuartCallouts
{
public static class UpdateChecker
{
private static readonly string pluginKey = "SSStuartCallouts";
private static readonly InitializationFile updateSettings = new InitializationFile("Plugins/SSStuart_UpdateChecker.ini");
private static readonly string url = "https://ssstuart.net/api/GTAModVersion/SSStuart%20Callouts";
private static readonly HttpClient httpClient = new HttpClient();
private static Version lastVersion = null;
private static readonly Version currentVersion = new Version(Main.pluginVersion);
private static string updateAvailable = "";
public static void CheckForUpdates()
{
if (!CheckUpdateSettings())
return;
System.Threading.Tasks.Task.Run(async () =>
{
updateAvailable = await CheckUpdate();
});
GameFiber.StartNew(updateNotification);
void updateNotification()
{
do
{
GameFiber.Yield();
if (updateAvailable == "yes")
{
DisplayUpdateNotification();
}
} while (updateAvailable == "");
}
}
private static bool CheckUpdateSettings()
{
// Dynamically create settings (+ hack for comments)
if (!updateSettings.DoesKeyExist("General", "# Number of days between update checks. 0"))
updateSettings.Write("General", "# Number of days between update checks. 0", " disable update checks globally.");
if (!updateSettings.DoesKeyExist("General", "UpdateChecksFrequency"))
updateSettings.Write("General", "UpdateChecksFrequency", 1);
if (!updateSettings.DoesKeyExist("IndividualPlugins", "# Whether to check for updates for each plugins. True"))
updateSettings.Write("IndividualPlugins", "# Whether to check for updates for each plugins. True", " enable update checks according to the configured frequency.");
if (!updateSettings.DoesKeyExist("IndividualPlugins", pluginKey))
updateSettings.Write("IndividualPlugins", pluginKey, true);
if (!updateSettings.DoesKeyExist("Datas", "# The following values should not be modified ("))
updateSettings.Write("Datas", "# The following values should not be modified (", "");
if (!updateSettings.DoesKeyExist("Datas", $"{pluginKey}_lastUpdate"))
updateSettings.Write("Datas", $"{pluginKey}_lastUpdate", DateTime.Now.AddDays(-1).ToString("O"));
bool shouldCheckForUpdate = updateSettings.ReadBoolean("IndividualPlugins", pluginKey);
int checkFrequency = updateSettings.ReadInt16("General", "UpdateChecksFrequency");
// Check if updates are disabled (globally or for this plugin)
if (!shouldCheckForUpdate || checkFrequency == 0)
{
Game.LogTrivial($"[{Main.pluginName}] Update check disabled");
return false;
}
// Check if the last update check was made more than one "interval" ago
DateTime lastCheck = DateTime.Parse(updateSettings.ReadString("Datas", $"{pluginKey}_lastUpdate"));
if (lastCheck.AddDays(checkFrequency) < DateTime.Now)
{
updateSettings.Write("Datas", $"{pluginKey}_lastUpdate", DateTime.Now.ToString("O"));
return true;
}
else
{
Game.LogTrivial($"[{Main.pluginName}] Updates already checked recently");
return false;
}
}
private static async System.Threading.Tasks.Task<string> CheckUpdate()
{
try
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseMessage = await response.Content.ReadAsStringAsync();
Match m = new Regex("last_version\":\"([\\d.]+)\"").Match(responseMessage);
if (m.Success)
{
System.Text.RegularExpressions.Group g = m.Groups[1];
CaptureCollection cc = g.Captures;
Capture c = cc[0];
lastVersion = new Version(c.ToString());
Game.LogTrivial($"[{Main.pluginName}] Current version: {currentVersion}, Latest version: {lastVersion}");
if (currentVersion < lastVersion)
{
Game.LogTrivial($"[{Main.pluginName}] Update available ! Current version: {currentVersion}, Latest version: {lastVersion}");
return "yes";
} else if (currentVersion >= lastVersion)
{
Game.LogTrivial($"[{Main.pluginName}] You are using the latest version ({currentVersion}).");
return "no";
}
} else
{
Game.LogTrivial($"[{Main.pluginName}] Update check failed: Could not parse version from response : {responseMessage}");
return "error";
}
}
catch (Exception ex)
{
Game.LogTrivial($"[{Main.pluginName}] Update check failed: {ex.InnerException}");
return "error";
}
return "error";
}
private static void DisplayUpdateNotification()
{
do
{
GameFiber.Yield();
GameFiber.Sleep(5000);
} while (Game.IsLoading);
Game.DisplayNotification("mpturf", "swap", Main.pluginName, $"V {lastVersion}", $"~y~Update available !");
}
}
}