-
Notifications
You must be signed in to change notification settings - Fork 12
/
CS2AutoUpdater.cs
243 lines (197 loc) · 9.68 KB
/
CS2AutoUpdater.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System.Text.Json;
using System.Text.RegularExpressions;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
namespace CS2AutoUpdater
{
[MinimumApiVersion(14)]
public partial class CS2AutoUpdater : BasePlugin
{
public override string ModuleName => "AutoUpdater";
public override string ModuleAuthor => "DRANIX";
public override string ModuleDescription => "Auto Updater for Counter-Strike 2.";
public override string ModuleVersion => "1.0.3";
private const string steamApiEndpoint = "http://api.steampowered.com/ISteamApps/UpToDateCheck/v0001/?appid=730&version={steamInfPatchVersion}";
private static CounterStrikeSharp.API.Modules.Timers.Timer updateCheck = null!;
private static readonly bool[] playersNotified = new bool[Server.MaxPlayers];
private static float updateFoundTime;
private static bool updateAvailable;
private static int requiredVersion;
private static bool isMapLoading;
public override void Load(bool hotReload)
{
Configuration.LoadConfig(this.ModuleDirectory);
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawn);
ConVar sv_hibernate_when_empty = ConVar.Find("sv_hibernate_when_empty")!;
this.RegisterListener<Listeners.OnGameServerSteamAPIActivated>(OnGameServerSteamAPIActivated);
this.RegisterListener<Listeners.OnMapStart>(OnMapStart);
this.RegisterListener<Listeners.OnMapEnd>(OnMapEnd);
this.RegisterListener<Listeners.OnClientConnected>(playerSlot => { playersNotified[playerSlot + 1] = false; });
this.RegisterListener<Listeners.OnClientDisconnectPost>(playerSlot => { playersNotified[playerSlot + 1] = false; });
updateCheck = AddTimer(Configuration.config.UpdateCheckInterval, CheckServerForUpdate, TimerFlags.REPEAT);
if (sv_hibernate_when_empty.GetPrimitiveValue<bool>()) ConsoleLog("'sv_hibernate_when_empty' is enabled. This plugin might not work as expected.", ConsoleColor.Yellow);
}
[GameEventHandler]
private static HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
if (!updateAvailable) return HookResult.Continue;
CCSPlayerController player = @event.Userid;
if (!player.IsValid || player.IsBot || player.TeamNum < (byte)CsTeam.Spectator) return HookResult.Continue;
if (playersNotified[player.EntityIndex!.Value.Value]) return HookResult.Continue;
NotifyPlayer(player);
return HookResult.Continue;
}
private async void CheckServerForUpdate()
{
try
{
if (!await IsUpdateAvailable()) return;
}
catch (Exception ex)
{
ConsoleLog($"Failed to request Steam API for updates: {ex.Message}");
return;
}
updateCheck?.Kill();
updateAvailable = true;
updateFoundTime = Server.CurrentTime;
List<CCSPlayerController> players = GetCurrentPlayers();
if (players.Count <= Configuration.config.MinimumPlayersBeforeInstantRestart)
{
PrepareServerShutdown();
return;
}
if (!isMapLoading) foreach (var player in players) NotifyPlayer(player);
AddTimer(Configuration.config.RestartDelay, PrepareServerShutdown);
}
private static void NotifyPlayer(CCSPlayerController player)
{
int remainingTime = Configuration.config.RestartDelay - (int)(Server.CurrentTime - updateFoundTime);
if (remainingTime < 0) remainingTime = 1;
string timeToRestart = remainingTime >= 60 ? $"{remainingTime / 60} minute{(remainingTime >= 120 ? "s" : "")}" : $"{remainingTime} second{(remainingTime > 1 ? "s" : "")}";
player.PrintToChat($" {Configuration.config.ChatTag} New Counter-Strike 2 update released (Version: {requiredVersion}) the server will restart in {timeToRestart}");
playersNotified[player.EntityIndex!.Value.Value] = true;
}
private void OnGameServerSteamAPIActivated()
{
ConsoleLog("Steam API activated. Server will be checked for updates.");
}
private static void OnMapStart(string mapName)
{
isMapLoading = false;
}
private static void OnMapEnd()
{
isMapLoading = true;
}
private async Task<bool> IsUpdateAvailable()
{
string steamInfPatchVersion = GetSteamInfPatchVersion();
if (string.IsNullOrEmpty(steamInfPatchVersion))
{
ConsoleLog("Unable to get the current patch version of Counter-Strike 2. The server will not be checked for updates.", ConsoleColor.Red);
Server.ExecuteCommand($"css_plugins stop {this.ModuleName}");
return false;
}
try
{
using HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(steamApiEndpoint.Replace("{steamInfPatchVersion}", steamInfPatchVersion));
if (response.IsSuccessStatusCode)
{
var upToDateObject = JsonSerializer.Deserialize<UpToDateCheckResponse>(await response.Content.ReadAsStringAsync())!;
if (upToDateObject.Response is { Success: true, UpToDate: false })
{
requiredVersion = upToDateObject.Response.RequiredVersion!;
ConsoleLog($"New Counter-Strike 2 update released (Version: {requiredVersion}, Current: {steamInfPatchVersion})");
return true;
}
}
else
{
ConsoleLog($"HTTP request failed with status code: {response.StatusCode}", ConsoleColor.Red);
}
}
catch (Exception ex)
{
ConsoleLog($"An error occurred: {ex.Message}", ConsoleColor.Red);
}
return false;
}
private void PrepareServerShutdown()
{
List<CCSPlayerController> players = GetCurrentPlayers();
foreach (var player in players)
{
switch (player.Connected)
{
case PlayerConnectedState.PlayerConnected:
case PlayerConnectedState.PlayerConnecting:
case PlayerConnectedState.PlayerReconnecting:
Server.ExecuteCommand($"kickid {player.UserId} Due to the game update (Version: {requiredVersion}), the server is now restarting.");
break;
}
}
AddTimer((float)Configuration.config.ShutdownDelay, ShutdownServer);
}
private string GetSteamInfPatchVersion()
{
string steamInfPath = Path.Combine(Server.GameDirectory, "csgo", "steam.inf");
if (File.Exists(steamInfPath))
{
try
{
Match match = PatchVersion().Match(File.ReadAllText(steamInfPath));
if (match.Success) return match.Groups[1].Value;
{
ConsoleLog("The 'PatchVersion' key could not be located in the steam.inf file.", ConsoleColor.Red);
}
}
catch (Exception ex)
{
ConsoleLog($"An error occurred while reading the 'steam.inf' file: {ex.Message}", ConsoleColor.Red);
}
}
else
{
ConsoleLog($"The 'steam.inf' file was not found in the root directory of Counter-Strike 2. Path: \"{steamInfPath}\"", ConsoleColor.Red);
}
return string.Empty;
}
private static List<CCSPlayerController> GetCurrentPlayers()
{
return Utilities.GetPlayers().Where(player => player is { IsValid: true, IsBot: false, IsHLTV: false }).ToList();
}
private void ConsoleLog(string message, ConsoleColor color = ConsoleColor.Green)
{
string path = Path.Combine(this.ModuleDirectory, "logs.txt");
string log = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{this.ModuleName}] {message}";
Console.ForegroundColor = color;
Console.WriteLine(log);
Console.ResetColor();
try
{
using StreamWriter file = new StreamWriter(path, true);
file.WriteLine(log);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{this.ModuleName}]Error writing to log file: {ex.Message}");
Console.ResetColor();
}
}
private void ShutdownServer()
{
ConsoleLog($"Restarting the server due to the new game update. (Version: {requiredVersion})");
Server.ExecuteCommand("quit");
}
[GeneratedRegex("PatchVersion=(.+)")]
private static partial Regex PatchVersion();
}
}