new game path loader

(cherry picked from commit dc853f2307)
This commit is contained in:
辉鸭蛋
2024-12-27 00:57:18 +08:00
parent 9633c8a995
commit 8b31a64fbe
4 changed files with 126 additions and 4 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,41 @@
using System;
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 cn;
}
var global = Registry.GetValue($@"HKEY_CURRENT_USER\Software\Cognosphere\HYP\1_0\hk4e_global", "GameInstallPath", null) as string;
if (!string.IsNullOrEmpty(global))
{
return global;
}
var bilibili = Registry.GetValue($@"HKEY_CURRENT_USER\Software\miHoYo\HYP\standalone\14_0\hk4e_bilibili\BilibiliGenshin\hk4e_bilibili", "GameInstallPath", null) as string;
if (!string.IsNullOrEmpty(bilibili))
{
return bilibili;
}
}
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

@@ -340,11 +340,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 = RegistryGameLocator.GetDefaultGameInstallPath();
if (!string.IsNullOrEmpty(p1))
{
Config.GenshinStartConfig.InstallPath = p1;
}
else
{
var path = await UnityLogGameLocator.LocateSingleGamePathAsync();
if (!string.IsNullOrEmpty(path))
{
Config.GenshinStartConfig.InstallPath = path;
}
}
});
}
}