Browse Source

Fixes regarding playlists.
Clear playlist filter if current playlist no longer exists.
Properly filter playlists containing songs from the original game.
Only upgrade song_browser_setting favorites if the
SongBrowserPluginFavorites.json playlist does not exist.

Stephen Damm 6 years ago
parent
commit
5ffe84bee7

+ 2 - 3
SongBrowserPlugin/DataAccess/PlaylistWriter.cs

@@ -32,9 +32,8 @@ namespace SongBrowserPlugin.DataAccess
 
                 songs.Add(jsonSong);
             }
-            _log.Debug("FUCK1");
-            File.WriteAllText(fileName, root.ToString());
-            _log.Debug("FUCK2");
+
+            File.WriteAllText(fileName, root.ToString(4));        
             return true;
         }
     }

+ 22 - 7
SongBrowserPlugin/DataAccess/SongBrowserModel.cs

@@ -36,6 +36,7 @@ namespace SongBrowserPlugin
         private Dictionary<string, ScoreSaberData> _levelIdToScoreSaberData = null;
         private Dictionary<string, int> _levelIdToPlayCount;
         private Dictionary<string, string> _levelIdToSongVersion;
+        private Dictionary<string, StandardLevelSO> _keyToSong;
         private Dictionary<String, DirectoryNode> _directoryTree;
         private Stack<DirectoryNode> _directoryStack = new Stack<DirectoryNode>();
 
@@ -171,6 +172,7 @@ namespace SongBrowserPlugin
             _levelIdToScoreSaberData = new Dictionary<string, ScoreSaberData>();
             _levelIdToPlayCount = new Dictionary<string, int>();
             _levelIdToSongVersion = new Dictionary<string, string>();
+            _keyToSong = new Dictionary<string, StandardLevelSO>();
 
             // Weights used for keeping the original songs in order
             // Invert the weights from the game so we can order by descending and make LINQ work with us...
@@ -290,6 +292,7 @@ namespace SongBrowserPlugin
                     {
                         //_log.Debug("MATCH");
                         _levelIdToSongVersion.Add(level.levelID, version);
+                        _keyToSong.Add(version, level);
                     }
                 }
             }
@@ -779,21 +782,33 @@ namespace SongBrowserPlugin
             {
                 _log.Error("Trying to load a null playlist...");
                 _filteredSongs = _originalSongs;
+                this.Settings.filterMode = SongFilterMode.None;
                 return;
             }
 
             _log.Debug("Filtering songs for playlist: {0}", this.CurrentPlaylist.Title);            
-            List<String> playlistKeysOrdered = this.CurrentPlaylist.Songs.Select(x => x.Key).Distinct().ToList();
-            Dictionary<String, int>playlistKeyToIndex = playlistKeysOrdered.Select((val, index) => new { Index = index, Value = val }).ToDictionary(i => i.Value, i => i.Index);
             LevelCollectionsForGameplayModes levelCollections = Resources.FindObjectsOfTypeAll<LevelCollectionsForGameplayModes>().FirstOrDefault();
             var levels = levelCollections.GetLevels(_currentGamePlayMode);
 
-            var songList = levels.Where(x => !x.levelID.StartsWith("Level_") && _levelIdToSongVersion.ContainsKey(x.levelID) && playlistKeyToIndex.ContainsKey(_levelIdToSongVersion[x.levelID])).ToList();
-            
+            Dictionary<String, StandardLevelSO> levelDict = levels.Select((val, index) => new { LevelId = val.levelID, Level = val }).ToDictionary(i => i.LevelId, i => i.Level);
+            List<StandardLevelSO> songList = new List<StandardLevelSO>();
+            foreach (PlaylistSong ps in this.CurrentPlaylist.Songs)
+            {
+                if (!String.IsNullOrEmpty(ps.LevelId))
+                {
+                    if (levelDict.ContainsKey(ps.LevelId))
+                    {
+                        songList.Add(levelDict[ps.LevelId]);
+                    }
+                }
+                else if (!ps.Key.StartsWith("Level_") && _keyToSong.ContainsKey(ps.Key))
+                {
+                    songList.Add(_keyToSong[ps.Key]);
+                }
+            }
+
             _originalSongs = songList;
-            _filteredSongs = _originalSongs
-                .OrderBy(x => playlistKeyToIndex[_levelIdToSongVersion[x.levelID]])
-                .ToList();
+            _filteredSongs = _originalSongs;
             
             _log.Debug("Playlist filtered song count: {0}", _filteredSongs.Count);
         }

+ 17 - 3
SongBrowserPlugin/DataAccess/SongBrowserSettings.cs

@@ -186,14 +186,28 @@ namespace SongBrowserPlugin.DataAccess
                 return;
             }
 
-            Log.Info("Converting {0} Favorites in song_browser_settings.xml to a Playlist...", this.Favorites.Count);
-
             // check if the playlist exists
             String playlistPath = Path.Combine(Environment.CurrentDirectory, "Playlists", DefaultConvertedFavoritesPlaylistName);
 
-            Playlist p = null;
+            bool playlistExists = false;
             if (File.Exists(playlistPath))
             {
+                playlistExists = true;
+            }
+
+            // abort here if playlist already exits.
+            if (playlistExists)
+            {
+                Log.Info("Not converting song_browser_setting.xml favorites because {0} already exists...", playlistPath);
+                return;
+            }
+
+            Log.Info("Converting {0} Favorites in song_browser_settings.xml to {1}...", this.Favorites.Count, playlistPath);
+
+            // written like this in case we ever want to support adding to this playlist
+            Playlist p = null;
+            if (playlistExists)
+            {
                 p = PlaylistsReader.ParsePlaylist(playlistPath);
             }
             else