forked from RusFeniks/MinecraftUpdater-ClientSide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientUpdater.cs
364 lines (324 loc) · 13.8 KB
/
ClientUpdater.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using MessageBox = System.Windows.MessageBox;
namespace MinecraftUpdater
{
class SyncOptions
{
public string root;
public string[] syncElements;
public string[] exceptions;
public OptionalFile[] optional;
}
class OptionalFile
{
public string link;
public string install;
}
class HashObject
{
public string file;
public string hash;
}
enum UpdateOperations
{
DOWNLOAD,
DELETE,
CREATE_DIR
}
class UpdateTask
{
public string file;
public string link;
public UpdateOperations task;
}
class ClientUpdater
{
string serverURL;
string clientPath;
public ClientUpdater(string _serverURL, string _clientPath)
{
serverURL = _serverURL;
clientPath = _clientPath;
}
/// <summary>
/// Обновить клиент игры
/// </summary>
/// <param name="progressBar">Прогрессбар для отображения прогресса обновления</param>
/// <returns></returns>
public async Task Update(ProgressBar progressBar)
{
List<UpdateTask> updateTasks = new List<UpdateTask>();
await Task.Run(() =>
{
SyncOptions syncOptions = GetSyncOptionsAsync($"{serverURL}/.syncOptions");
List<HashObject> clientHashList = GetClientHashListAsync(syncOptions.syncElements);
List<HashObject> serverHashList = GetServerHashListAsync($"{serverURL}/.hash");
updateTasks.AddRange(CompareHashLists(clientHashList, serverHashList, syncOptions.exceptions));
updateTasks.AddRange(CheckOptionalFiles(syncOptions.optional));
});
await ApplyUpdate(updateTasks, progressBar);
}
public async Task<bool> UpdatesCheck()
{
List<UpdateTask> updateTasks = new List<UpdateTask>();
await Task.Run(() =>
{
SyncOptions syncOptions = GetSyncOptionsAsync($"{serverURL}/.syncOptions");
if(syncOptions == null) { return; }
List<HashObject> clientHashList = GetClientHashListAsync(syncOptions.syncElements);
List<HashObject> serverHashList = GetServerHashListAsync($"{serverURL}/.hash");
updateTasks.AddRange(CompareHashLists(clientHashList, serverHashList, syncOptions.exceptions));
updateTasks.AddRange(CheckOptionalFiles(syncOptions.optional));
});
return updateTasks.Count > 0;
}
/// <summary>
/// Получить опции синхронизации с сервера
/// </summary>
/// <param name="link">Ссылка на файл опций</param>
/// <returns></returns>
private SyncOptions GetSyncOptionsAsync(string link)
{
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
string syncOptionsString = "";
try {
syncOptionsString = webClient.DownloadString(link);
} catch
{
return null;
}
SyncOptions syncOptions = JsonConvert.DeserializeObject<SyncOptions>(syncOptionsString);
for(int i = 0; i < syncOptions.syncElements.Length; i++)
{
syncOptions.syncElements[i] = Path.Combine(Regex.Split(clientPath + "/" + syncOptions.syncElements[i], "[\\/]+"));
}
for (int i = 0; i < syncOptions.exceptions.Length; i++)
{
syncOptions.exceptions[i] = Path.Combine(Regex.Split(clientPath + "/" + syncOptions.exceptions[i], "[\\/]+"));
}
return syncOptions;
}
/// <summary>
/// Построение hash листа файлов о отслеживаемых папках
/// </summary>
/// <param name="syncOptions"></param>
/// <returns></returns>
private List<HashObject> GetClientHashListAsync(string[] syncElements)
{
List<HashObject> clientHashList = new List<HashObject>();
for(int i = 0; i < syncElements.Length; i++)
{
clientHashList.AddRange(GetHashByObjectsInFolder(syncElements[i]));
}
return clientHashList;
}
/// <summary>
/// Рекурсивный обход папок и получение хеша, находящихся в них файлов
/// </summary>
/// <param name="fullPath"></param>
/// <returns></returns>
private List<HashObject> GetHashByObjectsInFolder(string fullPath)
{
List<HashObject> hashObjectsList = new List<HashObject>();
if(Directory.Exists(fullPath))
{
hashObjectsList.Add(new HashObject { file = fullPath, hash = "" });
foreach(string directory in Directory.GetDirectories(fullPath))
{
hashObjectsList.AddRange(GetHashByObjectsInFolder(directory));
}
foreach(string file in Directory.GetFiles(fullPath))
{
hashObjectsList.AddRange(GetHashByObjectsInFolder (file));
}
}
else if (File.Exists(fullPath))
{
hashObjectsList.Add(new HashObject { file = fullPath, hash = GetFileHash(fullPath) });
}
return hashObjectsList;
}
/// <summary>
/// Получить хеш файла
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private string GetFileHash( string filePath )
{
StreamReader streamReader = new StreamReader(filePath);
byte[] hashBytes = SHA256.Create().ComputeHash(streamReader.BaseStream);
streamReader.Close();
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
/// <summary>
/// Получить хеш-лист с сервера
/// </summary>
/// <param name="hashFileLink"></param>
/// <returns></returns>
private List<HashObject> GetServerHashListAsync( string hashFileLink )
{
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
string serverHashListString = webClient.DownloadString(hashFileLink);
List<HashObject> serverHashList = JsonConvert.DeserializeObject<List<HashObject>>(serverHashListString);
serverHashList.ForEach(self =>
{
self.file = Path.Combine(Regex.Split(clientPath + "/" + self.file, "[\\/]+"));
});
return serverHashList;
}
/// <summary>
/// Получает список модов пользователя из папки userMods.
/// Эти моды, в дальнейшем, игнорируются при проверке обновлений.
/// </summary>
/// <returns></returns>
private List<string> GetUserModsList()
{
List<string> userModsList = new List<string>();
string userModsFolderPath = Path.Combine(clientPath, "UserMods");
string modsFolder = Path.Combine(clientPath, "mods");
if(Directory.Exists(userModsFolderPath))
{
string[] userMods = Directory.GetFiles(userModsFolderPath);
userModsList.AddRange(userMods);
foreach(string userMod in userMods)
{
string fileName = Path.GetFileName(userMod);
userModsList.Add(Path.Combine(modsFolder, fileName));
}
}
return userModsList;
}
/// <summary>
/// Производит сравнение между хеш-листами сервера и клиента, создавая список заданий на обновление.
/// </summary>
/// <param name="clientHashList">Хеш-лист клиента</param>
/// <param name="serverHashList">Хеш-лист сервера</param>
/// <param name="exceptions">Исключения</param>
/// <returns></returns>
private List<UpdateTask> CompareHashLists(List<HashObject> clientHashList, List<HashObject> serverHashList, string[] exceptions)
{
List<UpdateTask> updateTasks = new List<UpdateTask>();
List<string> ignoredFiles = new List<string>();
ignoredFiles.AddRange(exceptions);
ignoredFiles.AddRange(GetUserModsList());
clientHashList.ForEach(clientHash =>
{
// Отсеиваем файлы, у которых всё хорошо и совпадает хеш
int index = serverHashList.FindIndex(serverHash =>
{
return clientHash.file == serverHash.file && clientHash.hash == serverHash.hash;
});
if (index > -1)
{
serverHashList.RemoveAt(index);
}
else
{
bool findException = false;
foreach (string exception in ignoredFiles)
{
findException = clientHash.file.Contains(exception);
if(findException) { break; }
}
if(!findException)
{
updateTasks.Add(new UpdateTask { file = clientHash.file, link = clientHash.file, task = UpdateOperations.DELETE });
}
}
});
serverHashList.ForEach(serverHash =>
{
if(serverHash.hash == "")
{
updateTasks.Add(new UpdateTask { file = serverHash.file, link = serverHash.file, task = UpdateOperations.CREATE_DIR });
} else
{
updateTasks.Add(new UpdateTask { file = serverHash.file, link = serverHash.file, task = UpdateOperations.DOWNLOAD });
}
});
return updateTasks;
}
private List<UpdateTask> CheckOptionalFiles (OptionalFile[] optionals)
{
List<UpdateTask> updateTasks = new List<UpdateTask>();
foreach(OptionalFile optional in optionals)
{
string localFilePath = Path.Combine(Regex.Split(clientPath + "/" + optional.install, "[\\/]+"));
if (!File.Exists(localFilePath))
{
updateTasks.Add(new UpdateTask { file = localFilePath, link = $"{serverURL}/{optional.link}", task = UpdateOperations.DOWNLOAD });
}
}
return updateTasks;
}
private void DownloadFileFromServerAsync(string localFilePath, string linkFilePath)
{
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
string url = linkFilePath.Replace(clientPath, serverURL).Replace('\\', '/');
Uri uri = new Uri(url);
byte[] fileData = webClient.DownloadData(uri);
string dir = Path.GetDirectoryName(localFilePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
FileStream file = File.OpenWrite(localFilePath);
file.Write(fileData, 0, fileData.Length);
file.Close();
return;
}
private async Task ApplyUpdate(List<UpdateTask> updateTasks, ProgressBar progressBar)
{
progressBar.Maximum = updateTasks.Count;
progressBar.Value = 0;
foreach(UpdateTask updateTask in updateTasks)
{
await Task.Run(() =>
{
switch (updateTask.task)
{
case UpdateOperations.DOWNLOAD:
DownloadFileFromServerAsync(updateTask.file, updateTask.link);
break;
case UpdateOperations.CREATE_DIR:
Directory.CreateDirectory(updateTask.file);
break;
case UpdateOperations.DELETE:
if (File.Exists(updateTask.file))
{
File.Delete(updateTask.file);
}
else if (Directory.Exists(updateTask.file))
{
try
{
Directory.Delete(updateTask.file, true);
} catch (Exception error)
{
MessageBox.Show($"Невозможно автоматически удалить каталог: {updateTask.file}\nДля корректной работы клиента игры, произведите удаление вручную!\nПричина: {error.Message}");
}
}
break;
}
});
progressBar.Value += 1;
}
await Task.Delay(1000);
progressBar.Maximum = 1;
progressBar.Value = 0;
return;
}
}
}