6 Commits

Author SHA1 Message Date
qhy040404
98ae857c60 revert to registry 2023-12-28 20:40:37 +08:00
qhy040404
738fe060bc code style 2023-12-28 20:34:23 +08:00
qhy040404
d3f8500ebe webview2 2023-12-28 20:06:36 +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
7 changed files with 154 additions and 17 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.6.0</version>
<version>1.9.0</version>
<authors>DGP Studio</authors>
<developmentDependency>true</developmentDependency>
<requireLicenseAcceptance>false</requireLicenseAcceptance>

View File

@@ -0,0 +1,95 @@
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace Snap.Hutao.Deployment;
internal static partial class EdgeWebView2Dependency
{
private const string EdgeWebView2DownloadUrl = "https://go.microsoft.com/fwlink/p/?LinkId=2124703";
private const string EdgeWebView2PerUserPath = @"HKEY_CURRENT_USER\Software\Microsoft\EdgeUpdate\Clients";
private const string EdgeWebView2PerMachinePath = @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients";
private const string EdgeWebView2GuidKey = "{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}";
public static async Task EnsureAsync(bool installWebView2, bool isUpdateMode)
{
if (!installWebView2 || isUpdateMode)
{
return;
}
if (Registry.GetValue(EdgeWebView2PerUserPath, EdgeWebView2GuidKey, null) is not null || Registry.GetValue(EdgeWebView2PerMachinePath, EdgeWebView2GuidKey, null) is not null)
{
Console.WriteLine("WebView2 already installed.");
return;
}
Console.WriteLine("WebView2 not found, start downloading and installing WebView2...");
await DownloadWebView2InstallerAndInstallAsync().ConfigureAwait(false);
}
private static async Task DownloadWebView2InstallerAndInstallAsync()
{
string webView2InstallerPath = Path.Combine(Path.GetTempPath(), "MicrosoftEdgeWebview2Setup.exe");
try
{
using (HttpClient httpClient = new())
{
HttpShardCopyWorkerOptions<DownloadStatus> options = new()
{
HttpClient = httpClient,
SourceUrl = EdgeWebView2DownloadUrl,
DestinationFilePath = webView2InstallerPath,
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 WebView2...");
Process installerProcess = new()
{
StartInfo = new()
{
Arguments = "/silent /install",
FileName = webView2InstallerPath,
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("-----> WebView2 Output begin -----");
installerProcess.BeginOutputReadLine();
installerProcess.BeginErrorReadLine();
await installerProcess.WaitForExitAsync().ConfigureAwait(false);
Console.WriteLine("<----- WebView2 Output end -------");
}
}
finally
{
if (File.Exists(webView2InstallerPath))
{
File.Delete(webView2InstallerPath);
}
}
static void ConsoleWriteProgress(DownloadStatus status)
{
Console.Write($"\r{status.ProgressDescription}");
}
}
}

View File

@@ -2,18 +2,25 @@
using System.CommandLine.Invocation;
using System.Diagnostics;
using System.IO;
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)
{
string? path = context.ParseResult.GetValueForOption(InvocationOptions.PackagePath);
string? name = context.ParseResult.GetValueForOption(InvocationOptions.FamilyName);
bool isUpdateMode = context.ParseResult.GetValueForOption(InvocationOptions.UpdateBehavior);
bool installWebView2 = context.ParseResult.GetValueForOption(InvocationOptions.InstallWebView2);
if (!isUpdateMode)
{
AllocConsole();
}
ArgumentException.ThrowIfNullOrEmpty(path);
@@ -29,8 +36,7 @@ internal static class Invocation
if (isUpdateMode)
{
Console.WriteLine("Exit in 10 seconds...");
await Task.Delay(10000).ConfigureAwait(false);
await ExitAsync(true).ConfigureAwait(false);
return;
}
else
@@ -42,10 +48,12 @@ internal static class Invocation
await Certificate.EnsureGlobalSignCodeSigningRootR45Async().ConfigureAwait(false);
await WindowsAppSDKDependency.EnsureAsync(path).ConfigureAwait(false);
await RunDeploymentCoreAsync(path, name).ConfigureAwait(false);
await EdgeWebView2Dependency.EnsureAsync(installWebView2, isUpdateMode).ConfigureAwait(false);
await RunDeploymentCoreAsync(path, name, isUpdateMode).ConfigureAwait(false);
await ExitAsync(isUpdateMode).ConfigureAwait(false);
}
private static async Task RunDeploymentCoreAsync(string path, string? name)
private static async Task RunDeploymentCoreAsync(string path, string? name, bool isUpdateMode)
{
try
{
@@ -76,10 +84,21 @@ internal static class Invocation
foreach (Windows.ApplicationModel.Package package in packageManager.FindPackages())
{
if (package is { DisplayName: "Snap Hutao", PublisherDisplayName: "DGP Studio" })
try
{
name = package.Id.FamilyName;
Console.WriteLine($"Package found: {name}");
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;
}
}
}
}
@@ -97,11 +116,7 @@ internal static class Invocation
ActivityId: {result.ActivityId}
ExtendedErrorCode: {result.ExtendedErrorCode}
ErrorText: {result.ErrorText}
Exit in 10 seconds...
""");
await Task.Delay(10000).ConfigureAwait(false);
}
}
catch (Exception ex)
@@ -109,11 +124,30 @@ internal static class Invocation
Console.WriteLine($"""
Exception occured:
{ex}
Exit in 10 seconds...
""");
}
}
private static async ValueTask ExitAsync(bool isUpdateMode)
{
if (!isUpdateMode)
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
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

@@ -19,4 +19,9 @@ internal static class InvocationOptions
"--update-behavior",
() => false,
"Change behavior of the tool into update mode");
public static readonly Option<bool> InstallWebView2 = new(
"--install-webview2",
() => false,
"Install WebView2 if not found.");
}

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)
{
@@ -15,6 +16,7 @@ internal static class Program
root.AddOption(InvocationOptions.PackagePath);
root.AddOption(InvocationOptions.FamilyName);
root.AddOption(InvocationOptions.UpdateBehavior);
root.AddOption(InvocationOptions.InstallWebView2);
root.SetHandler(Invocation.RunDeploymentAsync);

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>