mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-06-21 10:31:22 +08:00
chore: remove some use of WinApi to avoid possible security check from thunderstore
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Diagnostics;
|
||||
using BepInEx.Configuration;
|
||||
using UXAssist.Common;
|
||||
|
||||
@@ -13,7 +13,6 @@ public static class WindowFunctions
|
||||
private const string GameWindowClass = "UnityWndClass";
|
||||
private static string _gameWindowTitle = "Dyson Sphere Program";
|
||||
|
||||
private static IntPtr _oldWndProc = IntPtr.Zero;
|
||||
private static IntPtr _gameWindowHandle = IntPtr.Zero;
|
||||
|
||||
public static ConfigEntry<int> ProcessPriority;
|
||||
@@ -36,8 +35,15 @@ public static class WindowFunctions
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
ProcessPriority.SettingChanged += (_, _) => WinApi.SetPriorityClass(WinApi.GetCurrentProcess(), ProrityFlags[ProcessPriority.Value]);
|
||||
WinApi.SetPriorityClass(WinApi.GetCurrentProcess(), ProrityFlags[ProcessPriority.Value]);
|
||||
ProcessPriority.SettingChanged += (_, _) => ApplyProcessPriority();
|
||||
ApplyProcessPriority();
|
||||
}
|
||||
|
||||
private static void ApplyProcessPriority()
|
||||
{
|
||||
// Use the managed BCL wrapper instead of a raw kernel32 SetPriorityClass P/Invoke.
|
||||
using var process = Process.GetCurrentProcess();
|
||||
process.PriorityClass = (ProcessPriorityClass)ProrityFlags[ProcessPriority.Value];
|
||||
}
|
||||
|
||||
private static string GetPriorityName(int priority)
|
||||
@@ -92,16 +98,20 @@ public static class WindowFunctions
|
||||
{
|
||||
if (_gameWindowHandle != IntPtr.Zero)
|
||||
return _gameWindowHandle;
|
||||
// Match the game window by its Unity window class plus our own process id. Matching the
|
||||
// class excludes the BepInEx console window (class "ConsoleWindowClass"), which
|
||||
// Process.MainWindowHandle would otherwise return when the console is enabled; the pid
|
||||
// check guards against other running Unity apps. Process.Id supplies the pid so no extra
|
||||
// kernel32 P/Invoke (GetCurrentProcessId/GetConsoleWindow) is needed.
|
||||
int currentProcessId;
|
||||
using (var process = Process.GetCurrentProcess())
|
||||
currentProcessId = process.Id;
|
||||
var wnd = IntPtr.Zero;
|
||||
var consoleWnd = WinApi.GetConsoleWindow();
|
||||
var currentProcessId = WinApi.GetCurrentProcessId();
|
||||
while (true)
|
||||
{
|
||||
wnd = WinApi.FindWindowEx(IntPtr.Zero, wnd, GameWindowClass, _gameWindowTitle);
|
||||
wnd = WinApi.FindWindowEx(IntPtr.Zero, wnd, GameWindowClass, null);
|
||||
if (wnd == IntPtr.Zero)
|
||||
return IntPtr.Zero;
|
||||
if (wnd == consoleWnd)
|
||||
continue;
|
||||
WinApi.GetWindowThreadProcessId(wnd, out var pid);
|
||||
if (pid == currentProcessId)
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user