Merge branch 'babalae:main' into main

This commit is contained in:
mfkvfhpdx 2024-12-27 21:48:28 +08:00 committed by GitHub
commit c439403231
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 135 additions and 8 deletions

View File

@ -9,6 +9,10 @@ using System.Text.RegularExpressions;
namespace BetterGenshinImpact.Genshin.Paths;
/// <summary>
/// 已经弃用
/// </summary>
[Obsolete]
internal partial class GameExePath
{
public static readonly FrozenSet<string> GameRegistryPaths = FrozenSet.ToFrozenSet(

View File

@ -0,0 +1,42 @@
using System;
using System.IO;
using BetterGenshinImpact.GameTask.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
namespace BetterGenshinImpact.Genshin.Paths;
/// <summary>
/// https://github.com/Scighost/Starward/blob/main/src%2FStarward%2FServices%2FLauncher%2FGameLauncherService.cs#L112-L112
/// </summary>
public class RegistryGameLocator
{
public static string? GetDefaultGameInstallPath()
{
try
{
var cn = Registry.GetValue($@"HKEY_CURRENT_USER\Software\miHoYo\HYP\1_1\hk4e_cn", "GameInstallPath", null) as string;
if (!string.IsNullOrEmpty(cn))
{
return Path.Combine(cn, "YuanShen.exe");
}
var global = Registry.GetValue($@"HKEY_CURRENT_USER\Software\Cognosphere\HYP\1_0\hk4e_global", "GameInstallPath", null) as string;
if (!string.IsNullOrEmpty(global))
{
return Path.Combine(global, "GenshinImpact.exe");
}
var bilibili = Registry.GetValue($@"HKEY_CURRENT_USER\Software\miHoYo\HYP\standalone\14_0\hk4e_cn\umfgRO5gh5\hk4e_cn", "GameInstallPath", null) as string;
if (!string.IsNullOrEmpty(bilibili))
{
return Path.Combine(bilibili, "YuanShen.exe");
}
}
catch (Exception e)
{
TaskControl.Logger.LogDebug(e, "Failed to locate game path.");
}
return null;
}
}

View File

@ -0,0 +1,66 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BetterGenshinImpact.GameTask.Common;
using Microsoft.Extensions.Logging;
namespace BetterGenshinImpact.Genshin.Paths;
/// <summary>
/// https://github.com/DGP-Studio/Snap.Hutao/blob/main/src/Snap.Hutao/Snap.Hutao/Service/Game/Locator/UnityLogGameLocator.cs
/// </summary>
public partial class UnityLogGameLocator
{
[GeneratedRegex(@".:/.+(?:GenshinImpact|YuanShen)(?=_Data)", RegexOptions.IgnoreCase)]
private static partial Regex WarmupFileLine();
public static async ValueTask<string?> LocateSingleGamePathAsync()
{
try
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string logFilePathOversea = Path.Combine(appDataPath, @"..\LocalLow\miHoYo\Genshin Impact\output_log.txt");
string logFilePathChinese = Path.Combine(appDataPath, @"..\LocalLow\miHoYo\原神\output_log.txt");
// Fallback to the CN server.
string logFilePath = File.Exists(logFilePathChinese) ? logFilePathChinese : logFilePathOversea;
return await LocateGamePathAsync(logFilePath).ConfigureAwait(false);
}
catch (Exception e)
{
TaskControl.Logger.LogDebug(e, "Failed to locate game path.");
return null;
}
}
private static async ValueTask<string?> LocateGamePathAsync(string logFilePath)
{
if (!File.Exists(logFilePath))
{
return null;
}
string content;
try
{
await using var fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new StreamReader(fileStream);
content = await reader.ReadToEndAsync();
}
catch (IOException)
{
return null;
}
Match matchResult = WarmupFileLine().Match(content);
if (!matchResult.Success)
{
return null;
}
string entryName = $"{matchResult.Value}.exe";
string fullPath = Path.GetFullPath(Path.Combine(matchResult.Value, "..", entryName));
return fullPath;
}
}

View File

@ -29,11 +29,14 @@ namespace BetterGenshinImpact.ViewModel.Pages;
public partial class HomePageViewModel : ObservableObject, INavigationAware, IViewModel
{
[ObservableProperty] private string[] _modeNames = GameCaptureFactory.ModeNames();
[ObservableProperty]
private string[] _modeNames = GameCaptureFactory.ModeNames();
[ObservableProperty] private string? _selectedMode = CaptureModes.BitBlt.ToString();
[ObservableProperty]
private string? _selectedMode = CaptureModes.BitBlt.ToString();
[ObservableProperty] private bool _taskDispatcherEnabled = false;
[ObservableProperty]
private bool _taskDispatcherEnabled = false;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(StartTriggerCommand))]
@ -237,6 +240,7 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi
_maskWindow?.Close();
_maskWindow = null;
}
TaskDispatcherEnabled = false;
_mouseKeyMonitor.Unsubscribe();
}
@ -331,11 +335,22 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi
// 检查用户是否配置了原神安装目录,如果没有,尝试从注册表中读取
if (string.IsNullOrEmpty(Config.GenshinStartConfig.InstallPath))
{
var path = GameExePath.GetWithoutCloud();
if (!string.IsNullOrEmpty(path))
Task.Run(async () =>
{
Config.GenshinStartConfig.InstallPath = path;
}
var p1 = await UnityLogGameLocator.LocateSingleGamePathAsync();
if (!string.IsNullOrEmpty(p1))
{
Config.GenshinStartConfig.InstallPath = p1;
}
else
{
var p2 = RegistryGameLocator.GetDefaultGameInstallPath();
if (!string.IsNullOrEmpty(p2))
{
Config.GenshinStartConfig.InstallPath = p2;
}
}
});
}
}
@ -360,4 +375,4 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi
win.NavigateToHtml(html);
win.ShowDialog();
}
}
}