From 9b98029899f891da4ae8e7b4324c559f77b8b99c Mon Sep 17 00:00:00 2001 From: FenPhoenix Date: Tue, 7 May 2019 21:52:40 -0700 Subject: [PATCH] Cleanup --- AngelLoader/AudioConverter.cs | 174 +++++++++--------- .../Common/DataClasses/MiscSupporting.cs | 2 +- AngelLoader/Common/Utility/Extensions.cs | 23 +-- AngelLoader/Core.cs | 21 +-- AngelLoader/InstallAndPlay.cs | 4 +- 5 files changed, 96 insertions(+), 128 deletions(-) diff --git a/AngelLoader/AudioConverter.cs b/AngelLoader/AudioConverter.cs index 35f26c7ca..9bf7f3f13 100644 --- a/AngelLoader/AudioConverter.cs +++ b/AngelLoader/AudioConverter.cs @@ -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; @@ -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"); + } } } diff --git a/AngelLoader/Common/DataClasses/MiscSupporting.cs b/AngelLoader/Common/DataClasses/MiscSupporting.cs index 87b09afa2..edabc7c27 100644 --- a/AngelLoader/Common/DataClasses/MiscSupporting.cs +++ b/AngelLoader/Common/DataClasses/MiscSupporting.cs @@ -314,7 +314,7 @@ internal void Clear() internal string InstalledName { get => _installedName; - set => _installedName = value.ThisOrNull(); + set => _installedName = value.IsEmpty() ? null : value; } /// /// The index relative to the first displayed item (not the first item period) in the list. diff --git a/AngelLoader/Common/Utility/Extensions.cs b/AngelLoader/Common/Utility/Extensions.cs index 92d80ad58..f5d0629fd 100644 --- a/AngelLoader/Common/Utility/Extensions.cs +++ b/AngelLoader/Common/Utility/Extensions.cs @@ -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; } @@ -533,22 +530,6 @@ internal static string GetTopmostDirName(this string path) return path.Substring(i + 1, end - (i + 1)); } - /// - /// If is null or empty, returns null. Otherwise, returns - /// unchanged. - /// - /// - /// - internal static string ThisOrNull(this string value) => value.IsEmpty() ? null : value; - - /// - /// If is null, empty, or whitespace, returns null. Otherwise, returns - /// unchanged. - /// - /// - /// - internal static string ThisOrNullWS(this string value) => value.IsWhiteSpace() ? null : value; - /// /// If is less than zero, returns zero. Otherwise, returns /// unchanged. diff --git a/AngelLoader/Core.cs b/AngelLoader/Core.cs index dca04a508..ad7c77141 100644 --- a/AngelLoader/Core.cs +++ b/AngelLoader/Core.cs @@ -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) { @@ -1141,7 +1141,7 @@ internal static async Task ConvertOGGsToWAVs(FanMission fm) try { ProgressBox.ShowConvertingFiles(); - await ac.ConvertOGGsToWAVsInternal(); + await ac.ConvertOGGsToWAVs(); } finally { @@ -1184,7 +1184,7 @@ internal static async Task ConvertWAVsTo16Bit(FanMission fm) try { ProgressBox.ShowConvertingFiles(); - await ac.ConvertWAVsTo16BitInternal(); + await ac.ConvertWAVsTo16Bit(); } finally { @@ -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) diff --git a/AngelLoader/InstallAndPlay.cs b/AngelLoader/InstallAndPlay.cs index 93ce5aed7..dfc513674 100644 --- a/AngelLoader/InstallAndPlay.cs +++ b/AngelLoader/InstallAndPlay.cs @@ -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)