SongBrowserSettings.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using UnityEngine;
  9. using Logger = SongBrowser.Logging.Logger;
  10. namespace SongBrowser.DataAccess
  11. {
  12. [Serializable]
  13. public enum SongSortMode
  14. {
  15. Default,
  16. Author,
  17. Original,
  18. Newest,
  19. YourPlayCount,
  20. Difficulty,
  21. Random,
  22. PP,
  23. UpVotes,
  24. Rating,
  25. Heat,
  26. PlayCount,
  27. Stars,
  28. // Allow mods to extend functionality.
  29. Custom,
  30. // Deprecated
  31. Favorites,
  32. Playlist,
  33. Search
  34. }
  35. static class SongSortModeMethods
  36. {
  37. public static bool NeedsScoreSaberData(this SongSortMode s)
  38. {
  39. switch (s)
  40. {
  41. case SongSortMode.UpVotes:
  42. case SongSortMode.Rating:
  43. case SongSortMode.PlayCount:
  44. case SongSortMode.Heat:
  45. case SongSortMode.PP:
  46. case SongSortMode.Stars:
  47. return true;
  48. default:
  49. return false;
  50. }
  51. }
  52. }
  53. [Serializable]
  54. public enum SongFilterMode
  55. {
  56. None,
  57. Favorites,
  58. Playlist,
  59. Search,
  60. Ranked,
  61. Unranked,
  62. // For other mods that extend SongBrowser
  63. Custom
  64. }
  65. [Serializable]
  66. public class SongBrowserSettings
  67. {
  68. public static readonly Encoding Utf8Encoding = Encoding.UTF8;
  69. public static readonly XmlSerializer SettingsSerializer = new XmlSerializer(typeof(SongBrowserSettings));
  70. public static readonly String DefaultConvertedFavoritesPlaylistName = "SongBrowserPluginFavorites.json";
  71. public static readonly String MigratedFavoritesPlaylistName = "SongBrowserPluginFavorites_Migrated.json";
  72. public static readonly String CUSTOM_SONGS_LEVEL_COLLECTION_NAME = "Custom Levels";
  73. public SongSortMode sortMode = default(SongSortMode);
  74. public SongFilterMode filterMode = default(SongFilterMode);
  75. public List<String> searchTerms = default(List<String>);
  76. public String currentLevelId = default(String);
  77. public String currentDirectory = default(String);
  78. public String currentLevelCollectionName = default(String);
  79. public String currentLevelCategoryName = default(String);
  80. public bool randomInstantQueue = false;
  81. public bool deleteNumberedSongFolder = true;
  82. public int randomSongSeed;
  83. public bool invertSortResults = false;
  84. [XmlIgnore]
  85. [NonSerialized]
  86. public bool DisableSavingSettings = false;
  87. /// <summary>
  88. /// Constructor.
  89. /// </summary>
  90. public SongBrowserSettings()
  91. {
  92. searchTerms = new List<string>();
  93. }
  94. /// <summary>
  95. /// Helper to acquire settings path at runtime.
  96. /// </summary>
  97. /// <returns></returns>
  98. public static String SettingsPath()
  99. {
  100. return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml");
  101. }
  102. /// <summary>
  103. /// Backup settings file location.
  104. /// </summary>
  105. /// <returns></returns>
  106. public static String SettingsBackupPath()
  107. {
  108. return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml.bak");
  109. }
  110. /// <summary>
  111. /// Path to the common favorites file location.
  112. /// </summary>
  113. /// <returns></returns>
  114. public static String DownloaderFavoritesFilePath()
  115. {
  116. return Path.Combine(Environment.CurrentDirectory, "favoriteSongs.cfg");
  117. }
  118. /// <summary>
  119. /// Load the settings file for this plugin.
  120. /// If we fail to load return Default settings.
  121. /// </summary>
  122. /// <returns>SongBrowserSettings</returns>
  123. public static SongBrowserSettings Load()
  124. {
  125. Logger.Trace("Load()");
  126. SongBrowserSettings retVal = null;
  127. // No Settings file.
  128. String settingsFilePath = SongBrowserSettings.SettingsPath();
  129. if (File.Exists(settingsFilePath))
  130. {
  131. // Deserialization from JSON
  132. FileStream fs = null;
  133. try
  134. {
  135. fs = File.OpenRead(settingsFilePath);
  136. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  137. retVal = (SongBrowserSettings)serializer.Deserialize(fs);
  138. // Success loading, sane time to make a backup
  139. retVal.SaveBackup();
  140. }
  141. catch (Exception e)
  142. {
  143. Logger.Exception("Unable to deserialize song browser settings file, using default settings: ", e);
  144. retVal = new SongBrowserSettings
  145. {
  146. DisableSavingSettings = true
  147. };
  148. }
  149. finally
  150. {
  151. if (fs != null) { fs.Close(); }
  152. }
  153. }
  154. else
  155. {
  156. Logger.Debug("Settings file does not exist, returning defaults: " + settingsFilePath);
  157. retVal = new SongBrowserSettings();
  158. }
  159. // check if the playlist directory exists, make it otherwise.
  160. String playlistDirPath = Path.Combine(Environment.CurrentDirectory, "Playlists");
  161. if (!Directory.Exists(playlistDirPath))
  162. {
  163. Directory.CreateDirectory(playlistDirPath);
  164. }
  165. MigrateFavorites();
  166. ApplyFixes(retVal);
  167. return retVal;
  168. }
  169. /// <summary>
  170. /// Fix potential breakages in settings.
  171. /// </summary>
  172. /// <param name="settings"></param>
  173. private static void ApplyFixes(SongBrowserSettings settings)
  174. {
  175. if (String.Equals(settings.currentLevelCollectionName, "CustomMaps"))
  176. {
  177. settings.currentLevelCollectionName = "ModdedCustomMaps";
  178. }
  179. else if (String.Equals(settings.currentLevelCollectionName, "ModdedCustomMaps"))
  180. {
  181. settings.currentLevelCollectionName = SongBrowserSettings.CUSTOM_SONGS_LEVEL_COLLECTION_NAME;
  182. }
  183. settings.Save();
  184. }
  185. /// <summary>
  186. /// Migrate old favorites into new system.
  187. /// </summary>
  188. public static void MigrateFavorites()
  189. {
  190. String migratedPlaylistPath = Path.Combine(Environment.CurrentDirectory, "Playlists", MigratedFavoritesPlaylistName);
  191. String oldFavoritesPath = Path.Combine(Environment.CurrentDirectory, "Playlists", DefaultConvertedFavoritesPlaylistName);
  192. // Skip if already migrated or if the song browser favorites do not exist
  193. if (!File.Exists(oldFavoritesPath) || File.Exists(migratedPlaylistPath))
  194. {
  195. return;
  196. }
  197. Logger.Info("Migrating [{0}] into the In-Game favorites.", oldFavoritesPath);
  198. Playlist oldFavorites = Playlist.LoadPlaylist(oldFavoritesPath);
  199. PlayerDataModel playerData = Resources.FindObjectsOfTypeAll<PlayerDataModel>().FirstOrDefault();
  200. foreach (PlaylistSong song in oldFavorites.songs)
  201. {
  202. string levelID = CustomLevelLoader.kCustomLevelPrefixId + song.hash;
  203. Logger.Info("Migrating song into ingame favorites: {0}", levelID);
  204. playerData.playerData.favoritesLevelIds.Add(levelID);
  205. }
  206. Logger.Info("Moving [{0}->{1}] into the In-Game favorites.", oldFavoritesPath, migratedPlaylistPath);
  207. File.Move(oldFavoritesPath, migratedPlaylistPath);
  208. playerData.Save();
  209. }
  210. /// <summary>
  211. /// Save this Settings insance to file.
  212. /// </summary>
  213. public void Save()
  214. {
  215. this._Save(SongBrowserSettings.SettingsPath());
  216. }
  217. /// <summary>
  218. /// Save a backup.
  219. /// </summary>
  220. public void SaveBackup()
  221. {
  222. this._Save(SongBrowserSettings.SettingsBackupPath());
  223. }
  224. /// <summary>
  225. /// Save helper.
  226. /// </summary>
  227. private void _Save(String path)
  228. {
  229. if (this.DisableSavingSettings)
  230. {
  231. Logger.Info("Saving settings has been disabled...");
  232. return;
  233. }
  234. if (searchTerms.Count > 10)
  235. {
  236. searchTerms.RemoveRange(10, searchTerms.Count - 10);
  237. }
  238. var settings = new XmlWriterSettings
  239. {
  240. OmitXmlDeclaration = false,
  241. Indent = true,
  242. NewLineOnAttributes = true,
  243. NewLineHandling = System.Xml.NewLineHandling.Entitize
  244. };
  245. using (var stream = new StreamWriter(path, false, Utf8Encoding))
  246. {
  247. using (var writer = XmlWriter.Create(stream, settings))
  248. {
  249. SettingsSerializer.Serialize(writer, this);
  250. }
  251. }
  252. }
  253. }
  254. }