Ejemplo n.º 1
0
        public IEnumerator SavePlaylistCoroutine(string path = "")
        {
            Logger.Log($"Saving playlist \"{playlistTitle}\"...");
            try
            {
                image     = Base64Sprites.SpriteToBase64(icon);
                songCount = songs.Count;
            }catch (Exception e)
            {
                Logger.Exception("Unable to save playlist! Exception: " + e);
                yield break;
            }
            foreach (PlaylistSong song in songs)
            {
                yield return(song.MatchKey());
            }

            try
            {
                if (!string.IsNullOrEmpty(path))
                {
                    fileLoc = Path.GetFullPath(path);
                }

                File.WriteAllText(fileLoc, JsonConvert.SerializeObject(this));

                Logger.Log("Playlist saved!");
            }
            catch (Exception e)
            {
                Logger.Exception("Unable to save playlist! Exception: " + e);
                yield break;
            }
        }
        /// <summary>
        /// Get the song cache from the game.
        /// </summary>
        public void UpdateLevelRecords()
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();

            // Calculate some information about the custom song dir
            String customSongsPath                   = Path.Combine(Environment.CurrentDirectory, CUSTOM_SONGS_DIR);
            String revSlashCustomSongPath            = customSongsPath.Replace('\\', '/');
            double currentCustomSongDirLastWriteTIme = (File.GetLastWriteTimeUtc(customSongsPath) - EPOCH).TotalMilliseconds;
            bool   customSongDirChanged              = false;

            if (_customSongDirLastWriteTime != currentCustomSongDirLastWriteTIme)
            {
                customSongDirChanged        = true;
                _customSongDirLastWriteTime = currentCustomSongDirLastWriteTIme;
            }

            if (!Directory.Exists(customSongsPath))
            {
                Logger.Error("CustomSong directory is missing...");
                return;
            }

            // Map some data for custom songs
            Regex     r = new Regex(@"(\d+-\d+)", RegexOptions.IgnoreCase);
            Stopwatch lastWriteTimer = new Stopwatch();

            lastWriteTimer.Start();
            foreach (KeyValuePair <string, CustomPreviewBeatmapLevel> level in SongCore.Loader.CustomLevels)
            {
                // If we already know this levelID, don't both updating it.
                // SongLoader should filter duplicates but in case of failure we don't want to crash
                if (!_cachedLastWriteTimes.ContainsKey(level.Value.levelID) || customSongDirChanged)
                {
                    double lastWriteTime = GetSongUserDate(level.Value);
                    _cachedLastWriteTimes[level.Value.levelID] = lastWriteTime;
                }
            }

            lastWriteTimer.Stop();
            Logger.Info("Determining song download time and determining mappings took {0}ms", lastWriteTimer.ElapsedMilliseconds);

            // Update song Infos, directory tree, and sort
            this.UpdatePlayCounts();

            // Check if we need to upgrade settings file favorites
            try
            {
                this.Settings.ConvertFavoritesToPlaylist(SongCore.Loader.CustomLevels);
            }
            catch (Exception e)
            {
                Logger.Exception("FAILED TO CONVERT FAVORITES TO PLAYLIST!", e);
            }

            // load the current editing playlist or make one
            if (CurrentEditingPlaylist == null && !String.IsNullOrEmpty(this.Settings.currentEditingPlaylistFile))
            {
                Logger.Debug("Loading playlist for editing: {0}", this.Settings.currentEditingPlaylistFile);
                CurrentEditingPlaylist = Playlist.LoadPlaylist(this.Settings.currentEditingPlaylistFile);
                PlaylistsCollection.MatchSongsForPlaylist(CurrentEditingPlaylist, true);
            }

            if (CurrentEditingPlaylist == null)
            {
                Logger.Debug("Current editing playlist does not exit, create...");
                CurrentEditingPlaylist = new Playlist
                {
                    playlistTitle  = "Song Browser Favorites",
                    playlistAuthor = "SongBrowser",
                    fileLoc        = this.Settings.currentEditingPlaylistFile,
                    image          = Base64Sprites.SpriteToBase64(Base64Sprites.BeastSaberLogo),
                    songs          = new List <PlaylistSong>(),
                };
            }

            CurrentEditingPlaylistLevelIds = new HashSet <string>();
            foreach (PlaylistSong ps in CurrentEditingPlaylist.songs)
            {
                // Sometimes we cannot match a song
                string levelId = null;
                if (ps.level != null)
                {
                    levelId = ps.level.levelID;
                }
                else if (!String.IsNullOrEmpty(ps.levelId))
                {
                    levelId = ps.levelId;
                }
                else
                {
                    //Logger.Debug("MISSING SONG {0}", ps.songName);
                    continue;
                }

                CurrentEditingPlaylistLevelIds.Add(levelId);
            }

            // Signal complete
            if (SongCore.Loader.CustomLevels.Count > 0)
            {
                didFinishProcessingSongs?.Invoke(SongCore.Loader.CustomLevels);
            }

            timer.Stop();

            Logger.Info("Updating songs infos took {0}ms", timer.ElapsedMilliseconds);
        }
        /// <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();
        }
        /// <summary>
        /// Load the settings file for this plugin.
        /// If we fail to load return Default settings.
        /// </summary>
        /// <returns>SongBrowserSettings</returns>
        public static SongBrowserSettings Load()
        {
            Logger.Trace("Load()");
            SongBrowserSettings retVal = null;

            // No Settings file.
            String settingsFilePath = SongBrowserSettings.SettingsPath();

            if (File.Exists(settingsFilePath))
            {
                // Deserialization from JSON
                FileStream fs = null;
                try
                {
                    fs = File.OpenRead(settingsFilePath);
                    XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
                    retVal = (SongBrowserSettings)serializer.Deserialize(fs);

                    // Success loading, sane time to make a backup
                    retVal.SaveBackup();
                }
                catch (Exception e)
                {
                    Logger.Exception("Unable to deserialize song browser settings file, using default settings: ", e);
                    retVal = new SongBrowserSettings
                    {
                        DisableSavingSettings = true
                    };
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }
            else
            {
                Logger.Debug("Settings file does not exist, returning defaults: " + settingsFilePath);
                retVal = new SongBrowserSettings();
            }

            // check if the playlist directory exists, make it otherwise.
            String playlistDirPath = Path.Combine(Environment.CurrentDirectory, "Playlists");

            if (!Directory.Exists(playlistDirPath))
            {
                Directory.CreateDirectory(playlistDirPath);
            }

            // Load Downloader favorites but only once, we'll convert them once, empty the song_browser_setting.xml favorites and never load it again.
            String playlistPath = Path.Combine(Environment.CurrentDirectory, "Playlists", DefaultConvertedFavoritesPlaylistName);

            if (!File.Exists(playlistPath))
            {
                if (File.Exists(SongBrowserSettings.DownloaderFavoritesFilePath()))
                {
                    String[] downloaderFavorites = File.ReadAllLines(SongBrowserSettings.DownloaderFavoritesFilePath());
                    retVal.Favorites.UnionWith(downloaderFavorites);
                }

                Playlist p = new Playlist
                {
                    playlistTitle  = "Song Browser Favorites",
                    playlistAuthor = "SongBrowser",
                    fileLoc        = "",
                    image          = Base64Sprites.SpriteToBase64(Base64Sprites.BeastSaberLogo),
                    songs          = new List <PlaylistSong>(),
                };
                p.CreateNew(playlistPath);
            }

            // If we do not have an editing playlist or the current one is missing, reset to default.
            if (String.IsNullOrEmpty(retVal.currentEditingPlaylistFile) || !File.Exists(retVal.currentEditingPlaylistFile))
            {
                retVal.currentEditingPlaylistFile = playlistPath;
            }

            ApplyFixes(retVal);

            return(retVal);
        }