BeatSaverApiResults.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Linq;
  3. using SimpleJSON;
  4. using SongLoaderPlugin;
  5. using SongLoaderPlugin.OverrideClasses;
  6. // From: https://github.com/andruzzzhka/BeatSaverDownloader
  7. namespace SongBrowser.DataAccess.BeatSaverApi
  8. {
  9. public enum SongQueueState { Queued, Downloading, Downloaded, Error };
  10. [Serializable]
  11. public class DifficultyLevel
  12. {
  13. public string difficulty;
  14. public int difficultyRank;
  15. public string jsonPath;
  16. public int? offset;
  17. public DifficultyLevel(CustomSongInfo.DifficultyLevel difficultyLevel)
  18. {
  19. difficulty = difficultyLevel.difficulty;
  20. difficultyRank = difficultyLevel.difficultyRank;
  21. jsonPath = difficultyLevel.jsonPath;
  22. }
  23. public DifficultyLevel(string Difficulty, int DifficultyRank, string JsonPath, int Offset = 0)
  24. {
  25. difficulty = Difficulty;
  26. difficultyRank = DifficultyRank;
  27. jsonPath = JsonPath;
  28. offset = Offset;
  29. }
  30. }
  31. [Serializable]
  32. public class Song
  33. {
  34. public string id;
  35. public string beatname;
  36. public string ownerid;
  37. public string downloads;
  38. public string upvotes;
  39. public string plays;
  40. public string beattext;
  41. public string uploadtime;
  42. public string songName;
  43. public string songSubName;
  44. public string authorName;
  45. public string beatsPerMinute;
  46. public string downvotes;
  47. public string coverUrl;
  48. public string downloadUrl;
  49. public DifficultyLevel[] difficultyLevels;
  50. public string img;
  51. public string hash;
  52. public string path;
  53. public SongQueueState songQueueState = SongQueueState.Queued;
  54. public float downloadingProgress = 0f;
  55. public Song()
  56. {
  57. }
  58. public Song(JSONNode jsonNode)
  59. {
  60. id = jsonNode["key"];
  61. beatname = jsonNode["name"];
  62. ownerid = jsonNode["uploaderId"];
  63. downloads = jsonNode["downloadCount"];
  64. upvotes = jsonNode["upVotes"];
  65. downvotes = jsonNode["downVotes"];
  66. plays = jsonNode["playedCount"];
  67. beattext = jsonNode["description"];
  68. uploadtime = jsonNode["createdAt"];
  69. songName = jsonNode["songName"];
  70. songSubName = jsonNode["songSubName"];
  71. authorName = jsonNode["authorName"];
  72. beatsPerMinute = jsonNode["bpm"];
  73. coverUrl = jsonNode["coverUrl"];
  74. downloadUrl = jsonNode["downloadUrl"];
  75. hash = jsonNode["hashMd5"];
  76. hash = hash.ToUpper();
  77. var difficultyNode = jsonNode["difficulties"];
  78. difficultyLevels = new DifficultyLevel[difficultyNode.Count];
  79. for (int i = 0; i < difficultyNode.Count; i++)
  80. {
  81. difficultyLevels[i] = new DifficultyLevel(difficultyNode[i]["difficulty"], difficultyNode[i]["difficultyRank"], difficultyNode[i]["audioPath"], difficultyNode[i]["jsonPath"]);
  82. }
  83. }
  84. public static Song FromSearchNode(JSONNode mainNode)
  85. {
  86. Song buffer = new Song();
  87. buffer.id = mainNode["key"];
  88. buffer.beatname = mainNode["name"];
  89. buffer.ownerid = mainNode["uploaderId"];
  90. buffer.downloads = mainNode["downloadCount"];
  91. buffer.upvotes = mainNode["upVotes"];
  92. buffer.downvotes = mainNode["downVotes"];
  93. buffer.plays = mainNode["playedCount"];
  94. buffer.uploadtime = mainNode["createdAt"];
  95. buffer.songName = mainNode["songName"];
  96. buffer.songSubName = mainNode["songSubName"];
  97. buffer.authorName = mainNode["authorName"];
  98. buffer.beatsPerMinute = mainNode["bpm"];
  99. buffer.coverUrl = mainNode["coverUrl"];
  100. buffer.downloadUrl = mainNode["downloadUrl"];
  101. buffer.hash = mainNode["hashMd5"];
  102. var difficultyNode = mainNode["difficulties"];
  103. buffer.difficultyLevels = new DifficultyLevel[difficultyNode.Count];
  104. for (int i = 0; i < difficultyNode.Count; i++)
  105. {
  106. buffer.difficultyLevels[i] = new DifficultyLevel(difficultyNode[i]["difficulty"], difficultyNode[i]["difficultyRank"], difficultyNode[i]["audioPath"], difficultyNode[i]["jsonPath"]);
  107. }
  108. return buffer;
  109. }
  110. public Song(JSONNode jsonNode, JSONNode difficultyNode)
  111. {
  112. id = jsonNode["key"];
  113. beatname = jsonNode["name"];
  114. ownerid = jsonNode["uploaderId"];
  115. downloads = jsonNode["downloadCount"];
  116. upvotes = jsonNode["upVotes"];
  117. downvotes = jsonNode["downVotes"];
  118. plays = jsonNode["playedCount"];
  119. beattext = jsonNode["description"];
  120. uploadtime = jsonNode["createdAt"];
  121. songName = jsonNode["songName"];
  122. songSubName = jsonNode["songSubName"];
  123. authorName = jsonNode["authorName"];
  124. beatsPerMinute = jsonNode["bpm"];
  125. coverUrl = jsonNode["coverUrl"];
  126. downloadUrl = jsonNode["downloadUrl"];
  127. hash = jsonNode["hashMd5"];
  128. difficultyLevels = new DifficultyLevel[difficultyNode.Count];
  129. for (int i = 0; i < difficultyNode.Count; i++)
  130. {
  131. difficultyLevels[i] = new DifficultyLevel(difficultyNode[i]["difficulty"], difficultyNode[i]["difficultyRank"], difficultyNode[i]["audioPath"], difficultyNode[i]["jsonPath"]);
  132. }
  133. }
  134. public bool Compare(Song compareTo)
  135. {
  136. if (compareTo != null && songName == compareTo.songName)
  137. {
  138. if (difficultyLevels != null && compareTo.difficultyLevels != null)
  139. {
  140. return (songSubName == compareTo.songSubName && authorName == compareTo.authorName && difficultyLevels.Length == compareTo.difficultyLevels.Length);
  141. }
  142. else
  143. {
  144. return (songSubName == compareTo.songSubName && authorName == compareTo.authorName);
  145. }
  146. }
  147. else
  148. {
  149. return false;
  150. }
  151. }
  152. public Song(CustomLevel _data)
  153. {
  154. songName = _data.songName;
  155. songSubName = _data.songSubName;
  156. authorName = _data.songAuthorName;
  157. difficultyLevels = ConvertDifficultyLevels(_data.difficultyBeatmapSets.SelectMany(x => x.difficultyBeatmaps).ToArray());
  158. path = _data.customSongInfo.path;
  159. hash = _data.levelID.Substring(0, 32);
  160. }
  161. public Song(CustomSongInfo _song)
  162. {
  163. songName = _song.songName;
  164. songSubName = _song.songSubName;
  165. authorName = _song.songAuthorName;
  166. difficultyLevels = ConvertDifficultyLevels(_song.difficultyLevels);
  167. path = _song.path;
  168. hash = _song.levelId.Substring(0, 32);
  169. }
  170. public DifficultyLevel[] ConvertDifficultyLevels(CustomSongInfo.DifficultyLevel[] _difficultyLevels)
  171. {
  172. if (_difficultyLevels != null && _difficultyLevels.Length > 0)
  173. {
  174. DifficultyLevel[] buffer = new DifficultyLevel[_difficultyLevels.Length];
  175. for (int i = 0; i < _difficultyLevels.Length; i++)
  176. {
  177. buffer[i] = new DifficultyLevel(_difficultyLevels[i]);
  178. }
  179. return buffer;
  180. }
  181. else
  182. {
  183. return null;
  184. }
  185. }
  186. public DifficultyLevel[] ConvertDifficultyLevels(IDifficultyBeatmap[] _difficultyLevels)
  187. {
  188. if (_difficultyLevels != null && _difficultyLevels.Length > 0)
  189. {
  190. DifficultyLevel[] buffer = new DifficultyLevel[_difficultyLevels.Length];
  191. for (int i = 0; i < _difficultyLevels.Length; i++)
  192. {
  193. buffer[i] = new DifficultyLevel(_difficultyLevels[i].difficulty.ToString(), _difficultyLevels[i].difficultyRank, string.Empty);
  194. }
  195. return buffer;
  196. }
  197. else
  198. {
  199. return null;
  200. }
  201. }
  202. }
  203. [Serializable]
  204. public class RootObject
  205. {
  206. public Song[] songs;
  207. }
  208. }