Skip to content

Commit

Permalink
Subs search fix, should fix #39
Browse files Browse the repository at this point in the history
  • Loading branch information
ToaHartor committed Oct 24, 2022
1 parent 0af9a42 commit 11b3f6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/FileTypes/ASS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _dialogLines;
Expand Down
18 changes: 10 additions & 8 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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");
}
Expand Down

0 comments on commit 11b3f6d

Please sign in to comment.