Skip to content

Commit 66820ac

Browse files
committed
启动时自动以管理员权限重启选项无法正常启动 #29
1 parent 191b8b4 commit 66820ac

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/Snap.Hutao.Remastered/Snap.Hutao.Remastered/Bootstrap.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Snap.Hutao.Remastered.Core.Security.Principal;
88
using Snap.Hutao.Remastered.Factory.Process;
99
using Snap.Hutao.Remastered.Service;
10-
using Snap.Hutao.Remastered.Service.Plugin;
1110
using Snap.Hutao.Remastered.Win32;
1211
using System.Diagnostics;
1312
using System.Runtime.CompilerServices;
@@ -35,6 +34,13 @@ public static void UseNamedPipeRedirection()
3534
[STAThread]
3635
private static void Main(string[] args)
3736
{
37+
// Check if we should restart as administrator
38+
if (ShouldRestartAsAdmin())
39+
{
40+
RestartAsAdministrator();
41+
return;
42+
}
43+
3844
if (Mutex.TryOpenExisting(LockName, out _))
3945
{
4046
return;
@@ -68,17 +74,10 @@ private static void Main(string[] args)
6874
ComWrappersSupport.InitializeComWrappers();
6975

7076
// By adding the using statement, we can dispose the injected services when closing
71-
using (ServiceProvider serviceProvider = DependencyInjection.Initialize())
77+
using (ServiceProvider serviceProvider = DependencyInjection.Initialize(true))
7278
{
7379
Thread.CurrentThread.Name = "Snap Hutao Remastered Application Main Thread";
7480

75-
// Check if we should restart as administrator
76-
if (ShouldRestartAsAdmin(serviceProvider))
77-
{
78-
RestartAsAdministrator();
79-
return;
80-
}
81-
8281
// If you hit a COMException REGDB_E_CLASSNOTREG (0x80040154) during debugging
8382
// You can delete bin and obj folder and then rebuild.
8483
// In a Desktop app this runs a message pump publicly,
@@ -112,11 +111,11 @@ private static bool IsRunningAsAdministrator()
112111
return principal.IsInRole(WindowsBuiltInRole.Administrator);
113112
}
114113

115-
private static bool ShouldRestartAsAdmin(IServiceProvider serviceProvider)
114+
private static bool ShouldRestartAsAdmin()
116115
{
117116
try
118117
{
119-
AppOptions appOptions = serviceProvider.GetRequiredService<AppOptions>();
118+
AppOptions appOptions = DependencyInjection.Initialize().GetRequiredService<AppOptions>();
120119
return !IsRunningAsAdministrator() && appOptions.AutoRestartAsAdmin.Value;
121120
}
122121
catch (Exception ex)

src/Snap.Hutao.Remastered/Snap.Hutao.Remastered/Core/DependencyInjection/DependencyInjection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Snap.Hutao.Remastered.Core.DependencyInjection;
1616

1717
public static class DependencyInjection
1818
{
19-
public static ServiceProvider Initialize()
19+
public static ServiceProvider Initialize(bool isDefault = false)
2020
{
2121
IServiceCollection services = new ServiceCollection()
2222

@@ -47,7 +47,10 @@ public static ServiceProvider Initialize()
4747

4848
ServiceProvider serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
4949

50-
Ioc.Default.ConfigureServices(serviceProvider);
50+
if (isDefault)
51+
{
52+
Ioc.Default.ConfigureServices(serviceProvider);
53+
}
5154

5255
serviceProvider.InitializeCulture();
5356
serviceProvider.InitializeNotification();

0 commit comments

Comments
 (0)