From 26991c9706bdddf12c44b5aa09d2e3dd5a1133a3 Mon Sep 17 00:00:00 2001 From: Kei-Luna Date: Wed, 13 May 2026 09:00:13 +0900 Subject: [PATCH] PatchDownload --- Common/Util/PatchDownloadService.cs | 47 +++++++++++++++++++++++++++++ MikuSB.Loader/GameLaunchService.cs | 1 + version.txt | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Common/Util/PatchDownloadService.cs diff --git a/Common/Util/PatchDownloadService.cs b/Common/Util/PatchDownloadService.cs new file mode 100644 index 0000000..17e5623 --- /dev/null +++ b/Common/Util/PatchDownloadService.cs @@ -0,0 +1,47 @@ +using System.Net.Http.Headers; + +namespace MikuSB.Util; + +public static class PatchDownloadService +{ + private static readonly Logger Logger = new("PatchDownloader"); + private const string PatchRelativePath = @"Patch\MikuSB-Patch.dll"; + private const string PatchDownloadUrl = + "https://github.com/Kei-Luna/MikuSB-Patch/releases/download/MikuSB-Patch/MikuSB-Patch.dll"; + private const int DownloadTimeoutSeconds = 60; + + public static void EnsurePatchPresent() + { + var patchPath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, PatchRelativePath)); + if (File.Exists(patchPath)) + return; + + Directory.CreateDirectory(Path.GetDirectoryName(patchPath)!); + Logger.Warn($"Patch DLL not found. Downloading to {patchPath}."); + + using var client = CreateHttpClient(); + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(DownloadTimeoutSeconds)); + using var response = client.GetAsync(PatchDownloadUrl, HttpCompletionOption.ResponseHeadersRead, cts.Token) + .GetAwaiter().GetResult(); + response.EnsureSuccessStatusCode(); + + using var source = response.Content.ReadAsStreamAsync(cts.Token).GetAwaiter().GetResult(); + using var destination = File.Create(patchPath); + source.CopyTo(destination); + + Logger.Info("Patch DLL download completed."); + } + + private static HttpClient CreateHttpClient() + { + var client = new HttpClient + { + Timeout = Timeout.InfiniteTimeSpan + }; + + client.DefaultRequestHeaders.UserAgent.Add( + new ProductInfoHeaderValue("MikuSB-PatchDownloader", BuildVersion.Current)); + + return client; + } +} diff --git a/MikuSB.Loader/GameLaunchService.cs b/MikuSB.Loader/GameLaunchService.cs index 2834210..23cf723 100644 --- a/MikuSB.Loader/GameLaunchService.cs +++ b/MikuSB.Loader/GameLaunchService.cs @@ -11,6 +11,7 @@ public static class GameLaunchService public static int Launch(params string[] extraGameArguments) { ConfigManager.LoadConfig(); + PatchDownloadService.EnsurePatchPresent(); var options = LaunchOptions.FromConfig(extraGameArguments); return Launch(options); } diff --git a/version.txt b/version.txt index 5bb3a00..c24093b 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v=2.4 \ No newline at end of file +v=2.5 \ No newline at end of file