public static ClientInfo MakeClientInfo() { var basePath = Directory.GetFiles(GameStoragePath, "*", SearchOption.AllDirectories); var clientInfo = new ClientInfo(); var files = new List <FileEntry>(); Parallel.ForEach(basePath, new ParallelOptions { MaxDegreeOfParallelism = 16 }, (file, state) => { var path = file.Substring(GameStoragePath.Length); // var fileData = File.ReadAllText(file); string hash; using (var sfile = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { hash = SHA128Computer.Compute(sfile); } Trace.WriteLine($"计算哈希完成: {path}."); files.Add(new FileEntry(path, hash)); }); clientInfo.Files = files; return(clientInfo); }
private async void Confirm(object sender, RoutedEventArgs e) { ConfirmBtn.IsEnabled = false; await Task.Run(() => { Parallel.ForEach(UpdateInfo.FilesToDelete, deleteEntry => { if (!isRunning) { return; } try { var path = ClientManager.GetGameStorageDirectory(deleteEntry.Path); if (File.Exists(path)) { File.Delete(path); } else { Trace.WriteLine($"文件不存在: {path}"); } try { var current = ClientManager.CurrentInfo.Files.Find(f => f.Equals(deleteEntry)); ClientManager.CurrentInfo.Files.Remove(current); } catch (Exception exception) { Trace.WriteLine(exception); } Dispatcher.Invoke(() => UpdatePanel.Children.Remove(_deleteDictionary[deleteEntry])); } catch (Exception exception) { MessageUploadManager.CrashReport(new UploadData($"更新器V2发生异常 {exception.SerializeException()} {exception.InnerException?.SerializeException()}")); isRunning = false; } }); Parallel.ForEach(UpdateInfo.FilesToDownload, downloadEntry => { if (!isRunning) { return; } try { var wc = new WebClient { Proxy = null }; var path = ClientManager.GetGameStorageDirectory(downloadEntry.Path); if (File.Exists(path)) { using (var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (SHA128Computer.Compute(file) == downloadEntry.Hash) { goto finish; } } File.Delete(path); } DirectoryHelper.EnsureDirectoryExists(Path.GetDirectoryName(path)); wc.DownloadProgressChanged += (o, args) => Dispatcher.InvokeAsync(() => { var progress = _downloadProgressDictionary[downloadEntry]; progress.Value = args.BytesReceived / (double)args.TotalBytesToReceive; }); wc.DownloadFileCompleted += (o, args) => ClientManager.CurrentInfo.Files.Add(new FileEntry(downloadEntry.Path, downloadEntry.Hash)); wc.DownloadFileTaskAsync(downloadEntry.Url, path).Wait(); finish: Dispatcher.InvokeAsync(() => { UpdatePanel.Children.Remove(_downloadProgressDictionary[downloadEntry]); UpdatePanel.Children.Remove(_downloadTbDictionary[downloadEntry]); }); } catch (Exception exception) { MessageUploadManager.CrashReport(new UploadData($"更新器V2发生异常 {exception.SerializeException()} {exception.InnerException?.SerializeException()}")); isRunning = false; } }); Result = isRunning; Close(); }); }