From 3c393087ebcd3614d1de9dd10ab9308fa70d50a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Wed, 1 Jan 2025 19:34:39 +0800 Subject: [PATCH] auto music opt --- .../Core/Simulator/Simulation.cs | 27 +++++++++++- .../AutoMusicGame/AutoMusicGameTask.cs | 44 +++++++++++-------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/BetterGenshinImpact/Core/Simulator/Simulation.cs b/BetterGenshinImpact/Core/Simulator/Simulation.cs index 9939ef9f..5f56046d 100644 --- a/BetterGenshinImpact/Core/Simulator/Simulation.cs +++ b/BetterGenshinImpact/Core/Simulator/Simulation.cs @@ -1,5 +1,8 @@ using Fischless.WindowsInput; using System; +using BetterGenshinImpact.GameTask.Common; +using Microsoft.Extensions.Logging; +using Vanara.PInvoke; namespace BetterGenshinImpact.Core.Simulator; @@ -13,4 +16,26 @@ public class Simulation { return new PostMessageSimulator(hWnd); } -} + + public static void ReleaseAllKey() + { + foreach (User32.VK key in Enum.GetValues(typeof(User32.VK))) + { + // 检查键是否被按下 + if (IsKeyDown(key)) // 强制转换 VK 枚举为 int + { + TaskControl.Logger.LogDebug($"解除{key}的按下状态."); + SendInput.Keyboard.KeyUp(key); + } + } + } + + private static bool IsKeyDown(User32.VK key) + { + // 获取按键状态 + var state = User32.GetAsyncKeyState((int)key); + + // 检查高位是否为 1(表示按键被按下) + return (state & 0x8000) != 0; + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/AutoMusicGame/AutoMusicGameTask.cs b/BetterGenshinImpact/GameTask/AutoMusicGame/AutoMusicGameTask.cs index a2c18cdf..0d3525b9 100644 --- a/BetterGenshinImpact/GameTask/AutoMusicGame/AutoMusicGameTask.cs +++ b/BetterGenshinImpact/GameTask/AutoMusicGame/AutoMusicGameTask.cs @@ -30,26 +30,32 @@ public class AutoMusicGameTask(AutoMusicGameParam taskParam) : ISoloTask private readonly IntPtr _hWnd = TaskContext.Instance().GameHandle; - public Task Start(CancellationToken ct) + public async Task Start(CancellationToken ct) { - Init(); - - var assetScale = TaskContext.Instance().SystemInfo.AssetScale; - var taskFactory = new TaskFactory(); - var taskList = new List(); - - // 计算按键位置 - var gameCaptureRegion = CaptureToRectArea(); - - foreach (var keyValuePair in _keyX) + try { - var (x, y) = gameCaptureRegion.ConvertPositionToGameCaptureRegion((int)(keyValuePair.Value * assetScale), (int)(_keyY * assetScale)); - // 添加任务 - taskList.Add(taskFactory.StartNew(async () => await DoWhitePressWin32(ct, keyValuePair.Key, new Point(x, y)), ct)); - } + Init(); - Task.WaitAll([.. taskList], ct); - return Task.CompletedTask; + var assetScale = TaskContext.Instance().SystemInfo.AssetScale; + // var taskFactory = new TaskFactory(); + var taskList = new List(); + + // 计算按键位置 + var gameCaptureRegion = CaptureToRectArea(); + + foreach (var keyValuePair in _keyX) + { + var (x, y) = gameCaptureRegion.ConvertPositionToGameCaptureRegion((int)(keyValuePair.Value * assetScale), (int)(_keyY * assetScale)); + // 添加任务 + taskList.Add(Task.Run(async () => await DoWhitePressWin32(ct, keyValuePair.Key, new Point(x, y)), ct)); + } + + await Task.WhenAll(taskList); + } + finally + { + Simulation.ReleaseAllKey(); + } } private async Task DoWhitePressWin32(CancellationToken ct, User32.VK key, Point point) @@ -106,7 +112,9 @@ public class AutoMusicGameTask(AutoMusicGameParam taskParam) : ISoloTask var gameScreenSize = SystemControl.GetGameScreenRect(TaskContext.Instance().GameHandle); if (gameScreenSize.Width * 9 != gameScreenSize.Height * 16) { - Logger.LogWarning("游戏窗口分辨率不是 16:9 !当前分辨率为 {Width}x{Height} , 非 16:9 分辨率的游戏可能无法正常使用自动活动音游功能 !", gameScreenSize.Width, gameScreenSize.Height); + Logger.LogError("游戏窗口分辨率不是 16:9 !当前分辨率为 {Width}x{Height} , 非 16:9 分辨率的游戏无法正常使用自动活动音游功能 !", gameScreenSize.Width, gameScreenSize.Height); + throw new Exception("游戏窗口分辨率不是 16:9"); } + Logger.LogInformation("回到游戏主界面时记得关闭自动音游任务!"); } } \ No newline at end of file