7 Commits

Author SHA1 Message Date
qhy040404
d8b3f8ae34 minor fix 2024-01-12 09:24:52 +08:00
DismissedLight
da33fc0ba3 Merge pull request #4 from DGP-Studio/fix/crash 2024-01-12 09:19:54 +08:00
qhy040404
d6f5b7374d global try and enter to exit 2024-01-12 09:13:13 +08:00
qhy040404
a16fcbd2bb fix ERROR_NOT_FOUND 2023-12-28 19:34:04 +08:00
Lightczx
cd6a3bbfd9 1.8.0 2023-12-28 14:01:46 +08:00
Lightczx
9d71ac82b7 fix ERROR_MRM_MAP_NOT_FOUND 2023-12-27 17:28:52 +08:00
Lightczx
21618e5583 certificates and windows app runtime 2023-12-27 16:52:08 +08:00
10 changed files with 363 additions and 83 deletions

View File

@@ -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.11.0</version>
<authors>DGP Studio</authors>
<developmentDependency>true</developmentDependency>
<requireLicenseAcceptance>false</requireLicenseAcceptance>

View File

@@ -0,0 +1,45 @@
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("Certificate [GlobalSign Code Signing Root R45] found");
return;
}
Console.WriteLine("Required Certificate [GlobalSign Code Signing Root R45] not found, download 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));
}
}
}
}

View File

@@ -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; }

View File

@@ -2,13 +2,13 @@
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,6 +16,11 @@ 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($"""
@@ -30,108 +35,120 @@ internal static class Invocation
if (isUpdateMode)
{
Console.WriteLine("Exit in 10 seconds...");
await Task.Delay(10000);
await ExitAsync(true).ConfigureAwait(false);
return;
}
else
{
Console.WriteLine("Start downloading package...");
await DownloadPackageAsync(path);
await PackageDownload.DownloadPackageAsync(path).ConfigureAwait(false);
}
}
try
{
Console.WriteLine("Initializing PackageManager...");
PackageManager packageManager = new();
AddPackageOptions addPackageOptions = new()
{
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...
""");
await Task.Delay(10000);
}
await Certificate.EnsureGlobalSignCodeSigningRootR45Async().ConfigureAwait(false);
await WindowsAppSDKDependency.EnsureAsync(path).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("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 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) ;
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();
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace Snap.Hutao.Deployment;
internal static class PackageDownload
{
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}");
}
}
}

View 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);
}
}

View File

