auto music opt

This commit is contained in:
辉鸭蛋 2025-01-01 19:34:39 +08:00
parent 85c7707c7a
commit 3c393087eb
2 changed files with 52 additions and 19 deletions

View File

@ -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;
}
}

View File

@ -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<Task>();
// 计算按键位置
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<Task>();
// 计算按键位置
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("回到游戏主界面时记得关闭自动音游任务!");
}
}