mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.Deployment.git
synced 2025-11-19 21:08:45 +08:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b81ef222bd | ||
|
|
2f6275e165 | ||
|
|
748f3e19e1 | ||
|
|
e31f23ec7c | ||
|
|
f39463f0d3 | ||
|
|
de074e40e0 | ||
|
|
4cf4c6caca | ||
|
|
02dae0e199 | ||
|
|
291c754deb | ||
|
|
a4fd59df08 | ||
|
|
39ccb73e2f | ||
|
|
ae96cdf793 | ||
|
|
0571c386e9 | ||
|
|
95d7a85494 | ||
|
|
d8b3f8ae34 | ||
|
|
da33fc0ba3 | ||
|
|
d6f5b7374d | ||
|
|
a16fcbd2bb | ||
|
|
cd6a3bbfd9 | ||
|
|
9d71ac82b7 | ||
|
|
21618e5583 |
5
.github/workflows/Publish.yml
vendored
5
.github/workflows/Publish.yml
vendored
@@ -7,7 +7,7 @@ on:
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: windows-latest
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
@@ -18,9 +18,6 @@ jobs:
|
||||
with:
|
||||
dotnet-version: '8.x'
|
||||
|
||||
- name: Build Tool
|
||||
run: dotnet publish src/Snap.Hutao.Deployment/Snap.Hutao.Deployment.csproj
|
||||
|
||||
- name: Pack
|
||||
run: dotnet pack src/Snap.Hutao.Deployment.Runtime/Snap.Hutao.Deployment.Runtime.csproj
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Snap.Hutao.Deployment.Runtime</id>
|
||||
<version>1.5.0</version>
|
||||
<version>1.16.1</version>
|
||||
<authors>DGP Studio</authors>
|
||||
<developmentDependency>true</developmentDependency>
|
||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||
|
||||
Binary file not shown.
52
src/Snap.Hutao.Deployment/Certificate.cs
Normal file
52
src/Snap.Hutao.Deployment/Certificate.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Snap.Hutao.Deployment;
|
||||
|
||||
internal static class Certificate
|
||||
{
|
||||
private const string CertificateName = "GlobalSign Code Signing Root R45";
|
||||
private const string CertificateUrl = "https://secure.globalsign.com/cacert/codesigningrootr45.crt";
|
||||
|
||||
public static async Task EnsureGlobalSignCodeSigningRootR45Async()
|
||||
{
|
||||
using (X509Store store = new(StoreName.Root, StoreLocation.LocalMachine))
|
||||
{
|
||||
store.Open(OpenFlags.ReadWrite);
|
||||
if (store.Certificates.Any(cert => cert.FriendlyName == CertificateName))
|
||||
{
|
||||
Console.WriteLine("""
|
||||
已找到证书 [GlobalSign Code Signing Root R45]
|
||||
Certificate [GlobalSign Code Signing Root R45] found
|
||||
""");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("""
|
||||
无法找到所需证书 [GlobalSign Code Signing Root R45],正在从 GlobalSign 下载
|
||||
Required Certificate [GlobalSign Code Signing Root R45] not found, downloading from GlobalSign
|
||||
""");
|
||||
|
||||
using (HttpClient httpClient = new())
|
||||
{
|
||||
byte[] rawData = await httpClient.GetByteArrayAsync(CertificateUrl).ConfigureAwait(false);
|
||||
|
||||
Console.WriteLine("""
|
||||
正在向 本地计算机/受信任的根证书颁发机构 添加证书
|
||||
如果你无法理解弹窗中的文本,请点击 [是]
|
||||
|
||||
Adding certificate to LocalMachine/ThirdParty Root CA store,
|
||||
please click [yes] on the [Security Waring] dialog
|
||||
|
||||
关于更多安全信息,请查看下方的网址
|
||||
For more security information, please visit the url down below
|
||||
https://support.globalsign.com/ca-certificates/root-certificates/globalsign-root-certificates
|
||||
""");
|
||||
store.Add(new X509Certificate2(rawData));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
namespace Snap.Hutao.Deployment;
|
||||
|
||||
internal sealed class PackageDownloadStatus
|
||||
internal sealed class DownloadStatus
|
||||
{
|
||||
public PackageDownloadStatus(long bytesRead, long totalBytes)
|
||||
public DownloadStatus(long bytesRead, long totalBytes)
|
||||
{
|
||||
ProgressDescription = bytesRead != totalBytes
|
||||
? $"Download Progress: {ToFileSizeString(bytesRead),8}/{ToFileSizeString(totalBytes),8} | {(double)bytesRead / totalBytes,8:P3}"
|
||||
: "Download Completed\n";
|
||||
: "\nDownload Completed\n";
|
||||
}
|
||||
|
||||
public string ProgressDescription { get; }
|
||||
@@ -55,7 +55,12 @@ internal sealed class HttpShardCopyWorker<TStatus> : IDisposable
|
||||
public Task CopyAsync(IProgress<TStatus> progress, CancellationToken token = default)
|
||||
{
|
||||
ShardProgress shardProgress = new(progress, statusFactory, contentLength);
|
||||
return Parallel.ForEachAsync(shards, token, (shard, token) => CopyShardAsync(shard, shardProgress, token));
|
||||
ParallelOptions options = new()
|
||||
{
|
||||
MaxDegreeOfParallelism = Math.Clamp(Environment.ProcessorCount, 2, 6),
|
||||
CancellationToken = token,
|
||||
};
|
||||
return Parallel.ForEachAsync(shards, options, (shard, token) => CopyShardAsync(shard, shardProgress, token));
|
||||
|
||||
async ValueTask CopyShardAsync(Shard shard, IProgress<ShardStatus> progress, CancellationToken token)
|
||||
{
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
using System;
|
||||
using System.CommandLine.Invocation;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Management.Deployment;
|
||||
|
||||
namespace Snap.Hutao.Deployment;
|
||||
|
||||
internal static class Invocation
|
||||
internal static partial class Invocation
|
||||
{
|
||||
public static async Task RunDeploymentAsync(InvocationContext context)
|
||||
{
|
||||
@@ -16,122 +15,166 @@ internal static class Invocation
|
||||
string? name = context.ParseResult.GetValueForOption(InvocationOptions.FamilyName);
|
||||
bool isUpdateMode = context.ParseResult.GetValueForOption(InvocationOptions.UpdateBehavior);
|
||||
|
||||
if (!isUpdateMode)
|
||||
{
|
||||
AllocConsole();
|
||||
}
|
||||
|
||||
ArgumentException.ThrowIfNullOrEmpty(path);
|
||||
|
||||
Console.WriteLine($"""
|
||||
Snap Hutao Deployment Tool [1.16.1]
|
||||
PackagePath: {path}
|
||||
FamilyName: {name}
|
||||
------------------------------------------------------------
|
||||
""");
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
Console.WriteLine($"Package file not found.");
|
||||
|
||||
if (isUpdateMode)
|
||||
{
|
||||
Console.WriteLine("Exit in 10 seconds...");
|
||||
await Task.Delay(10000);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Start downloading package...");
|
||||
await DownloadPackageAsync(path);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Initializing PackageManager...");
|
||||
PackageManager packageManager = new();
|
||||
AddPackageOptions addPackageOptions = new()
|
||||
if (!Package.EnsurePackage(path))
|
||||
{
|
||||
ForceAppShutdown = true,
|
||||
RetainFilesOnFailure = true,
|
||||
StageInPlace = true,
|
||||
};
|
||||
|
||||
Console.WriteLine("Start deploying...");
|
||||
IProgress<DeploymentProgress> progress = new Progress<DeploymentProgress>(p =>
|
||||
{
|
||||
Console.WriteLine($"[Deploying]: State: {p.state} Progress: {p.percentage}%");
|
||||
});
|
||||
DeploymentResult result = await packageManager.AddPackageByUriAsync(new Uri(path), addPackageOptions).AsTask(progress);
|
||||
|
||||
if (result.IsRegistered)
|
||||
{
|
||||
Console.WriteLine("Package deployed.");
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
Console.WriteLine("FamilyName not provided, enumerating packages.");
|
||||
|
||||
foreach (Windows.ApplicationModel.Package package in packageManager.FindPackages())
|
||||
{
|
||||
|
||||
if (package is { DisplayName: "Snap Hutao", PublisherDisplayName: "DGP Studio" })
|
||||
{
|
||||
name = package.Id.FamilyName;
|
||||
Console.WriteLine($"Package found: {name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Starting app...");
|
||||
Process.Start(new ProcessStartInfo()
|
||||
{
|
||||
UseShellExecute = true,
|
||||
FileName = $@"shell:AppsFolder\{name}!App",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"""
|
||||
ActivityId: {result.ActivityId}
|
||||
ExtendedErrorCode: {result.ExtendedErrorCode}
|
||||
ErrorText: {result.ErrorText}
|
||||
|
||||
Exit in 10 seconds...
|
||||
Console.WriteLine("""
|
||||
未找到包文件或包文件损坏。
|
||||
Package file not found or corrupted.
|
||||
""");
|
||||
|
||||
await Task.Delay(10000);
|
||||
if (isUpdateMode)
|
||||
{
|
||||
await ExitAsync(true).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("""
|
||||
开始下载包文件...
|
||||
Start downloading package...
|
||||
""");
|
||||
await Package.DownloadPackageAsync(path).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
await Certificate.EnsureGlobalSignCodeSigningRootR45Async().ConfigureAwait(false);
|
||||
await RunDeploymentCoreAsync(path, name, isUpdateMode).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"""
|
||||
Exception occured:
|
||||
{ex}
|
||||
|
||||
Exit in 10 seconds...
|
||||
""");
|
||||
|
||||
await Task.Delay(10000);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await ExitAsync(isUpdateMode).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task DownloadPackageAsync(string packagePath)
|
||||
private static async Task RunDeploymentCoreAsync(string path, string? name, bool isUpdateMode)
|
||||
{
|
||||
using (HttpClient httpClient = new())
|
||||
Console.WriteLine("""
|
||||
初始化 PackageManager...
|
||||
Initializing PackageManager...
|
||||
""");
|
||||
PackageManager packageManager = new();
|
||||
AddPackageOptions addPackageOptions = new()
|
||||
{
|
||||
HttpShardCopyWorkerOptions<PackageDownloadStatus> options = new()
|
||||
{
|
||||
HttpClient = httpClient,
|
||||
SourceUrl = "https://api.snapgenshin.com/patch/hutao/download",
|
||||
DestinationFilePath = packagePath,
|
||||
StatusFactory = (bytesRead, totalBytes) => new PackageDownloadStatus(bytesRead, totalBytes),
|
||||
};
|
||||
ForceAppShutdown = true,
|
||||
RetainFilesOnFailure = true,
|
||||
};
|
||||
|
||||
using (HttpShardCopyWorker<PackageDownloadStatus> worker = await HttpShardCopyWorker<PackageDownloadStatus>.CreateAsync(options).ConfigureAwait(false))
|
||||
Console.WriteLine("""
|
||||
开始部署...
|
||||
Start deploying...
|
||||
""");
|
||||
IProgress<DeploymentProgress> progress = new Progress<DeploymentProgress>(p =>
|
||||
{
|
||||
Console.WriteLine($"[Deploying]: State: {p.state} Progress: {p.percentage}%");
|
||||
});
|
||||
DeploymentResult result = await packageManager
|
||||
.AddPackageByUriAsync(new Uri(path), addPackageOptions)
|
||||
.AsTask(progress)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (result.IsRegistered)
|
||||
{
|
||||
Console.WriteLine("""
|
||||
包部署成功。
|
||||
Package deployed.
|
||||
""");
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
Progress<PackageDownloadStatus> progress = new(ConsoleWriteProgress);
|
||||
await worker.CopyAsync(progress).ConfigureAwait(false);
|
||||
Console.WriteLine("""
|
||||
未提供 FamilyName,正在枚举包。
|
||||
FamilyName not provided, enumerating packages.
|
||||
""");
|
||||
|
||||
foreach (Windows.ApplicationModel.Package package in packageManager.FindPackages())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (package is { DisplayName: "Snap Hutao", PublisherDisplayName: "DGP Studio" })
|
||||
{
|
||||
name = package.Id.FamilyName;
|
||||
Console.WriteLine($"Package found: {name}");
|
||||
}
|
||||
}
|
||||
catch (COMException ex)
|
||||
{
|
||||
// ERROR_MRM_MAP_NOT_FOUND or ERROR_NOT_FOUND
|
||||
if (ex.HResult is not (unchecked((int)0x80073B1F) or unchecked((int)0x80070490)))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ConsoleWriteProgress(PackageDownloadStatus status)
|
||||
Console.WriteLine("""
|
||||
正在启动应用...
|
||||
Starting app...
|
||||
""");
|
||||
Process.Start(new ProcessStartInfo()
|
||||
{
|
||||
UseShellExecute = true,
|
||||
FileName = $@"shell:AppsFolder\{name}!App",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Write($"\r{status.ProgressDescription}");
|
||||
Console.WriteLine($"""
|
||||
ActivityId: {result.ActivityId}
|
||||
ExtendedErrorCode: {result.ExtendedErrorCode}
|
||||
ErrorText: {result.ErrorText}
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
private static async ValueTask ExitAsync(bool isUpdateMode)
|
||||
{
|
||||
if (!isUpdateMode)
|
||||
{
|
||||
Console.WriteLine("""
|
||||
按下回车键退出...
|
||||
Press enter to exit...
|
||||
""");
|
||||
while (Console.ReadKey(true).Key != ConsoleKey.Enter)
|
||||
{
|
||||
//Pending enter key
|
||||
}
|
||||
FreeConsole();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Exit in 10 seconds...");
|
||||
await Task.Delay(10000).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
[LibraryImport("kernel32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static partial bool AllocConsole();
|
||||
|
||||
[LibraryImport("kernel32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static partial bool FreeConsole();
|
||||
}
|
||||
59
src/Snap.Hutao.Deployment/Package.cs
Normal file
59
src/Snap.Hutao.Deployment/Package.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Snap.Hutao.Deployment;
|
||||
|
||||
internal static class Package
|
||||
{
|
||||
public static bool EnsurePackage(string packagePath)
|
||||
{
|
||||
if (!File.Exists(packagePath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (FileStream packageStream = File.OpenRead(packagePath))
|
||||
{
|
||||
using (new ZipArchive(packageStream, ZipArchiveMode.Read))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
File.Delete(packagePath);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task DownloadPackageAsync(string packagePath)
|
||||
{
|
||||
using (HttpClient httpClient = new())
|
||||
{
|
||||
HttpShardCopyWorkerOptions<DownloadStatus> options = new()
|
||||
{
|
||||
HttpClient = httpClient,
|
||||
SourceUrl = "https://api.snapgenshin.com/patch/hutao/download",
|
||||
DestinationFilePath = packagePath,
|
||||
StatusFactory = (bytesRead, totalBytes) => new DownloadStatus(bytesRead, totalBytes),
|
||||
};
|
||||
|
||||
using (HttpShardCopyWorker<DownloadStatus> worker = await HttpShardCopyWorker<DownloadStatus>.CreateAsync(options).ConfigureAwait(false))
|
||||
{
|
||||
Progress<DownloadStatus> progress = new(ConsoleWriteProgress);
|
||||
await worker.CopyAsync(progress).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void ConsoleWriteProgress(DownloadStatus status)
|
||||
{
|
||||
Console.Write($"\r{status.ProgressDescription}");
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/Snap.Hutao.Deployment/PackageVersionExtension.cs
Normal file
12
src/Snap.Hutao.Deployment/PackageVersionExtension.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
namespace Snap.Hutao.Deployment;
|
||||
|
||||
internal static class PackageVersionExtension
|
||||
{
|
||||
public static Version ToVersion(this PackageVersion packageVersion)
|
||||
{
|
||||
return new Version(packageVersion.Major, packageVersion.Minor, packageVersion.Build, packageVersion.Revision);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Snap.Hutao.Deployment;
|
||||
|
||||
internal static class Program
|
||||
internal static partial class Program
|
||||
{
|
||||
internal static async Task<int> Main(string[] args)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
@@ -12,6 +12,7 @@
|
||||
<SelfContained>true</SelfContained>
|
||||
<DebugType>embedded</DebugType>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -20,6 +21,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
|
||||
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user