Files
better-genshin-impact/BetterGenshinImpact/GameTask/GenshinHdrRegistryHelper.cs
辉鸭蛋 de75d02104 fix: 修复禁用HDR时错误删除注册表项的问题
将删除注册表值改为修改值为0,避免HDR设置被完全移除导致游戏可能重新启用HDR
2026-05-03 16:40:03 +08:00

75 lines
2.5 KiB
C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BetterGenshinImpact.GameTask;
public static class GenshinHdrRegistryHelper
{
public const string HdrRegistryEntryName = "WINDOWS_HDR_ON_h3132281285";
public const string CnHdrRegistrySubKeyPath = @"Software\miHoYo\原神\WINDOWS_HDR_ON_h3132281285";
public const string GlobalHdrRegistrySubKeyPath = @"Software\miHoYo\Genshin Impact\WINDOWS_HDR_ON_h3132281285";
public const string CnHdrRegistryParentKeyPath = @"Software\miHoYo\原神";
public const string GlobalHdrRegistryParentKeyPath = @"Software\miHoYo\Genshin Impact";
public static readonly IReadOnlyList<string> HdrRegistrySubKeyPaths =
[
CnHdrRegistrySubKeyPath,
GlobalHdrRegistrySubKeyPath
];
public static readonly IReadOnlyList<string> HdrRegistryParentKeyPaths =
[
CnHdrRegistryParentKeyPath,
GlobalHdrRegistryParentKeyPath
];
public static IReadOnlyList<string> HdrRegistryFullKeyPaths =>
HdrRegistrySubKeyPaths.Select(static path => $@"HKEY_CURRENT_USER\{path}").ToArray();
public static bool TryDisableHdr(out IReadOnlyList<string> deletedFullKeyPaths)
{
var updatedPaths = new List<string>();
foreach (var parentKeyPath in HdrRegistryParentKeyPaths)
{
try
{
using var key = Registry.CurrentUser.OpenSubKey(parentKeyPath, writable: true);
if (key == null)
{
continue;
}
var value = key.GetValue(HdrRegistryEntryName);
if (value == null)
{
continue;
}
var isHdrEnabled = value switch
{
int intValue => intValue == 1,
long longValue => longValue == 1L,
_ => false
};
if (!isHdrEnabled)
{
continue;
}
key.SetValue(HdrRegistryEntryName, 0, RegistryValueKind.DWord);
updatedPaths.Add($@"HKEY_CURRENT_USER\{parentKeyPath}\{HdrRegistryEntryName}");
}
catch
{
// 忽略写入失败,避免影响启动流程
}
}
deletedFullKeyPaths = updatedPaths.Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
return deletedFullKeyPaths.Count > 0;
}
}