From cf9d601b27320a9693e3efb9ca7fd62b614f66dc Mon Sep 17 00:00:00 2001 From: HolographicHat Date: Sat, 30 Nov 2024 11:56:41 +0800 Subject: [PATCH] rin --- src/GlobalVars.cs | 5 +++-- src/Program.cs | 2 +- src/Utils.cs | 30 +++++++++++++++++------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/GlobalVars.cs b/src/GlobalVars.cs index ccddb27..288004d 100644 --- a/src/GlobalVars.cs +++ b/src/GlobalVars.cs @@ -25,13 +25,14 @@ public static class GlobalVars { public const string AppVersionName = "5.2"; public const string PipeName = "YaeAchievementPipe"; - public const string BucketHost = "https://cn-cd-1259389942.file.myqcloud.com"; + public const string RinBucketHost = "https://rin.holohat.work"; + public const string SakuraBucketHost = "https://cn-cd-1259389942.file.myqcloud.com"; public static AchievementInfo AchievementInfo { get; } static GlobalVars() { Directory.CreateDirectory(DataPath); Directory.CreateDirectory(CachePath); - AchievementInfo = AchievementInfo.Parser.ParseFrom(Utils.GetBucketFileAsByteArray("schicksal/metadata")); + AchievementInfo = AchievementInfo.Parser.ParseFrom(Utils.GetBucketFile("schicksal/metadata").GetAwaiter().GetResult()); } } diff --git a/src/Program.cs b/src/Program.cs index ceac811..9819c84 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -23,7 +23,7 @@ Console.WriteLine(@"----------------------------------------------------"); AppConfig.Load(args.GetOrNull(0) ?? "auto"); Export.ExportTo = ToUIntOrNull(args.GetOrNull(1)) ?? uint.MaxValue; -CheckUpdate(ToBooleanOrFalse(args.GetOrNull(2))); +await CheckUpdate(ToBooleanOrFalse(args.GetOrNull(2))); var historyCache = new CacheFile("ExportData"); diff --git a/src/Utils.cs b/src/Utils.cs index d991fd2..35c6c13 100644 --- a/src/Utils.cs +++ b/src/Utils.cs @@ -28,30 +28,33 @@ public static class Utils { } }; - public static byte[] GetBucketFileAsByteArray(string path, bool cache = true) { + public static async Task GetBucketFile(string path, bool cache = true) { try { + return await await Task.WhenAny(GetFile(GlobalVars.RinBucketHost), GetFile(GlobalVars.SakuraBucketHost)); + } catch (Exception e) when(e is SocketException or TaskCanceledException) { + Console.WriteLine(App.NetworkError, e.Message); + Environment.Exit(-1); + return null!; + } + async Task GetFile(string host) { using var msg = new HttpRequestMessage(); msg.Method = HttpMethod.Get; - msg.RequestUri = new Uri($"{GlobalVars.BucketHost}/{path}"); + msg.RequestUri = new Uri($"{host}/{path}"); var cacheFile = new CacheFile(path); if (cache && cacheFile.Exists()) { msg.Headers.TryAddWithoutValidation("If-None-Match", $"{cacheFile.Read().Etag}"); } - using var response = CHttpClient.Send(msg); + using var response = await CHttpClient.SendAsync(msg); if (cache && response.StatusCode == HttpStatusCode.NotModified) { return cacheFile.Read().Content.ToByteArray(); } response.EnsureSuccessStatusCode(); - var responseBytes = response.Content.ReadAsByteArrayAsync().Result; + var responseBytes = await response.Content.ReadAsByteArrayAsync(); if (cache) { var etag = response.Headers.ETag!.Tag; cacheFile.Write(responseBytes, etag); } return responseBytes; - } catch (Exception e) when(e is SocketException or TaskCanceledException) { - Console.WriteLine(App.NetworkError, e.Message); - Environment.Exit(-1); - return null!; } } @@ -88,18 +91,18 @@ public static class Utils { // ReSharper disable once NotAccessedField.Local private static UpdateInfo _updateInfo = null!; - public static void CheckUpdate(bool useLocalLib) { - var info = UpdateInfo.Parser.ParseFrom(GetBucketFileAsByteArray("schicksal/version"))!; + public static async Task CheckUpdate(bool useLocalLib) { + var info = UpdateInfo.Parser.ParseFrom(await GetBucketFile("schicksal/version"))!; if (GlobalVars.AppVersionCode < info.VersionCode) { Console.WriteLine(App.UpdateNewVersion, GlobalVars.AppVersionName, info.VersionName); Console.WriteLine(App.UpdateDescription, info.Description); if (info.EnableAutoUpdate) { Console.WriteLine(App.UpdateDownloading); var tmpPath = Path.GetTempFileName(); - File.WriteAllBytes(tmpPath, GetBucketFileAsByteArray(info.PackageLink)); + await File.WriteAllBytesAsync(tmpPath, await GetBucketFile(info.PackageLink)); var updaterArgs = $"{Environment.ProcessId}|{Environment.ProcessPath}|{tmpPath}"; var updaterPath = Path.Combine(GlobalVars.DataPath, "update.exe"); - File.WriteAllBytes(updaterPath, App.Updater); + await File.WriteAllBytesAsync(updaterPath, App.Updater); ShellOpen(updaterPath, updaterArgs.ToBytes().ToBase64()); GlobalVars.PauseOnExit = false; Environment.Exit(0); @@ -113,7 +116,8 @@ public static class Utils { Console.WriteLine(@"[DEBUG] Use local native lib."); File.Copy(Path.Combine(GlobalVars.AppPath, "YaeAchievementLib.dll"), GlobalVars.LibFilePath, true); } else if (info.EnableLibDownload) { - File.WriteAllBytes(GlobalVars.LibFilePath, GetBucketFileAsByteArray("schicksal/lib.dll")); + var data = await GetBucketFile("schicksal/lib.dll"); + await File.WriteAllBytesAsync(GlobalVars.LibFilePath, data); } _updateInfo = info; }