From c96a1adffd859b1a8fb6efd62eddcddd78bf198e Mon Sep 17 00:00:00 2001 From: MikeMoolenaar Date: Thu, 7 Mar 2024 21:21:31 +0100 Subject: [PATCH] Add better message when files are rejected, improves #11 --- MatroskaLib/MatroskaLib/MkvFilesContainer.cs | 7 ++++--- MatroskaLib/MatroskaLib/Types/MkvFile.cs | 15 ++++++++++----- .../MkvDefaultTrackChanger/MainForm.xeto.cs | 7 +++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/MatroskaLib/MatroskaLib/MkvFilesContainer.cs b/MatroskaLib/MatroskaLib/MkvFilesContainer.cs index 9499db6..5cdbd2e 100644 --- a/MatroskaLib/MatroskaLib/MkvFilesContainer.cs +++ b/MatroskaLib/MatroskaLib/MkvFilesContainer.cs @@ -8,7 +8,7 @@ namespace MatroskaLib; public class MkvFilesContainer { public readonly List MkvFiles = new(); - public readonly List MkFilesRejected = new(); + public readonly List<(MkvFile file, string error)> MkFilesRejected = new(); public MkvFilesContainer(string[] filePaths) { @@ -16,10 +16,11 @@ public MkvFilesContainer(string[] filePaths) MkvFiles.Add(files[0]); for (int i = 1; i < files.Count; i++) { - if (files[0].CompareTo(files[i]) == 0) + string? error = files[0].CompareToGetError(files[i]); + if (error is null) MkvFiles.Add(files[i]); else - MkFilesRejected.Add(files[i]); + MkFilesRejected.Add((files[i], error)); } } diff --git a/MatroskaLib/MatroskaLib/Types/MkvFile.cs b/MatroskaLib/MatroskaLib/Types/MkvFile.cs index aa538ab..99e1ae6 100644 --- a/MatroskaLib/MatroskaLib/Types/MkvFile.cs +++ b/MatroskaLib/MatroskaLib/Types/MkvFile.cs @@ -6,7 +6,7 @@ namespace MatroskaLib.Types; -public record MkvFile : IComparable +public record MkvFile { public required string filePath { get; init; } public required List tracks { get; init; } @@ -18,7 +18,7 @@ public record MkvFile : IComparable public required int tracksPosition { get; init; } public required int beginHeaderPosition { get; init; } - public int CompareTo(MkvFile? other) + public string? CompareToGetError(MkvFile? other) { if (other is null) throw new ArgumentNullException(nameof(other)); @@ -27,11 +27,16 @@ public int CompareTo(MkvFile? other) { var track = tracks[i]; var trackOther = other.tracks.ElementAtOrDefault(i); - if (trackOther is null || track.number != trackOther.number || track.language != trackOther.language) - return -1; + + if (trackOther is null) + return $"Track at index {i} does not exist, expected {track.type}{track.language}."; + if (track.number != trackOther.number) + return $"Track number {i} does not match. Expected {track.number}, got {trackOther.number}."; + if (track.language != trackOther.language) + return $"Track language {i} does not match. Expected {track.language}, got {trackOther.language}."; } - return 0; + return null; } public override string ToString() => diff --git a/MkvDefaultTrackChanger/MkvDefaultTrackChanger/MainForm.xeto.cs b/MkvDefaultTrackChanger/MkvDefaultTrackChanger/MainForm.xeto.cs index 381c32f..600e904 100644 --- a/MkvDefaultTrackChanger/MkvDefaultTrackChanger/MainForm.xeto.cs +++ b/MkvDefaultTrackChanger/MkvDefaultTrackChanger/MainForm.xeto.cs @@ -58,12 +58,15 @@ private void LoadFiles() mkvContainer = new MkvFilesContainer(filePaths); if (mkvContainer.MkFilesRejected.Count > 0) { + var sourceFile = Path.GetFileName(filePaths[0]); + string rejectedFiles = Environment.NewLine + Environment.NewLine; mkvContainer.MkFilesRejected.ForEach((x) => { - rejectedFiles += Path.GetFileName(x.filePath) + Environment.NewLine + Environment.NewLine; + rejectedFiles += $"- {Path.GetFileName(x.file.filePath)}: {x.error} {Environment.NewLine}{Environment.NewLine}"; }); - MessageBox.Show("The following files were rejected: " + rejectedFiles, MessageBoxType.Warning); + MessageBox.Show($"The following files have different tracks or the order is different than {sourceFile}: {rejectedFiles}These files cannot be processed.", + MessageBoxType.Warning); } var lsSubtitleTracks = mkvContainer.GetSubtitleTracks();