SongBrowserSettings.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml.Serialization;
  5. namespace SongBrowserPlugin.DataAccess
  6. {
  7. [Serializable]
  8. public enum SongSortMode
  9. {
  10. Default,
  11. Author,
  12. Favorites,
  13. Original,
  14. Newest,
  15. PlayCount,
  16. Difficulty,
  17. Random,
  18. Search
  19. }
  20. [Serializable]
  21. public class SongBrowserSettings
  22. {
  23. public SongSortMode sortMode = default(SongSortMode);
  24. public List<String> favorites = default(List<String>);
  25. public List<String> searchTerms = default(List<String>);
  26. public String currentLevelId = default(String);
  27. public String currentDirectory = default(String);
  28. [NonSerialized]
  29. private static Logger Log = new Logger("SongBrowserSettings");
  30. /// <summary>
  31. /// Constructor.
  32. /// </summary>
  33. public SongBrowserSettings()
  34. {
  35. favorites = new List<String>();
  36. searchTerms = new List<string>();
  37. }
  38. /// <summary>
  39. /// Helper to acquire settings path at runtime.
  40. /// </summary>
  41. /// <returns></returns>
  42. public static String SettingsPath()
  43. {
  44. return Path.Combine(Environment.CurrentDirectory, "song_browser_settings.xml");
  45. }
  46. /// <summary>
  47. /// Load the settings file for this plugin.
  48. /// If we fail to load return Default settings.
  49. /// </summary>
  50. /// <returns>SongBrowserSettings</returns>
  51. public static SongBrowserSettings Load()
  52. {
  53. Log.Trace("Load()");
  54. SongBrowserSettings retVal = null;
  55. String settingsFilePath = SongBrowserSettings.SettingsPath();
  56. if (!File.Exists(settingsFilePath))
  57. {
  58. Log.Debug("Settings file does not exist, returning defaults: " + settingsFilePath);
  59. return new SongBrowserSettings();
  60. }
  61. // Deserialization from JSON
  62. FileStream fs = null;
  63. try
  64. {
  65. fs = File.OpenRead(settingsFilePath);
  66. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  67. retVal = (SongBrowserSettings)serializer.Deserialize(fs);
  68. }
  69. catch (Exception e)
  70. {
  71. Log.Exception("Unable to deserialize song browser settings file: ", e);
  72. // Return default settings
  73. retVal = new SongBrowserSettings();
  74. }
  75. finally
  76. {
  77. if (fs != null) { fs.Close(); }
  78. }
  79. return retVal;
  80. }
  81. /// <summary>
  82. /// Save this Settings insance to file.
  83. /// </summary>
  84. public void Save()
  85. {
  86. String settingsFilePath = SongBrowserSettings.SettingsPath();
  87. // TODO - not here
  88. if (searchTerms.Count > 10)
  89. {
  90. searchTerms.RemoveRange(10, searchTerms.Count - 10);
  91. }
  92. FileStream fs = new FileStream(settingsFilePath, FileMode.Create, FileAccess.Write);
  93. XmlSerializer serializer = new XmlSerializer(typeof(SongBrowserSettings));
  94. serializer.Serialize(fs, this);
  95. fs.Close();
  96. }
  97. }
  98. }