Skip to content

Commit c298505

Browse files
committed
Replace managed process APIs with ProcessChecker
GamePresetProperty.GetGameProcessWithActiveWindow -> ProcessChecker.TryGetProcessIdWithActiveWindow
1 parent 8de77f3 commit c298505

File tree

3 files changed

+29
-44
lines changed

3 files changed

+29
-44
lines changed

CollapseLauncher/Classes/GamePresetProperty.cs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
using Hi3Helper.SentryHelper;
1414
using Hi3Helper.Win32.Native.ManagedTools;
1515
using Hi3Helper;
16+
using Microsoft.Extensions.Logging;
1617
using Microsoft.UI.Xaml;
1718
using System;
18-
using System.Diagnostics;
1919
using System.IO;
2020
using System.Diagnostics.CodeAnalysis;
2121

@@ -80,6 +80,7 @@ internal GamePresetProperty(UIElement uiElementParent, RegionResourceProp apiRes
8080
}
8181

8282
GamePlaytime = new Playtime(GameVersion, GameSettings);
83+
GamePropLogger = ILoggerHelper.GetILogger($"[GameProp: {GameVersion.GameName} - {GameVersion.GameRegion}]");
8384

8485
SentryHelper.CurrentGameCategory = GameVersion.GameName;
8586
SentryHelper.CurrentGameRegion = GameVersion.GameRegion;
@@ -95,7 +96,8 @@ internal GamePresetProperty(UIElement uiElementParent, RegionResourceProp apiRes
9596
internal IGamePlaytime GamePlaytime { get; set; }
9697
internal IRepair? GameRepair { get; set; }
9798
internal ICache? GameCache { get; set; }
98-
internal IGameVersion GameVersion { get; set; }
99+
internal ILogger GamePropLogger { get; set; }
100+
internal IGameVersion GameVersion { get; set; }
99101
internal IGameInstallManager GameInstall { get; set; }
100102
internal PresetConfig GamePreset { get => GameVersion.GamePreset; }
101103

@@ -122,50 +124,29 @@ internal string GameExecutableNameWithoutExtension
122124
}
123125

124126
[field: AllowNull, MaybeNull]
125-
internal string GameExecutablePath
127+
internal string GameExecutableDir
126128
{
127-
get => field ??= Path.Combine(GameVersion.GameDirPath, GameExecutableName);
129+
get => field ??= GameVersion.GameDirPath;
128130
}
129131

130-
internal bool IsGameRunning
132+
[field: AllowNull, MaybeNull]
133+
internal string GameExecutablePath
131134
{
132-
get => ProcessChecker.IsProcessExist(GameExecutableName, out _, out _, GameExecutablePath, ILoggerHelper.GetILogger());
135+
get => field ??= Path.Combine(GameExecutableDir, GameExecutableName);
133136
}
134137

135-
internal Process? GetGameProcessWithActiveWindow()
138+
internal bool IsGameRunning
136139
{
137-
Process[] processArr = Process.GetProcessesByName(GameExecutableNameWithoutExtension);
138-
int selectedIndex = -1;
139-
140-
try
141-
{
142-
for (int i = 0; i < processArr.Length; i++)
143-
{
144-
Process process = processArr[i];
145-
int processId = process.Id;
146-
147-
string? processPath = ProcessChecker.GetProcessPathByProcessId(processId, ILoggerHelper.GetILogger());
148-
string expectedProcessPath = Path.Combine(GameVersion?.GameDirPath ?? "", GameExecutableName);
149-
if (string.IsNullOrEmpty(processPath) || !expectedProcessPath.Equals(processPath, StringComparison.OrdinalIgnoreCase)
150-
|| process.MainWindowHandle == IntPtr.Zero)
151-
continue;
152-
153-
selectedIndex = i;
154-
return process;
155-
}
156-
}
157-
finally
158-
{
159-
for (int i = 0; i < processArr.Length; i++)
160-
{
161-
if (i == selectedIndex)
162-
continue;
163-
processArr[i].Dispose();
164-
}
165-
}
166-
return null;
140+
get => ProcessChecker.IsProcessExist(GameExecutableName, out _, out _, GameExecutablePath, GamePropLogger);
167141
}
168142

143+
internal bool TryGetGameProcessIdWithActiveWindow(out int processId, out nint windowHandle)
144+
=> ProcessChecker.TryGetProcessIdWithActiveWindow(GameExecutableNameWithoutExtension,
145+
out processId,
146+
out windowHandle,
147+
GameExecutableDir,
148+
logger: GamePropLogger);
149+
169150
~GamePresetProperty()
170151
{
171152
Dispose();

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,9 +1095,11 @@ private async void CheckRunningGameInstance(CancellationToken token)
10951095
PlaytimeIdleStack.Visibility = Visibility.Collapsed;
10961096
PlaytimeRunningStack.Visibility = Visibility.Visible;
10971097

1098-
Process currentGameProcess = CurrentGameProperty.GetGameProcessWithActiveWindow();
1099-
if (currentGameProcess != null)
1098+
int processId;
1099+
if (!CurrentGameProperty.TryGetGameProcessIdWithActiveWindow(out processId, out _))
11001100
{
1101+
Process currentGameProcess = Process.GetProcessById(processId);
1102+
11011103
// HACK: For some reason, the text still unchanged.
11021104
// Make sure the start game button text also changed.
11031105
startGameBtnText.Text = Lang._HomePage.StartBtnRunning;
@@ -1116,8 +1118,7 @@ private async void CheckRunningGameInstance(CancellationToken token)
11161118
int? width = gameSettings.SettingsScreen.width;
11171119

11181120
// Start the resizable window payload
1119-
StartResizableWindowPayload(
1120-
gamePreset.GameExecutableName,
1121+
StartResizableWindowPayload(gamePreset.GameExecutableName,
11211122
gameSettings,
11221123
gamePreset.GameType, height, width);
11231124

@@ -2837,19 +2838,22 @@ private static void GenshinHDREnforcer()
28372838
private async Task GameBoost_Invoke(GamePresetProperty gameProp)
28382839
{
28392840
#nullable enable
2840-
// Init new target process
28412841
Process? toTargetProc = null;
28422842
try
28432843
{
2844+
int processId;
28442845
// Try catching the non-zero MainWindowHandle pointer and assign it to "toTargetProc" variable by using GetGameProcessWithActiveWindow()
2845-
while ((toTargetProc = gameProp.GetGameProcessWithActiveWindow()) == null)
2846+
while (!gameProp.TryGetGameProcessIdWithActiveWindow(out processId, out _))
28462847
{
28472848
await Task.Delay(1000); // Waiting the process to be found and assigned to "toTargetProc" variable.
28482849
// This is where the magic happen. When the "toTargetProc" doesn't meet the comparison to be compared as null,
28492850
// it will instead return a non-null value and assign it to "toTargetProc" variable,
28502851
// which it will break the loop and execute the next code below it.
28512852
}
28522853

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

0 commit comments

Comments
 (0)