/// <summary>
        /// Migrate old favorites into new system.
        /// </summary>
        public static void MigrateFavorites()
        {
            String migratedPlaylistPath = Path.Combine(Environment.CurrentDirectory, "Playlists", MigratedFavoritesPlaylistName);
            String oldFavoritesPath     = Path.Combine(Environment.CurrentDirectory, "Playlists", DefaultConvertedFavoritesPlaylistName);

            // Skip if already migrated or if the song browser favorites do not exist
            if (!File.Exists(oldFavoritesPath) || File.Exists(migratedPlaylistPath))
            {
                return;
            }

            Logger.Info("Migrating [{0}] into the In-Game favorites.", oldFavoritesPath);

            Playlist        oldFavorites = Playlist.LoadPlaylist(oldFavoritesPath);
            PlayerDataModel playerData   = Resources.FindObjectsOfTypeAll <PlayerDataModel>().FirstOrDefault();

            foreach (PlaylistSong song in oldFavorites.songs)
            {
                string levelID = CustomLevelLoader.kCustomLevelPrefixId + song.hash;
                Logger.Info("Migrating song into ingame favorites: {0}", levelID);
                playerData.playerData.favoritesLevelIds.Add(levelID);
            }

            Logger.Info("Moving [{0}->{1}] into the In-Game favorites.", oldFavoritesPath, migratedPlaylistPath);
            File.Move(oldFavoritesPath, migratedPlaylistPath);

            playerData.Save();
        }
        /// <summary>
        /// Favorites used to exist as part of the song_browser_settings.xml
        /// This makes little sense now.  This is the upgrade path.
        /// Convert all existing favorites to the best of our effort into a playlist.
        /// </summary>
        /// <param name="levelIdToCustomLevel"></param>
        /// <param name="levelIdToSongVersion"></param>
        public void ConvertFavoritesToPlaylist(Dictionary <String, SongLoaderPlugin.OverrideClasses.CustomLevel> levelIdToCustomLevel,
                                               Dictionary <string, string> levelIdToSongVersion)
        {
            // Check if we have favorites to convert to the playlist
            if (this.Favorites.Count <= 0)
            {
                return;
            }

            // check if the playlist exists
            String playlistPath   = Path.Combine(Environment.CurrentDirectory, "Playlists", DefaultConvertedFavoritesPlaylistName);
            bool   playlistExists = false;

            if (File.Exists(playlistPath))
            {
                playlistExists = true;
            }

            // abort here if playlist already exits.
            if (playlistExists)
            {
                Logger.Info("Not converting song_browser_setting.xml favorites because {0} already exists...", playlistPath);
                return;
            }

            Logger.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 = Playlist.LoadPlaylist(playlistPath);
            }
            else
            {
                p = new Playlist
                {
                    playlistTitle  = "Song Browser Favorites",
                    playlistAuthor = "SongBrowser",
                    fileLoc        = "",
                    image          = Base64Sprites.PlaylistIconB64,
                    songs          = new List <PlaylistSong>(),
                };
            }

            List <String> successfullyRemoved = new List <string>();

            this.Favorites.RemoveWhere(levelId =>
            {
                PlaylistSong playlistSong = new PlaylistSong
                {
                    levelId = levelId
                };

                if (levelIdToCustomLevel.ContainsKey(levelId) && levelIdToSongVersion.ContainsKey(levelId))
                {
                    playlistSong.songName = levelIdToCustomLevel[levelId].songName;
                    playlistSong.key      = levelIdToSongVersion[levelId];
                }
                else
                {
                    // No easy way to look up original songs... They will still work but have wrong song name in playlist.
                    playlistSong.songName = levelId;
                    playlistSong.key      = "";
                }

                p.songs.Add(playlistSong);

                return(true);
            });

            p.SavePlaylist(playlistPath);

            if (String.IsNullOrEmpty(this.currentEditingPlaylistFile))
            {
                this.currentEditingPlaylistFile = playlistPath;
            }

            this.Save();
        }
Beispiel #3
0
        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 (SongLoader.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);
            }
        }
        /// <summary>
        /// Favorites used to exist as part of the song_browser_settings.xml
        /// This makes little sense now.  This is the upgrade path.
        /// Convert all existing favorites to the best of our effort into a playlist.
        /// </summary>
        /// <param name="levelIdToCustomLevel"></param>
        /// <param name="levelIdToSongVersion"></param>
        public void ConvertFavoritesToPlaylist(Dictionary <String, CustomPreviewBeatmapLevel> customSongsMap)
        {
            // map songs in case we are converting a huge list
            Dictionary <String, CustomPreviewBeatmapLevel> levelIdToCustomLevel = new Dictionary <string, CustomPreviewBeatmapLevel>(StringComparer.OrdinalIgnoreCase);

            foreach (var kp in customSongsMap)
            {
                if (levelIdToCustomLevel.ContainsKey(kp.Value.levelID))
                {
                    continue;
                }
                levelIdToCustomLevel.Add(kp.Value.levelID, kp.Value);
            }

            // Check if we have favorites to convert to the playlist
            if (this.Favorites.Count <= 0)
            {
                return;
            }

            // check if the playlist exists
            String playlistPath   = Path.Combine(Environment.CurrentDirectory, "Playlists", DefaultConvertedFavoritesPlaylistName);
            bool   playlistExists = false;

            if (File.Exists(playlistPath))
            {
                playlistExists = true;
            }

            // abort here if playlist already exits.
            if (playlistExists)
            {
                Logger.Info("Not converting song_browser_setting.xml favorites because {0} already exists...", playlistPath);
                return;
            }

            Logger.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 = Playlist.LoadPlaylist(playlistPath);
            }
            else
            {
                p = new Playlist
                {
                    playlistTitle  = "Song Browser Favorites",
                    playlistAuthor = "SongBrowser",
                    fileLoc        = "",
                    image          = Base64Sprites.SpriteToBase64(Base64Sprites.BeastSaberLogo),
                    songs          = new List <PlaylistSong>(),
                };
            }

            List <String> successfullyRemoved = new List <string>();

            this.Favorites.RemoveWhere(levelId =>
            {
                PlaylistSong playlistSong = new PlaylistSong
                {
                    levelId = levelId
                };

                if (levelIdToCustomLevel.ContainsKey(levelId))
                {
                    playlistSong.songName = levelIdToCustomLevel[levelId].songName;
                    playlistSong.levelId  = levelId;
                    playlistSong.hash     = CustomHelpers.GetSongHash(levelId);
                }
                else
                {
                    // No easy way to look up original songs... They will still work but have wrong song name in playlist.
                    playlistSong.levelId = levelId;
                    playlistSong.hash    = CustomHelpers.GetSongHash(levelId);
                    playlistSong.key     = "";
                }

                p.songs.Add(playlistSong);

                return(true);
            });

            p.SavePlaylist(playlistPath);

            if (String.IsNullOrEmpty(this.currentEditingPlaylistFile))
            {
                this.currentEditingPlaylistFile = playlistPath;
            }

            this.Save();
        }