113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
namespace ThreadPilot.Services
|
|
{
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Win32;
|
|
|
|
public class GameModeService : IGameModeService
|
|
{
|
|
private readonly ILogger<GameModeService> logger;
|
|
private const string GameBarKeyPath = @"Software\Microsoft\GameBar";
|
|
private const string GameModeValueName = "AutoGameModeEnabled";
|
|
|
|
public GameModeService(ILogger<GameModeService> logger)
|
|
{
|
|
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public async Task<bool> IsGameModeEnabledAsync()
|
|
{
|
|
await Task.CompletedTask; // Make async for consistency
|
|
|
|
try
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(GameBarKeyPath, writable: false);
|
|
if (key == null)
|
|
{
|
|
this.logger.LogDebug("GameBar registry key not found, assuming Game Mode is disabled");
|
|
return false;
|
|
}
|
|
|
|
var value = key.GetValue(GameModeValueName);
|
|
if (value is int intValue)
|
|
{
|
|
bool isEnabled = intValue != 0;
|
|
this.logger.LogDebug("Game Mode status: {Status}", isEnabled ? "Enabled" : "Disabled");
|
|
return isEnabled;
|
|
}
|
|
|
|
this.logger.LogDebug("GameMode value not found, assuming disabled");
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogWarning(ex, "Failed to read Game Mode registry key, assuming disabled");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> SetGameModeAsync(bool enabled)
|
|
{
|
|
await Task.CompletedTask; // Make async for consistency
|
|
|
|
try
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(GameBarKeyPath, writable: true);
|
|
if (key == null)
|
|
{
|
|
this.logger.LogWarning("GameBar registry key not found, cannot modify Game Mode");
|
|
return false;
|
|
}
|
|
|
|
key.SetValue(GameModeValueName, enabled ? 1 : 0, RegistryValueKind.DWord);
|
|
this.logger.LogInformation("Set Windows Game Mode to {State}", enabled ? "enabled" : "disabled");
|
|
return true;
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
this.logger.LogWarning(ex, "Insufficient permissions to modify Game Mode registry key");
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Failed to set Game Mode to {State}", enabled ? "enabled" : "disabled");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DisableGameModeForAffinityAsync()
|
|
{
|
|
try
|
|
{
|
|
bool isEnabled = await this.IsGameModeEnabledAsync();
|
|
if (!isEnabled)
|
|
{
|
|
this.logger.LogDebug("Game Mode already disabled, no action needed");
|
|
return false;
|
|
}
|
|
|
|
this.logger.LogInformation("Game Mode is enabled, disabling for better CPU affinity control");
|
|
bool success = await this.SetGameModeAsync(false);
|
|
|
|
if (success)
|
|
{
|
|
this.logger.LogInformation("Successfully disabled Windows Game Mode for CPU affinity optimization");
|
|
}
|
|
else
|
|
{
|
|
this.logger.LogWarning("Failed to disable Game Mode, CPU affinity may be affected");
|
|
}
|
|
|
|
return success;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogError(ex, "Error disabling Game Mode for affinity");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|