@@ -1,9 +1,10 @@
using System.CommandLine;
using System.Runtime.InteropServices;
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)
{

View File

@@ -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>

View File

@@ -0,0 +1,171 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.Management.Deployment;
namespace Snap.Hutao.Deployment;
internal static partial class WindowsAppSDKDependency
{
private const string SDKInstallerDownloadFormat = "https://aka.ms/windowsappsdk/{0}/{1}/windowsappruntimeinstall-x64.exe";
public static async Task EnsureAsync(string packagePath)
{
using FileStream packageStream = File.OpenRead(packagePath);
using ZipArchive package = new(packageStream, ZipArchiveMode.Read);
(string packageName, string msixVersion) = await ExtractRuntimePackageNameAndMsixMinVersionFromAppManifestAsync(package).ConfigureAwait(false);
if (string.IsNullOrEmpty(packageName) || string.IsNullOrEmpty(msixVersion))
{
Console.WriteLine("No Windows App Runtime version found in Msix/AppxManifest.xml");
return;
}
if (CheckRuntimeInstalled(packageName, msixVersion))
{
return;
}
string sdkVersion = await ExtractSDKVersionFromDepsJsonAsync(package).ConfigureAwait(false);
if (string.IsNullOrEmpty(sdkVersion))
{
Console.WriteLine("No Windows App SDK version found in Msix/Snap.Hutao.deps.json");
return;
}
Console.WriteLine("Start downloading SDK installer...");
await DownloadWindowsAppRuntimeInstallAndInstallAsync(sdkVersion).ConfigureAwait(false);
}
private static async Task<string> ExtractSDKVersionFromDepsJsonAsync(ZipArchive package)
{
ZipArchiveEntry? depsJson = package.GetEntry("Snap.Hutao.deps.json");
ArgumentNullException.ThrowIfNull(depsJson);
using (StreamReader reader = new(depsJson.Open()))
{
while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line)
{
if (WindowsAppSDKVersion().Match(line) is { Success: true } match)
{
string sdkVersion = match.Groups[1].Value;
Console.WriteLine($"Using Windows App SDK version: {sdkVersion}");
return sdkVersion;
}
}
}
return string.Empty;
}
private static bool CheckRuntimeInstalled(string packageName, string msixVersion)
{
Version msixMinVersion = new(msixVersion);
foreach (Windows.ApplicationModel.Package installed in new PackageManager().FindPackages())
{
if (installed.Id.Name == packageName && installed.Id.Version.ToVersion() >= msixMinVersion)
{
return true;
}
}
return false;
}
private static async Task DownloadWindowsAppRuntimeInstallAndInstallAsync(string version)
{
string sdkInstallerPath = Path.Combine(Path.GetTempPath(), "windowsappruntimeinstall-x64.exe");
try
{
using (HttpClient httpClient = new())
{
HttpShardCopyWorkerOptions<DownloadStatus> options = new()
{
HttpClient = httpClient,
SourceUrl = string.Format(SDKInstallerDownloadFormat, MajorMinorVersion().Match(version).Value, version),
DestinationFilePath = sdkInstallerPath,
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);
}
}
Console.WriteLine("Start installing SDK...");
Process installerProcess = new()
{
StartInfo = new()
{
FileName = sdkInstallerPath,
RedirectStandardOutput = true,
RedirectStandardError = true,
},
};
using (installerProcess)
{
installerProcess.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
installerProcess.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);
installerProcess.Start();
Console.WriteLine("-----> WindowsAppRuntimeInstall Output begin -----");
installerProcess.BeginOutputReadLine();
installerProcess.BeginErrorReadLine();
await installerProcess.WaitForExitAsync().ConfigureAwait(false);
Console.WriteLine("<----- WindowsAppRuntimeInstall Output end -------");
}
}
finally
{
if (File.Exists(sdkInstallerPath))
{
File.Delete(sdkInstallerPath);
}
}
static void ConsoleWriteProgress(DownloadStatus status)
{
Console.Write($"\r{status.ProgressDescription}");
}
}
private static async Task<(string PackageName, string MsixVersion)> ExtractRuntimePackageNameAndMsixMinVersionFromAppManifestAsync(ZipArchive package)
{
ZipArchiveEntry? appxManifest = package.GetEntry("AppxManifest.xml");
ArgumentNullException.ThrowIfNull(appxManifest);
using (StreamReader reader = new(appxManifest.Open()))
{
while (await reader.ReadLineAsync().ConfigureAwait(false) is { } line)
{
if (WindowsAppRuntimeMsixMinVersion().Match(line) is { Success: true } match)
{
string packageName = match.Groups[1].Value;
string msixVersion = match.Groups[2].Value;
Console.WriteLine($"Using {packageName} version: {msixVersion}");
return (packageName, msixVersion);
}
}
}
return (string.Empty, string.Empty);
}
[GeneratedRegex("<PackageDependency Name=\"(Microsoft\\.WindowsAppRuntime.+?)\" MinVersion=\"(.+?)\"")]
private static partial Regex WindowsAppRuntimeMsixMinVersion();
[GeneratedRegex("\"Microsoft\\.WindowsAppSDK\": \"(.+?)\",")]
private static partial Regex WindowsAppSDKVersion();
[GeneratedRegex(@"\d+\.\d+")]
private static partial Regex MajorMinorVersion();
}