Skip to content

Commit 39ecc2b

Browse files
committed
Modify UI & Update Info Page & Fix Log & Add Counter for Zeyu Server
1 parent de963f8 commit 39ecc2b

26 files changed

+425
-147
lines changed

ModAssistant/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
<setting name="AssetsDownloadServer" serializeAs="String">
7272
<value>默认@default</value>
7373
</setting>
74+
<setting name="ZeyuCount" serializeAs="String">
75+
<value />
76+
</setting>
7477
</ModAssistant.Properties.Settings>
7578
<ModAssistant.Settings1>
7679
<setting name="InstallFolder" serializeAs="String">

ModAssistant/App.xaml.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +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-
int DownloadServerIndex = Options.serverList.ToList().FindIndex((string server) => server == ModAssistant.Properties.Settings.Default.DownloadServer);
58-
Options.Instance.DownloadServerSelectComboBox.SelectedIndex = DownloadServerIndex == -1 ? 0 : DownloadServerIndex;
59-
60-
Options.Instance.AssetsDownloadServerSelectComboBox.ItemsSource = Options.assetsServerList.ToList();
61-
int AssetsDownloadServerIndex = Options.assetsServerList.ToList().FindIndex((string server) => server == ModAssistant.Properties.Settings.Default.AssetsDownloadServer);
62-
Options.Instance.AssetsDownloadServerSelectComboBox.SelectedIndex = AssetsDownloadServerIndex == -1 ? 0 : AssetsDownloadServerIndex;
55+
ModAssistant.ZeyuCount.loadData();
6356

6457
while (string.IsNullOrEmpty(BeatSaberInstallDirectory))
6558
{
@@ -111,6 +104,18 @@ private async Task Init()
111104
if (GUI)
112105
{
113106
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+
114119
window.Show();
115120
}
116121
else

ModAssistant/Classes/External Interfaces/BeatSaver.cs

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
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
@@ -31,19 +33,19 @@ private static string BeatSaverCDNURLPrefix
3133
private static readonly string CustomWIPSongsFolder = Path.Combine("Beat Saber_Data", "CustomWIPLevels");
3234
private const bool BypassDownloadCounter = false;
3335

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)
3537
{
3638
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);
3840
}
3941

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)
4143
{
4244
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);
4446
}
4547

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)
4749
{
4850
string urlSegment;
4951
switch (type)
@@ -66,7 +68,12 @@ private static async Task<BeatSaverMap> GetMap(string id, string type, bool show
6668
if (showNotification) Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Installing"), id)}");
6769
try
6870
{
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);
7077
if (beatsaver != null && beatsaver.map != null)
7178
{
7279
map.response = beatsaver;
@@ -83,14 +90,19 @@ private static async Task<BeatSaverMap> GetMap(string id, string type, bool show
8390
}
8491
map.HashToDownload = mapVersion.hash;
8592
}
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+
}
8798
map.Success = true;
8899
}
89100
}
90101
catch (Exception e)
91102
{
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");
93104
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Failed"), (map.Name ?? id))}");
105+
Utils.SetMessage(e.Message);
94106
App.CloseWindowOnFinish = false;
95107
}
96108
return map;
@@ -143,12 +155,19 @@ private static async Task<BeatSaverApiResponse> GetResponse(string url, bool sho
143155
}
144156
}
145157

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)
147159
{
148160
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
151163
{
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) {
152171
throw new Exception("Could not find map version.");
153172
}
154173

@@ -164,11 +183,11 @@ public static async Task<string> InstallMap(BeatSaverMap Map, bool showNotificat
164183
#pragma warning disable CS0162 // Unreachable code detected
165184
if (BypassDownloadCounter)
166185
{
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);
168187
}
169188
else
170189
{
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);
172191
}
173192
#pragma warning restore CS0162 // Unreachable code detected
174193

@@ -263,9 +282,11 @@ public static DateTime UnixTimestampToDateTime(double unixTime)
263282
return new DateTime(unixStart.Ticks + unixTimeStampInTicks, DateTimeKind.Utc);
264283
}
265284

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)
267286
{
268-
url = proxyURL(url);
287+
var result = proxyURL(url, false, fromPlaylist);
288+
url = result[0];
289+
fallback = result[1];
269290
if (retries == 0)
270291
{
271292
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)
282303
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:RatelimitHit"), ratelimit.ResetTime.ToLocalTime())}");
283304

284305
await ratelimit.Wait();
285-
await Download(url, output, retries - 1);
306+
await Download(url, output, fromPlaylist, fallback, retries - 1);
286307
}
287308

