Skip to content

Commit

Permalink
auto skip: background skip use key (5.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Nov 22, 2024
1 parent 382f31b commit 2a2acb5
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 28 deletions.
20 changes: 19 additions & 1 deletion BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AutoPickAssets : BaseAssets<AutoPickAssets>

public User32.VK PickVk = User32.VK.VK_F;
public RecognitionObject PickRo;
public RecognitionObject ChatPickRo;

private AutoPickAssets()
{
Expand Down Expand Up @@ -59,6 +60,7 @@ private AutoPickAssets()
{
PickRo = LoadCustomPickKey(keyName);
PickVk = User32Helper.ToVk(keyName);
ChatPickRo = LoadCustomChatPickKey(keyName);
}
catch (Exception e)
{
Expand All @@ -67,6 +69,7 @@ private AutoPickAssets()
TaskContext.Instance().Config.AutoPickConfig.PickKey = "F";
return;
}

if (keyName != "F")
{
_logger.LogInformation("自定义拾取按键:{Key}", keyName);
Expand All @@ -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();
}
}
3 changes: 1 addition & 2 deletions BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public partial class AutoPickTrigger : ITaskTrigger
/// 拾取白名单
/// </summary>
private List<string> _whiteList = [];

private User32.VK _pickVk = User32.VK.VK_F;

private RecognitionObject _pickRo;

// 外部配置
Expand Down
52 changes: 37 additions & 15 deletions BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using BetterGenshinImpact.GameTask.AutoSkip.Model;

namespace BetterGenshinImpact.GameTask.AutoSkip;

Expand All @@ -16,12 +17,14 @@ public partial class AutoSkipConfig : ObservableObject
/// 2. 自动点击一个识别到的选项
/// 3. 黑屏过长自动点击跳过
/// </summary>
[ObservableProperty] private bool _enabled = true;
[ObservableProperty]
private bool _enabled = true;

/// <summary>
/// 快速跳过对话
/// </summary>
[ObservableProperty] private bool _quicklySkipConversationsEnabled = true;
[ObservableProperty]
private bool _quicklySkipConversationsEnabled = true;

public int ChatOptionTextWidth { get; set; } = 280;

Expand All @@ -30,50 +33,59 @@ public partial class AutoSkipConfig : ObservableObject
/// <summary>
/// 选择选项前的延迟(毫秒)
/// </summary>
[ObservableProperty] private int _afterChooseOptionSleepDelay = 0;
[ObservableProperty]
private int _afterChooseOptionSleepDelay = 0;

/// <summary>
/// 自动领取每日委托奖励
/// </summary>
[ObservableProperty] private bool _autoGetDailyRewardsEnabled = true;
[ObservableProperty]
private bool _autoGetDailyRewardsEnabled = true;

/// <summary>
/// 自动重新派遣
/// </summary>
[ObservableProperty] private bool _autoReExploreEnabled = true;
[ObservableProperty]
private bool _autoReExploreEnabled = true;

/// <summary>
/// 自动重新派遣使用角色配置,逗号分割
/// </summary>
[Obsolete]
[ObservableProperty] private string _autoReExploreCharacter = "";
[ObservableProperty]
private string _autoReExploreCharacter = "";

/// <summary>
/// 优先选择第一个选项
/// 优先选择最后一个选项
/// 不选择选项
/// </summary>
[ObservableProperty] private string _clickChatOption = "优先选择最后一个选项";
[ObservableProperty]
private string _clickChatOption = "优先选择第一个选项";

/// <summary>
/// 自动邀约启用
/// </summary>
[ObservableProperty] private bool _autoHangoutEventEnabled = false;
[ObservableProperty]
private bool _autoHangoutEventEnabled = false;

/// <summary>
/// 自动邀约分支选择
/// </summary>
[ObservableProperty] private string _autoHangoutEndChoose = string.Empty;
[ObservableProperty]
private string _autoHangoutEndChoose = string.Empty;

/// <summary>
/// 自动邀约选择选项前的延迟(毫秒)
/// </summary>
[ObservableProperty] private int _autoHangoutChooseOptionSleepDelay = 0;
[ObservableProperty]
private int _autoHangoutChooseOptionSleepDelay = 0;

/// <summary>
/// 自动邀约自动点击跳过按钮
/// </summary>
[ObservableProperty] private bool _autoHangoutPressSkipEnabled = true;
[ObservableProperty]
private bool _autoHangoutPressSkipEnabled = true;

public bool IsClickFirstChatOption()
{
Expand All @@ -93,15 +105,25 @@ public bool IsClickNoneChatOption()
/// <summary>
/// 后台运行
/// </summary>
[ObservableProperty] private bool _runBackgroundEnabled = false;
[ObservableProperty]
private bool _runBackgroundEnabled = false;

/// <summary>
/// 提交物品
/// </summary>
[ObservableProperty] private bool _submitGoodsEnabled = true;
[ObservableProperty]
private bool _submitGoodsEnabled = true;

/// <summary>
/// 关闭弹出层
/// </summary>
[ObservableProperty] private bool _closePopupPagedEnabled = true;
}
[ObservableProperty]
private bool _closePopupPagedEnabled = true;

// /// <summary>
// /// 使用交互键
// /// 使用鼠标
// /// </summary>
// [ObservableProperty]
// private string _selectChatOptionType = SelectChatOptionTypes.UseMouse;
}
91 changes: 84 additions & 7 deletions BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -71,6 +74,7 @@ public void Init()
{
IsEnabled = _config.Enabled;
IsBackgroundRunning = _config.RunBackgroundEnabled;
// IsUseInteractionKey = _config.SelectChatOptionType == SelectChatOptionTypes.UseInteractionKey;
_postMessageSimulator = TaskContext.Instance().PostMessageSimulator;

try
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -234,6 +247,7 @@ private bool ClickBlackGameScreen(CaptureContent content)
return true;
}
}

