From a6b677261805c3bf6c90638f7f75187b686b89db Mon Sep 17 00:00:00 2001 From: wwbweibo Date: Mon, 27 Nov 2023 22:13:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=80=BB=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E8=B7=AF=E5=BE=84=E7=9B=B4=E6=8E=A5=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=8E=9F=E7=A5=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BetterGenshinImpact.csproj | 1 + BetterGenshinImpact/Core/Config/AllConfig.cs | 6 +++ BetterGenshinImpact/GameTask/SystemControl.cs | 12 +++++- BetterGenshinImpact/View/Pages/HomePage.xaml | 29 +++++++++++++ .../ViewModel/Pages/HomePageViewModel.cs | 43 ++++++++++++++++++- 5 files changed, 88 insertions(+), 3 deletions(-) diff --git a/BetterGenshinImpact/BetterGenshinImpact.csproj b/BetterGenshinImpact/BetterGenshinImpact.csproj index fa99618d..e2d2395d 100644 --- a/BetterGenshinImpact/BetterGenshinImpact.csproj +++ b/BetterGenshinImpact/BetterGenshinImpact.csproj @@ -32,6 +32,7 @@ + diff --git a/BetterGenshinImpact/Core/Config/AllConfig.cs b/BetterGenshinImpact/Core/Config/AllConfig.cs index 2e179c0a..6d388d78 100644 --- a/BetterGenshinImpact/Core/Config/AllConfig.cs +++ b/BetterGenshinImpact/Core/Config/AllConfig.cs @@ -73,6 +73,12 @@ namespace BetterGenshinImpact.Core.Config /// public HotKeyConfig HotKeyConfig { get; set; } = new(); + /// + /// 原神安装路径 + /// + [ObservableProperty] + private string _installPath = ""; + [JsonIgnore] public Action? OnAnyChangedAction { get; set; } public void InitEvent() diff --git a/BetterGenshinImpact/GameTask/SystemControl.cs b/BetterGenshinImpact/GameTask/SystemControl.cs index e3a07a94..8e28e7eb 100644 --- a/BetterGenshinImpact/GameTask/SystemControl.cs +++ b/BetterGenshinImpact/GameTask/SystemControl.cs @@ -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(); diff --git a/BetterGenshinImpact/View/Pages/HomePage.xaml b/BetterGenshinImpact/View/Pages/HomePage.xaml index bf81a7ee..11f8331e 100644 --- a/BetterGenshinImpact/View/Pages/HomePage.xaml +++ b/BetterGenshinImpact/View/Pages/HomePage.xaml @@ -106,6 +106,35 @@ + + + + + + + + + + + + + diff --git a/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs index b16f3fb2..c13cba6a 100644 --- a/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs @@ -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; + } + }); + } + } \ No newline at end of file From e16f24ce2ae402b29b28f01b9b05425191d53245 Mon Sep 17 00:00:00 2001 From: wwbweibo Date: Mon, 27 Nov 2023 22:19:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90=E7=95=8C=E9=9D=A2=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E5=99=A8=EF=BC=8C=20=E7=9B=B4=E6=8E=A5=E8=BF=9B?= =?UTF-8?q?=E5=85=A5=E6=B8=B8=E6=88=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GameTask/GameLoading/GameLoading.cs | 64 +++++++++++++++++++ .../GameTask/GameTaskManager.cs | 3 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 BetterGenshinImpact/GameTask/GameLoading/GameLoading.cs diff --git a/BetterGenshinImpact/GameTask/GameLoading/GameLoading.cs b/BetterGenshinImpact/GameTask/GameLoading/GameLoading.cs new file mode 100644 index 00000000..6da9793f --- /dev/null +++ b/BetterGenshinImpact/GameTask/GameLoading/GameLoading.cs @@ -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 + { + "点", "击", "进", "入" + }, + 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; + } + } + } + +} diff --git a/BetterGenshinImpact/GameTask/GameTaskManager.cs b/BetterGenshinImpact/GameTask/GameTaskManager.cs index d3053ee1..d8eed600 100644 --- a/BetterGenshinImpact/GameTask/GameTaskManager.cs +++ b/BetterGenshinImpact/GameTask/GameTaskManager.cs @@ -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();