This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.cs
286 lines (235 loc) · 9.72 KB
/
controller.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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using ANDREICSLIB;
using ANDREICSLIB.ClassExtras;
using ANDREICSLIB.Helpers;
using Protocol;
namespace Cod4ServerTool
{
public static class Controller
{
private static void ExtractServers(Servers serversInstance, threads threadsInstance, byte[] data)
{
if (data == null)
return;
String datastr = Encoding.ASCII.GetString(data);
var seps = new string[1];
seps[0] = "\\";
string[] ips = datastr.Split(seps, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in ips)
{
if (s.Length != 6)
continue;
String ip = "";
ip += ((int)s[0]).ToString(CultureInfo.InvariantCulture) + ".";
ip += ((int)s[1]).ToString(CultureInfo.InvariantCulture) + ".";
ip += ((int)s[2]).ToString(CultureInfo.InvariantCulture) + ".";
ip += ((int)s[3]).ToString(CultureInfo.InvariantCulture);
int port = (s[4]) * 256 + (s[5]);
threadsInstance.addServerThread(ip, port, serversInstance);
}
}
public static void MasterServerAdd(ToolStripLabel toolStripLabel, StatusStrip SS, Servers serversInstance,
threads threadsInstance)
{
//we need to increase the 15th byte to change the region
byte[] gs = Protocol.Protocol.cod4getservers;
for (int region = 0; region < 6; region++)
{
ToolStripText(toolStripLabel, ref SS, "Adding region:" + region + "/6");
byte[] rec =
Networking.SendUDPPacketGetBlockingResponse(
NetExtras.HostnameToIP(Protocol.Protocol.MasterServer), 20810, gs);
gs[15]++;
ExtractServers(serversInstance, threadsInstance, rec);
}
ToolStripText(toolStripLabel, ref SS, "Ready");
}
public static void ToolStripText(ToolStripLabel toolStripLabel, ref StatusStrip SS, String text)
{
toolStripLabel.Text = text;
SS.Invalidate();
SS.Update();
}
public static Image GetEmbeddedImage(String name)
{
return Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Cod4ServerTool." + name));
}
public static int GetLatencyDrop(ComboBox cb)
{
String s = cb.Text;
return int.Parse(s.Remove(0, 1));
}
public static void AddServerToServerView(Server s, ListView serverView)
{
string port = s.getVariable(Protocol.Protocol.portSTR);
string ip = s.getVariable(Protocol.Protocol.ipAddressSTR);
var lvi = new ListViewItem();
String ipport = ip + Server.separator + port;
lvi.Name = ipport;
lvi.Text = s.getVariable(Protocol.Protocol.ipAddressSTR);
for (int a = 1; a < serverView.Columns.Count; a++)
{
lvi.SubItems.Add(serverView.Columns[a].Name == Protocol.Protocol.portSTR ? port : "");
}
serverView.Items.Add(lvi);
UpdateServerInServerView(s, serverView);
}
public static int GetColumnIndex(ListView.ColumnHeaderCollection chc, String name)
{
int a = 0;
foreach (ColumnHeader ch in chc)
{
if (ch.Name == name)
return a;
a++;
}
return -1;
}
public static void UpdateServerInServerView(Server s, ListView serverView)
{
String ipport = s.getServerIdentifier(Protocol.Protocol.ipAddressSTR, Protocol.Protocol.portSTR);
int count = 0;
foreach (ColumnHeader ch in serverView.Columns)
{
String val = s.getVariable(ch.Name);
serverView.Items[ipport].SubItems[count].Text = string.IsNullOrEmpty(val) == false ? val : "";
count++;
}
}
public static void UpdateFavourite(ListView serverView, Servers serversInstance, bool isFavourite)
{
foreach (ListViewItem lvi in serverView.SelectedItems)
{
Server s = serversInstance.masterServers[lvi.Name];
s.setVariable(Protocol.Protocol.favouriteSTR, isFavourite.ToString());
UpdateServerInServerView(s, serverView);
}
}
//refresh selected items in listview
public static void Refreshselected(ListView serverView, Protocol.Protocol protocolInstance,
Servers serversInstance)
{
Server serv = serversInstance.masterServers[serverView.SelectedItems[0].Name];
protocolInstance.updateServerInfo(serv, Protocol.Protocol.basic);
UpdateServerInServerView(serv, serverView);
}
public static void ServersFromFile(ToolStripLabel toolStripLabel, StatusStrip ss, Servers serversInstance,
threads threadsInstance, ListView serverView, view baseView)
{
var ofd = new OpenFileDialog();
ofd.Title = "Select file to extract ip:ports from";
ofd.ShowDialog();
if (string.IsNullOrEmpty(ofd.FileName))
return;
var fs = new FileStream(ofd.FileName, FileMode.Open);
var sr = new StreamReader(fs);
String file = sr.ReadToEnd();
sr.Close();
fs.Close();
threadsInstance.addServerCollectionThread(toolStripLabel, ref ss, file, serversInstance, threadsInstance);
ToolStripText(toolStripLabel, ref ss, "Ready");
baseView.allServersToServerView();
}
public static async Task ServersFromUrl(ToolStripLabel toolStripLabel, StatusStrip ss, Servers serversInstance,
threads threadsInstance, ListView serverView, view baseView)
{
var sb = new getStringBox();
sb.label1.Text = "Enter URL to extract ip:ports from";
sb.ShowDialog();
String ret = sb.returnvalue;
if (ret == "")
return;
ToolStripText(toolStripLabel, ref ss, "Extracting Web Page Text");
var page = await NetExtras.DownloadWebPage(ret);
if (page == null)
{
MessageBox.Show("Not a valid webpage");
return;
}
threadsInstance.addServerCollectionThread(toolStripLabel, ref ss, page, serversInstance, threadsInstance);
ToolStripText(toolStripLabel, ref ss, "Ready");
baseView.allServersToServerView();
}
public static void ChangeDirectory(String exe)
{
String s = exe;
s = s.Substring(0, s.LastIndexOf("\\", StringComparison.Ordinal));
Environment.CurrentDirectory = s;
}
public static void Connecttoserver(String ipaddr, String gameLocation)
{
String codfile = gameLocation;
if (File.Exists(codfile) == false)
{
var ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Title = "Select iw3mp.exe to run an ip address with";
ofd.ShowDialog();
view.resetDirectory();
if (string.IsNullOrEmpty(ofd.FileName))
return;
var fs = new FileStream(codfile, FileMode.Create);
var sw = new StreamWriter(fs);
sw.WriteLine(ofd.FileName);
sw.Close();
fs.Close();
}
String codlocation = "";
try
{
var fs = new FileStream(codfile, FileMode.Open);
var sr = new StreamReader(fs);
codlocation = sr.ReadToEnd();
codlocation = codlocation.Substring(0, codlocation.IndexOf("iw3mp.exe", StringComparison.Ordinal) + 9);
sr.Close();
fs.Close();
ChangeDirectory(codlocation);
Process.Start(codlocation, "connect " + ipaddr);
}
catch
{
MessageBox.Show("An error has occurred starting:" + codlocation);
File.Delete(codfile);
}
}
public static void DeleteSelected(ListView serverView, Servers serversInstance)
{
ListView.SelectedListViewItemCollection slvic = serverView.SelectedItems;
int count = 0;
String ips = "";
foreach (ListViewItem lvi in slvic)
{
if (count > 10)
{
ips += "...";
break;
}
count++;
ips += lvi.Name + "\n";
}
DialogResult dr = MessageBox.Show("Are you sure you want to delete these ips?\n" + ips, "question",
MessageBoxButtons.YesNo);
if (dr == DialogResult.No)
return;
foreach (ListViewItem lvi in slvic)
{
serverView.Items.Remove(lvi);
serversInstance.masterServers.Remove(lvi.Name);
}
}
public static bool IsInt(String s)
{
var r = new Regex("^[0-9]+$");
return r.IsMatch(s);
}
}
}