288309
using (var stream = await resp.Content.ReadAsStreamAsync())
289310
using (var fs = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write))
290311
{
291312
await stream.CopyToAsync(fs);
313+
if (fromPlaylist) {
314+
ModAssistant.ZeyuCount.downloadBeatsaverMultiple();
315+
} else {
316+
ModAssistant.ZeyuCount.downloadBeatsaverSingle();
317+
}
292318
}
293319
}
294320

@@ -403,11 +429,23 @@ public class ParitySummary
403429
}
404430
}
405431

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")
408444
.Replace("https://na.cdn.beatsaver.com", "https://cdn.beatsaver.com")
409445
.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;
411449
}
412450
}
413451
}

ModAssistant/Classes/External Interfaces/ModelSaber.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
3+
using System.Windows;
34

45
namespace ModAssistant.API
56
{
@@ -19,22 +20,42 @@ private static string ModelSaberURLPrefix
1920

2021
public static async Task GetModel(Uri uri)
2122
{
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+
2235
switch (uri.Host)
2336
{
2437
case "avatar":
25-
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);
2639
break;
2740
case "saber":
28-
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);
2942
break;
3043
case "platform":
31-
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);
3245
break;
3346
case "bloq":
34-
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);
3548
break;
3649
}
3750
}
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+
}
3857

58+
return ModelSaberURLPrefix;
59+
}
3960
}
4061
}

ModAssistant/Classes/External Interfaces/Playlists.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,31 @@ public static async Task DownloadFrom(string file)
5858
int Errors = 0;
5959
int Minimum = 0;
6060
int Value = 0;
61+
string fallback = string.Empty;
6162

6263
Playlist playlist = JsonSerializer.Deserialize<Playlist>(File.ReadAllText(file));
6364
int Maximum = playlist.songs.Length;
65+
if (ModAssistant.Properties.Settings.Default.AssetsDownloadServer == "国内源@WGzeyu")
66+
{
67+
if (playlist.songs.Length > 20)
68+
{
69+
fallback = ModAssistant.Properties.Settings.Default.AssetsDownloadServer;
70+
ModAssistant.Properties.Settings.Default.AssetsDownloadServer = "默认@default";
71+
ModAssistant.Properties.Settings.Default.Save();
72+
Utils.SetMessage((string)Application.Current.FindResource("Options:PlaylistsFallback"));
73+
}
74+
}
6475

6576
foreach (Playlist.Song song in playlist.songs)
6677
{
6778
API.BeatSaver.BeatSaverMap response = new BeatSaver.BeatSaverMap();
6879
if (!string.IsNullOrEmpty(song.hash))
6980
{
70-
response = await BeatSaver.GetFromHash(song.hash, false);
81+
response = await BeatSaver.GetFromHash(song.hash, false, true);
7182
}
7283
else if (!string.IsNullOrEmpty(song.key))
7384
{
74-
response = await BeatSaver.GetFromKey(song.key, false);
85+
response = await BeatSaver.GetFromKey(song.key, false, true);
7586
}
7687
Value++;
7788

@@ -89,6 +100,12 @@ public static async Task DownloadFrom(string file)
89100
}
90101
}
91102
Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("Options:FinishedPlaylist"), Errors, playlist.playlistTitle)}");
103+
104+
if (!string.IsNullOrEmpty(fallback))
105+
{
106+
ModAssistant.Properties.Settings.Default.AssetsDownloadServer = fallback;
107+
ModAssistant.Properties.Settings.Default.Save();
108+
}
92109
}
93110

94111
private static string TextProgress(int min, int max, int value)

ModAssistant/Classes/External Interfaces/Utils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void SetMessage(string message)
2929
}
3030
}
3131

32-
public static async Task<string> DownloadAsset(string link, string folder, string fileName = null, string displayName = null, bool showNotification = true, bool beatsaver = false, bool preferContentDisposition = false)
32+
public static async Task<string> DownloadAsset(string link, string folder, string fileName = null, string displayName = null, bool showNotification = true, bool beatsaver = false, bool preferContentDisposition = false, bool fromPlaylist = false, bool fallback = false)
3333
{
3434
if (string.IsNullOrEmpty(BeatSaberPath))
3535
{
@@ -48,11 +48,11 @@ public static async Task<string> DownloadAsset(string link, string folder, strin
4848
if (beatsaver)
4949
{
5050
fileName = WebUtility.UrlDecode(Path.Combine(parentDir, fileName));
51-
await BeatSaver.Download(link, fileName);
51+
await BeatSaver.Download(link, fileName, fromPlaylist, fallback);
5252
}
5353
else
5454
{
55-
fileName = await ModAssistant.Utils.Download(link, parentDir, fileName, preferContentDisposition);
55+
fileName = await ModAssistant.Utils.Download(link, parentDir, fileName, preferContentDisposition, true, fallback);
5656
}
5757

5858
if (string.IsNullOrEmpty(displayName))

0 commit comments

Comments
 (0)