SongBrowserSettings.cs 9.2 KB

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