private void SaveEditingIgnoreFileButton_Click(object sender, RoutedEventArgs e) { var errors = new StringBuilder(); int counter = 0; var safeIgnoreFile = new CommonSaveFileDialog(); safeIgnoreFile.DefaultExtension = "repignore"; if (safeIgnoreFile.ShowDialog() == CommonFileDialogResult.Ok) { var ignoreFile = _ignoreFile ?? new IgnoreFile(); var toIgnoreFiles = editIgnoreFileTextBox.Text.Split('\n').Select(f => f.Trim(' ', '\t', '\n', '\r')); ignoreFile.Clear(); foreach (var toIgnoreFile in toIgnoreFiles) { if (string.IsNullOrWhiteSpace(toIgnoreFile)) { continue; } string hash = null; try { hash = FileHasher.GetMd5Hash(toIgnoreFile); if (hash == null) { counter++; errors.AppendLine($"{counter}. Failed to compute hash for file {toIgnoreFile}"); continue; } ignoreFile.Ignore(new Tuple <string, string>(toIgnoreFile, hash)); } catch (Exception ex) { ErrorLogger.GetInstance()?.LogError($"An error occurred while trying to compute the hash for file {toIgnoreFile}", ex: ex); counter++; errors.AppendLine($"{counter}. An error occurred while processing the file {toIgnoreFile}"); } } _ignoreFileManager.Save(ignoreFile, safeIgnoreFile.FileName, true); _replaySorterConfiguration.IgnoreFilePath = safeIgnoreFile.FileName; } if (errors.Length > 0) { MessageBox.Show($"The following replays encountered errors:{Environment.NewLine}{errors.ToString()}", "Invalid files", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); } this.Close(); }
public ReplayParser.ReplaySorter.Backup.Models.Backup ToBackup() { var backup = new ReplayParser.ReplaySorter.Backup.Models.Backup(); backup.Name = Name; backup.Comment = Comment; backup.RootDirectory = RootDirectory; var rootDirectory = string.Empty; if (RootDirectory.Last() != Path.DirectorySeparatorChar || RootDirectory.Last() != Path.AltDirectorySeparatorChar) { rootDirectory = RootDirectory + Path.DirectorySeparatorChar; } ICollection <ReplayParser.ReplaySorter.Backup.Models.ReplayBackup> replayBackups = new Collection <ReplayParser.ReplaySorter.Backup.Models.ReplayBackup>(); var replayDictionary = new Dictionary <string, ReplayParser.ReplaySorter.Backup.Models.Replay>(); foreach (var file in Replays) { try { byte[] bytes = System.IO.File.ReadAllBytes(file); string hash = FileHasher.GetMd5Hash(bytes); ReplayParser.ReplaySorter.Backup.Models.Replay replay = null; if (!replayDictionary.TryGetValue(hash, out replay)) { replay = new ReplayParser.ReplaySorter.Backup.Models.Replay { Hash = hash, Bytes = bytes }; replayDictionary.Add(hash, replay); } var replayBackup = new ReplayParser.ReplaySorter.Backup.Models.ReplayBackup { Backup = backup, FileName = file.Contains(rootDirectory) ? file.Substring(rootDirectory.Length) : file, //FileName = file, Replay = replay }; if (replay.ReplayBackups == null) { replay.ReplayBackups = new Collection <ReplayParser.ReplaySorter.Backup.Models.ReplayBackup>(); } replay.ReplayBackups.Add(replayBackup); replayBackups.Add(replayBackup); } catch (Exception ex) { //TODO If a backup can't backup everything, it should fail instead... ErrorLogger.GetInstance()?.LogError($"{DateTime.Now} - Something went wrong while opening or hashing the file", ex: ex); } } backup.ReplayBackups = replayBackups; return(backup); }