forked from fredriksa/WoWLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerDownloader.cs
68 lines (58 loc) · 2.3 KB
/
ServerDownloader.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Windows.Forms;
namespace Launcher
{
class ServerDownloader
{
private Form1 form;
private Stopwatch stopWatch;
private string directoryPath;
public ServerDownloader(Form1 form)
{
this.form = form;
stopWatch = new Stopwatch();
stopWatch.Stop();
directoryPath = string.Empty;
}
public void downloadServer(string url, string directoryPath)
{
this.directoryPath = directoryPath;
using (WebClient webClient = new WebClient())
{
if (!ResourceHelper.resourceExists(URLFormatter.format(url) + "/server.dat"))
{
MessageBox.Show("Server does not support the launcher's simple setup", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
webClient.DownloadProgressChanged += downloadServerProgressChanged;
webClient.DownloadFileAsync(new System.Uri(URLFormatter.format(url) + "/server.dat"), "server.dat");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.downloadServerCompleted);
stopWatch.Reset();
stopWatch.Start();
}
}
private void downloadServerProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
form.progressBar.Value = e.ProgressPercentage;
form.downloadStatusLabel.Text = $"{e.BytesReceived / 1024f}Kb / {e.TotalBytesToReceive / 1024f}Kb @ {(e.BytesReceived / 1024d / stopWatch.Elapsed.TotalSeconds).ToString("0.00")} Kb/s Downloaded";
}
public void downloadServerCompleted(object sender, AsyncCompletedEventArgs e)
{
form.downloadStatusLabel.Text = "Status: Download complete";
form.progressBar.Value = 0;
WebClient client = (WebClient)sender;
client.CancelAsync();
client.Dispose();
form.addServer("server.dat", directoryPath);
stopWatch.Stop();
}
}
}