Skip to content

Commit

Permalink
check hash if using default package path
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Oct 24, 2024
1 parent 5947280 commit 738fa7e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Snap.Hutao.Deployment.Runtime</id>
<version>1.16.3</version>
<version>1.16.4</version>
<authors>DGP Studio</authors>
<developmentDependency>true</developmentDependency>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand Down
Binary file modified src/Snap.Hutao.Deployment.Runtime/Snap.Hutao.Deployment.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Snap.Hutao.Deployment/Invocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public static async Task RunDeploymentAsync(InvocationContext context)
ArgumentException.ThrowIfNullOrEmpty(path);

Console.WriteLine($"""
Snap Hutao Deployment Tool [1.16.3]
Snap Hutao Deployment Tool [1.16.4]
PackagePath: {path}
FamilyName: {name}
------------------------------------------------------------
""");

try
{
if (!Package.EnsurePackage(path))
if (!await Package.EnsurePackageAsync(path).ConfigureAwait(false))
{
Console.WriteLine("""
未找到包文件或包文件损坏。
Expand Down
4 changes: 3 additions & 1 deletion src/Snap.Hutao.Deployment/InvocationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ namespace Snap.Hutao.Deployment;

internal static class InvocationOptions
{
public static readonly string DefaultPackagePath = Path.Combine(AppContext.BaseDirectory, "Snap.Hutao.msix");

public static readonly Option<string> PackagePath = new(
"--package-path",
() => Path.Combine(AppContext.BaseDirectory, "Snap.Hutao.msix"),
() => DefaultPackagePath,
"The path of the package to be deployed.");

public static readonly Option<string> FamilyName = new(
Expand Down
29 changes: 27 additions & 2 deletions src/Snap.Hutao.Deployment/Package.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Snap.Hutao.Deployment;

internal static class Package
{
public static bool EnsurePackage(string packagePath)
private const string HutaoPatchDownloadEndpoint = "https://api.snapgenshin.com/patch/hutao/download";

public static async Task<bool> EnsurePackageAsync(string packagePath)
{
if (!File.Exists(packagePath))
{
return false;
}

if (packagePath.Equals(InvocationOptions.DefaultPackagePath, StringComparison.OrdinalIgnoreCase))
{
using (HttpClientHandler handler = new() { AllowAutoRedirect = false })
{
using (HttpClient httpClient = new(handler))
{
using (HttpResponseMessage headResponse = await httpClient.HeadAsync(HutaoPatchDownloadEndpoint, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
{
if (!headResponse.Headers.TryGetValues("X-Checksum-Sha256", out IEnumerable<string>? checksums))
{
return false;
}

string checksum = checksums.First();
string actualChecksum = await SHA256.HashFileAsync(packagePath).ConfigureAwait(false);
return checksum.Equals(actualChecksum, StringComparison.OrdinalIgnoreCase);
}
}
}
}

try
{
using (FileStream packageStream = File.OpenRead(packagePath))
Expand All @@ -41,7 +66,7 @@ public static async Task DownloadPackageAsync(string packagePath)
HttpShardCopyWorkerOptions<DownloadStatus> options = new()
{
HttpClient = httpClient,
SourceUrl = "https://api.snapgenshin.com/patch/hutao/download",
SourceUrl = HutaoPatchDownloadEndpoint,
DestinationFilePath = packagePath,
StatusFactory = (bytesRead, totalBytes) => new DownloadStatus(bytesRead, totalBytes),
};
Expand Down
26 changes: 26 additions & 0 deletions src/Snap.Hutao.Deployment/SHA256.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Snap.Hutao.Deployment;

internal static class SHA256
{
public static async Task<string> HashFileAsync(string filePath, CancellationToken token = default)
{
using (FileStream stream = File.OpenRead(filePath))
{
return await HashAsync(stream, token).ConfigureAwait(false);
}
}

public static async Task<string> HashAsync(Stream stream, CancellationToken token = default)
{
byte[] bytes = await System.Security.Cryptography.SHA256.HashDataAsync(stream, token).ConfigureAwait(false);
return Convert.ToHexString(bytes);
}
}

0 comments on commit 738fa7e

Please sign in to comment.