-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2SInfo.cs
153 lines (138 loc) · 4.82 KB
/
A2SInfo.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
namespace MyApp
{
public class A2SInfo
{
public byte Header;
public byte Protocol;
public string Name = string.Empty;
public string Map = string.Empty;
public string Folder = string.Empty;
public string Game = string.Empty;
public short ID;
public byte Players;
public byte MaxPlayers;
public byte Bots;
public byte ServerType;
public byte Environment;
public byte Visibility;
public byte VAC;
public string Version = string.Empty;
public byte EDF;
public short port;
public long SteamID;
public string Keywords = string.Empty;
public List<string> KeywordParts = new List<string>();
public A2SInfo(byte[] data)
{
ReadInfo(data);
}
public const int EXT_VERSION = 0;
public const int EXT_GAMETYPE = 1;
public const int EXT_SPECTATORS = 11;
public const int EXT_MAXSPECTATORS = 12;
public const int EXT_PLAYERS_INGAME = 15;
public const int EXT_MMR = 16;
public int GetPlayersIngame()
{
if (KeywordParts.Count <= EXT_PLAYERS_INGAME) return Players;
int number;
int.TryParse(KeywordParts[EXT_PLAYERS_INGAME], out number);
return number;
}
public int GetSpectators()
{
if (KeywordParts.Count <= EXT_SPECTATORS) return 0;
int number;
int.TryParse(KeywordParts[EXT_SPECTATORS], out number);
return number;
}
public int GetMaxSpectators()
{
if (KeywordParts.Count <= EXT_MAXSPECTATORS) return 0;
int number;
int.TryParse(KeywordParts[EXT_MAXSPECTATORS], out number);
return number;
}
public string GetGameType()
{
if (KeywordParts.Count <= EXT_GAMETYPE) return "N/A";
return KeywordParts[EXT_GAMETYPE];
}
public string GetMMR()
{
string result = "N/A";
if (KeywordParts.Count > EXT_MMR)
{
string val = KeywordParts[EXT_MMR];
int number;
if (int.TryParse(val, out number))
{
if (number > 0) result = "" + number;
}
}
return result;
}
public bool ReadInfo(byte[] data)
{
bool hasChange = false;
var reader = new Reader(data);
reader.Skip(4);
Header = reader.ReadByte();
Protocol = reader.ReadByte();
Name = reader.ReadUTF8String();
string oldMap = Map;
Map = reader.ReadString();
if (!Map.Equals(oldMap)) hasChange = true;
Folder = reader.ReadString();
Game = reader.ReadString();
ID = reader.ReadShort();
byte oldPlayers = Players;
Players = reader.ReadByte();
if (Players != oldPlayers) hasChange = true;
MaxPlayers = reader.ReadByte();
Bots = reader.ReadByte();
ServerType = reader.ReadByte();
Environment = reader.ReadByte();
Visibility = reader.ReadByte();
VAC = reader.ReadByte();
Version = reader.ReadString();
EDF = reader.ReadByte();
if ((EDF & 0x80) != 0) port = reader.ReadShort();
if ((EDF & 0x10) != 0) SteamID = reader.ReadLong();
if ((EDF & 0x20) != 0)
{
Keywords = reader.ReadString();
var split = Keywords.Split('|');
for (int i = 0; i < split.Length; i++)
{
var value = split[i];
if (i < KeywordParts!.Count)
{
// Compare and overwrite
if (KeywordParts[i] != value)
{
switch (i)
{
case EXT_GAMETYPE:
case EXT_MAXSPECTATORS:
case EXT_MMR:
case EXT_PLAYERS_INGAME:
case EXT_SPECTATORS:
case EXT_VERSION:
hasChange = true;
break;
}
//Console.WriteLine($"Parameter {i} changed from {KeywordParts[i]} to {value}");
}
KeywordParts[i] = value;
}
else
{
KeywordParts.Add(value);
}
}
}
return hasChange;
}
}
}