5
5
using System . Linq ;
6
6
using System . Net ;
7
7
using System . Net . Http . Headers ;
8
+ using System . Security . Policy ;
8
9
using System . Threading . Tasks ;
9
10
using System . Web ;
10
11
using System . Windows ;
12
+ using System . Windows . Input ;
11
13
using static ModAssistant . Http ;
12
14
13
15
namespace ModAssistant . API
@@ -31,19 +33,19 @@ private static string BeatSaverCDNURLPrefix
31
33
private static readonly string CustomWIPSongsFolder = Path . Combine ( "Beat Saber_Data" , "CustomWIPLevels" ) ;
32
34
private const bool BypassDownloadCounter = false ;
33
35
34
- public static async Task < BeatSaverMap > GetFromKey ( string Key , bool showNotification = true )
36
+ public static async Task < BeatSaverMap > GetFromKey ( string Key , bool showNotification = true , bool fromPlaylist = false )
35
37
{
36
38
if ( showNotification && App . OCIWindow != "No" && App . OCIWindow != "Notify" ) OneClickInstaller . Status . Show ( ) ;
37
- return await GetMap ( Key , "key" , showNotification ) ;
39
+ return await GetMap ( Key , "key" , showNotification , fromPlaylist ) ;
38
40
}
39
41
40
- public static async Task < BeatSaverMap > GetFromHash ( string Hash , bool showNotification = true )
42
+ public static async Task < BeatSaverMap > GetFromHash ( string Hash , bool showNotification = true , bool fromPlaylist = false )
41
43
{
42
44
if ( showNotification && App . OCIWindow != "No" && App . OCIWindow != "Notify" ) OneClickInstaller . Status . Show ( ) ;
43
- return await GetMap ( Hash , "hash" , showNotification ) ;
45
+ return await GetMap ( Hash , "hash" , showNotification , fromPlaylist ) ;
44
46
}
45
47
46
- private static async Task < BeatSaverMap > GetMap ( string id , string type , bool showNotification )
48
+ private static async Task < BeatSaverMap > GetMap ( string id , string type , bool showNotification , bool fromPlaylist = false )
47
49
{
48
50
string urlSegment ;
49
51
switch ( type )
@@ -66,7 +68,12 @@ private static async Task<BeatSaverMap> GetMap(string id, string type, bool show
66
68
if ( showNotification ) Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "OneClick:Installing" ) , id ) } ") ;
67
69
try
68
70
{
69
- BeatSaverApiResponse beatsaver = await GetResponse ( BeatSaverURLPrefix + urlSegment + id ) ;
71
+ var result = proxyURL ( "" , true , fromPlaylist , showNotification ) ;
72
+ if ( result [ 1 ] )
73
+ {
74
+ Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "OneClick:Fallback" ) , "默认@default" ) } ") ;
75
+ }
76
+ BeatSaverApiResponse beatsaver = await GetResponse ( result [ 0 ] + urlSegment + id ) ;
70
77
if ( beatsaver != null && beatsaver . map != null )
71
78
{
72
79
map . response = beatsaver ;
@@ -83,14 +90,19 @@ private static async Task<BeatSaverMap> GetMap(string id, string type, bool show
83
90
}
84
91
map . HashToDownload = mapVersion . hash ;
85
92
}
86
- map . Name = await InstallMap ( map , showNotification ) ;
93
+ map . Name = await InstallMap ( map , showNotification , fromPlaylist , result [ 1 ] ) ;
94
+ if ( ModAssistant . Properties . Settings . Default . AssetsDownloadServer == "国内源@WGzeyu" && ! result [ 1 ] )
95
+ {
96
+ Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "MainWindow:AssetsServerLimitLabel" ) , ZeyuCount . getCount ( ) . ToString ( ) ) } ") ;
97
+ }
87
98
map . Success = true ;
88
99
}
89
100
}
90
101
catch ( Exception e )
91
102
{
92
- ModAssistant . Utils . Log ( $ "Failed downloading BeatSaver map: { id } | Error: { e . Message } ", "ERROR" ) ;
103
+ ModAssistant . Utils . Log ( $ "Failed downloading BeatSaver map: { id } | Error: { e . Message } \n { e . StackTrace } ", "ERROR" ) ;
93
104
Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "OneClick:Failed" ) , ( map . Name ?? id ) ) } ") ;
105
+ Utils . SetMessage ( e . Message ) ;
94
106
App . CloseWindowOnFinish = false ;
95
107
}
96
108
return map ;
@@ -143,12 +155,19 @@ private static async Task<BeatSaverApiResponse> GetResponse(string url, bool sho
143
155
}
144
156
}
145
157
146
- public static async Task < string > InstallMap ( BeatSaverMap Map , bool showNotification = true )
158
+ public static async Task < string > InstallMap ( BeatSaverMap Map , bool showNotification = true , bool fromPlaylist = false , bool fallback = false )
147
159
{
148
160
BeatSaverApiResponseMap responseMap = Map . response . map ;
149
- BeatSaverApiResponseMap . BeatsaverMapVersion mapVersion = responseMap . versions . Where ( r => r . hash == Map . HashToDownload ) . First ( ) ;
150
- if ( mapVersion == null )
161
+ BeatSaverApiResponseMap . BeatsaverMapVersion mapVersion = null ;
162
+ try
151
163
{
164
+ mapVersion = responseMap . versions . Where ( r => r . hash == Map . HashToDownload ) . First ( ) ;
165
+ if ( mapVersion == null )
166
+ {
167
+ throw new Exception ( "Could not find map version." ) ;
168
+ }
169
+ }
170
+ catch ( Exception ) {
152
171
throw new Exception ( "Could not find map version." ) ;
153
172
}
154
173
@@ -164,11 +183,11 @@ public static async Task<string> InstallMap(BeatSaverMap Map, bool showNotificat
164
183
#pragma warning disable CS0162 // Unreachable code detected
165
184
if ( BypassDownloadCounter )
166
185
{
167
- await Utils . DownloadAsset ( mapVersion . downloadURL , targetSongDirectory , Map . HashToDownload + ".zip" , mapName , showNotification , true ) ;
186
+ await Utils . DownloadAsset ( mapVersion . downloadURL , targetSongDirectory , Map . HashToDownload + ".zip" , mapName , showNotification , true , false , fromPlaylist , fallback ) ;
168
187
}
169
188
else
170
189
{
171
- await Utils . DownloadAsset ( mapVersion . downloadURL , targetSongDirectory , Map . HashToDownload + ".zip" , mapName , showNotification , true ) ;
190
+ await Utils . DownloadAsset ( mapVersion . downloadURL , targetSongDirectory , Map . HashToDownload + ".zip" , mapName , showNotification , true , false , fromPlaylist , fallback ) ;
172
191
}
173
192
#pragma warning restore CS0162 // Unreachable code detected
174
193
@@ -263,9 +282,11 @@ public static DateTime UnixTimestampToDateTime(double unixTime)
263
282
return new DateTime ( unixStart . Ticks + unixTimeStampInTicks , DateTimeKind . Utc ) ;
264
283
}
265
284
266
- public static async Task Download ( string url , string output , int retries = 3 )
285
+ public static async Task Download ( string url , string output , bool fromPlaylist , bool fallback = false , int retries = 3 )
267
286
{
268
- url = proxyURL ( url ) ;
287
+ var result = proxyURL ( url , false , fromPlaylist ) ;
288
+ url = result [ 0 ] ;
289
+ fallback = result [ 1 ] ;
269
290
if ( retries == 0 )
270
291
{
271
292
Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "OneClick:RatelimitSkip" ) , url ) } ") ;
@@ -282,13 +303,18 @@ public static async Task Download(string url, string output, int retries = 3)
282
303
Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "OneClick:RatelimitHit" ) , ratelimit . ResetTime . ToLocalTime ( ) ) } ") ;
283
304
284
305
await ratelimit . Wait ( ) ;
285
- await Download ( url , output , retries - 1 ) ;
306
+ await Download ( url , output , fromPlaylist , fallback , retries - 1 ) ;
286
307
}
287
308
288
309
using ( var stream = await resp . Content . ReadAsStreamAsync ( ) )
289
310
using ( var fs = new FileStream ( output , FileMode . OpenOrCreate , FileAccess . Write ) )
290
311
{
291
312
await stream . CopyToAsync ( fs ) ;
313
+ if ( fromPlaylist ) {
314
+ ModAssistant . ZeyuCount . downloadBeatsaverMultiple ( ) ;
315
+ } else {
316
+ ModAssistant . ZeyuCount . downloadBeatsaverSingle ( ) ;
317
+ }
292
318
}
293
319
}
294
320
@@ -403,11 +429,23 @@ public class ParitySummary
403
429
}
404
430
}
405
431
406
- public static string proxyURL ( string url ) {
407
- return url . Replace ( "https://as.cdn.beatsaver.com" , "https://cdn.beatsaver.com" )
432
+ public static dynamic [ ] proxyURL ( string url , bool api = false , bool fromPlaylist = false , bool showNotification = false ) {
433
+ string ProxyURL = api ? BeatSaverURLPrefix : BeatSaverCDNURLPrefix ;
434
+ bool fallback = false ;
435
+
436
+ if ( Properties . Settings . Default . AssetsDownloadServer == "国内源@WGzeyu" && ( ( fromPlaylist && ! ModAssistant . ZeyuCount . checkBeatsaverMultiple ( ) ) || ( ! fromPlaylist && ! ModAssistant . ZeyuCount . checkBeatsaverSingle ( ) ) ) )
437
+ {
438
+ if ( showNotification ) Utils . SetMessage ( $ "{ string . Format ( ( string ) Application . Current . FindResource ( "OneClick:Fallback" ) , "默认@default" ) } ") ;
439
+ ProxyURL = api ? ModAssistant . Utils . Constants . BeatSaverURLPrefix_default : ModAssistant . Utils . Constants . BeatSaverCDNURLPrefix_default ;
440
+ fallback = true ;
441
+ }
442
+
443
+ dynamic [ ] result = new dynamic [ ] { api ? ProxyURL : url . Replace ( "https://as.cdn.beatsaver.com" , "https://cdn.beatsaver.com" )
408
444
. Replace ( "https://na.cdn.beatsaver.com" , "https://cdn.beatsaver.com" )
409
445
. Replace ( "https://r2cdn.beatsaver.com" , "https://cdn.beatsaver.com" )
410
- . Replace ( "https://cdn.beatsaver.com" , BeatSaverCDNURLPrefix ) ;
446
+ . Replace ( "https://cdn.beatsaver.com" , ProxyURL ) , fallback } ;
447
+
448
+ return result ;
411
449
}
412
450
}
413
451
}
0 commit comments