From b61bed3b55f6cbe14903cca64bfa10686db83ef3 Mon Sep 17 00:00:00 2001 From: MrKev312 <34964788+MrKev312@users.noreply.github.com> Date: Thu, 5 Sep 2024 14:02:36 +0200 Subject: [PATCH] Refactor file handling and add FFmpeg existence check Refactored file handling across multiple converters: - AudioConverter.cs: Construct WAV file path using `wavPath.Name + wavPath.Extension`. - ConvertUbiArtToUnity.cs: Added a check for `ffmpeg.exe` before downloading FFmpeg. - MenuArtConverter.cs: * Used `convert.FileSystem.GetAllFiles` to get menu art files. * Replaced file path arrays with `CookedFile` objects. * Updated sorting logic to prioritize cooked files and then by name. * Switched from `Parallel.ForEach` to `foreach` for file processing. - VideoConverter.cs: * Used `convert.FileSystem.GetAllFiles` to get video files. * Converted `CookedFile` objects to strings for processing. Added a new method in FileSystem.cs: - `GetAllFiles`: Retrieves all files matching a pattern from a folder and its subdirectories. --- .../Converters/Audio/AudioConverter.cs | 2 +- .../Converters/ConvertUbiArtToUnity.cs | 3 +- .../Converters/Images/MenuArtConverter.cs | 14 ++++----- .../Converters/Video/VideoConverter.cs | 4 +-- JustDanceEditor.Converter/Files/FileSystem.cs | 29 +++++++++++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/JustDanceEditor.Converter/Converters/Audio/AudioConverter.cs b/JustDanceEditor.Converter/Converters/Audio/AudioConverter.cs index 1bd0468..b574267 100644 --- a/JustDanceEditor.Converter/Converters/Audio/AudioConverter.cs +++ b/JustDanceEditor.Converter/Converters/Audio/AudioConverter.cs @@ -152,7 +152,7 @@ static void ConvertAudioFiles(ConvertUbiArtToUnity convert, SoundSetClip[] audio string relativePath = Path.ChangeExtension(audioVibrationClip.SoundSetPath, ".wav"); if (!convert.FileSystem.GetFilePath(relativePath, out CookedFile? wavPath)) continue; - string newWavPath = Path.Combine(convert.FileSystem.TempFolders.AudioFolder, Path.GetFileName(wavPath)); + string newWavPath = Path.Combine(convert.FileSystem.TempFolders.AudioFolder, wavPath.Name + wavPath.Extension); if (!File.Exists(newWavPath)) audioConverter.Convert(wavPath, newWavPath).Wait(); } diff --git a/JustDanceEditor.Converter/Converters/ConvertUbiArtToUnity.cs b/JustDanceEditor.Converter/Converters/ConvertUbiArtToUnity.cs index cce2974..9cdb9ac 100644 --- a/JustDanceEditor.Converter/Converters/ConvertUbiArtToUnity.cs +++ b/JustDanceEditor.Converter/Converters/ConvertUbiArtToUnity.cs @@ -208,7 +208,8 @@ async Task GenerateMapPackageAsync() async Task ConvertMediaAsync() { // First, download FFmpeg if needed - await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official); + if (!File.Exists("ffmpeg.exe")) + await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official); // Then, convert the audio and video await Task.WhenAll( diff --git a/JustDanceEditor.Converter/Converters/Images/MenuArtConverter.cs b/JustDanceEditor.Converter/Converters/Images/MenuArtConverter.cs index 8c6fd25..114b730 100644 --- a/JustDanceEditor.Converter/Converters/Images/MenuArtConverter.cs +++ b/JustDanceEditor.Converter/Converters/Images/MenuArtConverter.cs @@ -11,16 +11,16 @@ public async static Task ConvertMenuArtAsync(ConvertUbiArtToUnity convert) => await Task.Run(() => ConvertMenuArt(convert)); public static void ConvertMenuArt(ConvertUbiArtToUnity convert) { - string menuArtFolder = convert.FileSystem.GetFolderPath(convert.FileSystem.InputFolders.MenuArtFolder); + CookedFile[] menuArtFiles = convert.FileSystem.GetAllFiles(convert.FileSystem.InputFolders.MenuArtFolder); - string[] menuArtFiles = [.. Directory.GetFiles(menuArtFolder) - .OrderByDescending(f => f.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) - .ThenBy(f => f)]; + menuArtFiles = [.. menuArtFiles + .OrderByDescending(f => f.IsCooked) + .ThenBy(f => (string)f)]; Logger.Log($"Converting {menuArtFiles.Length} menu art files..."); Stopwatch stopwatch = Stopwatch.StartNew(); - Parallel.ForEach(menuArtFiles, (file) => + foreach (CookedFile file in menuArtFiles) { try { @@ -29,7 +29,7 @@ public static void ConvertMenuArt(ConvertUbiArtToUnity convert) // If the output file already exists, skip it if (File.Exists(pngPath)) - return; + continue; TextureConverter.TextureConverter.ExtractToPNG(file, pngPath); } @@ -37,7 +37,7 @@ public static void ConvertMenuArt(ConvertUbiArtToUnity convert) { Logger.Log($"Failed to convert menu art file {file}: {ex.Message}", LogLevel.Error); } - }); + } stopwatch.Stop(); Logger.Log($"Finished converting menu art files in {stopwatch.ElapsedMilliseconds}ms"); diff --git a/JustDanceEditor.Converter/Converters/Video/VideoConverter.cs b/JustDanceEditor.Converter/Converters/Video/VideoConverter.cs index 254a77e..8a63be3 100644 --- a/JustDanceEditor.Converter/Converters/Video/VideoConverter.cs +++ b/JustDanceEditor.Converter/Converters/Video/VideoConverter.cs @@ -69,8 +69,8 @@ static string GetVideoFile(ConvertUbiArtToUnity convert) if (videofiles.Length > 0) return videofiles[0]; - if (convert.FileSystem.GetFolderPath(Path.Combine(convert.FileSystem.InputFolders.MapWorldFolder, "videoscoach"), out string? coachFolder)) - videofiles = Directory.GetFiles(coachFolder, "*.webm"); + videofiles = convert.FileSystem.GetAllFiles(Path.Combine(convert.FileSystem.InputFolders.MapWorldFolder, "videoscoach"), "*.webm") + .Select(x => (string)x).ToArray(); if (videofiles.Length > 0) return videofiles[0]; diff --git a/JustDanceEditor.Converter/Files/FileSystem.cs b/JustDanceEditor.Converter/Files/FileSystem.cs index 746a3cf..0e4cc93 100644 --- a/JustDanceEditor.Converter/Files/FileSystem.cs +++ b/JustDanceEditor.Converter/Files/FileSystem.cs @@ -147,6 +147,35 @@ public CookedFile GetFilePath(string relativeFilePath) : throw new FileNotFoundException($"The file {relativeFilePath} was not found in the input folders."); } + public CookedFile[] GetAllFiles(string relativeFolderPath, string pattern = "*") + { + List files = []; + string parentFolder = Path.Combine(InputFolders.InputFolder, ".."); + string[] searchPaths = Directory.GetDirectories(parentFolder); + + foreach (string searchPath in searchPaths) + { + string[] searchLocations = [ + searchPath, + Path.Combine(searchPath, "cache", "itf_cooked", PlatformType) + ]; + + foreach (string location in searchLocations) + { + string folder = Path.Combine(location, relativeFolderPath); + if (Directory.Exists(folder)) + { + foreach (string file in Directory.GetFiles(folder, pattern)) + { + files.Add(new(file)); + } + } + } + } + + return [.. files]; + } + public bool GetFolderPath(string relativeFolderPath, [MaybeNullWhen(false)] out string folderPath) { folderPath = null;