From 2a2acb5b0df635a5293507916fa1d1db3cab4aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Sat, 23 Nov 2024 00:49:11 +0800 Subject: [PATCH] auto skip: background skip use key (5.2) --- .../AutoPick/Assets/AutoPickAssets.cs | 20 +++- .../GameTask/AutoPick/AutoPickTrigger.cs | 3 +- .../GameTask/AutoSkip/AutoSkipConfig.cs | 52 ++++++++--- .../GameTask/AutoSkip/AutoSkipTrigger.cs | 91 +++++++++++++++++-- .../AutoSkip/Model/SelectChatOptionTypes.cs | 12 +++ .../View/Pages/TriggerSettingsPage.xaml | 30 +++++- .../Pages/TriggerSettingsPageViewModel.cs | 6 +- 7 files changed, 186 insertions(+), 28 deletions(-) create mode 100644 BetterGenshinImpact/GameTask/AutoSkip/Model/SelectChatOptionTypes.cs diff --git a/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs b/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs index 2f6e90ff..ff5286ed 100644 --- a/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs +++ b/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs @@ -19,6 +19,7 @@ public class AutoPickAssets : BaseAssets public User32.VK PickVk = User32.VK.VK_F; public RecognitionObject PickRo; + public RecognitionObject ChatPickRo; private AutoPickAssets() { @@ -59,6 +60,7 @@ private AutoPickAssets() { PickRo = LoadCustomPickKey(keyName); PickVk = User32Helper.ToVk(keyName); + ChatPickRo = LoadCustomChatPickKey(keyName); } catch (Exception e) { @@ -67,6 +69,7 @@ private AutoPickAssets() TaskContext.Instance().Config.AutoPickConfig.PickKey = "F"; return; } + if (keyName != "F") { _logger.LogInformation("自定义拾取按键:{Key}", keyName); @@ -88,4 +91,19 @@ public RecognitionObject LoadCustomPickKey(string key) DrawOnWindow = false }.InitTemplate(); } -} + + public RecognitionObject LoadCustomChatPickKey(string key) + { + return new RecognitionObject + { + Name = "chatPick" + key, + RecognitionType = RecognitionTypes.TemplateMatch, + TemplateImageMat = GameTaskManager.LoadAssetImage("AutoPick", key + ".png"), + RegionOfInterest = new Rect((int)(1200 * AssetScale), + (int)(350 * AssetScale), + (int)(50 * AssetScale), + CaptureRect.Height - (int)(220 * AssetScale) - (int)(350 * AssetScale)), + DrawOnWindow = false + }.InitTemplate(); + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs b/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs index 30e80911..f9d64cfa 100644 --- a/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs +++ b/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs @@ -40,8 +40,7 @@ public partial class AutoPickTrigger : ITaskTrigger /// 拾取白名单 /// private List _whiteList = []; - - private User32.VK _pickVk = User32.VK.VK_F; + private RecognitionObject _pickRo; // 外部配置 diff --git a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs index 816fa1db..cc571b49 100644 --- a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs +++ b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Mvvm.ComponentModel; using System; +using BetterGenshinImpact.GameTask.AutoSkip.Model; namespace BetterGenshinImpact.GameTask.AutoSkip; @@ -16,12 +17,14 @@ public partial class AutoSkipConfig : ObservableObject /// 2. 自动点击一个识别到的选项 /// 3. 黑屏过长自动点击跳过 /// - [ObservableProperty] private bool _enabled = true; + [ObservableProperty] + private bool _enabled = true; /// /// 快速跳过对话 /// - [ObservableProperty] private bool _quicklySkipConversationsEnabled = true; + [ObservableProperty] + private bool _quicklySkipConversationsEnabled = true; public int ChatOptionTextWidth { get; set; } = 280; @@ -30,50 +33,59 @@ public partial class AutoSkipConfig : ObservableObject /// /// 选择选项前的延迟(毫秒) /// - [ObservableProperty] private int _afterChooseOptionSleepDelay = 0; + [ObservableProperty] + private int _afterChooseOptionSleepDelay = 0; /// /// 自动领取每日委托奖励 /// - [ObservableProperty] private bool _autoGetDailyRewardsEnabled = true; + [ObservableProperty] + private bool _autoGetDailyRewardsEnabled = true; /// /// 自动重新派遣 /// - [ObservableProperty] private bool _autoReExploreEnabled = true; + [ObservableProperty] + private bool _autoReExploreEnabled = true; /// /// 自动重新派遣使用角色配置,逗号分割 /// [Obsolete] - [ObservableProperty] private string _autoReExploreCharacter = ""; + [ObservableProperty] + private string _autoReExploreCharacter = ""; /// /// 优先选择第一个选项 /// 优先选择最后一个选项 /// 不选择选项 /// - [ObservableProperty] private string _clickChatOption = "优先选择最后一个选项"; + [ObservableProperty] + private string _clickChatOption = "优先选择第一个选项"; /// /// 自动邀约启用 /// - [ObservableProperty] private bool _autoHangoutEventEnabled = false; + [ObservableProperty] + private bool _autoHangoutEventEnabled = false; /// /// 自动邀约分支选择 /// - [ObservableProperty] private string _autoHangoutEndChoose = string.Empty; + [ObservableProperty] + private string _autoHangoutEndChoose = string.Empty; /// /// 自动邀约选择选项前的延迟(毫秒) /// - [ObservableProperty] private int _autoHangoutChooseOptionSleepDelay = 0; + [ObservableProperty] + private int _autoHangoutChooseOptionSleepDelay = 0; /// /// 自动邀约自动点击跳过按钮 /// - [ObservableProperty] private bool _autoHangoutPressSkipEnabled = true; + [ObservableProperty] + private bool _autoHangoutPressSkipEnabled = true; public bool IsClickFirstChatOption() { @@ -93,15 +105,25 @@ public bool IsClickNoneChatOption() /// /// 后台运行 /// - [ObservableProperty] private bool _runBackgroundEnabled = false; + [ObservableProperty] + private bool _runBackgroundEnabled = false; /// /// 提交物品 /// - [ObservableProperty] private bool _submitGoodsEnabled = true; + [ObservableProperty] + private bool _submitGoodsEnabled = true; /// /// 关闭弹出层 /// - [ObservableProperty] private bool _closePopupPagedEnabled = true; -} + [ObservableProperty] + private bool _closePopupPagedEnabled = true; + + // /// + // /// 使用交互键 + // /// 使用鼠标 + // /// + // [ObservableProperty] + // private string _selectChatOptionType = SelectChatOptionTypes.UseMouse; +} \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs index 9325db62..183b1d57 100644 --- a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs +++ b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs @@ -21,6 +21,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; +using BetterGenshinImpact.GameTask.AutoPick.Assets; using Vanara.PInvoke; using Region = BetterGenshinImpact.GameTask.Model.Area.Region; @@ -38,7 +39,9 @@ public partial class AutoSkipTrigger : ITaskTrigger public int Priority => 20; public bool IsExclusive => false; - public bool IsBackgroundRunning { get; set; } + public bool IsBackgroundRunning { get; private set; } + + public bool IsUseInteractionKey { get; set; } = false; private readonly AutoSkipAssets _autoSkipAssets; @@ -71,6 +74,7 @@ public void Init() { IsEnabled = _config.Enabled; IsBackgroundRunning = _config.RunBackgroundEnabled; + // IsUseInteractionKey = _config.SelectChatOptionType == SelectChatOptionTypes.UseInteractionKey; _postMessageSimulator = TaskContext.Instance().PostMessageSimulator; try @@ -173,18 +177,27 @@ public void OnCapture(CaptureContent content) _prevPlayingTime = DateTime.Now; if (TaskContext.Instance().Config.AutoSkipConfig.QuicklySkipConversationsEnabled) { - if (IsBackgroundRunning) + if (IsUseInteractionKey) { - _postMessageSimulator?.KeyPressBackground(User32.VK.VK_SPACE); + _postMessageSimulator?.KeyPressBackground(User32.VK.VK_F); // 注意这里不是交互键 } else { - Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_SPACE); + _postMessageSimulator?.KeyPressBackground(User32.VK.VK_SPACE); } } // 对话选项选择 - var hasOption = ChatOptionChoose(content.CaptureRectArea); + bool hasOption; + if (IsBackgroundRunning || IsUseInteractionKey) + { + hasOption = ChatOptionChooseUseKey(content.CaptureRectArea); + } + else + { + hasOption = ChatOptionChoose(content.CaptureRectArea); + } + // 邀约选项选择 1s 1次 if (_config.AutoHangoutEventEnabled && !hasOption) @@ -234,6 +247,7 @@ private bool ClickBlackGameScreen(CaptureContent content) return true; } } + return false; } @@ -325,6 +339,7 @@ private void HangoutOptionChoose(ImageRegion captureRegion) { skipRa.Click(); } + AutoHangoutSkipLog("点击跳过按钮"); } } @@ -374,6 +389,63 @@ private void GetDailyRewardsEsc(AutoSkipConfig config, CaptureContent content) [GeneratedRegex(@"^[a-zA-Z0-9]+$")] private static partial Regex EnOrNumRegex(); + /// + /// 5.2 版本直接交互键就能使用的对话选择 + /// + /// + /// + private bool ChatOptionChooseUseKey(ImageRegion region) + { + if (_config.IsClickNoneChatOption()) + { + return false; + } + + using var chatOptionResult = region.Find(_autoSkipAssets.OptionIconRo); + var isInChat = false; + isInChat = chatOptionResult.IsExist(); + if (!isInChat) + { + using var pickRa = region.Find(AutoPickAssets.Instance.ChatPickRo); + isInChat = pickRa.IsExist(); + } + + if (isInChat) + { + var fKey = AutoPickAssets.Instance.PickVk; + if (_config.IsClickFirstChatOption()) + { + _postMessageSimulator?.KeyPressBackground(fKey); + } + else if (_config.IsClickRandomChatOption()) + { + var random = new Random(); + // 随机 0~4 的数字 + var r = random.Next(0, 5); + for (var j = 0; j < r; j++) + { + _postMessageSimulator?.KeyPressBackground(User32.VK.VK_S); + Thread.Sleep(100); + } + + Thread.Sleep(50); + _postMessageSimulator?.KeyPressBackground(fKey); + } + else + { + _postMessageSimulator?.KeyPressBackground(User32.VK.VK_W); + Thread.Sleep(100); + _postMessageSimulator?.KeyPressBackground(fKey); + } + + AutoSkipLog("交互键点击(后台)"); + + return true; + } + + return false; + } + /// /// 新的对话选项选择 /// @@ -385,6 +457,7 @@ private bool ChatOptionChoose(ImageRegion region) { return false; } + var assetScale = TaskContext.Instance().SystemInfo.AssetScale; // 感叹号识别 遇到直接点击 @@ -428,6 +501,7 @@ private bool ChatOptionChoose(ImageRegion region) { continue; } + if (i != ocrResList.Count - 1) { if (ocrResList[i + 1].Y - ocrResList[i].Y > 150) @@ -520,7 +594,7 @@ private bool ChatOptionChoose(ImageRegion region) // 没OCR到文字,直接选择气泡选项 Thread.Sleep(_config.AfterChooseOptionSleepDelay); - clickRect.Click(); + ClickOcrRegion(clickRect); var msg = _config.IsClickFirstChatOption() ? "第一个" : "最后一个"; AutoSkipLog($"点击{msg}气泡选项"); } @@ -537,6 +611,7 @@ private void ClickOcrRegion(Region region, string optionType = "") { Thread.Sleep(_config.AfterChooseOptionSleepDelay); } + if (IsBackgroundRunning && !SystemControl.IsGenshinImpactActive()) { region.BackgroundClick(); @@ -545,6 +620,7 @@ private void ClickOcrRegion(Region region, string optionType = "") { region.Click(); } + AutoSkipLog(region.Text); } @@ -554,6 +630,7 @@ private void HangoutOptionClick(HangoutOption option) { Thread.Sleep(_config.AutoHangoutChooseOptionSleepDelay); } + if (IsBackgroundRunning && !SystemControl.IsGenshinImpactActive()) { option.BackgroundClick(); @@ -720,4 +797,4 @@ private bool SubmitGoods(CaptureContent content) return false; } -} +} \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/AutoSkip/Model/SelectChatOptionTypes.cs b/BetterGenshinImpact/GameTask/AutoSkip/Model/SelectChatOptionTypes.cs new file mode 100644 index 00000000..09b0d28d --- /dev/null +++ b/BetterGenshinImpact/GameTask/AutoSkip/Model/SelectChatOptionTypes.cs @@ -0,0 +1,12 @@ + + +namespace BetterGenshinImpact.GameTask.AutoSkip.Model; + +public class SelectChatOptionTypes +{ + // 使用交互键 + public const string UseInteractionKey = "使用交互键"; + + // 使用鼠标 + public const string UseMouse = "使用鼠标"; +} \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml b/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml index 139ecf8f..56684c16 100644 --- a/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml @@ -234,7 +234,7 @@ + @@ -284,7 +310,7 @@