-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceRconTools.cs
266 lines (236 loc) · 10.8 KB
/
SourceRconTools.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
using RConControl.Properties;
using System.Windows.Forms;
namespace RConControl {
public class SourceRconTools {
/// <summary>
/// Server play class
/// </summary>
public class Player {
public string name { get; set; }
public string id { get; set; }
public string ip { get; set; }
public override string ToString() {
return name;
}
}
/// <summary>
/// Load config
/// </summary>
/// <param name="config">config</param>
public static void LoadConfigFile(ConfigFile config) {
SourceRconConnection rcon = SourceRconConnection.Instance;
StringReader reader = new StringReader(config.content);
string line;
while ((line = reader.ReadLine()) != null) {
if (!String.IsNullOrEmpty(line) && !line.Contains(GlobalConstants.STRING_COMMENT)) {
rcon.Send(line);
//Thread.Sleep(10);
}
}
}
/// <summary>
/// Get player list with IP, ID, and name
/// throws exception if wrong message received
/// </summary>
/// <returns>List with players</returns>
public static List<Player> GetAllPlayers() {
SourceRconConnection rcon = SourceRconConnection.Instance;
List<Player> resultPlayers = new List<Player>();
string message = null;
rcon.Send(GlobalConstants.RCONCMD_STATUS);
try {
message = Receive(GlobalConstants.RCONMSG_FILTER_STATUS);
} catch (Exception ex) {
throw ex;
}
StringReader readMessage = new StringReader(message);
string line;
while ((line = readMessage.ReadLine()) != null) {
//# 2 "!#.viivii" STEAM_0:X:XXXXX 26:35 40 0 active XX.XX.XX.XX:27005
if (line.Contains(GlobalConstants.STEAMID_PREFIX)) {
bool success = false;
int firstNameIdx = line.IndexOf("\"") + 1;
int firstIdIdx = line.IndexOf(GlobalConstants.STEAMID_PREFIX);
int secondIpIdx = line.LastIndexOf(":");
if (firstNameIdx != -1 && firstIdIdx != -1 && secondIpIdx != -1) {
int secondNameIdx = line.IndexOf("\"", firstNameIdx);
int secondIdIdx = line.IndexOf(" ", firstIdIdx);
int firstIpIdx = line.LastIndexOf(" ", secondIpIdx) + 1;
if (secondNameIdx != -1 && secondIdIdx != -1) {
string userName = line.Substring(firstNameIdx, secondNameIdx - firstNameIdx);
string userId = line.Substring(firstIdIdx, secondIdIdx - firstIdIdx);
string userIp = line.Substring(firstIpIdx, secondIpIdx - firstIpIdx);
resultPlayers.Add(new Player { name = userName, id = userId, ip = userIp });
success = true;
}
}
if (!success) {
Exception ex = new Exception("wrong message received.");
ex.Data.Add("message", message);
throw ex;
}
}
}
return resultPlayers;
}
/// <summary>
/// Check if zBlock is installed and active on server
/// </summary>
/// <returns>true if zBlock is installed and active</returns>
public static List<string> GetAllMaps() {
SourceRconConnection rcon = SourceRconConnection.Instance;
List<string> resultList = new List<string>();
string message = null;
rcon.Send(GlobalConstants.RCONCMD_MAPS);
try {
message = Receive(GlobalConstants.MAPEXTENSION);
} catch (Exception ex) {
throw ex;
}
//PENDING: (fs) cs_assault.bsp
StringReader readMessage = new StringReader(message);
string line;
while ((line = readMessage.ReadLine()) != null) {
if (line.Contains(GlobalConstants.MAPEXTENSION)) {
bool success = false;
int secondIdx = line.LastIndexOf(GlobalConstants.MAPEXTENSION);
if (secondIdx != -1) {
int firstIdx = line.LastIndexOf(" ") + 1;
if (firstIdx < secondIdx) {
resultList.Add(line.Substring(firstIdx, secondIdx - firstIdx));
success = true;
}
}
if (!success) {
Exception ex = new Exception("wrong message received.");
ex.Data.Add("message", message);
throw ex;
}
}
}
return resultList;
}
/// <summary>
/// Receive message from server
/// throws exception, if no message received
/// </summary>
/// <param name="filter">filter out message, leave empty for no filter</param>
/// <returns>recieved message</returns>
public static string Receive(string filter = null) {
SourceRconConnection rcon = SourceRconConnection.Instance;
string message = null;
int tries = 0;
bool msgReceived = false;
while (!msgReceived && tries++ < GlobalConstants.RCON_RECEIVE_TRIES) {
message = rcon.lastMessage;
if (!String.IsNullOrEmpty(message)) {
if (!String.IsNullOrEmpty(filter) && message.Contains(filter) || String.IsNullOrEmpty(filter)) {
msgReceived = true;
}
}
Thread.Sleep(GlobalConstants.RCON_RECEIVE_WAIT);
}
if (String.IsNullOrEmpty(message) || !msgReceived) {
Exception ex = new Exception("wrong answer from server.");
ex.Data.Add("filter", filter);
ex.Data.Add("message", message);
throw ex;
}
return message;
}
/// <summary>
/// Kick player from server
/// </summary>
/// <param name="userId">steamid of user</param>
/// <param name="message">message to kick (optional)</param>
public static void KickPlayer(Player player, string message = GlobalConstants.RCON_DEFAULT_KICKMSG) {
SourceRconConnection rcon = SourceRconConnection.Instance;
rcon.Send(String.Format(GlobalConstants.RCONCMD_KICKID, player.id, message));
}
/// <summary>
/// Ban player from server
/// </summary>
/// <param name="player">player to ban</param>
/// <param name="banBy">0 = Ban ID, 1 = Ban IP, 2 = Ban IP+ID</param>
/// <param name="time">time how long a player is banned (0 for permanent)</param>
/// <param name="kick">kick player</param>
public static void BanPlayer(Player player, int banBy, int time, bool kick) {
SourceRconConnection rcon = SourceRconConnection.Instance;
if (banBy == 0 || banBy == 2) {
rcon.Send(String.Format(GlobalConstants.RCONCMD_BANID, time, player.id, (kick ? "kick" : "")));
rcon.Send(String.Format(GlobalConstants.RCONCMD_WRITEID));
}
if (banBy == 1 || banBy == 2) {
rcon.Send(String.Format(GlobalConstants.RCONCMD_BANIP, time, player.id));
rcon.Send(String.Format(GlobalConstants.RCONCMD_WRITEIP));
}
}
/// <summary>
/// Change map on server
/// </summary>
/// <param name="map">map</param>
public static void ChangeMap(string map) {
SourceRconConnection rcon = SourceRconConnection.Instance;
rcon.Send(String.Format(GlobalConstants.RCONCMD_CHANGELEVEL, map));
}
/// <summary>
/// Restart 3 times, self or zblock
/// </summary>
public static void Restart3() {
SourceRconConnection rcon = SourceRconConnection.Instance;
try {
if (Settings.Default.UseZblock && CheckForZblock()) {
rcon.Send(GlobalConstants.RCONCMD_ZB_RESTART);
} else {
Thread threadRestart = new Thread(delegate() { DoRestart3(); });
threadRestart.Start();
}
} catch (Exception ex) {
throw ex;
}
}
//*************************************************
// Private helpter methods
//*************************************************
/// <summary>
/// Do a 3 times restart
/// </summary>
private static void DoRestart3() {
SourceRconConnection rcon = SourceRconConnection.Instance;
rcon.Send(String.Format(GlobalConstants.RCONCMD_RESTART, "1"));
Thread.Sleep(1100);
rcon.Send(String.Format(GlobalConstants.RCONCMD_RESTART, "1"));
Thread.Sleep(1100);
rcon.Send(String.Format(GlobalConstants.RCONCMD_RESTART, "3"));
Thread.Sleep(3000);
for (int i = 0; i < 8; i++) rcon.Send(String.Format(GlobalConstants.RCONCMD_CHATSAY, GlobalConstants.RESTARTMSG_HFGL));
}
/// <summary>
/// Check if zBlock is installed and active on server
/// </summary>
/// <returns>true if zBlock is installed and active</returns>
private static bool CheckForZblock() {
SourceRconConnection rcon = SourceRconConnection.Instance;
bool result = false;
string message = null;
rcon.Send(GlobalConstants.RCONCMD_ZBLOCK);
try {
message = Receive(GlobalConstants.RCONCMD_ZBLOCK);
} catch (Exception ex) {
throw ex;
}
//"zb_active" = "1" min. 0.000000 max. 1.000000
if (message.Contains(GlobalConstants.RCONCMD_ZBLOCK) && message[15] == '1') {
result = true;
}
return result;
}
}
}