Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FenPhoenix committed May 8, 2019
1 parent fe44a69 commit 9b98029
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 128 deletions.
174 changes: 88 additions & 86 deletions AngelLoader/AudioConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,96 +24,12 @@ internal AudioConverter(FanMission fm, string installedFMsBasePath)
InstalledFMsBasePath = installedFMsBasePath;
}

#region Audio conversion

// TODO: ffmpeg can do multiple files in one run. Switch to that, and see if ffprobe can do it too.
// TODO: Handle if any files (or containing folders) to be converted are read-only (set them to not)

private string GetFMSoundPathByGame()
{
Debug.Assert(FM.Game != null, nameof(FM.Game) + " is null");
Debug.Assert(FM.Game != Game.Unsupported, nameof(FM.Game) + " is " + nameof(Game.Unsupported));
Debug.Assert(GameIsDark(FM), FM.Archive + " is not T1/T2");

// Only T1/T2 can have audio converted for now, because it looks like SU's FMSel pointedly doesn't do
// any conversion whatsoever, neither automatically nor even with a menu option. I'll assume Thief 3
// doesn't need it and leave it at that.
return Path.Combine(InstalledFMsBasePath, FM.InstalledDir, "snd");
}

internal async Task ConvertMP3sToWAVs() => await ConvertToWAVs("*.mp3");

// From the FMSel manual:
// "The game _can_ play OGG files but it can under some circumstance cause short hiccups, on less powerful
// computers, performance heavy missions or with large OGG files. In such cases it might help to convert
// them to WAV files during installation."
internal async Task ConvertOGGsToWAVsInternal() => await ConvertToWAVs("*.ogg");

private async Task ConvertToWAVs(string pattern)
{
if (!GameIsDark(FM)) return;

await Task.Run(async () =>
{
var fmSndPath = GetFMSoundPathByGame();
if (!Directory.Exists(fmSndPath)) return;

try
{
var di = new DirectoryInfo(fmSndPath) { Attributes = FileAttributes.Normal };
}
catch (Exception ex)
{
Log("Unable to set directory attributes on " + fmSndPath, ex);
}

string[] files;
try
{
files = Directory.GetFiles(fmSndPath, pattern, SearchOption.AllDirectories);
}
catch (Exception ex)
{
Log("Exception during file enumeration of " + fmSndPath, ex);
return;
}

foreach (var f in files)
{
try
{
new FileInfo(f).IsReadOnly = false;
}
catch (Exception ex)
{
Log("Unable to set file attributes on " + f, ex);
}

try
{
var engine = new Engine(Paths.FFmpegExe);
await engine.ConvertAsync(new MediaFile(f), new MediaFile(f.RemoveExtension() + ".wav"));
}
catch (Exception ex)
{
Log("Exception in FFmpeg convert", ex);
}

try
{
File.Delete(f);
}
catch (Exception ex)
{
Log("Exception in deleting file " + f, ex);
}
}
});
}

// OpenAL doesn't play nice with anything over 16 bits, blasting out white noise when it tries to play
// such. Converting all >16bit wavs to 16 bit fixes this.
internal async Task ConvertWAVsTo16BitInternal()
internal async Task ConvertWAVsTo16Bit()
{
if (!GameIsDark(FM)) return;

Expand Down Expand Up @@ -232,6 +148,92 @@ await Task.Run(async () =>
});
}

#endregion
// Dark engine games can't play MP3s, so they must be converted in all cases.
internal async Task ConvertMP3sToWAVs() => await ConvertToWAVs("*.mp3");

// From the FMSel manual:
// "The game _can_ play OGG files but it can under some circumstance cause short hiccups, on less powerful
// computers, performance heavy missions or with large OGG files. In such cases it might help to convert
// them to WAV files during installation."
internal async Task ConvertOGGsToWAVs() => await ConvertToWAVs("*.ogg");

private async Task ConvertToWAVs(string pattern)
{
if (!GameIsDark(FM)) return;

await Task.Run(async () =>
{
try
{
var fmSndPath = GetFMSoundPathByGame();
if (!Directory.Exists(fmSndPath)) return;

try
{
var di = new DirectoryInfo(fmSndPath) { Attributes = FileAttributes.Normal };
}
catch (Exception ex)
{
Log("Unable to set directory attributes on " + fmSndPath, ex);
}

string[] files;
try
{
files = Directory.GetFiles(fmSndPath, pattern, SearchOption.AllDirectories);
}
catch (Exception ex)
{
Log("Exception during file enumeration of " + fmSndPath, ex);
return;
}

foreach (var f in files)
{
try
{
new FileInfo(f).IsReadOnly = false;
}
catch (Exception ex)
{
Log("Unable to set file attributes on " + f, ex);
}

try
{
var engine = new Engine(Paths.FFmpegExe);
await engine.ConvertAsync(new MediaFile(f), new MediaFile(f.RemoveExtension() + ".wav"));
}
catch (Exception ex)
{
Log("Exception in FFmpeg convert", ex);
}

try
{
File.Delete(f);
}
catch (Exception ex)
{
Log("Exception in deleting file " + f, ex);
}
}
}
catch (Exception ex)
{
Log("Exception in file conversion", ex);
}
});
}

