Merge pull request #97 from wwbweibo/main

添加本地安装路径配置,直接启动原神
This commit is contained in:
辉鸭蛋
2023-11-28 12:14:17 +08:00
committed by GitHub
7 changed files with 154 additions and 4 deletions

View File

@@ -32,6 +32,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.1" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.Managed" Version="1.16.1" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageReference Include="OpenCvSharp4.Extensions" Version="4.8.0.20230708" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.8.0.20230708" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />

View File

@@ -73,6 +73,12 @@ namespace BetterGenshinImpact.Core.Config
/// </summary>
public HotKeyConfig HotKeyConfig { get; set; } = new();
/// <summary>
/// 原神安装路径
/// </summary>
[ObservableProperty]
private string _installPath = "";
[JsonIgnore] public Action? OnAnyChangedAction { get; set; }
public void InitEvent()

View File

@@ -0,0 +1,64 @@
using BetterGenshinImpact.Core.Recognition;
using BetterGenshinImpact.Core.Simulator;
using BetterGenshinImpact.GameTask.AutoSkip.Assets;
using BetterGenshinImpact.GameTask.Model;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BetterGenshinImpact.GameTask.GameLoading
{
public class GameLoadingTrigger : ITaskTrigger
{
private RecognitionObject? _startGameRo;
public string Name => "GameLoading";
public bool IsEnabled { get; set; }
public int Priority => 999;
public bool IsExclusive => false;
public void Init()
{
var info = TaskContext.Instance().SystemInfo;
_startGameRo = new RecognitionObject
{
Name = "StartGame",
RecognitionType = RecognitionTypes.Ocr,
// ROI 应该时捕捉窗口的中间底部部分
RegionOfInterest = new Rect((int)(info.CaptureAreaRect.Width / 2 - 100),
(int)(info.CaptureAreaRect.Height - 100),
200,
100),
OneContainMatchText = new List<string>
{
"点", "击", "进", "入"
},
DrawOnWindow = true
}.InitTemplate();
IsEnabled = true;
}
public void OnCapture(CaptureContent content)
{
using var foundRectArea = content.CaptureRectArea.Find(_startGameRo!);
if (!foundRectArea.IsEmpty())
{
// 在游戏窗口中心点击
var info = TaskContext.Instance().SystemInfo;
var x = info.CaptureAreaRect.Right - (info.CaptureAreaRect.Width / 2);
var y = info.CaptureAreaRect.Bottom - (info.CaptureAreaRect.Height / 2);
Simulation.MouseEvent.Click(x, y);
// 一旦进入游戏,这个触发器就不再需要了
// TODO如果其他触发器成功这个触发器同样也不再需要了考虑使用其他触发器的成功来禁用该事件
IsEnabled = false;
}
}
}
}

View File

@@ -26,7 +26,8 @@ namespace BetterGenshinImpact.GameTask
{ "RecognitionTest", new TestTrigger() },
{ "AutoPick", new AutoPick.AutoPickTrigger() },
{ "AutoSkip", new AutoSkip.AutoSkipTrigger() },
{ "AutoFishing", new AutoFishing.AutoFishingTrigger() }
{ "AutoFishing", new AutoFishing.AutoFishingTrigger() },
{ "GameLoading", new GameLoading.GameLoadingTrigger() }
};
var loadedTriggers = TriggerDictionary.Values.ToList();

View File

@@ -1,6 +1,8 @@
using System;
using BetterGenshinImpact.Core.Simulator;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Vanara.PInvoke;
namespace BetterGenshinImpact.GameTask;
@@ -12,6 +14,14 @@ public class SystemControl
return FindHandleByProcessName("YuanShen", "GenshinImpact", "Genshin Impact Cloud Game");
}
public static nint StartFromLocal(string path)
{
// 使用 path 在新的线程中启动游戏
var process = Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
Thread.Sleep(10000);
return FindGenshinImpactHandle();
}
public static bool IsGenshinImpactActiveByProcess()
{
var name = GetActiveProcessName();

View File

@@ -106,6 +106,35 @@
</Grid>
</ui:CardExpander.Header>
<StackPanel>
<Grid Margin="16" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock
Grid.Row="0"
Margin="0,0,0,5"
Grid.Column="0"
FontTypography="Body"
Text="安装位置"
TextWrapping="Wrap" />
<ui:TextBox x:Name="InstallPathTextBox"
Grid.Row="1"
Grid.Column="0"
MinWidth="90"
Margin="0,0,10,0"
Text="{Binding Config.InstallPath, Mode=TwoWay}" />
<ui:Button x:Name="SelectInstallPathButton"
Grid.Row="1"
Grid.Column="1"
Margin="0,0,36,0"
Command="{Binding SelectInstallPathCommand}"
Content="..." />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />

View File

@@ -12,6 +12,9 @@ using Fischless.GameCapture;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
@@ -122,8 +125,16 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware
var hWnd = SystemControl.FindGenshinImpactHandle();
if (hWnd == IntPtr.Zero)
{
System.Windows.MessageBox.Show("未找到原神窗口,请先启动原神!");
return;
if (! string.IsNullOrEmpty(Config.InstallPath))
{
var path = Path.Combine(Config.InstallPath, "Yuanshen.exe");
hWnd = SystemControl.StartFromLocal(path);
}
if (hWnd ==IntPtr.Zero)
{
System.Windows.MessageBox.Show("未找到原神窗口,请先启动原神!");
return;
}
}
@@ -192,4 +203,32 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware
// MessageBox.Show(e.StackTrace);
//}
}
[RelayCommand]
public async Task SelectInstallPathAsync()
{
await Task.Run(() =>
{
// 弹出选择文件夹对话框
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
if (dialog.ShowDialog() == true)
{
var path = dialog.SelectedPath;
if (string.IsNullOrEmpty(path))
{
return;
}
// 检查是否有Yuanshen.exe
var gamePath = Path.Combine(path, "Yuanshen.exe");
if (!File.Exists(gamePath))
{
System.Windows.MessageBox.Show("请选择正确的原神安装目录");
return;
}
Config.InstallPath = path;
}
});
}
}