private List <UpdateFileEntryYaml> StartUpdate() { if (Cancelled) { return(new List <UpdateFileEntryYaml>()); } Label_ProgressBar1.SafeInvoke(() => Label_ProgressBar1.Text = Label_ProgressBar2.Text); var newUpdateInfo = UpdateInfoYaml.Deserialize(NewUpdateInfoFile.ReadAllText()); var crc32 = new Crc32(); var sha1 = new SHA1Managed(); var notValidFileEntries = new List <UpdateFileEntryYaml>(); PercentageProgressBar.SafeInvoke(delegate { PercentageProgressBar.Maximum = newUpdateInfo.Count(); PercentageProgressBar.Step = 1; PercentageProgressBar.Value = 0; }); Parallel.ForEach(newUpdateInfo, (updateFileEntry, state) => { if (Cancelled) { state.Stop(); } PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.PerformStep()); if (UpdatedFolder.CheckExists(updateFileEntry.AbsoluteFilePath) != ExistenceCheckResult.FileExists) { notValidFileEntries.Add(updateFileEntry); return; } using (var fs = UpdatedFolder.GetFile(updateFileEntry.AbsoluteFilePath).Open(PCLExt.FileStorage.FileAccess.Read)) { var crc32Hash = string.Empty; var sha1Hash = string.Empty; crc32Hash = crc32.ComputeHash(fs).Aggregate(crc32Hash, (current, b) => current + b.ToString("x2").ToLower()); if (crc32Hash == updateFileEntry.CRC32) { sha1Hash = sha1.ComputeHash(fs).Aggregate(sha1Hash, (current, b) => current + b.ToString("x2").ToLower()); if (sha1Hash == updateFileEntry.SHA1) { return; } else { notValidFileEntries.Add(updateFileEntry); return; } } else { notValidFileEntries.Add(updateFileEntry); return; } } }); return(notValidFileEntries); }
private async void DownloadUpdateInfoFiles() { OldUpdateInfoFile.Delete(); NewUpdateInfoFile.Delete(); try { using (Downloader = new WebClient()) { Downloader.DownloadProgressChanged += (sender, args) => PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.Value = (int)args.BytesReceived); PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.Maximum = OldUpdateInfoAsset.Size); await Downloader.DownloadFileTaskAsync(new Uri(OldUpdateInfoAsset.BrowserDownloadUrl), OldUpdateInfoFile.Path); PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.Maximum = NewUpdateInfoAsset.Size); await Downloader.DownloadFileTaskAsync(new Uri(NewUpdateInfoAsset.BrowserDownloadUrl), NewUpdateInfoFile.Path); } } catch (WebException) { DownloadErrorMessage(); return; } }
private void CheckOldFiles() { var oldUpdateInfo = UpdateInfoYaml.Deserialize(OldUpdateInfoFile.ReadAllText()); var newUpdateInfo = UpdateInfoYaml.Deserialize(NewUpdateInfoFile.ReadAllText()); var toBeDeleted = new List <IFile>(); var crc32 = new Crc32(); var sha1 = new SHA1Managed(); PercentageProgressBar.SafeInvoke(delegate { PercentageProgressBar.Maximum = oldUpdateInfo.Count(); PercentageProgressBar.Step = 1; PercentageProgressBar.Value = 0; }); Parallel.ForEach(oldUpdateInfo, (oldUpdateFileEntry, state) => { var newUpdateFileEntry = newUpdateInfo.FirstOrDefault(newUpdateInfoFile => newUpdateInfoFile.AbsoluteFilePath == oldUpdateFileEntry.AbsoluteFilePath); if (Cancelled) { state.Stop(); } PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.PerformStep()); if (UpdatedFolder.CheckExists(oldUpdateFileEntry.AbsoluteFilePath) != ExistenceCheckResult.FileExists) { state.Break(); } var folder = UpdatedFolder; var folders = oldUpdateFileEntry.AbsoluteFilePath.Split('\\'); foreach (var folderName in folders.Reverse().Skip(1).Reverse()) { folder = folder.GetFolder(folderName); } var file = UpdatedFolder.GetFile(folders.Reverse().Take(1).ToArray()[0]); using (var fs = file.Open(PCLExt.FileStorage.FileAccess.Read)) { var crc32Hash = string.Empty; var sha1Hash = string.Empty; crc32Hash = crc32.ComputeHash(fs).Aggregate(crc32Hash, (current, b) => current + b.ToString("x2").ToLower()); if (crc32Hash == oldUpdateFileEntry.CRC32 && crc32Hash == newUpdateFileEntry.CRC32) { sha1Hash = sha1.ComputeHash(fs).Aggregate(sha1Hash, (current, b) => current + b.ToString("x2").ToLower()); if (sha1Hash == oldUpdateFileEntry.SHA1 && sha1Hash == newUpdateFileEntry.SHA1) { return; // -- Everything matches, do nothing. } else { toBeDeleted.Add(file); return; // -- Should be deleted; } } else { toBeDeleted.Add(file); return; // -- Should be deleted; } } }); foreach (var file in toBeDeleted) { file.Delete(); } }