Skip to content

Commit 3ef0a16

Browse files
author
WGzeyu
authored
Merge pull request #3 from beatmods-top/dev20230607
Add Assets Download Server Selection
2 parents 18970e5 + 39ecc2b commit 3ef0a16

27 files changed

+519
-131
lines changed

ModAssistant/App.config

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<userSettings>
1313
<ModAssistant.Properties.Settings>
1414
<setting name="InstallFolder" serializeAs="String">
15-
<value/>
15+
<value />
1616
</setting>
1717
<setting name="StoreType" serializeAs="String">
18-
<value/>
18+
<value />
1919
</setting>
2020
<setting name="SaveSelected" serializeAs="String">
2121
<value>False</value>
@@ -33,22 +33,19 @@
3333
<value>False</value>
3434
</setting>
3535
<setting name="GameVersion" serializeAs="String">
36-
<value/>
36+
<value />
3737
</setting>
3838
<setting name="AllGameVersions" serializeAs="String">
39-
<value/>
39+
<value />
4040
</setting>
4141
<setting name="UpgradeRequired" serializeAs="String">
4242
<value>True</value>
4343
</setting>
4444
<setting name="LastTab" serializeAs="String">
45-
<value/>
45+
<value />
4646
</setting>
4747
<setting name="SelectedTheme" serializeAs="String">
48-
<value/>
49-
</setting>
50-
<setting name="AppliedAnniversaryTheme" serializeAs="String">
51-
<value>False</value>
48+
<value />
5249
</setting>
5350
<setting name="ReinstallInstalled" serializeAs="String">
5451
<value>True</value>
@@ -57,17 +54,26 @@
5754
<value>False</value>
5855
</setting>
5956
<setting name="LanguageCode" serializeAs="String">
60-
<value/>
57+
<value />
6158
</setting>
6259
<setting name="OCIWindow" serializeAs="String">
63-
<value/>
60+
<value />
6461
</setting>
6562
<setting name="DownloadServer" serializeAs="String">
6663
<value>国际源@BeatMods</value>
6764
</setting>
6865
<setting name="Setting" serializeAs="String">
6966
<value />
7067
</setting>
68+
<setting name="AppliedAnniversaryTheme" serializeAs="String">
69+
<value>False</value>
70+
</setting>
71+
<setting name="AssetsDownloadServer" serializeAs="String">
72+
<value>默认@default</value>
73+
</setting>
74+
<setting name="ZeyuCount" serializeAs="String">
75+
<value />
76+
</setting>
7177
</ModAssistant.Properties.Settings>
7278
<ModAssistant.Settings1>
7379
<setting name="InstallFolder" serializeAs="String">

ModAssistant/App.xaml.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ private async void Application_Startup(object sender, StartupEventArgs e)
5252
BeatSaberInstallDirectory = Utils.GetInstallDir();
5353

5454
Languages.LoadLanguages();
55-
56-
Options.Instance.DownloadServerSelectComboBox.ItemsSource = Options.serverList.ToList();
57-
Options.Instance.DownloadServerSelectComboBox.SelectedIndex = Options.serverList.ToList().FindIndex((string server) => server == ModAssistant.Properties.Settings.Default.DownloadServer);
55+
ModAssistant.ZeyuCount.loadData();
5856

