1010using System . Security . Principal ;
1111using Microsoft . Win32 ;
1212using System . IO . Compression ;
13+ using System . Web . Script . Serialization ;
1314
1415namespace ModAssistant
1516{
1617 class OneClickInstaller
1718 {
1819 private const string ModelSaberURLPrefix = "https://modelsaber.com/files/" ;
19- private const string BeatSaverURLPrefix = "https://beatsaver.com/download/ " ;
20+ private const string BeatSaverURLPrefix = "https://beatsaver.com" ;
2021
2122 private static string BeatSaberPath = App . BeatSaberInstallDirectory ;
2223
2324 private const string CustomAvatarsFolder = "CustomAvatars" ;
2425 private const string CustomSabersFolder = "CustomSabers" ;
2526 private const string CustomPlatformsFolder = "CustomPlatforms" ;
26- private const string CustomSongsFolder = "CustomSongs" ;
27+ private static readonly string CustomSongsFolder = Path . Combine ( "Beat Saber_Data" , "CustomLevels" ) ;
2728
2829 private static readonly string [ ] Protocols = new [ ] { "modelsaber" , "beatsaver" } ;
2930
@@ -45,11 +46,37 @@ public static void InstallAsset(string link)
4546
4647 private static void BeatSaver ( Uri uri )
4748 {
48- string ID = uri . Host ;
49- DownloadAsset ( BeatSaverURLPrefix + ID , CustomSongsFolder , ID + ".zip" ) ;
50- string directory = Path . Combine ( BeatSaberPath , CustomSongsFolder , ID ) ;
49+ string Key = uri . Host ;
5150
52- using ( FileStream stream = new FileStream ( directory + ".zip" , FileMode . Open ) )
51+ string json = string . Empty ;
52+ BeatSaverApiResponse Response ;
53+
54+ HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( BeatSaverURLPrefix + "/api/maps/detail/" + Key ) ;
55+ request . AutomaticDecompression = DecompressionMethods . GZip ;
56+ request . UserAgent = "ModAssistant/" + App . Version ;
57+
58+ try
59+ {
60+ using ( HttpWebResponse response = ( HttpWebResponse ) request . GetResponse ( ) )
61+ using ( Stream stream = response . GetResponseStream ( ) )
62+ using ( StreamReader reader = new StreamReader ( stream ) )
63+ {
64+ var serializer = new JavaScriptSerializer ( ) ;
65+ Response = serializer . Deserialize < BeatSaverApiResponse > ( reader . ReadToEnd ( ) ) ;
66+ }
67+ }
68+ catch ( Exception e )
69+ {
70+ MessageBox . Show ( "Could not get map details.\n \n " + e ) ;
71+ return ;
72+ }
73+
74+ string directory = Path . Combine ( BeatSaberPath , CustomSongsFolder , Response . key + " (" + Response . metadata . songName + " - " + Response . metadata . levelAuthorName + ")" ) . Trim ( Path . GetInvalidPathChars ( ) ) ;
75+ string zip = Path . Combine ( BeatSaberPath , CustomSongsFolder , Response . hash ) + ".zip" ;
76+
77+ DownloadAsset ( BeatSaverURLPrefix + Response . downloadURL , CustomSongsFolder , Response . hash + ".zip" ) ;
78+
79+ using ( FileStream stream = new FileStream ( zip , FileMode . Open ) )
5380 {
5481 using ( ZipArchive archive = new ZipArchive ( stream ) )
5582 {
@@ -65,7 +92,7 @@ private static void BeatSaver(Uri uri)
6592 }
6693 }
6794
68- File . Delete ( Path . Combine ( BeatSaberPath , CustomSongsFolder , ID + ". zip" ) ) ;
95+ File . Delete ( zip ) ;
6996 }
7097
7198 private static void ModelSaber ( Uri uri )
@@ -192,4 +219,56 @@ public static bool IsRegistered(string Protocol)
192219 return false ;
193220 }
194221 }
222+
223+ class BeatSaverApiResponse
224+ {
225+ public Metadata metadata { get ; set ; }
226+ public Stats stats { get ; set ; }
227+ public string description { get ; set ; }
228+ public DateTime ? deletedAt { get ; set ; }
229+ public string _id { get ; set ; }
230+ public string key { get ; set ; }
231+ public string name { get ; set ; }
232+ public Uploader uploader { get ; set ; }
233+ public DateTime uploaded { get ; set ; }
234+ public string hash { get ; set ; }
235+ public string downloadURL { get ; set ; }
236+ public string coverURL { get ; set ; }
237+
238+ public class Difficulties
239+ {
240+ public bool easy { get ; set ; }
241+ public bool normal { get ; set ; }
242+ public bool hard { get ; set ; }
243+ public bool expert { get ; set ; }
244+ public bool expertPlus { get ; set ; }
245+ }
246+
247+ public class Metadata
248+ {
249+ public Difficulties difficulties { get ; set ; }
250+ public string [ ] characteristics { get ; set ; }
251+ public string songName { get ; set ; }
252+ public string songSubName { get ; set ; }
253+ public string songAuthorName { get ; set ; }
254+ public string levelAuthorName { get ; set ; }
255+ public int bpm { get ; set ; }
256+ }
257+
258+ public class Stats
259+ {
260+ public int downloads { get ; set ; }
261+ public int plays { get ; set ; }
262+ public int downVotes { get ; set ; }
263+ public int upVotes { get ; set ; }
264+ public double heat { get ; set ; }
265+ public double rating { get ; set ; }
266+ }
267+
268+ public class Uploader
269+ {
270+ public string _id { get ; set ; }
271+ public string username { get ; set ; }
272+ }
273+ }
195274}
0 commit comments