public void UpdatePlaylists() { _CachedPlaylists = new List <Playlist>(); Stopwatch timer = new Stopwatch(); timer.Start(); foreach (String path in _PlaylistsDirectories) { Logger.Debug("Reading playlists located at: {0}", path); if (!Directory.Exists(path)) { Logger.Info("Playlist path does not exist: {0}", path); continue; } string[] files = Directory.GetFiles(path); foreach (string file in files) { Logger.Debug("Checking file {0}", file); if (Path.GetExtension(file) == ".json" || Path.GetExtension(file) == ".bplist") { Playlist p = ParsePlaylist(file); _CachedPlaylists.Add(p); } } } timer.Stop(); Logger.Debug("Processing playlists took {0}ms", timer.ElapsedMilliseconds); }
public static bool WritePlaylist(Playlist p, String fileName) { Logger.Debug("WritePlaylist - {0}", fileName); JSONNode root = new JSONObject(); root.Add("playlistTitle", p.Title); root.Add("playlistAuthor", p.Author); root.Add("image", p.Image); JSONArray songs = new JSONArray(); root.Add("songs", songs); foreach (PlaylistSong ps in p.Songs) { var jsonSong = new JSONObject(); jsonSong.Add("key", ps.Key); jsonSong.Add("songName", ps.SongName); jsonSong.Add("levelId", ps.LevelId); songs.Add(jsonSong); } File.WriteAllText(fileName, root.ToString(4)); return(true); }
/// <summary> /// Save helper. /// </summary> private void _Save(String path) { if (this.DisableSavingSettings) { Logger.Info("Saving settings has been disabled..."); return; } // TODO - not here if (searchTerms.Count > 10) { searchTerms.RemoveRange(10, searchTerms.Count - 10); } var settings = new XmlWriterSettings { OmitXmlDeclaration = false, Indent = true, NewLineOnAttributes = true, NewLineHandling = System.Xml.NewLineHandling.Entitize }; using (var stream = new StreamWriter(path, false, Utf8Encoding)) { using (var writer = XmlWriter.Create(stream, settings)) { SettingsSerializer.Serialize(writer, this); } } }
public ScoreSaberDataFile(byte[] data) { Stopwatch timer = new Stopwatch(); timer.Start(); SongVersionToScoreSaberData = new Dictionary <string, ScoreSaberData>(); System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint; string result = System.Text.Encoding.UTF8.GetString(data); JSONNode rootNode = JSON.Parse(result); foreach (KeyValuePair <string, JSONNode> kvp in rootNode) { JSONNode difficultyNodes = kvp.Value; foreach (KeyValuePair <string, JSONNode> innerKvp in difficultyNodes) { JSONNode node = innerKvp.Value; String version = node["key"]; String name = node["name"]; String difficultyName = node["difficulty"]; if (difficultyName == "Expert+") { difficultyName = "ExpertPlus"; } float pp = 0; float.TryParse(node["pp"], style, System.Globalization.CultureInfo.InvariantCulture, out pp); float starDifficulty = 0; float.TryParse(node["star"], style, System.Globalization.CultureInfo.InvariantCulture, out starDifficulty); ScoreSaberData ppData = null; if (!SongVersionToScoreSaberData.ContainsKey(version)) { ppData = new ScoreSaberData { version = version, name = name }; SongVersionToScoreSaberData.Add(version, ppData); } else { ppData = SongVersionToScoreSaberData[version]; } // add difficulty ppData.AddDifficultyRating(difficultyName, pp, starDifficulty); } } timer.Stop(); Logger.Debug("Processing ScoreSaber data took {0}ms", timer.ElapsedMilliseconds); }
public static Playlist ParsePlaylist(String path) { try { if (!File.Exists(path)) { Logger.Debug("Playlist file no longer exists: {0}", path); return(null); } Logger.Debug("Parsing playlist at {0}", path); String json = File.ReadAllText(path); Playlist playlist = new Playlist(); JSONNode playlistNode = JSON.Parse(json); playlist.Image = playlistNode["image"]; playlist.Title = playlistNode["playlistTitle"]; playlist.Author = playlistNode["playlistAuthor"]; playlist.Songs = new List <PlaylistSong>(); playlist.CustomDetailUrl = playlistNode["customDetailUrl"]; playlist.CustomArchiveUrl = playlistNode["customArchiveUrl"]; if (!string.IsNullOrEmpty(playlist.CustomDetailUrl)) { if (!playlist.CustomDetailUrl.EndsWith("/")) { playlist.CustomDetailUrl += "/"; } Logger.Debug("Found playlist with custom URL! Name: " + playlist.Title + ", CustomDetailURL: " + playlist.CustomDetailUrl); } foreach (JSONNode node in playlistNode["songs"].AsArray) { PlaylistSong song = new PlaylistSong { Key = node["key"], SongName = node["songName"], LevelId = node["levelId"], Hash = node["hash"] }; playlist.Songs.Add(song); } playlist.Path = path; return(playlist); } catch (Exception e) { Logger.Exception("Exception parsing playlist: ", e); } return(null); }
/// <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 = PlaylistsReader.ParsePlaylist(playlistPath); } else { p = new Playlist { Title = "Song Browser Favorites", Author = "SongBrowserPlugin", Path = "", 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); }); PlaylistWriter.WritePlaylist(p, 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); } } if (String.IsNullOrEmpty(retVal.currentEditingPlaylistFile)) { retVal.currentEditingPlaylistFile = playlistPath; } return(retVal); }