-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
442 lines (399 loc) · 16.2 KB
/
MainWindow.xaml.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Threading;
namespace MowerUpdater
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
internal ViewModel ViewModel => DataContext as ViewModel;
RsyncHost _host;
ConcurrentQueue<string> _buffer = new();
DispatcherTimer _updateTimer;
FileSystemWatcher _watcher;
RsyncHost host
{
get => _host;
set
{
if (_host != value)
{
_host?.Dispose();
_host = value;
}
}
}
public MainWindow()
{
App.Logger = _buffer.Enqueue;
InitializeComponent();
if (VersionsComboBox.ItemsSource is INotifyCollectionChanged col1)
{ // 稍稍改变多选框的逻辑,使得有可选项时立刻选中第一项
col1.CollectionChanged += (e, args) =>
{
if (args.OldItems == null || args.OldItems.Count == 0)
{
VersionsComboBox.SelectedIndex = 0;
}
};
}
if (PossibleInstallPathsComboBox.ItemsSource is INotifyCollectionChanged col2)
{
col2.CollectionChanged += (e, args) =>
{
if (args.OldItems == null || args.OldItems.Count == 0)
{
PossibleInstallPathsComboBox.SelectedIndex = 0;
}
};
}
var configDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"mower_updater"
);
var configPath = Path.Combine(configDir, "config.json");
if (!Directory.Exists(configDir))
{
Directory.CreateDirectory(configDir);
}
if (!File.Exists(configPath))
{
File.WriteAllText(configPath, UpdaterConfig.DefaultConfigJson);
}
ViewModel.ConfigPath = configPath;
_watcher = new(configDir, "config.json");
_watcher.Changed += async (e, args) =>
{
await Task.Delay(500);
Dispatcher.Invoke(() => ViewModel.Load(ViewModel.ConfigPath));
};
_watcher.EnableRaisingEvents = true;
// 缓冲区刷新计时器
_updateTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(125) };
_updateTimer.Tick += UpdateUIPerTick;
_updateTimer.Start();
}
private void SelectConfigPathButtonClicked(object sender, RoutedEventArgs e)
{
using var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*";
openFileDialog.DefaultExt = ".json";
var result = openFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
ViewModel.ConfigPath = openFileDialog.FileName;
}
}
private void SelectInstallPathButtonClicked(object sender, RoutedEventArgs e)
{
using var folderDialog = new FolderBrowserDialog();
var result = folderDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
ViewModel.InstallPath = folderDialog.SelectedPath;
}
}
private async Task OnInstallSuccess()
{
_buffer.Enqueue("====================== Done ======================");
_buffer.Enqueue("正在检查运行Mower的必要依赖");
var deps = new IDepInstaller[] { new VCInstaller(), new VC2013Installer(), new WebViewInstaller() };
var depsToInstall = deps.Where(dep => !dep.CheckIfInstalled());
if (depsToInstall.Any())
{
var depNames = string.Join(", ", depsToInstall.Select(dep => dep.Name));
var result = System.Windows.Forms.MessageBox.Show(
$"检测到以下依赖未被安装: {depNames}, 是否需要安装?", "确认安装依赖", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == System.Windows.Forms.DialogResult.OK)
{
foreach (var dep in depsToInstall)
{
_buffer.Enqueue($"正在安装 {dep.Name} ...");
await dep.Install(ViewModel.Client);
}
depsToInstall = deps.Where(dep => !dep.CheckIfInstalled());
if (!depsToInstall.Any()) {
_buffer.Enqueue("Mower所有必要的依赖已经安装");
}
else
{
depNames = string.Join(", ", depsToInstall.Select(dep => dep.Name));
_buffer.Enqueue($"检测到以下依赖未安装成功: {depNames}");
}
}
else
{
_buffer.Enqueue($"跳过以下依赖项: {depNames}");
}
}
else
{
_buffer.Enqueue("Mower所有必要的依赖已经安装");
}
System.Windows.Forms.MessageBox.Show(
$"已经成功安装版本 {VersionsComboBox.SelectedValue}", "安装成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private RsyncHost InitilizeHost()
{
var host = new RsyncHost();
host.OutputDataReceived += (sender, args) =>
{
if (args.Data != null)
{
_buffer.Enqueue(args.Data);
}
};
host.ErrorDataReceived += (sender, args) =>
{
if (args.Data != null)
{
_buffer.Enqueue(args.Data);
}
};
host.HostExited += (sender, args) =>
{
if (host.ExitCode != 0)
{
_buffer.Enqueue("=================== Terminate ===================");
Dispatcher.Invoke(() => System.Windows.Forms.MessageBox.Show(
$"rsync返回值: {host.ExitCode}\n详细错误请参见底部文本框输出", "安装失败", MessageBoxButtons.OK, MessageBoxIcon.Error));
}
else
{
Dispatcher.Invoke(OnInstallSuccess);
}
};
return host;
}
const long BytesPerMB = 1024 * 1024;
private void ProgressUpdated((long downloaded, long total) info)
{
if (info.total != -1)
{
_buffer.Enqueue($"Download progress: {info.downloaded / BytesPerMB} / {info.total / BytesPerMB} MB");
}
else
{
_buffer.Enqueue($"Download progress: {info.downloaded / BytesPerMB} MB");
}
}
private void OnDownloadFailed((int retries, Exception ex) info)
{
_buffer.Enqueue($"下载失败 #{info.retries}: {info.ex.Message}");
}
private async Task EnsureDownloaded(string url, string file, bool forceRedownload = false)
{
if (!File.Exists(file) || forceRedownload)
{
var downloading_file = file + ".downloading";
Directory.CreateDirectory(Path.GetDirectoryName(file));
using (var fs = File.Open(downloading_file, FileMode.OpenOrCreate, FileAccess.Write))
{
var downloader = new FileDownloader(ViewModel.Client, url, fs);
downloader.ProgressUpdated += ProgressUpdated;
downloader.OnFailed += OnDownloadFailed;
await downloader.DownloadAsync();
}
File.Move(downloading_file, file);
}
}
private async Task NewInstall(string mirror, string version, string path)
{
var ghproxyUrl = $"https://ghproxy.com/https://github.com/ArkMowers/arknights-mower/releases/download/{version}/{version}.zip";
var url = $"{mirror}/{version}.zip";
var dest = Path.Combine(Path.GetTempPath(), "MowerUpdater", $"{version}.zip");
try
{
if (ViewModel.UseGhproxy)
{
_buffer.Enqueue("尝试使用GhProxy下载");
var client = ViewModel.Client;
using var resp = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, ghproxyUrl));
resp.EnsureSuccessStatusCode();
_buffer.Enqueue("GhProxy访问成功,正在下载");
await EnsureDownloaded(ghproxyUrl, dest);
_buffer.Enqueue("GhProxy下载成功");
}
else
{
_buffer.Enqueue("使用镜像下载");
}
}
catch
{
// 如果GhProxy下载失败,使用镜像源
// 注意,如果下载成功,则不会在没有指定forceRedownload时再次下载,所以下面的调用是安全的、快速的
_buffer.Enqueue("GhProxy下载失败,将使用镜像下载");
}
await EnsureDownloaded(url, dest);
await Task.Run(() =>
{
using var fs = File.OpenRead(dest);
using var zipArchive = new ZipArchive(fs);
Directory.CreateDirectory(path);
zipArchive.ExtractToDirectory(path);
var folder = new DirectoryInfo(Path.Combine(path, version));
if (folder.Exists)
{
foreach (var item in folder.EnumerateFileSystemInfos())
{
if (item is DirectoryInfo d)
{
d.MoveTo(Path.Combine(path, d.Name));
}
if (item is FileInfo f)
{
f.MoveTo(Path.Combine(path, f.Name));
}
}
folder.Delete();
}
});
await Dispatcher.Invoke(OnInstallSuccess);
}
private bool PreInstallCheck()
{
var installPath = ViewModel.SelectedInstallPath.Path;
var containsNonAscii = installPath.Any(ch => ch >= (char)128);
if (containsNonAscii)
{
System.Windows.Forms.MessageBox.Show("安装目录路径必须不包含中文,否则Mower无法正常运行", "安装失败");
return false;
}
var procs = Process.GetProcessesByName("mower")
.Concat(Process.GetProcessesByName("Mower0"));
var failToFetch = false;
foreach (var proc in procs)
{
try
{
if (proc.MainModule.FileName.StartsWith(installPath, StringComparison.OrdinalIgnoreCase))
{
System.Windows.Forms.MessageBox.Show("检测到Mower正在运行,请关闭后再更新", "安装失败", MessageBoxButtons.OK);
return false;
}
}
catch
{
failToFetch = true;
}
}
if (failToFetch)
{
var result = System.Windows.Forms.MessageBox.Show(
"检测到Mower正在运行,未能获取到正在运行Mower的安装目录,请确认关闭当前安装目录下的Mower再继续",
"安装确认", MessageBoxButtons.OKCancel);
return result == System.Windows.Forms.DialogResult.OK;
}
return true;
}
private async void InstallButtonClicked(object sender, RoutedEventArgs e)
{
ViewModel.Busy = true;
try
{
if (!PreInstallCheck())
{
return;
}
int interval = 1000; // 之前因为没关闭文件流导致报错的一个修复尝试
int retries = 3; // 想了想也没必要删除
while (retries-- >= 0) // 是不是很像TCP超时重传
{
try
{
ViewModel.Save();
break;
}
catch
{
await Task.Delay(interval);
//interval *= 2;
if (retries == 0) throw;
}
}
Dispatcher.Invoke(() => ViewModel.OutputLogs = string.Empty);
var local = ViewModel.SelectedInstallPath;
if (local.IsInstalled == false
&& Directory.Exists(local.Path)
&& Directory.EnumerateFileSystemEntries(local.Path).Any())
{
var result = System.Windows.Forms.MessageBox.Show(
$"将在 {local.Path} 下执行全新安装,该目录非空,确定要这么做吗?", "安装须知", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}
}
if (local.IsInstalled == false)
{
await NewInstall(ViewModel.Mirror, ((VersionInfo)VersionsComboBox.SelectedValue).VersionName, ViewModel.InstallPath);
return;
}
host = InitilizeHost();
host.Start(ViewModel.ConfigPath, ((VersionInfo)VersionsComboBox.SelectedValue).VersionName);
await host.WaitForExit();
}
catch (Exception ex)
{
Dispatcher.Invoke(() =>
{
System.Windows.Forms.MessageBox.Show(
ex.ToString(), "安装失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
finally
{
ViewModel.Busy = false;
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
_host?.Dispose();
}
private void ConsoleOutputTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
bool isAtBottom =
ConsoleOutputScrollViewer.VerticalOffset == ConsoleOutputScrollViewer.ScrollableHeight;
if (isAtBottom)
{
ConsoleOutputScrollViewer.ScrollToBottom();
}
}
private void UpdateUIPerTick(object sender, EventArgs e) // 将缓冲区的内容一次性推到文本框内容
{
var sb = new StringBuilder();
while (_buffer.TryDequeue(out var item))
{
sb.AppendLine(item);
}
ViewModel.OutputLogs += sb.ToString();
}
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start(e.Uri.ToString());
}
private void EditConfigButtonClicked(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start(ViewModel.ConfigPath);
}
}
}