123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
- using UnityEngine;
- using Logger = SongBrowser.Logging.Logger;
- namespace SongBrowser.DataAccess
- {
- [Serializable]
- public enum SongSortMode
- {
- Default,
- Author,
- Original,
- Newest,
- YourPlayCount,
- Difficulty,
- Random,
- PP,
- UpVotes,
- Rating,
- Heat,
- PlayCount,
- Stars,
- // Deprecated
- Favorites,
- Playlist,
- Search
- }
- static class SongSortModeMethods
- {
- public static bool NeedsBeatSaverData(this SongSortMode s)
- {
- switch (s)
- {
- case SongSortMode.UpVotes:
- case SongSortMode.Rating:
- case SongSortMode.PlayCount:
- case SongSortMode.Heat:
- return true;
- default:
- return false;
- }
- }
- public static bool NeedsScoreSaberData(this SongSortMode s)
- {
- switch (s)
- {
- case SongSortMode.PP:
- case SongSortMode.Stars:
- return true;
- default:
- return false;
- }
- }
- }
- [Serializable]
- public enum SongFilterMode
- {
- None,
- Favorites,
- Playlist,
- Search,
- Ranked,
- Unranked,
- // For other mods that extend SongBrowser
- Custom
- }
- [Serializable]
- public class SongBrowserSettings
- {
- public static readonly Encoding Utf8Encoding = Encoding.UTF8;
- public static readonly XmlSerializer SettingsSerializer = new XmlSerializer(typeof(SongBrowserSettings));
- public static readonly String DefaultConvertedFavoritesPlaylistName = "SongBrowserPluginFavorites.json";
- public static readonly String MigratedFavoritesPlaylistName = "SongBrowserPluginFavorites_Migrated.json";
- public static readonly String CUSTOM_SONG_LEVEL_PACK_ID = "custom_levelpack_CustomLevels";
- public SongSortMode sortMode = default(SongSortMode);
- public SongFilterMode filterMode = default(SongFilterMode);
- public List<String> searchTerms = default(List<String>);
- public String currentLevelId = default(String);
- public String currentDirectory = default(String);
- public String currentLevelPackId = default(String);
- public bool randomInstantQueue = false;
- public bool deleteNumberedSongFolder = true;
- public int randomSongSeed;
- public bool invertSortResults = false;
- [XmlIgnore]
- [NonSerialized]
- public bool DisableSavingSettings = false;
- /// <summary>
- /// Constructor.
- /// </summary>
- public SongBrowserSettings()
- {
- searchTerms = new List<string>();
- }
- /// <summary>
- /// Helper to acquire settings path at runtime.
- /// </summary>
- /// <returns></returns>
- public static String SettingsPath()
- {
- return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml");
- }
- /// <summary>
- /// Backup settings file location.
- /// </summary>
- /// <returns></returns>
- public static String SettingsBackupPath()
- {
- return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml.bak");
- }
- /// <summary>
- /// Path to the common favorites file location.
- /// </summary>
- /// <returns></returns>
- public static String DownloaderFavoritesFilePath()
- {
- return Path.Combine(Environment.CurrentDirectory, "favoriteSongs.cfg");
- }
- /// <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);
- }
- MigrateFavorites();
- ApplyFixes(retVal);
- return retVal;
- }
- /// <summary>
- /// Fix potential breakages in settings.
- /// </summary>
- /// <param name="settings"></param>
- private static void ApplyFixes(SongBrowserSettings settings)
- {
- if (String.Equals(settings.currentLevelPackId, "CustomMaps"))
- {
- settings.currentLevelPackId = "ModdedCustomMaps";
- }
- else if (String.Equals(settings.currentLevelPackId, "ModdedCustomMaps"))
- {
- settings.currentLevelPackId = SongBrowserSettings.CUSTOM_SONG_LEVEL_PACK_ID;
- }
- settings.Save();
- }
- /// <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);
- PlayerDataModelSO playerData = Resources.FindObjectsOfTypeAll<PlayerDataModelSO>().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>
- /// Save this Settings insance to file.
- /// </summary>
- public void Save()
- {
- this._Save(SongBrowserSettings.SettingsPath());
- }
- /// <summary>
- /// Save a backup.
- /// </summary>
- public void SaveBackup()
- {
- this._Save(SongBrowserSettings.SettingsBackupPath());
- }
- /// <summary>
- /// Save helper.
- /// </summary>
- private void _Save(String path)
- {
- if (this.DisableSavingSettings)
- {
- Logger.Info("Saving settings has been disabled...");
- return;
- }
- 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);
- }
- }
- }
- }
- }
|