mirror of
https://github.com/babalae/better-genshin-impact
synced 2025-01-08 11:57:53 +08:00
auto music opt
This commit is contained in:
parent
85c7707c7a
commit
3c393087eb
@ -1,5 +1,8 @@
|
|||||||
using Fischless.WindowsInput;
|
using Fischless.WindowsInput;
|
||||||
using System;
|
using System;
|
||||||
|
using BetterGenshinImpact.GameTask.Common;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Vanara.PInvoke;
|
||||||
|
|
||||||
namespace BetterGenshinImpact.Core.Simulator;
|
namespace BetterGenshinImpact.Core.Simulator;
|
||||||
|
|
||||||
@ -13,4 +16,26 @@ public class Simulation
|
|||||||
{
|
{
|
||||||
return new PostMessageSimulator(hWnd);
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -30,26 +30,32 @@ public class AutoMusicGameTask(AutoMusicGameParam taskParam) : ISoloTask
|
|||||||
|
|
||||||
private readonly IntPtr _hWnd = TaskContext.Instance().GameHandle;
|
private readonly IntPtr _hWnd = TaskContext.Instance().GameHandle;
|
||||||
|
|
||||||
public Task Start(CancellationToken ct)
|
public async Task Start(CancellationToken ct)
|
||||||
{
|
{
|
||||||
Init();
|
try
|
||||||
|
|
||||||
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));
|
Init();
|
||||||
// 添加任务
|
|
||||||
taskList.Add(taskFactory.StartNew(async () => await DoWhitePressWin32(ct, keyValuePair.Key, new Point(x, y)), ct));
|
|
||||||
}
|
|
||||||
|
|
||||||
Task.WaitAll([.. taskList], ct);
|
var assetScale = TaskContext.Instance().SystemInfo.AssetScale;
|
||||||
return Task.CompletedTask;
|
// 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)
|
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);
|
var gameScreenSize = SystemControl.GetGameScreenRect(TaskContext.Instance().GameHandle);
|
||||||
if (gameScreenSize.Width * 9 != gameScreenSize.Height * 16)
|
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("回到游戏主界面时记得关闭自动音游任务!");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user