支持任意文件名的启动 (#2819)

This commit is contained in:
辉鸭蛋
2026-02-23 13:18:18 +08:00
committed by GitHub
parent b1c6e9e4e3
commit f86abe3ceb
3 changed files with 56 additions and 9 deletions

View File

@@ -13,7 +13,8 @@ public class SystemControl
{
public static nint FindGenshinImpactHandle()
{
return FindHandleByProcessName("YuanShen", "GenshinImpact", "Genshin Impact Cloud Game", "Genshin Impact Cloud");
var processNames = TaskContext.Instance().GetGenshinGameProcessNameList();
return FindHandleByProcessName(processNames.ToArray());
}
public static async Task<nint> StartFromLocalAsync(string path)
@@ -69,7 +70,13 @@ public class SystemControl
public static bool IsGenshinImpactActiveByProcess()
{
var name = GetActiveProcessName();
return name is "YuanShen" or "yuanshen" or "GenshinImpact" or "Genshin Impact Cloud Game";
if (string.IsNullOrEmpty(name))
{
return false;
}
var processNames = TaskContext.Instance().GetGenshinGameProcessNameList();
return processNames.Any(p => string.Equals(p, name, StringComparison.OrdinalIgnoreCase));
}
public static string GetActiveByProcess()
@@ -312,11 +319,19 @@ public class SystemControl
{
try
{
// 尝试通过进程名称查找原神进程
var processes = Process.GetProcessesByName("YuanShen")
.Concat(Process.GetProcessesByName("GenshinImpact"))
.Concat(Process.GetProcessesByName("Genshin Impact Cloud Game"))
.ToArray();
var processNames = TaskContext.Instance().GetGenshinGameProcessNameList();
var allProcesses = processNames
.SelectMany(Process.GetProcessesByName)
.ToList();
var processMap = allProcesses
.GroupBy(p => p.Id)
.ToDictionary(g => g.Key, g => g.First());
// 释放重复的 Process 包装对象
foreach (var p in allProcesses.Where(p => !ReferenceEquals(p, processMap[p.Id])))
{
p.Dispose();
}
var processes = processMap.Values.ToArray();
if (processes.Length > 0)
{

View File

@@ -5,6 +5,8 @@ using BetterGenshinImpact.Genshin.Settings;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using BetterGenshinImpact.Core.Script.Group;
@@ -74,5 +76,35 @@ namespace BetterGenshinImpact.GameTask
/// 注意 IsInitialized = false 时,这个值就会被设置
/// </summary>
public DateTime LinkedStartGenshinTime { get; set; } = DateTime.MinValue;
public List<string> GetGenshinGameProcessNameList()
{
if (IsInitialized)
{
return [SystemInfo.GameProcessName];
}
else
{
List<string> list = ["YuanShen", "GenshinImpact", "Genshin Impact Cloud Game", "Genshin Impact Cloud"];
try
{
var installPath = Config.GenshinStartConfig.InstallPath;
if (!string.IsNullOrEmpty(installPath))
{
var customName = Path.GetFileNameWithoutExtension(installPath);
if (!string.IsNullOrEmpty(customName) && !list.Contains(customName))
{
list.Insert(0, customName); // 将用户自定义的进程名放在列表前面,优先匹配
}
}
}
catch
{
/* ignore */
}
return list;
}
}
}
}
}

View File

@@ -383,7 +383,7 @@ public partial class HomePageViewModel : ViewModel
// 弹出选择文件夹对话框
var dialog = new Ookii.Dialogs.Wpf.VistaOpenFileDialog
{
Filter = "原神|YuanShen.exe|原神国际服|GenshinImpact.exe|所有文件|*.*"
Filter = "原神|YuanShen.exe;GenshinImpact.exe|可执行文件|*.exe|所有文件|*.*"
};
if (dialog.ShowDialog() == true)
{