Skip to content

Commit

Permalink
Don't add track to Playlist File if it already exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
Soapwood committed Aug 7, 2024
1 parent 3c42894 commit 3f184c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
22 changes: 21 additions & 1 deletion VXMusic/FileWriter/PlaylistFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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 = "";
Expand Down
2 changes: 1 addition & 1 deletion VXMusicDesktop/VXMusicActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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})");

Expand Down
2 changes: 1 addition & 1 deletion VXMusicDesktop/VXMusicDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageIcon>VXLogo.png</PackageIcon>
<ApplicationIcon>Images\VXLogoIcon.ico</ApplicationIcon>
<RunPostBuildEvent>Always</RunPostBuildEvent>
<Version>0.6.5.7</Version>
<Version>0.6.5.8</Version>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<Title>VXMusicDesktop</Title>
<Authors>VirtualXtensions</Authors>
Expand Down

0 comments on commit 3f184c7

Please sign in to comment.