-
Notifications
You must be signed in to change notification settings - Fork 24
/
PatchDownloader.cs
184 lines (149 loc) · 6.5 KB
/
PatchDownloader.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace Launcher
{
class PatchDownloader
{
private Form1 form;
private Stopwatch stopWatch = new Stopwatch();
private string directoryPath;
private Server serverToDownload;
private List<Patch> patchesToDownload;
//When false skips one moveAway call
public static bool shouldMoveAway = true;
public PatchDownloader(Form1 form)
{
this.form = form;
directoryPath = string.Empty;
}
public void patch(Server server)
{
serverToDownload = server;
if (ApplicationStatus.oldServer != null && shouldMoveAway)
PatchMover.moveAway((Server)ApplicationStatus.oldServer);
else if (!shouldMoveAway)
shouldMoveAway = true;
using (WebClient webClient = new WebClient())
{
string patchFileURL = URLFormatter.format($"{server.website}/{server.downloadDirectory}/patchfile.dat");
if (server.downloadDirectory == string.Empty || server.patchesDirectory == string.Empty ||
!ResourceHelper.resourceExists(patchFileURL))
{
form.downloadStatusLabel.Text = "Status: Server does not support the launcher's patcher";
form.playButton.Text = "Play";
form.playButton.Enabled = true;
ApplicationStatus.downloading = false;
return;
}
ApplicationStatus.downloading = true;
webClient.DownloadProgressChanged += downloadPatchProgressChanged;
webClient.DownloadFileAsync(new System.Uri(patchFileURL), "patchfile.dat");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.downloadPatchFileCompleted);
}
}
public void downloadPatches(List<Patch> patches)
{
form.playButton.Text = "Downloading";
form.playButton.Enabled = false;
patchesToDownload = patches;
downloadPatch(patchesToDownload[0]);
}
public void downloadPatch(Patch patch)
{
string downloadURL = URLFormatter.format($"{serverToDownload.website}/{serverToDownload.downloadDirectory}/");
using (WebClient webClient = new WebClient())
{
string patchDownloadURL = downloadURL + "/" + patch.fileName;
if (!ResourceHelper.resourceExists(patchDownloadURL))
{
form.downloadStatusLabel.Text = $"Status: Could not download {patch.fileName} - It does not exist";
return;
}
ApplicationStatus.downloading = true;
string localPatchDirectory = $"{serverToDownload.clientDirectory}/Data/";
string localPatchPath = localPatchDirectory + patch.fileName;
if (!Directory.Exists(localPatchDirectory))
Directory.CreateDirectory(localPatchDirectory);
webClient.DownloadProgressChanged += downloadPatchProgressChanged;
webClient.DownloadFileAsync(new System.Uri(patchDownloadURL), localPatchPath);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(this.downloadPatchCompleted);
stopWatch.Start();
patchesToDownload.Remove(patch);
}
}
private void downloadPatchProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
form.progressBar.Value = e.ProgressPercentage;
form.downloadStatusLabel.Text = $"{(e.BytesReceived / 1024f / 1024f).ToString("0.0")}Mb / {(e.TotalBytesToReceive / 1024f / 1024f).ToString("0.0")}Mb @ {(e.BytesReceived / 1024f / 1024f / stopWatch.Elapsed.TotalSeconds).ToString("0.0")} Mb/s Downloaded";
if (!ApplicationStatus.downloading && e.ProgressPercentage != 100)
{
form.playButton.Text = "Downloading";
form.playButton.Enabled = false;
ApplicationStatus.downloading = true;
}
}
public void downloadPatchCompleted(object sender, AsyncCompletedEventArgs e)
{
stopWatch.Stop();
stopWatch.Reset();
if (patchesToDownload.Count > 0)
{
downloadPatches(patchesToDownload);
}
else
{
form.downloadStatusLabel.Text = "Status: Patching Complete";
form.playButton.Text = "Play";
form.playButton.Enabled = true;
ApplicationStatus.downloading = false;
form.progressBar.Value = 0;
}
}
public void downloadPatchFileCompleted(object sender, AsyncCompletedEventArgs e)
{
form.downloadStatusLabel.Text = "Status: Download complete";
form.progressBar.Value = 0;
PatchMover.moveActive(serverToDownload);
List<Patch> patches = patchFilesToPatch(PatchReader.readPatches("patchfile.dat"));
if (patches.Count > 0)
downloadPatches(patches);
form.playButton.Enabled = true;
form.playButton.Text = "Play";
ApplicationStatus.downloading = false;
WebClient client = (WebClient)sender;
client.CancelAsync();
client.Dispose();
}
private List<Patch> patchFilesToPatch(List<Patch> patches)
{
List<Patch> patchesToPatch = new List<Patch>();
string dataDir = serverToDownload.clientDirectory + "/Data/";
foreach (Patch patch in patches)
{
string patchPath = dataDir + patch.fileName;
if (File.Exists(patchPath))
{
if (computeHash(patchPath).SequenceEqual(patch.md5Hash))
continue;
}
patchesToPatch.Add(patch);
}
return patchesToPatch;
}
private byte[] computeHash(string filePath)
{
using (var md5 = MD5.Create())
using (var stream = File.OpenRead(filePath))
return md5.ComputeHash(stream);
}
}
}