Skip to content

Commit

Permalink
fix the issue of switching honkai3rd client
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jan 29, 2024
1 parent f6e2851 commit c3fb7bb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
40 changes: 40 additions & 0 deletions src/Starward/Pages/SwitchClientPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ private async Task GetDownloadFilesAsync(CancellationToken cancellationToken)
toPkgVersions = await GetPkgVersionsAsync($"{toGameResource.Game.Latest.DecompressedPath.TrimEnd('/')}/pkg_version", cancellationToken);
removeFiles = fromPkgVersions.ExceptBy(toPkgVersions.Select(x => x.MD5), x => x.MD5).ToList();
addFiles = toPkgVersions.ExceptBy(fromPkgVersions.Select(x => x.MD5), x => x.MD5).ToList();

if (ToGameBiz.ToGame() is GameBiz.Honkai3rd)
{
var bh3base_from = await GetContentLengthAndMd5Async($"{fromGameResource.Game.Latest.DecompressedPath.TrimEnd('/')}/BH3Base.dll", cancellationToken);
var bh3base_to = await GetContentLengthAndMd5Async($"{toGameResource.Game.Latest.DecompressedPath.TrimEnd('/')}/BH3Base.dll", cancellationToken);
removeFiles.Add(new DownloadFileTask
{
FileName = "BH3Base.dll",
MD5 = bh3base_from.Md5,
Size = bh3base_from.Length ?? 0,
});
addFiles.Add(new DownloadFileTask
{
FileName = "BH3Base.dll",
MD5 = bh3base_to.Md5,
Size = bh3base_to.Length ?? 0,
});
}

if (ToGameBiz.IsBilibiliServer() && toGameResource.Sdk is GameSDK sdk)
{
addFiles.Add(new DownloadFileTask
Expand Down Expand Up @@ -693,6 +712,27 @@ public async Task<List<DownloadFileTask>> GetPkgVersionsAsync(string url, Cancel



public async Task<(long? Length, string Md5)> GetContentLengthAndMd5Async(string url, CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(HttpMethod.Get, url)
{
Version = HttpVersion.Version11,
};
var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
long? contentLength = response.Content.Headers.ContentLength;
string md5 = "";
if (response.Headers.TryGetValues("ETag", out var etags))
{
md5 = etags.FirstOrDefault() ?? "";
}
string contentMD5 = md5.Trim('"');
return (contentLength, contentMD5);
}




public class TargetGameBiz
{

Expand Down
38 changes: 24 additions & 14 deletions src/Starward/Services/InstallGame/Honkai3rdInstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using Starward.Core;
using Starward.Core.Launcher;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -123,30 +124,39 @@ protected async Task DownloadBH3BaseAsync(CancellationToken cancellationToken)
TotalCount = 1;
progressCount = 0;

string BH3Base = Path.Combine(InstallPath, "BH3Base.dll");
string BH3Base_tmp = BH3Base + "_tmp";
if (File.Exists(BH3Base))
{
File.Delete(BH3Base);
}
if (File.Exists(BH3Base_tmp))
{
File.Delete(BH3Base_tmp);
}

string prefix = launcherGameResource.Game.Latest.DecompressedPath;
string url = $"{prefix}/BH3Base.dll";
TotalBytes = await GetContentLengthAsync(url).ConfigureAwait(false) ?? 0;

var request = new HttpRequestMessage(HttpMethod.Get, url)
{
Version = HttpVersion.Version11,
};
var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
long? contentLength = response.Content.Headers.ContentLength;
string md5 = "";
if (response.Headers.TryGetValues("ETag", out var etags))
{
md5 = etags.FirstOrDefault() ?? "";
}
string contentMD5 = md5.Trim('"');
var task = new DownloadFileTask
{
FileName = "BH3Base.dll",
MD5 = "",
MD5 = contentMD5,
Size = TotalBytes,
DownloadSize = 0,
Url = url,
IsSegment = false,
};

bool success = await VerifyFileAsync(task, cancellationToken).ConfigureAwait(false);
if (success)
{
return;
}

TotalBytes = contentLength ?? 0;
await DownloadFileAsync(task, cancellationToken, noTmp: true).ConfigureAwait(false);
}

Expand Down

0 comments on commit c3fb7bb

Please sign in to comment.