Skip to content

Commit 0d7f1e1

Browse files
committed
Use ProcessChecker API to set process priority
+ Tho, this won't fix an issue where process priority can't get set due to Anti-cheat's process protection
1 parent a79b7c5 commit 0d7f1e1

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

CollapseLauncher/Classes/GamePresetProperty.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using CollapseLauncher.InstallManager.Zenless;
1212
using CollapseLauncher.Interfaces;
1313
using Hi3Helper.SentryHelper;
14+
using Hi3Helper.Win32.Native.Enums;
1415
using Hi3Helper.Win32.Native.ManagedTools;
1516
using Hi3Helper;
1617
using Microsoft.Extensions.Logging;
@@ -140,13 +141,25 @@ internal bool IsGameRunning
140141
get => ProcessChecker.IsProcessExist(GameExecutableName, out _, out _, GameExecutablePath, GamePropLogger);
141142
}
142143

144+
internal bool GetIsGameProcessRunning(int processId)
145+
=> ProcessChecker.IsProcessExist(processId);
146+
143147
internal bool TryGetGameProcessIdWithActiveWindow(out int processId, out nint windowHandle)
144148
=> ProcessChecker.TryGetProcessIdWithActiveWindow(GameExecutableNameWithoutExtension,
145149
out processId,
146150
out windowHandle,
147151
GameExecutableDir,
148152
logger: GamePropLogger);
149153

154+
internal bool TrySetGameProcessPriority(PriorityClass priorityClass = PriorityClass.NORMAL_PRIORITY_CLASS)
155+
=> ProcessChecker.TrySetProcessPriority(GameExecutableNameWithoutExtension,
156+
priorityClass,
157+
GameExecutableDir,
158+
logger: GamePropLogger);
159+
160+
internal bool TrySetGameProcessPriority(int processId, PriorityClass priorityClass = PriorityClass.NORMAL_PRIORITY_CLASS)
161+
=> ProcessChecker.TrySetProcessPriority(processId, priorityClass, GamePropLogger);
162+
150163
~GamePresetProperty()
151164
{
152165
Dispose();

CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,10 +2838,10 @@ private static void GenshinHDREnforcer()
28382838
private async Task GameBoost_Invoke(GamePresetProperty gameProp)
28392839
{
28402840
#nullable enable
2841-
Process? toTargetProc = null;
2841+
string processName = gameProp.GameExecutableName;
2842+
int processId = -1;
28422843
try
28432844
{
2844-
int processId;
28452845
// Try catching the non-zero MainWindowHandle pointer and assign it to "toTargetProc" variable by using GetGameProcessWithActiveWindow()
28462846
while (!gameProp.TryGetGameProcessIdWithActiveWindow(out processId, out _))
28472847
{
@@ -2851,11 +2851,8 @@ private async Task GameBoost_Invoke(GamePresetProperty gameProp)
28512851
// which it will break the loop and execute the next code below it.
28522852
}
28532853

2854-
// Init new target process
2855-
toTargetProc = Process.GetProcessById(processId);
2856-
28572854
LogWriteLine($"[HomePage::GameBoost_Invoke] Found target process! Waiting 10 seconds for process initialization...\r\n\t" +
2858-
$"Target Process : {toTargetProc.ProcessName} [{toTargetProc.Id}]", LogType.Default, true);
2855+
$"Target Process : {processName} [{processId}]", LogType.Default, true);
28592856

28602857
// Wait 20 (or 10 if its first try) seconds before applying
28612858
if (GameBoostInvokeTryCount == 0)
@@ -2868,38 +2865,37 @@ private async Task GameBoost_Invoke(GamePresetProperty gameProp)
28682865
}
28692866

28702867
// Check early exit
2871-
if (toTargetProc.HasExited)
2868+
if (!gameProp.GetIsGameProcessRunning(processId))
28722869
{
2873-
LogWriteLine($"[HomePage::GameBoost_Invoke] Game process {toTargetProc.ProcessName} [{toTargetProc.Id}] has exited!",
2870+
LogWriteLine($"[HomePage::GameBoost_Invoke] Game process {processName} [{processId}] has exited!",
28742871
LogType.Warning, true);
28752872
return;
28762873
}
28772874

28782875
// Assign the priority to the process and write a log (just for displaying any info)
2879-
toTargetProc.PriorityClass = ProcessPriorityClass.AboveNormal;
2876+
if (!gameProp.TrySetGameProcessPriority(processId, Hi3Helper.Win32.Native.Enums.PriorityClass.ABOVE_NORMAL_PRIORITY_CLASS))
2877+
{
2878+
throw new Win32Exception();
2879+
}
28802880
GameBoostInvokeTryCount = 0;
2881-
LogWriteLine($"[HomePage::GameBoost_Invoke] Game process {toTargetProc.ProcessName} " +
2882-
$"[{toTargetProc.Id}] priority is boosted to above normal!", LogType.Warning, true);
2881+
LogWriteLine($"[HomePage::GameBoost_Invoke] Game process {processName} " +
2882+
$"[{processId}] priority is boosted to above normal!", LogType.Warning, true);
28832883
}
28842884
catch (Exception ex) when (GameBoostInvokeTryCount < 5)
28852885
{
28862886
LogWriteLine($"[HomePage::GameBoost_Invoke] (Try #{GameBoostInvokeTryCount})" +
28872887
$"There has been error while boosting game priority to Above Normal! Retrying...\r\n" +
2888-
$"\tTarget Process : {toTargetProc?.ProcessName} [{toTargetProc?.Id}]\r\n{ex}",
2888+
$"\tTarget Process : {processName} [{processId}]\r\n{ex}",
28892889
LogType.Error, true);
28902890
GameBoostInvokeTryCount++;
28912891
_ = Task.Run(async () => { await GameBoost_Invoke(gameProp); });
28922892
}
28932893
catch (Exception ex)
28942894
{
28952895
LogWriteLine($"[HomePage::GameBoost_Invoke] There has been error while boosting game priority to Above Normal!\r\n" +
2896-
$"\tTarget Process : {toTargetProc?.ProcessName} [{toTargetProc?.Id}]\r\n{ex}",
2896+
$"\tTarget Process : {processName} [{processId}]\r\n{ex}",
28972897
LogType.Error, true);
28982898
}
2899-
finally
2900-
{
2901-
toTargetProc?.Dispose();
2902-
}
29032899
#nullable restore
29042900
}
29052901
#endregion

0 commit comments

Comments
 (0)