Skip to content

Commit

Permalink
Refactor file handling and add FFmpeg existence check
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MrKev312 committed Sep 5, 2024
1 parent 02abf80 commit b61bed3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion JustDanceEditor.Converter/Converters/ConvertUbiArtToUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions JustDanceEditor.Converter/Converters/Images/MenuArtConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -29,15 +29,15 @@ 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);
}
catch (Exception ex)
{
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");
Expand Down
4 changes: 2 additions & 2 deletions JustDanceEditor.Converter/Converters/Video/VideoConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
29 changes: 29 additions & 0 deletions JustDanceEditor.Converter/Files/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CookedFile> 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;
Expand Down

0 comments on commit b61bed3

Please sign in to comment.