From 3f184c7df1ec2a74fc08afc35b7f29fc551b3c9a Mon Sep 17 00:00:00 2001 From: Soapwood Date: Wed, 7 Aug 2024 22:38:14 +0100 Subject: [PATCH] Don't add track to Playlist File if it already exists. --- VXMusic/FileWriter/PlaylistFileWriter.cs | 22 +++++++++++++++++++++- VXMusicDesktop/VXMusicActions.cs | 2 +- VXMusicDesktop/VXMusicDesktop.csproj | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/VXMusic/FileWriter/PlaylistFileWriter.cs b/VXMusic/FileWriter/PlaylistFileWriter.cs index 9f6ed3d..66e36b8 100644 --- a/VXMusic/FileWriter/PlaylistFileWriter.cs +++ b/VXMusic/FileWriter/PlaylistFileWriter.cs @@ -33,7 +33,7 @@ private string GetPlaylistFileHeader() Environment.NewLine + Environment.NewLine; } - public void AddLineToFileIfDateMatches(string currentWorldName, string textToAdd) + public void AddTrackEntryToPlaylistFile(string currentWorldName, string textToAdd) { string fileName = GetCurrentFileName(currentWorldName); @@ -45,6 +45,12 @@ public void AddLineToFileIfDateMatches(string currentWorldName, string textToAdd { _logger.LogTrace($"Playlist file already exists. Will add to discovered file: {filePath}"); + if (IsTrackAlreadyInPlaylist(filePath, textToAdd)) + { + _logger.LogTrace($"Track was already found in Playlist file. Will skip add."); + return; + } + // Append the text to the existing file File.AppendAllText(filePath, textToAdd + Environment.NewLine); } @@ -58,6 +64,20 @@ public void AddLineToFileIfDateMatches(string currentWorldName, string textToAdd } } + private bool IsTrackAlreadyInPlaylist(string filePath, string trackLine) + { + try + { + string[] lines = File.ReadAllLines(filePath); + return Array.Exists(lines, line => line == trackLine); + } + catch (Exception ex) + { + _logger.LogWarning($"Could not read Playlist track file: {ex.Message}"); + return false; + } + } + private string GetCurrentFileName(string currentWorldName) { string fileName = ""; diff --git a/VXMusicDesktop/VXMusicActions.cs b/VXMusicDesktop/VXMusicActions.cs index 70dd231..0ca5fa6 100644 --- a/VXMusicDesktop/VXMusicActions.cs +++ b/VXMusicDesktop/VXMusicActions.cs @@ -110,7 +110,7 @@ await Task.Run(() => if (result.Result != null) { - VXMusicSession.PlaylistFileWriter.AddLineToFileIfDateMatches(App.VXMusicSession.VRChatLogParser + VXMusicSession.PlaylistFileWriter.AddTrackEntryToPlaylistFile(App.VXMusicSession.VRChatLogParser .CurrentVrChatWorld, $"{result.Result.Artist} - {result.Result.Title} {result.Result.Album} ({result.Result.ReleaseDate})"); diff --git a/VXMusicDesktop/VXMusicDesktop.csproj b/VXMusicDesktop/VXMusicDesktop.csproj index 636e4c5..22b89a7 100644 --- a/VXMusicDesktop/VXMusicDesktop.csproj +++ b/VXMusicDesktop/VXMusicDesktop.csproj @@ -9,7 +9,7 @@ VXLogo.png Images\VXLogoIcon.ico Always - 0.6.5.7 + 0.6.5.8 en VXMusicDesktop VirtualXtensions