forked from fredriksa/WoWLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
299 lines (228 loc) · 9.23 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.NetworkInformation;
namespace Launcher
{
public partial class Form1 : Form
{
public static double version = 1.0;
public ServerContainer serverContainer;
private Server? selectedServer;
private Timer timer;
private Stopwatch downloadMessageWatch;
public AddServerForm addServerForm;
public EditServerForm editServerForm;
private int pingInterval = 20;
public Form1()
{
InitializeComponent();
ApplicationStatus.activeServer = null;
serverContainer = new ServerContainer(this);
addServerForm = new AddServerForm(this);
editServerForm = new EditServerForm(this);
timer = new Timer();
timer.Interval = pingInterval * 1000;
timer.Tick += new EventHandler(timerTick);
timer.Start();
downloadMessageWatch = new Stopwatch();
downloadMessageWatch.Start();
serverList.View = View.Details;
serverList.MultiSelect = false;
int columnWidth = 130;
serverList.Columns.Add("Name", columnWidth, HorizontalAlignment.Left);
serverList.Columns.Add("Website", columnWidth, HorizontalAlignment.Left);
serverList.Columns.Add("Version", columnWidth, HorizontalAlignment.Left);
serverList.Columns.Add("Status", columnWidth, HorizontalAlignment.Left);
VersionChecker.checkForNewVersion();
}
public void updateServerList()
{
serverList.Items.Clear();
updateStatus();
foreach (Server server in serverContainer.getServers())
serverList.Items.Add(new ListViewItem(new[] { server.name, server.website, server.version, server.status}));
updateStatusColors();
}
private void addServerButton_Click(object sender, EventArgs e)
{
if (addServerForm == null)
addServerForm = new AddServerForm(this);
if (!addServerForm.Visible)
addServerForm.Show();
}
private void deleteServerButton_Click(object sender, EventArgs e)
{
if (!isServerSelected()) return;
if (isServerDownloading()) return;
Server server = (Server)selectedServer;
PatchDeleter.delete(server);
selectedServer = null;
serverContainer.removeServer(server.name);
updateServerList();
}
public void addServer(string filename)
{
serverContainer.addServer("server.dat");
}
public void addServer(string filename, string directoryPath)
{
serverContainer.addServer(filename, directoryPath);
}
public void addServer(Server server)
{
serverContainer.addServer(server);
patch();
}
private void websiteButton_Click(object sender, EventArgs e)
{
if (!isServerSelected()) return;
Server server = (Server)selectedServer;
System.Diagnostics.Process.Start(server.website);
}
private void serverList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (ApplicationStatus.downloading)
if (e.IsSelected)
e.Item.Selected = false;
}
private void serverList_SelectedIndexChanged(object sender, EventArgs e)
{
if (isServerDownloading()) return;
if (serverList.SelectedItems.Count <= 0) return;
ListViewItem selectedItem = serverList.SelectedItems[0];
if (selectedItem.SubItems.Count <= 0) return;
string name = selectedItem.SubItems[0].Text;
if (name == null || name == string.Empty) return;
Server? server = serverContainer.getServerByName(name);
if (server == null) return;
selectedServer = (Server) server;
Server srv = (Server)server;
ApplicationStatus.updateActiveServer(srv);
patch();
Client.updateRealmlist(srv.clientDirectory, srv);
}
private void playButton_Click(object sender, EventArgs e)
{
if (!isServerSelected()) return;
if (isServerDownloading()) return;
Server server = (Server)selectedServer;
Client.clearCache(ClientHelper.cacheFolderPath(server));
Server srv = (Server)selectedServer;
string executablePath = Path.Combine(srv.clientDirectory, "Wow.exe");
if (File.Exists(executablePath))
Process.Start(executablePath);
else
MessageBox.Show($"Could not find World of Warcraft executable!\n{executablePath}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists("local_servers.dat"))
{
serverContainer.addServers(ServerReader.readMultiple("local_servers.dat"));
updateServerList();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (ApplicationStatus.downloading)
{
DialogResult result = MessageBox.Show("Application is currently downloading files - interrupting file download will destroy patch files and could dislocate patch files.\nDo you want to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result != DialogResult.Yes) return;
}
if (!(serverContainer.getServers().Count > 0)) return;
ServerWriter.write(serverContainer.getServers(), "local_servers.dat");
if (ApplicationStatus.activeServer != null)
PatchMover.moveAway((Server)ApplicationStatus.activeServer);
}
private void updateStatus()
{
List<Server> servers = serverContainer.getServers();
List<Server> iterateServers = new List<Server>(servers);
for (int i = 0; i < iterateServers.Count; i++)
{
Server server = iterateServers[i];
PingReply reply;
bool status = false;
using (Ping ping = new Ping())
{
try
{
reply = ping.Send(URLFormatter.formatPingUrl(server.realmlist), 15);
status = reply.Status == IPStatus.Success;
} catch (Exception e){ }
}
serverContainer.updateStatus(server, status ? "Online" : "Offline");
}
}
private void updateStatusColors()
{
foreach (ListViewItem item in serverList.Items)
{
item.UseItemStyleForSubItems = false;
var subItem = item.SubItems[3];
if (subItem.Text == "Online")
subItem.ForeColor = Color.Green;
else if (subItem.Text == "Offline")
subItem.ForeColor = Color.Red;
}
}
private void openButton_Click(object sender, EventArgs e)
{
if (!isServerSelected()) return;
Server srv = (Server)selectedServer;
Process.Start(srv.clientDirectory);
}
private void patch()
{
playButton.Text = "Downloading";
playButton.Enabled = false;
PatchDownloader downloader = new PatchDownloader(this);
downloader.patch((Server)ApplicationStatus.activeServer);
}
private void editServerButton_Click_1(object sender, EventArgs e)
{
if (!isServerSelected()) return;
if (isServerDownloading()) return;
if (editServerForm == null)
editServerForm = new EditServerForm(this);
if (!editServerForm.Visible)
editServerForm.Show();
}
private void timerTick(object sender, EventArgs e)
{
updateStatus();
updateStatusColors();
}
private bool isServerSelected()
{
if (selectedServer == null)
{
MessageBox.Show("You must select a server before continuing.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
private bool isServerDownloading()
{
if (ApplicationStatus.downloading)
{
int elapsedSeconds = downloadMessageWatch.Elapsed.Seconds;
if (elapsedSeconds < 1) return true;
MessageBox.Show("Can't perform action while server is downloading!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
downloadMessageWatch.Restart();
return true;
}
return false;
}
}
}