public static void ReloadPlaylists(bool fullRefresh = true) { try { List <string> playlistFiles = new List <string>(); if (PluginConfig.beatDropInstalled) { String beatDropPath = Path.Combine(PluginConfig.beatDropPlaylistsLocation, "playlists"); if (Directory.Exists(beatDropPath)) { string[] beatDropJSONPlaylists = Directory.GetFiles(beatDropPath, "*.json"); string[] beatDropBPLISTPlaylists = Directory.GetFiles(beatDropPath, "*.bplist"); playlistFiles.AddRange(beatDropJSONPlaylists); playlistFiles.AddRange(beatDropBPLISTPlaylists); Logger.Log($"Found {beatDropJSONPlaylists.Length + beatDropBPLISTPlaylists.Length} playlists in BeatDrop folder"); } } string[] localJSONPlaylists = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "Playlists"), "*.json"); string[] localBPLISTPlaylists = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "Playlists"), "*.bplist"); playlistFiles.AddRange(localJSONPlaylists); playlistFiles.AddRange(localBPLISTPlaylists); Logger.Log($"Found {localJSONPlaylists.Length + localBPLISTPlaylists.Length} playlists in Playlists folder"); if (fullRefresh) { loadedPlaylists.Clear(); foreach (string path in playlistFiles) { try { Playlist playlist = Playlist.LoadPlaylist(path); if (Path.GetFileName(path) == "favorites.json" && playlist.playlistTitle == "Your favorite songs") { continue; } loadedPlaylists.Add(playlist); Logger.Log($"Found \"{playlist.playlistTitle}\" by {playlist.playlistAuthor}"); } catch (Exception e) { Logger.Log($"Unable to parse playlist @ {path}! Exception: {e}"); } } } else { foreach (string path in playlistFiles) { if (!loadedPlaylists.Any(x => x.fileLoc == path)) { Logger.Log("Found new playlist! Path: " + path); try { Playlist playlist = Playlist.LoadPlaylist(path); if (Path.GetFileName(path) == "favorites.json" && playlist.playlistTitle == "Your favorite songs") { continue; } loadedPlaylists.Add(playlist); Logger.Log($"Found \"{playlist.playlistTitle}\" by {playlist.playlistAuthor}"); if (SongCore.Loader.AreSongsLoaded) { MatchSongsForPlaylist(playlist); } } catch (Exception e) { Logger.Log($"Unable to parse playlist @ {path}! Exception: {e}"); } } } } } catch (Exception e) { Logger.Exception("Unable to load playlists! Exception: " + e); } }
public static void MatchSongsForPlaylist(Playlist playlist, bool matchAll = false) { if (!SongCore.Loader.AreSongsLoaded || SongCore.Loader.AreSongsLoading || playlist.playlistTitle == "All songs" || playlist.playlistTitle == "Your favorite songs") { return; } Dictionary <string, CustomPreviewBeatmapLevel> songMap = new Dictionary <string, CustomPreviewBeatmapLevel>(StringComparer.OrdinalIgnoreCase); foreach (var kp in SongCore.Loader.CustomLevels) { var songHash = CustomHelpers.GetSongHash(kp.Value.levelID); if (songMap.ContainsKey(songHash)) { continue; } songMap.Add(songHash, kp.Value); } if (!playlist.songs.All(x => x.level != null) || matchAll) { playlist.songs.AsParallel().ForAll(x => { if (x.level == null || matchAll) { try { // try to use levelID if (!string.IsNullOrEmpty(x.levelId)) { string songHash = CustomHelpers.GetSongHash(x.levelId); if (songMap.ContainsKey(songHash)) { x.level = songMap[songHash]; x.hash = songHash; } } // failed, try again using hash if (x.level == null && !string.IsNullOrEmpty(x.hash)) { // fix broken playlists from a bug in song browser if (x.hash.Contains("custom_level")) { x.hash = CustomHelpers.GetSongHash(x.hash); } if (songMap.ContainsKey(x.hash)) { x.level = songMap[x.hash]; } } if (x.level == null && !string.IsNullOrEmpty(x.key)) { x.level = SongCore.Loader.CustomLevels.FirstOrDefault(y => y.Value.customLevelPath.Contains(x.key)).Value; if (x.level != null && !String.IsNullOrEmpty(x.level.levelID)) { x.hash = CustomHelpers.GetSongHash(x.level.levelID); } } } catch (Exception e) { Logger.Warning($"Unable to match song with {(string.IsNullOrEmpty(x.key) ? " unknown key!" : ("key " + x.key + " !"))}"); } } }); } }