5957
while (string.IsNullOrEmpty(BeatSaberInstallDirectory))
6058
{
@@ -106,6 +104,18 @@ private async Task Init()
106104
if (GUI)
107105
{
108106
window = new MainWindow();
107+
108+
ModAssistant.MainWindow.Instance.ModServersBox.ItemsSource = ModAssistant.MainWindow.serverList.ToList();
109+
int DownloadServerIndex = ModAssistant.MainWindow.serverList.ToList().FindIndex((string server) => server == ModAssistant.Properties.Settings.Default.DownloadServer);
110+
ModAssistant.MainWindow.Instance.ModServersBox.SelectedIndex = DownloadServerIndex == -1 ? 0 : DownloadServerIndex;
111+
112+
ModAssistant.MainWindow.Instance.AssetsServerBox.ItemsSource = ModAssistant.MainWindow.assetsServerList.ToList();
113+
int AssetsDownloadServerIndex = ModAssistant.MainWindow.assetsServerList.ToList().FindIndex((string server) => server == ModAssistant.Properties.Settings.Default.AssetsDownloadServer);
114+
ModAssistant.MainWindow.Instance.AssetsServerBox.SelectedIndex = AssetsDownloadServerIndex == -1 ? 0 : AssetsDownloadServerIndex;
115+
116+
117+
Utils.UpdateCountIndicator();
118+
109119
window.Show();
110120
}
111121
else

ModAssistant/Classes/External Interfaces/BeatSaver.cs

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,47 @@
55
using System.Linq;
66
using System.Net;
77
using System.Net.Http.Headers;
8+
using System.Security.Policy;
89
using System.Threading.Tasks;
910
using System.Web;
1011
using System.Windows;
12+
using System.Windows.Input;
1113
using static ModAssistant.Http;
1214

1315
namespace ModAssistant.API
1416
{
1517
public class BeatSaver
1618
{
17-
private const string BeatSaverURLPrefix = "https://api.beatsaver.com";
19+
private static string BeatSaverURLPrefix { get =>
20+
Properties.Settings.Default.AssetsDownloadServer == AssetsServer.Default ? ModAssistant.Utils.Constants.BeatSaverURLPrefix_default :
21+
(Properties.Settings.Default.AssetsDownloadServer == AssetsServer.WGZeyu ? ModAssistant.Utils.Constants.BeatSaverURLPrefix_wgzeyu :
22+
ModAssistant.Utils.Constants.BeatSaverURLPrefix_beatsaberchina);
23+
}
24+
25+
private static string BeatSaverCDNURLPrefix
26+
{
27+
get =>
28+
Properties.Settings.Default.AssetsDownloadServer == AssetsServer.Default ? ModAssistant.Utils.Constants.BeatSaverCDNURLPrefix_default :
29+
(Properties.Settings.Default.AssetsDownloadServer == AssetsServer.WGZeyu ? ModAssistant.Utils.Constants.BeatSaverCDNURLPrefix_wgzeyu :
30+
ModAssistant.Utils.Constants.BeatSaverCDNURLPrefix_beatsaberchina);
31+
}
1832
private static readonly string CustomSongsFolder = Path.Combine("Beat Saber_Data", "CustomLevels");
1933
private static readonly string CustomWIPSongsFolder = Path.Combine("Beat Saber_Data", "CustomWIPLevels");
2034
private const bool BypassDownloadCounter = false;
2135

22-
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)
2337
{
2438
if (showNotification && App.OCIWindow != "No" && App.OCIWindow != "Notify") OneClickInstaller.Status.Show();
25-
return await GetMap(Key, "key", showNotification);
39+
return await GetMap(Key, "key", showNotification, fromPlaylist);
2640
}
2741

28-
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)
2943
{
3044
if (showNotification && App.OCIWindow != "No" && App.OCIWindow != "Notify") OneClickInstaller.Status.Show();
31-
return await GetMap(Hash, "hash", showNotification);
45+
return await GetMap(Hash, "hash", showNotification, fromPlaylist);
3246
}
3347

34-
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)
3549
{
3650
string urlSegment;
3751
switch (type)
@@ -54,7 +68,12 @@ private static async Task<BeatSaverMap> GetMap(string id, string type, bool show
5468
if (showNotification) Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Installing"), id)}");
5569
try
5670
{
57-
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);
5877
if (beatsaver != null && beatsaver.map != null)
5978
{
6079
map.response = beatsaver;
@@ -71,14 +90,19 @@ private static async Task<BeatSaverMap> GetMap(string id, string type, bool show
7190
}
7291
map.HashToDownload = mapVersion.hash;
7392
}
74-
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+
}
7598
map.Success = true;
7699
}
77100
}
78101
catch (Exception e)
79102
{
80-
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");
81104
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Failed"), (map.Name ?? id))}");
105+
Utils.SetMessage(e.Message);
82106
App.CloseWindowOnFinish = false;
83107
}
84108
return map;
@@ -131,12 +155,19 @@ private static async Task<BeatSaverApiResponse> GetResponse(string url, bool sho
131155
}
132156
}
133157

134-
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)
135159
{
136160
BeatSaverApiResponseMap responseMap = Map.response.map;
137-
BeatSaverApiResponseMap.BeatsaverMapVersion mapVersion = responseMap.versions.Where(r => r.hash == Map.HashToDownload).First();
138-
if (mapVersion == null)
161+
BeatSaverApiResponseMap.BeatsaverMapVersion mapVersion = null;
162+
try
139163
{
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) {
140171
throw new Exception("Could not find map version.");
141172
}
142173

@@ -152,11 +183,11 @@ public static async Task<string> InstallMap(BeatSaverMap Map, bool showNotificat
152183
#pragma warning disable CS0162 // Unreachable code detected
153184
if (BypassDownloadCounter)
154185
{
155-
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);
156187
}
157188
else
158189
{
159-
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);
160191
}
161192
#pragma warning restore CS0162 // Unreachable code detected
162193

@@ -251,8 +282,11 @@ public static DateTime UnixTimestampToDateTime(double unixTime)
251282
return new DateTime(unixStart.Ticks + unixTimeStampInTicks, DateTimeKind.Utc);
252283
}
253284

254-
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)
255286
{
287+
var result = proxyURL(url, false, fromPlaylist);
288+
url = result[0];
289+
fallback = result[1];
256290
if (retries == 0)
257291
{
258292
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:RatelimitSkip"), url)}");
@@ -269,13 +303,18 @@ public static async Task Download(string url, string output, int retries = 3)
269303
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:RatelimitHit"), ratelimit.ResetTime.ToLocalTime())}");
270304

