SongBrowserSettings.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. namespace SongBrowserPlugin.DataAccess
  7. {
  8. [Serializable]
  9. public enum SongSortMode
  10. {
  11. Default,
  12. Author,
  13. Original,
  14. Newest,
  15. PlayCount,
  16. Difficulty,
  17. Random,
  18. // Deprecated
  19. Favorites,
  20. Playlist,
  21. Search
  22. }
  23. [Serializable]
  24. public enum SongFilterMode
  25. {
  26. None,
  27. Favorites,
  28. Playlist,
  29. Search
  30. }
  31. [Serializable]
  32. public class SongBrowserSettings
  33. {
  34. public SongSortMode sortMode = default(SongSortMode);
  35. public SongFilterMode filterMode = default(SongFilterMode);
  36. // Deprecated
  37. private List<String> _oldFavorites = default(List<String>);
  38. public List<String> searchTerms = default(List<String>);
  39. public String currentLevelId = default(String);
  40. public String currentDirectory = default(String);
  41. public String currentPlaylistFile = default(String);
  42. public bool folderSupportEnabled = false;
  43. public bool randomInstantQueue = false;
  44. [NonSerialized]
  45. private static Logger Log = new Logger("SongBrowserSettings");
  46. [NonSerialized]
  47. [XmlIgnore]
  48. private HashSet<String> _favorites;
  49. [XmlArray(@"favorites")]
  50. public List<String> OldFavorites
  51. {
  52. get
  53. {
  54. return _oldFavorites;
  55. }
  56. set
  57. {
  58. _oldFavorites = value;
  59. }
  60. }
  61. [XmlIgnore]
  62. public HashSet<String> Favorites
  63. {
  64. get
  65. {
  66. return _favorites;
  67. }
  68. }
  69. /// <summary>
  70. /// Constructor.
  71. /// </summary>
  72. public SongBrowserSettings()
  73. {
  74. _oldFavorites = new List<String>();
  75. searchTerms = new List<string>();
  76. _favorites = new HashSet<String>();
  77. }
  78. /// <summary>
  79. /// Helper to acquire settings path at runtime.
  80. /// </summary>
  81. /// <returns></returns>
  82. public static String SettingsPath()
  83. {
  84. return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml");
  85. }
  86. /// <summary>
  87. /// Path to the common favorites file location.
  88. /// </summary>
  89. /// <returns></returns>
  90. public static String FavoritesFilePath()
  91. {
  92. return Path.Combine(Environment.CurrentDirectory, "favoriteSongs.cfg");
  93. }
  94. /// <summary>
  95. /// Load the settings file for this plugin.
  96. /// If we fail to load return Default settings.
  97. /// </summary>
  98. /// <returns>SongBrowserSettings</returns>
  99. public static SongBrowserSettings Load()
  100. {
  101. Log.Trace("Load()");
  102. SongBrowserSettings retVal = null;
  103. // No Settings file.
  104. String settingsFilePath = SongBrowserSettings.SettingsPath();
  105. if (File.Exists(settingsFilePath))
  106. {
  107. // Deserialization from JSON
  108. FileStream fs = null;
  109. try
  110. {
  111. fs = File.OpenRead(settingsFilePath);
  112. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  113. retVal = (SongBrowserSettings)serializer.Deserialize(fs);
  114. }
  115. catch (Exception e)
  116. {
  117. Log.Exception("Unable to deserialize song browser settings file: ", e);
  118. throw e;
  119. }
  120. finally
  121. {
  122. if (fs != null) { fs.Close(); }
  123. }
  124. }
  125. else
  126. {
  127. Log.Debug("Settings file does not exist, returning defaults: " + settingsFilePath);
  128. retVal = new SongBrowserSettings();
  129. }
  130. // Load favorites
  131. if (File.Exists(SongBrowserSettings.FavoritesFilePath()))
  132. {
  133. retVal.Favorites.UnionWith(File.ReadAllLines(SongBrowserSettings.FavoritesFilePath()));
  134. }
  135. // if we have old favorites then this file has not been merged with favoriteSongs.cfg yet.
  136. Log.Debug("Old favorites, count={0}", retVal._oldFavorites.Count);
  137. if (retVal._oldFavorites.Count > 0)
  138. {
  139. retVal.Favorites.UnionWith(retVal._oldFavorites);
  140. retVal._oldFavorites.Clear();
  141. retVal.SaveFavorites();
  142. }
  143. return retVal;
  144. }
  145. /// <summary>
  146. /// Save this Settings insance to file.
  147. /// </summary>
  148. public void Save()
  149. {
  150. // TODO - not here
  151. if (searchTerms.Count > 10)
  152. {
  153. searchTerms.RemoveRange(10, searchTerms.Count - 10);
  154. }
  155. FileStream fs = new FileStream(SongBrowserSettings.SettingsPath(), FileMode.Create, FileAccess.Write);
  156. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  157. serializer.Serialize(fs, this);
  158. fs.Close();
  159. }
  160. public void SaveFavorites()
  161. {
  162. // dump favorites
  163. File.WriteAllLines(SongBrowserSettings.FavoritesFilePath(), this.Favorites);
  164. }
  165. }
  166. }