mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Snap.Hutao.Service.Game.Launching.Handler;
|
||||
|
||||
internal sealed class LaunchExecutionEnsureGameNotRunningHandler : ILaunchExecutionDelegateHandler
|
||||
{
|
||||
public static bool IsGameRunning([NotNullWhen(true)] out System.Diagnostics.Process? runningProcess)
|
||||
public static bool IsGameRunning([NotNullWhen(true)] out Process? runningProcess)
|
||||
{
|
||||
int currentSessionId = Process.GetCurrentProcess().SessionId;
|
||||
|
||||
// GetProcesses once and manually loop is O(n)
|
||||
foreach (ref readonly System.Diagnostics.Process process in System.Diagnostics.Process.GetProcesses().AsSpan())
|
||||
foreach (ref readonly Process process in Process.GetProcesses().AsSpan())
|
||||
{
|
||||
if (string.Equals(process.ProcessName, GameConstants.YuanShenProcessName, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(process.ProcessName, GameConstants.GenshinImpactProcessName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (process.SessionId != currentSessionId)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
runningProcess = process;
|
||||
return true;
|
||||
}
|
||||
@@ -24,7 +33,7 @@ internal sealed class LaunchExecutionEnsureGameNotRunningHandler : ILaunchExecut
|
||||
|
||||
public async ValueTask OnExecutionAsync(LaunchExecutionContext context, LaunchExecutionDelegate next)
|
||||
{
|
||||
if (IsGameRunning(out System.Diagnostics.Process? process))
|
||||
if (IsGameRunning(out Process? process))
|
||||
{
|
||||
context.Logger.LogInformation("Game process detected, id: {Id}", process.Id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user