271305
await ratelimit.Wait();
272-
await Download(url, output, retries - 1);
306+
await Download(url, output, fromPlaylist, fallback, retries - 1);
273307
}
274308

275309
using (var stream = await resp.Content.ReadAsStreamAsync())
276310
using (var fs = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write))
277311
{
278312
await stream.CopyToAsync(fs);
313+
if (fromPlaylist) {
314+
ModAssistant.ZeyuCount.downloadBeatsaverMultiple();
315+
} else {
316+
ModAssistant.ZeyuCount.downloadBeatsaverSingle();
317+
}
279318
}
280319
}
281320

@@ -389,6 +428,25 @@ public class ParitySummary
389428
public int resets { get; set; }
390429
}
391430
}
431+
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")
444+
.Replace("https://na.cdn.beatsaver.com", "https://cdn.beatsaver.com")
445+
.Replace("https://r2cdn.beatsaver.com", "https://cdn.beatsaver.com")
446+
.Replace("https://cdn.beatsaver.com", ProxyURL), fallback};
447+
448+
return result;
449+
}
392450
}
393451
}
394452
#pragma warning restore IDE1006 // Naming Styles
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,61 @@
11
using System;
22
using System.Threading.Tasks;
3+
using System.Windows;
34

45
namespace ModAssistant.API
56
{
67
class ModelSaber
78
{
8-
private const string ModelSaberURLPrefix = "https://modelsaber.com/files/";
9+
private static string ModelSaberURLPrefix
10+
{
11+
get =>
12+
Properties.Settings.Default.AssetsDownloadServer == AssetsServer.Default ? ModAssistant.Utils.Constants.ModelSaberURLPrefix_default :
13+
(Properties.Settings.Default.AssetsDownloadServer == AssetsServer.WGZeyu ? ModAssistant.Utils.Constants.ModelSaberURLPrefix_wgzeyu :
14+
ModAssistant.Utils.Constants.ModelSaberURLPrefix_beatsaberchina);
15+
}
916
private const string CustomAvatarsFolder = "CustomAvatars";
1017
private const string CustomSabersFolder = "CustomSabers";
1118
private const string CustomPlatformsFolder = "CustomPlatforms";
1219
private const string CustomBloqsFolder = "CustomNotes";
1320

1421
public static async Task GetModel(Uri uri)
1522
{
23+
string proxyURL = ModelSaberURLPrefix;
24+
bool throughProxy = true;
25+
bool fallback = false;
26+
27+
if (Properties.Settings.Default.AssetsDownloadServer == "国内源@WGzeyu" && !ModAssistant.ZeyuCount.checkModelSaberSingle())
28+
{
29+
proxyURL = ModAssistant.Utils.Constants.ModelSaberURLPrefix_default;
30+
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Fallback"), "默认@default")}");
31+
throughProxy = false;
32+
fallback = true;
33+
}
34+
1635
switch (uri.Host)
1736
{
1837
case "avatar":
19-
await Utils.DownloadAsset(ModelSaberURLPrefix + uri.Host + uri.AbsolutePath, CustomAvatarsFolder);
38+
await Utils.DownloadAsset(proxyURL + uri.Host + uri.AbsolutePath, CustomAvatarsFolder, null, null, true, false, false, throughProxy, fallback);
2039
break;
2140
case "saber":
22-
await Utils.DownloadAsset(ModelSaberURLPrefix + uri.Host + uri.AbsolutePath, CustomSabersFolder);
41+
await Utils.DownloadAsset(proxyURL + uri.Host + uri.AbsolutePath, CustomSabersFolder, null, null, true, false, false, throughProxy, fallback);
2342
break;
2443
case "platform":
25-
await Utils.DownloadAsset(ModelSaberURLPrefix + uri.Host + uri.AbsolutePath, CustomPlatformsFolder);
44+
await Utils.DownloadAsset(proxyURL + uri.Host + uri.AbsolutePath, CustomPlatformsFolder, null, null, true, false, false, throughProxy, fallback);
2645
break;
2746
case "bloq":
28-
await Utils.DownloadAsset(ModelSaberURLPrefix + uri.Host + uri.AbsolutePath, CustomBloqsFolder);
47+
await Utils.DownloadAsset(proxyURL + uri.Host + uri.AbsolutePath, CustomBloqsFolder, null, null, true, false, false, throughProxy, fallback);
2948
break;
3049
}
3150
}
51+
public static string proxyURL()
52+
{
53+
if (Properties.Settings.Default.AssetsDownloadServer == "国内源@WGzeyu" && !ModAssistant.ZeyuCount.checkModelSaberSingle())
54+
{
55+
return ModAssistant.Utils.Constants.ModelSaberURLPrefix_default;
56+
}
3257

58+
return ModelSaberURLPrefix;
59+
}
3360
}
3461
}

0 commit comments

Comments
 (0)