From 11b3f6df48ce0ad058022031bb45ae950ae47204 Mon Sep 17 00:00:00 2001 From: ToaHartor <41634031+ToaHartor@users.noreply.github.com> Date: Tue, 25 Oct 2022 00:15:36 +0900 Subject: [PATCH] Subs search fix, should fix #39 --- src/FileTypes/ASS.cs | 1 + src/Program.cs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/FileTypes/ASS.cs b/src/FileTypes/ASS.cs index a9da757..31bcbea 100644 --- a/src/FileTypes/ASS.cs +++ b/src/FileTypes/ASS.cs @@ -4,6 +4,7 @@ namespace GICutscenes.FileTypes { internal class ASS { + public static readonly string[] SubsExtensions = {".ass", ".srt", ".txt"}; private readonly string _srt; private readonly string _fontname; private readonly List _dialogLines; diff --git a/src/Program.cs b/src/Program.cs index 87d81ab..b827144 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -264,14 +264,22 @@ private static void MergeFiles(string outputPath, string basename, string engine foreach (string d in Directory.EnumerateDirectories(subsFolder)) { string lang = Path.GetFileName(d) ?? throw new DirectoryNotFoundException(); - string[] search = Directory.GetFiles(d, $"{subName}_{lang}.*"); + string[] search = Directory.GetFiles(d, $"{subName}_{lang}.*").OrderBy(f => f).ToArray(); // Sorting by name switch (search.Length) { case 0: Console.WriteLine($"No subtitle for {subName} could be found for the language {lang}, skipping..."); break; case 1: - ASS sub = new(search[0], lang); + // Could be either the presence of both .srt and .txt files (following the 3.0 release), but also .ass + case 2: + // Might be .srt+.txt+.ass + case 3: + // The "search" array is sorted by name, which means that the file order would be ASS > SRT > TXT + string res = Array.Find(search, name => ASS.SubsExtensions.Contains(Path.GetExtension(name))) ?? throw new FileNotFoundException( + $"No valid file could be found for the subs {subName} while the files corresponding to the name is {search.Length}"); ; + Console.WriteLine($"Using subs file {Path.GetFileName(res)}"); + ASS sub = new(res, lang); string subFile = search[0]; if (!sub.IsAss()) { @@ -281,12 +289,6 @@ private static void MergeFiles(string outputPath, string basename, string engine merger.AddSubtitlesTrack(subFile, lang); break; - case 2: - string res = Array.Find(search, name => Path.GetExtension(name) == ".ass") ?? - throw new FileNotFoundException( - $"No ASS file could be found for the subs {subName}, but two files were matched previously, please report this case."); - merger.AddSubtitlesTrack(res, lang); - break; default: throw new Exception($"Too many results ({search.Length}), please report this case"); }