return false;
}

Expand Down Expand Up @@ -325,6 +339,7 @@ private void HangoutOptionChoose(ImageRegion captureRegion)
{
skipRa.Click();
}

AutoHangoutSkipLog("点击跳过按钮");
}
}
Expand Down Expand Up @@ -374,6 +389,63 @@ private void GetDailyRewardsEsc(AutoSkipConfig config, CaptureContent content)
[GeneratedRegex(@"^[a-zA-Z0-9]+$")]
private static partial Regex EnOrNumRegex();

/// <summary>
/// 5.2 版本直接交互键就能使用的对话选择
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
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;
}

/// <summary>
/// 新的对话选项选择
///
Expand All @@ -385,6 +457,7 @@ private bool ChatOptionChoose(ImageRegion region)
{
return false;
}

var assetScale = TaskContext.Instance().SystemInfo.AssetScale;

// 感叹号识别 遇到直接点击
Expand Down Expand Up @@ -428,6 +501,7 @@ private bool ChatOptionChoose(ImageRegion region)
{
continue;
}

if (i != ocrResList.Count - 1)
{
if (ocrResList[i + 1].Y - ocrResList[i].Y > 150)
Expand Down Expand Up @@ -520,7 +594,7 @@ private bool ChatOptionChoose(ImageRegion region)

// 没OCR到文字,直接选择气泡选项
Thread.Sleep(_config.AfterChooseOptionSleepDelay);
clickRect.Click();
ClickOcrRegion(clickRect);
var msg = _config.IsClickFirstChatOption() ? "第一个" : "最后一个";
AutoSkipLog($"点击{msg}气泡选项");
}
Expand All @@ -537,6 +611,7 @@ private void ClickOcrRegion(Region region, string optionType = "")
{
Thread.Sleep(_config.AfterChooseOptionSleepDelay);
}

if (IsBackgroundRunning && !SystemControl.IsGenshinImpactActive())
{
region.BackgroundClick();
Expand All @@ -545,6 +620,7 @@ private void ClickOcrRegion(Region region, string optionType = "")
{
region.Click();
}

AutoSkipLog(region.Text);
}

Expand All @@ -554,6 +630,7 @@ private void HangoutOptionClick(HangoutOption option)
{
Thread.Sleep(_config.AutoHangoutChooseOptionSleepDelay);
}

if (IsBackgroundRunning && !SystemControl.IsGenshinImpactActive())
{
option.BackgroundClick();
Expand Down Expand Up @@ -720,4 +797,4 @@ private bool SubmitGoods(CaptureContent content)

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


namespace BetterGenshinImpact.GameTask.AutoSkip.Model;

public class SelectChatOptionTypes
{
// 使用交互键
public const string UseInteractionKey = "使用交互键";

// 使用鼠标
public const string UseMouse = "使用鼠标";
}
Loading

0 comments on commit 2a2acb5

Please sign in to comment.