Playlist.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Newtonsoft.Json;
  2. using SimpleJSON;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using Logger = SongBrowser.Logging.Logger;
  8. using Sprites = SongBrowser.UI.Base64Sprites;
  9. /// <summary>
  10. /// Only here to support migration of favorites playlist to.
  11. /// TODO - replace with PlaylistCore when available.
  12. /// </summary>
  13. namespace SongBrowser.DataAccess
  14. {
  15. public class PlaylistSong
  16. {
  17. public string key { get; set; }
  18. public string songName { get; set; }
  19. public string hash { get; set; }
  20. [NonSerialized]
  21. public string levelId;
  22. [NonSerialized]
  23. public CustomPreviewBeatmapLevel level;
  24. [NonSerialized]
  25. public bool oneSaber;
  26. [NonSerialized]
  27. public string path;
  28. }
  29. public class Playlist
  30. {
  31. public string playlistTitle { get; set; }
  32. public string playlistAuthor { get; set; }
  33. public string image { get; set; }
  34. public int playlistSongCount { get; set; }
  35. public List<PlaylistSong> songs { get; set; }
  36. public string fileLoc { get; set; }
  37. public string customDetailUrl { get; set; }
  38. public string customArchiveUrl { get; set; }
  39. [NonSerialized]
  40. public Sprite icon;
  41. public Playlist()
  42. {
  43. }
  44. public Playlist(JSONNode playlistNode)
  45. {
  46. string image = playlistNode["image"].Value;
  47. // If we cannot find an image or parse the provided one correctly, fall back to anything.
  48. // It will never be displayed by SongBrowser.
  49. if (!string.IsNullOrEmpty(image))
  50. {
  51. try
  52. {
  53. icon = Sprites.Base64ToSprite(image.Substring(image.IndexOf(",") + 1));
  54. }
  55. catch
  56. {
  57. Logger.Exception("Unable to convert playlist image to sprite!");
  58. icon = Sprites.StarFullIcon;
  59. }
  60. }
  61. else
  62. {
  63. icon = Sprites.StarFullIcon;
  64. }
  65. playlistTitle = playlistNode["playlistTitle"];
  66. playlistAuthor = playlistNode["playlistAuthor"];
  67. customDetailUrl = playlistNode["customDetailUrl"];
  68. customArchiveUrl = playlistNode["customArchiveUrl"];
  69. if (!string.IsNullOrEmpty(customDetailUrl))
  70. {
  71. if (!customDetailUrl.EndsWith("/"))
  72. customDetailUrl += "/";
  73. Logger.Log("Found playlist with customDetailUrl! Name: " + playlistTitle + ", CustomDetailUrl: " + customDetailUrl);
  74. }
  75. if (!string.IsNullOrEmpty(customArchiveUrl) && customArchiveUrl.Contains("[KEY]"))
  76. {
  77. Logger.Log("Found playlist with customArchiveUrl! Name: " + playlistTitle + ", CustomArchiveUrl: " + customArchiveUrl);
  78. }
  79. songs = new List<PlaylistSong>();
  80. foreach (JSONNode node in playlistNode["songs"].AsArray)
  81. {
  82. PlaylistSong song = new PlaylistSong();
  83. song.key = node["key"];
  84. song.songName = node["songName"];
  85. song.hash = node["hash"];
  86. song.levelId = node["levelId"];
  87. songs.Add(song);
  88. }
  89. if (playlistNode["playlistSongCount"] != null)
  90. {
  91. playlistSongCount = playlistNode["playlistSongCount"].AsInt;
  92. }
  93. if (playlistNode["fileLoc"] != null)
  94. fileLoc = playlistNode["fileLoc"];
  95. if (playlistNode["playlistURL"] != null)
  96. fileLoc = playlistNode["playlistURL"];
  97. }
  98. public static Playlist LoadPlaylist(string path)
  99. {
  100. Playlist playlist = new Playlist(JSON.Parse(File.ReadAllText(path)));
  101. playlist.fileLoc = path;
  102. return playlist;
  103. }
  104. public bool PlaylistEqual(object obj)
  105. {
  106. if (obj == null) return false;
  107. var playlist = obj as Playlist;
  108. if (playlist == null) return false;
  109. int songCountThis = (songs != null ? (songs.Count > 0 ? songs.Count : playlistSongCount) : playlistSongCount);
  110. int songCountObj = (playlist.songs != null ? (playlist.songs.Count > 0 ? playlist.songs.Count : playlist.playlistSongCount) : playlist.playlistSongCount);
  111. return playlistTitle == playlist.playlistTitle &&
  112. playlistAuthor == playlist.playlistAuthor &&
  113. songCountThis == songCountObj;
  114. }
  115. public void CreateNew(String fileLoc)
  116. {
  117. File.WriteAllText(fileLoc, JsonConvert.SerializeObject(this, Formatting.Indented));
  118. }
  119. }
  120. }