1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2026-06-21 08:41:10 +08:00

chore: remove some use of WinApi to avoid possible security check from thunderstore

This commit is contained in:
2026-06-16 00:59:20 +08:00
parent 33921e7177
commit 63aaceda6f
2 changed files with 23 additions and 44 deletions
+4 -35
View File
@@ -45,7 +45,6 @@ public static class WinApi
public const int GWLP_ID = -12; public const int GWLP_ID = -12;
public const int GWL_STYLE = -16; public const int GWL_STYLE = -16;
public const int GWLP_USERDATA = -21; public const int GWLP_USERDATA = -21;
public const int GWLP_WNDPROC = -4;
public const int DWLP_DLGPROC = 0x4; public const int DWLP_DLGPROC = 0x4;
public const int DWLP_MSGRESULT = 0; public const int DWLP_MSGRESULT = 0;
public const int DWLP_USER = 0x8; public const int DWLP_USER = 0x8;
@@ -99,10 +98,11 @@ public static class WinApi
#region Functions #region Functions
public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport("user32", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
[DllImport("kernel32", ExactSpelling = true)] [DllImport("user32", ExactSpelling = true, SetLastError = true)]
public static extern int GetLastError(); public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int lpdwProcessId);
[DllImport("user32", CharSet = CharSet.Unicode)] [DllImport("user32", CharSet = CharSet.Unicode)]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex); public static extern int GetWindowLong(IntPtr hwnd, int nIndex);
@@ -110,12 +110,6 @@ public static class WinApi
[DllImport("user32", CharSet = CharSet.Unicode)] [DllImport("user32", CharSet = CharSet.Unicode)]
public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[DllImport("user32", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
[DllImport("user32", ExactSpelling = true, SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int lpdwProcessId);
[DllImport("user32", ExactSpelling = true)] [DllImport("user32", ExactSpelling = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out Rect lpRect); public static extern bool GetWindowRect(IntPtr hwnd, out Rect lpRect);
@@ -125,33 +119,8 @@ public static class WinApi
[DllImport("user32", CharSet = CharSet.Unicode)] [DllImport("user32", CharSet = CharSet.Unicode)]
public static extern bool SetWindowText(IntPtr hwnd, string lpString); public static extern bool SetWindowText(IntPtr hwnd, string lpString);
[DllImport("user32", ExactSpelling = true)]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32", ExactSpelling = true)] [DllImport("user32", ExactSpelling = true)]
public static extern IntPtr MonitorFromRect([In] ref Rect lpRect, uint dwFlags); public static extern IntPtr MonitorFromRect([In] ref Rect lpRect, uint dwFlags);
[DllImport("kernel32", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetCurrentProcess();
[DllImport("kernel32", ExactSpelling = true)]
public static extern int GetCurrentProcessId();
[DllImport("kernel32", ExactSpelling = true)]
public static extern IntPtr GetConsoleWindow();
// GetPriorityClass and SetPriorityClass
[DllImport("kernel32", ExactSpelling = true, SetLastError = true)]
public static extern int GetPriorityClass(IntPtr hProcess);
[DllImport("kernel32", ExactSpelling = true, SetLastError = true)]
public static extern bool SetPriorityClass(IntPtr hProcess, int dwPriorityClass);
[DllImport("user32", CharSet = CharSet.Unicode)]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32", CharSet = CharSet.Unicode)]
public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
#endregion #endregion
} }
+19 -9
View File
@@ -1,5 +1,5 @@
using System; using System;
using System.Runtime.InteropServices; using System.Diagnostics;
using BepInEx.Configuration; using BepInEx.Configuration;
using UXAssist.Common; using UXAssist.Common;
@@ -13,7 +13,6 @@ public static class WindowFunctions
private const string GameWindowClass = "UnityWndClass"; private const string GameWindowClass = "UnityWndClass";
private static string _gameWindowTitle = "Dyson Sphere Program"; private static string _gameWindowTitle = "Dyson Sphere Program";
private static IntPtr _oldWndProc = IntPtr.Zero;
private static IntPtr _gameWindowHandle = IntPtr.Zero; private static IntPtr _gameWindowHandle = IntPtr.Zero;
public static ConfigEntry<int> ProcessPriority; public static ConfigEntry<int> ProcessPriority;
@@ -36,8 +35,15 @@ public static class WindowFunctions
public static void Start() public static void Start()
{ {
ProcessPriority.SettingChanged += (_, _) => WinApi.SetPriorityClass(WinApi.GetCurrentProcess(), ProrityFlags[ProcessPriority.Value]); ProcessPriority.SettingChanged += (_, _) => ApplyProcessPriority();
WinApi.SetPriorityClass(WinApi.GetCurrentProcess(), ProrityFlags[ProcessPriority.Value]); 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) private static string GetPriorityName(int priority)
@@ -92,16 +98,20 @@ public static class WindowFunctions
{ {
if (_gameWindowHandle != IntPtr.Zero) if (_gameWindowHandle != IntPtr.Zero)
return _gameWindowHandle; 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 wnd = IntPtr.Zero;
var consoleWnd = WinApi.GetConsoleWindow();
var currentProcessId = WinApi.GetCurrentProcessId();
while (true) while (true)
{ {
wnd = WinApi.FindWindowEx(IntPtr.Zero, wnd, GameWindowClass, _gameWindowTitle); wnd = WinApi.FindWindowEx(IntPtr.Zero, wnd, GameWindowClass, null);
if (wnd == IntPtr.Zero) if (wnd == IntPtr.Zero)
return IntPtr.Zero; return IntPtr.Zero;
if (wnd == consoleWnd)
continue;
WinApi.GetWindowThreadProcessId(wnd, out var pid); WinApi.GetWindowThreadProcessId(wnd, out var pid);
if (pid == currentProcessId) if (pid == currentProcessId)
break; break;