private string GetFMSoundPathByGame()
{
// Only T1/T2 can have audio converted for now, because it looks like SU's FMSel pointedly doesn't do
// any conversion whatsoever, neither automatically nor even with a menu option. I'll assume Thief 3
// doesn't need it and leave it at that.
Debug.Assert(GameIsDark(FM), !FM.Archive.IsEmpty() ? FM.Archive : FM.InstalledDir + " is not T1/T2");

return Path.Combine(InstalledFMsBasePath, FM.InstalledDir, "snd");
}
}
}
2 changes: 1 addition & 1 deletion AngelLoader/Common/DataClasses/MiscSupporting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ internal void Clear()
internal string InstalledName
{
get => _installedName;
set => _installedName = value.ThisOrNull();
set => _installedName = value.IsEmpty() ? null : value;
}
/// <summary>
/// The index relative to the first displayed item (not the first item period) in the list.
Expand Down
23 changes: 2 additions & 21 deletions AngelLoader/Common/Utility/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,9 @@ private static string ToInstDirName(string archiveName, string illegalChars, boo
{
archiveName = archiveName.RemoveExtension();

if (truncate) archiveName = archiveName.Length > 30 ? archiveName.Substring(0, 30) : archiveName;
if (truncate && archiveName.Length > 30) archiveName = archiveName.Substring(0, 30);

for (var i = 0; i < illegalChars.Length; i++)
{
archiveName = archiveName.Replace(illegalChars[i], '_');
}
for (var i = 0; i < illegalChars.Length; i++) archiveName = archiveName.Replace(illegalChars[i], '_');

return archiveName;
}
Expand Down Expand Up @@ -533,22 +530,6 @@ internal static string GetTopmostDirName(this string path)
return path.Substring(i + 1, end - (i + 1));
}

/// <summary>
/// If <paramref name="value"/> is null or empty, returns null. Otherwise, returns <paramref name="value"/>
/// unchanged.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
internal static string ThisOrNull(this string value) => value.IsEmpty() ? null : value;

/// <summary>
/// If <paramref name="value"/> is null, empty, or whitespace, returns null. Otherwise, returns
/// <paramref name="value"/> unchanged.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
internal static string ThisOrNullWS(this string value) => value.IsWhiteSpace() ? null : value;

/// <summary>
/// If <paramref name="value"/> is less than zero, returns zero. Otherwise, returns <paramref name="value"/>
/// unchanged.
Expand Down
21 changes: 3 additions & 18 deletions AngelLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ internal static async Task RefreshFromDisk()
await View.SortAndSetFilter();
}

#region Install, Uninstall, Play
#region Audio conversion (mainly for pre-checks)

internal static async Task ConvertOGGsToWAVs(FanMission fm)
{
Expand Down Expand Up @@ -1141,7 +1141,7 @@ internal static async Task ConvertOGGsToWAVs(FanMission fm)
try
{
ProgressBox.ShowConvertingFiles();
await ac.ConvertOGGsToWAVsInternal();
await ac.ConvertOGGsToWAVs();
}
finally
{
Expand Down Expand Up @@ -1184,7 +1184,7 @@ internal static async Task ConvertWAVsTo16Bit(FanMission fm)
try
{
ProgressBox.ShowConvertingFiles();
await ac.ConvertWAVsTo16BitInternal();
await ac.ConvertWAVsTo16Bit();
}
finally
{
Expand All @@ -1194,21 +1194,6 @@ internal static async Task ConvertWAVsTo16Bit(FanMission fm)

#endregion

internal static void ReportFMExtractProgress(int percent)
{
View.InvokeSync(new Action(() => ProgressBox.ReportFMExtractProgress(percent)));
}

internal static void HideProgressBox()
{
View.InvokeSync(new Action(() => ProgressBox.HideThis()));
}

internal static void ProgressBoxSetCancelingFMInstall()
{
View.InvokeSync(new Action(ProgressBox.SetCancelingFMInstall));
}

#region DML

internal static bool AddDML(FanMission fm, string sourceDMLPath)
Expand Down
4 changes: 2 additions & 2 deletions AngelLoader/InstallAndPlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,11 @@ await Task.Run(() =>

if (Config.ConvertOGGsToWAVsOnInstall)
{
await ac.ConvertOGGsToWAVsInternal();
await ac.ConvertOGGsToWAVs();
}
else if (Config.ConvertWAVsTo16BitOnInstall)
{
await ac.ConvertWAVsTo16BitInternal();
await ac.ConvertWAVsTo16Bit();
}
}
catch (Exception ex)
Expand Down

0 comments on commit 9b98029

Please sign in to comment.