PlaylistReader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using SimpleJSON;
  2. using SongBrowserPlugin.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using Logger = SongBrowserPlugin.Logging.Logger;
  8. namespace SongBrowserPlugin.DataAccess
  9. {
  10. public class PlaylistsReader
  11. {
  12. private List<String> _PlaylistsDirectories = new List<string>();
  13. private List<Playlist> _CachedPlaylists;
  14. public List<Playlist> Playlists
  15. {
  16. get
  17. {
  18. return _CachedPlaylists;
  19. }
  20. }
  21. public PlaylistsReader()
  22. {
  23. // Hack, add beatdrop location
  24. String localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  25. String beatDropPlaylistPath = Path.Combine(localAppDataPath, "Programs", "BeatDrop", "playlists");
  26. String beatDropCuratorPlaylistPath = Path.Combine(localAppDataPath, "Programs", "BeatDropCurator", "playlists");
  27. String beatSaberPlaylistPath = Path.Combine(Environment.CurrentDirectory, "Playlists");
  28. _PlaylistsDirectories.Add(beatDropPlaylistPath);
  29. _PlaylistsDirectories.Add(beatDropCuratorPlaylistPath);
  30. _PlaylistsDirectories.Add(beatSaberPlaylistPath);
  31. }
  32. public void UpdatePlaylists()
  33. {
  34. _CachedPlaylists = new List<Playlist>();
  35. Stopwatch timer = new Stopwatch();
  36. timer.Start();
  37. foreach (String path in _PlaylistsDirectories)
  38. {
  39. Logger.Debug("Reading playlists located at: {0}", path);
  40. if (!Directory.Exists(path))
  41. {
  42. Logger.Info("Playlist path does not exist: {0}", path);
  43. continue;
  44. }
  45. string[] files = Directory.GetFiles(path);
  46. foreach (string file in files)
  47. {
  48. Logger.Debug("Checking file {0}", file);
  49. if (Path.GetExtension(file) == ".json" || Path.GetExtension(file) == ".bplist")
  50. {
  51. Playlist p = ParsePlaylist(file);
  52. _CachedPlaylists.Add(p);
  53. }
  54. }
  55. }
  56. timer.Stop();
  57. Logger.Debug("Processing playlists took {0}ms", timer.ElapsedMilliseconds);
  58. }
  59. public static Playlist ParsePlaylist(String path)
  60. {
  61. try
  62. {
  63. if (!File.Exists(path))
  64. {
  65. Logger.Debug("Playlist file no longer exists: {0}", path);
  66. return null;
  67. }
  68. Logger.Debug("Parsing playlist at {0}", path);
  69. String json = File.ReadAllText(path);
  70. Playlist playlist = new Playlist();
  71. JSONNode playlistNode = JSON.Parse(json);
  72. playlist.Image = playlistNode["image"];
  73. playlist.Title = playlistNode["playlistTitle"];
  74. playlist.Author = playlistNode["playlistAuthor"];
  75. playlist.Songs = new List<PlaylistSong>();
  76. playlist.CustomDetailUrl = playlistNode["customDetailUrl"];
  77. playlist.CustomArchiveUrl = playlistNode["customArchiveUrl"];
  78. if (!string.IsNullOrEmpty(playlist.CustomDetailUrl))
  79. {
  80. if (!playlist.CustomDetailUrl.EndsWith("/"))
  81. {
  82. playlist.CustomDetailUrl += "/";
  83. }
  84. Logger.Debug("Found playlist with custom URL! Name: " + playlist.Title + ", CustomDetailURL: " + playlist.CustomDetailUrl);
  85. }
  86. foreach (JSONNode node in playlistNode["songs"].AsArray)
  87. {
  88. PlaylistSong song = new PlaylistSong
  89. {
  90. Key = node["key"],
  91. SongName = node["songName"],
  92. LevelId = node["levelId"],
  93. Hash = node["hash"]
  94. };
  95. playlist.Songs.Add(song);
  96. }
  97. playlist.Path = path;
  98. return playlist;
  99. }
  100. catch (Exception e)
  101. {
  102. Logger.Exception("Exception parsing playlist: ", e);
  103. }
  104. return null;
  105. }
  106. }
  107. }