-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
327 lines (270 loc) · 13.5 KB
/
Program.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections;
using System.IO;
using Newtonsoft.Json;
using System.Text;
using Microsoft.Win32;
namespace TeamFortressData
{
class Program
{
/// <summary>
/// GLobal static data.
/// </summary>
static HttpServer DataServer;
static string TF2Dir;
static int linesRead = 0;
static ArrayList Players = new ArrayList();
static TF2DataContainer tfdata = new TF2DataContainer();
/// <summary>
/// Setup, tf2 needs to log its console output into a file, you can do that by running a command "con_logfile "filename.log""
/// A autoexec.cfg exists within the directory of tf2, which allows for autoexecution of certain commands. By changing the config
/// to include the con_logfile command it makes sure the log files that this programmas needs to read exists.
/// This is all done automatically.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("Checking TF2 Directory location in register!");
bool found = false;
RegistryKey key;
key = Registry.CurrentUser.OpenSubKey("TF2Data_TF2Dir");
if(key != null)
{
TF2Dir = key.GetValue("DIR") as string;
}
if (Directory.Exists(TF2Dir)) {
key.Close();
Console.WriteLine("Directory in register exists :D, dir: " + TF2Dir);
found = true;
} else
{
Console.WriteLine("Searching for TF2 Directory");
string path = @"c:\";
string searchPattern = "Team Fortress 2";
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives)
{
TF2Dir = SearchDir(d.Name, searchPattern, "");
if (TF2Dir.Length > 0)
{
TF2Dir = TF2Dir + @"\tf";
Console.WriteLine("Found Dir At: " + TF2Dir);
key = Registry.CurrentUser.CreateSubKey("TF2Data_TF2Dir");
key.SetValue("DIR", TF2Dir);
key.Close();
found = true;
break;
}
}
}
if (!found)
{
Console.WriteLine("Could not locate TF2 Directory in all of your drives, make sure you have it installed!");
Console.WriteLine("Exist by pressing a key");
Console.ReadLine();
return;
}
string ConfigData = "";
bool OverWrite = false;
using (StreamReader Reader = new StreamReader(new FileStream(TF2Dir + @"\cfg\autoexec.cfg", FileMode.Open, FileAccess.ReadWrite)))
{
ConfigData = Reader.ReadToEnd();
Console.WriteLine("Adding TFData command to cfg if not present!");
if (!ConfigData.Contains("con_logfile \"tfdata.log\"")){
OverWrite = true;
Console.WriteLine("Log Command is not present in Config");
Console.WriteLine("Creating Backup of Current Config");
using (StreamWriter Writer = new StreamWriter(new FileStream(TF2Dir + @"\cfg\autoexecTFDBack.cfg", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
Writer.Write(ConfigData);
}
ConfigData = ConfigData + Environment.NewLine + "echo \"TFData Script Succesfully Loaded!\"" + Environment.NewLine + "con_logfile \"tfdata.log\"";
} else
{
Console.WriteLine("Config is compatible with TFData :D, you can launch TF2 now ^^");
}
}
if (OverWrite)
{
Console.WriteLine("Writing new Config");
using (StreamWriter Writer = new StreamWriter(new FileStream(TF2Dir + @"\cfg\autoexec.cfg", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
{
Writer.Write("");
Writer.Flush();
Writer.Write(ConfigData);
Writer.Flush();
}
Console.WriteLine("Config creation finished! You can launch TF2 now!");
}
tfdata.ServerIP = "0.0.0.0:0000";
tfdata.TotalKills = 0;
tfdata.TotalPlayers = 0;
tfdata.TotalSuicides = 0;
tfdata.TotalAmountOfSentryKills = 0;
tfdata.TotalAmountOfHeadshots = 0;
tfdata.TotalAmountOfCrits = 0;
tfdata.TotalAmountOfBackstabs = 0;
tfdata.TotalAmountOfReflectKills = 0;
DataServer = new HttpServer(MessageReceivedCallback, 6365);
try
{
System.Diagnostics.Process.Start("http://127.0.0.1:6365/TF2LocalStat.html");
} catch
{
}
Console.ReadLine();
}
/// <summary>
/// Sends Data back to client when clients asks for it. Fist the log file is being parsed on a few attributes, which are
/// later combined into a json format which the client can easily parse!
/// </summary>
/// <param name="messagereceived"></param>
private static void MessageReceivedCallback(string messagereceived)
{
Console.Clear();
if (messagereceived.Contains("tf2data"))
{
if(File.Exists(TF2Dir + @"\tfdata.log"))
{
bool shouldRemove = false;
using (StreamReader Reader = new StreamReader(new FileStream(TF2Dir + @"\tfdata.log", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)))
{
string Data = Reader.ReadToEnd();
string[] ConnectedParts = Data.Split(new string[] { "Connecting to" }, StringSplitOptions.None);
int amountOfConnections = ConnectedParts.Length;
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(ConnectedParts[amountOfConnections - 1]);
string parsed = utf8.GetString(utfBytes);
string[] Lines = parsed.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Console.WriteLine("New added lines since previous: " + (Lines.Length - linesRead));
if((Lines.Length - linesRead) < 0)
{
linesRead = 0;
}
for (int LinesRead = linesRead; LinesRead < Lines.Length; LinesRead++)
{
linesRead = LinesRead;
string Line;
Line = Lines[LinesRead];
Console.WriteLine(Line);
if (Line.ToLower().Contains("connected to"))
{
Players = new ArrayList();
tfdata = new TF2DataContainer();
tfdata.TotalKills = 0;
tfdata.TotalPlayers = 0;
tfdata.TotalSuicides = 0;
tfdata.TotalAmountOfSentryKills = 0;
tfdata.TotalAmountOfHeadshots = 0;
tfdata.TotalAmountOfCrits = 0;
tfdata.TotalAmountOfBackstabs = 0;
tfdata.TotalAmountOfReflectKills = 0;
tfdata.ServerIP = Line.Replace("connected to", "");
linesRead = 0;
}
if (Line.ToLower().Contains("shutdowngc"))
{
shouldRemove = true;
}
if (Line.ToLower().Contains("killed"))
{
Console.WriteLine("Killed Found!");
tfdata.TotalKills = tfdata.TotalKills + 1;
string playerWhoKilled = Line.Split(' ')[0];
string playerWhoDied = Line.Split(' ')[2];
if (!Players.Contains(playerWhoKilled))
{
Players.Add(playerWhoKilled);
}
if (!Players.Contains(playerWhoDied))
{
Players.Add(playerWhoDied);
}
}
if (Line.ToLower().Contains("suicided"))
{
tfdata.TotalSuicides = tfdata.TotalSuicides + 1;
}
if (Line.ToLower().Contains("(crit)"))
{
tfdata.TotalAmountOfCrits = tfdata.TotalAmountOfCrits + 1;
}
if (Line.ToLower().Contains("obj_sentrygun"))
{
tfdata.TotalAmountOfSentryKills = tfdata.TotalAmountOfSentryKills + 1;
}
if (Line.ToLower().Contains("deflect_rocket"))
{
tfdata.TotalAmountOfReflectKills = tfdata.TotalAmountOfReflectKills + 1;
}
if ((Line.ToLower().Contains("sniperrifle") || Line.ToLower().Contains("machina") || Line.ToLower().Contains("awper_hand") || Line.ToLower().Contains("tf_projectile_arrow") || Line.ToLower().Contains("fortified_compound") || Line.ToLower().Contains("bazaar_bargain") || Line.ToLower().Contains("hitmans's_heatmaker") || Line.ToLower().Contains("classic") || Line.ToLower().Contains("ambassador")) && Line.ToLower().Contains("(crit)"))
{
tfdata.TotalAmountOfHeadshots = tfdata.TotalAmountOfHeadshots + 1;
}
if ((Line.ToLower().Contains("knife") || Line.ToLower().Contains("spy_cicle") || Line.ToLower().Contains("sharp_dresser") || Line.ToLower().Contains("black_rose") || Line.ToLower().Contains("saxxy") || Line.ToLower().Contains("your_eternal_reward") || Line.ToLower().Contains("wanga_prick") || Line.ToLower().Contains("connivers's_kunai") || Line.ToLower().Contains("big_earner")) && Line.ToLower().Contains("(crit)"))
{
tfdata.TotalAmountOfBackstabs = tfdata.TotalAmountOfBackstabs + 1;
}
}
tfdata.AllPlayers = Players;
var json = JsonConvert.SerializeObject(tfdata);
DataServer.JsonToSend(json);
}
if (shouldRemove)
{
try
{
File.Delete(TF2Dir + @"\tfdata.log");
Console.WriteLine("TF2 IS NOT RUNNING (ANYMORE)");
}
catch (IOException ex)
{
Console.WriteLine("COULD NOT REMOVE LOG: " + ex.ToString());
}
}
} else
{
Console.WriteLine("File \"tfdata.log\" Does Not Exist :( ");
DataServer.SendMessage("FILE NOT AVAILABLE :(");
}
}
}
/// <summary>
/// Searches recursively folders with given search parameter, parameter dir tells the method where it should start looking
/// dirFound is used to check if there is a directory found.
/// Not a well made method as it stops on the first occurence that resembles the search parameter, instead of being exact.
/// </summary>
/// <param name="dir"></param>
/// <param name="search"></param>
/// <param name="dirFound"></param>
/// <returns></returns>
public static string SearchDir(string dir, string search, string dirFound)
{
try
{
string[] dirs = Directory.GetDirectories(dir);
foreach (string directory in dirs)
{
// Console.WriteLine("Searching: " + directory);
if (directory.Contains(search))
{
dirFound = directory;
}
if (dirFound.Length > 0)
{
break;
}
else
{
dirFound = SearchDir(directory, search, dirFound);
}
}
}
catch
{
}
return dirFound;
}
}
}