better-genshin-impact/BetterGenshinImpact/Helpers/RuntimeHelper.cs

171 lines
5.1 KiB
C#
Raw Normal View History

2023-11-26 02:01:13 +08:00
using BetterGenshinImpact.Core.Config;
2024-11-04 03:45:08 +08:00
using BetterGenshinImpact.GameTask;
2023-11-26 02:01:13 +08:00
using Microsoft.Extensions.Hosting;
using System;
using System.ComponentModel;
using System.Diagnostics;
2024-08-14 16:03:43 +08:00
using System.IO;
2023-11-26 02:01:13 +08:00
using System.Linq;
using System.Security.Principal;
2024-08-14 16:03:43 +08:00
using System.Text;
2023-11-26 02:01:13 +08:00
using System.Threading;
using System.Threading.Tasks;
2024-11-04 03:45:08 +08:00
using System.Windows;
using System.Windows.Interop;
2023-11-26 02:01:13 +08:00
namespace BetterGenshinImpact.Helpers;
internal static class RuntimeHelper
{
public static bool IsElevated { get; } = GetElevated();
public static bool IsDebuggerAttached => Debugger.IsAttached;
public static bool IsDesignMode { get; } = GetDesignMode();
2024-04-06 12:00:14 +08:00
public static bool IsDebug =>
#if DEBUG
true;
2024-04-06 12:00:14 +08:00
#else
false;
#endif
2023-11-26 02:01:13 +08:00
private static bool GetElevated()
{
using WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static bool GetDesignMode()
{
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
return true;
}
else if (Process.GetCurrentProcess().ProcessName == "devenv")
{
return true;
}
return false;
}
public static void EnsureElevated()
{
if (!IsElevated)
{
RestartAsElevated();
}
}
public static string ReArguments()
{
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
for (int i = default; i < args.Length; i++)
{
args[i] = $@"""{args[i]}""";
}
return string.Join(" ", args);
}
public static void RestartAsElevated(string fileName = null!, string dir = null!, string args = null!, int? exitCode = null, bool forced = false)
{
try
{
ProcessStartInfo startInfo = new()
{
UseShellExecute = true,
WorkingDirectory = dir ?? Global.StartUpPath,
FileName = fileName ?? "BetterGI.exe",
Arguments = args ?? ReArguments(),
Verb = "runas"
};
try
{
_ = Process.Start(startInfo);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
2024-08-20 23:43:38 +08:00
MessageBox.Error("以管理员权限启动 BetterGI 失败,非管理员权限下所有模拟操作功能均不可用!\r\n请尝试 右键 —— 以管理员身份运行 的方式启动 BetterGI");
2024-04-06 12:00:14 +08:00
return;
2023-11-26 02:01:13 +08:00
}
}
catch (Win32Exception)
{
return;
}
if (forced)
{
Process.GetCurrentProcess().Kill();
}
Environment.Exit(exitCode ?? 'r' + 'u' + 'n' + 'a' + 's');
}
public static void CheckSingleInstance(string instanceName, Action<bool> callback = null!)
{
EventWaitHandle? handle;
try
{
handle = EventWaitHandle.OpenExisting(instanceName);
handle.Set();
callback?.Invoke(false);
Environment.Exit(0xFFFF);
}
catch (WaitHandleCannotBeOpenedException)
{
callback?.Invoke(true);
handle = new EventWaitHandle(false, EventResetMode.AutoReset, instanceName);
}
2024-08-14 11:13:40 +08:00
2024-11-04 03:45:08 +08:00
_ = Task.Factory.StartNew(() =>
2023-11-26 02:01:13 +08:00
{
while (handle.WaitOne())
{
2024-11-04 03:45:08 +08:00
Application.Current.Dispatcher?.BeginInvoke(() =>
2023-11-26 02:01:13 +08:00
{
2024-11-04 03:45:08 +08:00
Application.Current.MainWindow?.Show();
Application.Current.MainWindow?.Activate();
SystemControl.RestoreWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle);
2023-11-26 02:01:13 +08:00
});
}
2024-11-04 03:45:08 +08:00
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);
2023-11-26 02:01:13 +08:00
}
2024-08-14 16:03:43 +08:00
public static void CheckIntegration()
{
2024-08-14 20:56:21 +08:00
if (!Directory.Exists(Global.Absolute("Assets")) || !Directory.Exists(Global.Absolute("GameTask")))
2024-08-14 16:03:43 +08:00
{
StringBuilder stringBuilder = new("发现有关键文件缺失,");
stringBuilder.Append(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) == Global.StartUpPath
2024-08-17 18:00:03 +08:00
? "请不要把主程序exe文件剪切到桌面。正确的做法请右键点击主程序在弹出的菜单中选择“发送到”选项然后选择“桌面创建快捷方式”。"
2024-08-14 16:03:43 +08:00
: "请重新安装软件");
2024-08-20 23:43:38 +08:00
MessageBox.Warning(stringBuilder.ToString());
2024-08-14 16:03:43 +08:00
Environment.Exit(0xFFFF);
}
}
2023-11-26 02:01:13 +08:00
}
internal static class RuntimeExtension
{
public static IHostBuilder UseElevated(this IHostBuilder app)
{
RuntimeHelper.EnsureElevated();
return app;
}
public static IHostBuilder UseSingleInstance(this IHostBuilder self, string instanceName, Action<bool> callback = null!)
{
RuntimeHelper.CheckSingleInstance(instanceName, callback);
return self;
}
2024-08-14 16:03:43 +08:00
public static IHostBuilder CheckIntegration(this IHostBuilder app)
{
2024-08-14 18:01:02 +08:00
RuntimeHelper.CheckIntegration();
2024-08-14 16:03:43 +08:00
return app;
}
2023-11-26 02:01:13 +08:00
}