10
10
using System . Security . Principal ;
11
11
using Microsoft . Win32 ;
12
12
using System . IO . Compression ;
13
+ using System . Web . Script . Serialization ;
13
14
14
15
namespace ModAssistant
15
16
{
16
17
class OneClickInstaller
17
18
{
18
19
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" ;
20
21
21
22
private static string BeatSaberPath = App . BeatSaberInstallDirectory ;
22
23
23
24
private const string CustomAvatarsFolder = "CustomAvatars" ;
24
25
private const string CustomSabersFolder = "CustomSabers" ;
25
26
private const string CustomPlatformsFolder = "CustomPlatforms" ;
26
- private const string CustomSongsFolder = "CustomSongs" ;
27
+ private static readonly string CustomSongsFolder = Path . Combine ( "Beat Saber_Data" , "CustomLevels" ) ;
27
28
28
29
private static readonly string [ ] Protocols = new [ ] { "modelsaber" , "beatsaver" } ;
29
30
@@ -45,11 +46,37 @@ public static void InstallAsset(string link)
45
46
46
47
private static void BeatSaver ( Uri uri )
47
48
{
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 ;
51
50
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 ) )
53
80
{
54
81
using ( ZipArchive archive = new ZipArchive ( stream ) )
55
82
{
@@ -65,7 +92,7 @@ private static void BeatSaver(Uri uri)
65
92
}
66
93
}
67
94
68
- File . Delete ( Path . Combine ( BeatSaberPath , CustomSongsFolder , ID + ". zip" ) ) ;
95
+ File . Delete ( zip ) ;
69
96
}
70
97
71
98
private static void ModelSaber ( Uri uri )
@@ -192,4 +219,56 @@ public static bool IsRegistered(string Protocol)
192
219
return false ;
193
220
}
194
221
}
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
+ }
195
274
}
0 commit comments