diff --git a/BetterGenshinImpact/GameTask/AutoWood/AutoWoodTask.cs b/BetterGenshinImpact/GameTask/AutoWood/AutoWoodTask.cs index 97a26073..24439cfa 100644 --- a/BetterGenshinImpact/GameTask/AutoWood/AutoWoodTask.cs +++ b/BetterGenshinImpact/GameTask/AutoWood/AutoWoodTask.cs @@ -51,8 +51,7 @@ public class AutoWoodTask _login3rdParty.RefreshAvailabled(); if (_login3rdParty.Type == Login3rdParty.The3rdPartyType.Bilibili) { - // TODO - throw new NormalEndException("暂未支持B服伐木,后续将会增加支持!"); + Logger.LogInformation("自动伐木启用B服模式"); } Felling(taskParam); VisionContext.Instance().DrawContent.ClearAll(); @@ -159,6 +158,13 @@ public class AutoWoodTask NewRetry.Do(() => { Sleep(1, taskParam.Cts); + + if (_login3rdParty.IsAvailabled) + { + _login3rdParty.Login(taskParam.Cts); + Sleep(2000, taskParam.Cts); + } + var content = CaptureToContent(taskParam.Dispatcher.GameCapture); var ra = content.CaptureRectArea.Find(_assets.EnterGameRo); if (ra.IsEmpty()) diff --git a/BetterGenshinImpact/GameTask/AutoWood/Utils/Login3rdParty.cs b/BetterGenshinImpact/GameTask/AutoWood/Utils/Login3rdParty.cs index e7d64f77..c0c387e4 100644 --- a/BetterGenshinImpact/GameTask/AutoWood/Utils/Login3rdParty.cs +++ b/BetterGenshinImpact/GameTask/AutoWood/Utils/Login3rdParty.cs @@ -1,9 +1,15 @@ -using System.Diagnostics; +using BetterGenshinImpact.Core.Recognition.OpenCv; +using BetterGenshinImpact.Helpers.Extensions; +using System; +using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; +using System.Threading; +using System.Threading.Tasks; using Vanara.PInvoke; +using static BetterGenshinImpact.GameTask.Common.TaskControl; namespace BetterGenshinImpact.GameTask.AutoWood.Utils; @@ -71,8 +77,68 @@ internal sealed class Login3rdParty } } - public void Login() + public async void Login(CancellationTokenSource cts) { - // TODO + await Task.Run(() => + { + while (!LoginPrivate(cts)) + { + Sleep(500, cts); + } + }, cts.Token); + } + + private bool LoginPrivate(CancellationTokenSource cts) + { + if (Type == The3rdPartyType.Bilibili) + { + if (Process.GetProcessesByName("YuanShen").FirstOrDefault() is Process process) + { + nint hwndLogin = IntPtr.Zero; + + _ = User32.EnumWindows((HWND hWnd, nint lParam) => + { + try + { + _ = User32.GetWindowThreadProcessId(hWnd, out uint pid); + + if (pid == process.Id) + { + int capacity = User32.GetWindowTextLength(hWnd); + StringBuilder title = new(capacity + 1); + _ = User32.GetWindowText(hWnd, title, title.Capacity); + + if (!string.IsNullOrEmpty(title.ToString())) + { + hwndLogin = (nint)hWnd; + return false; + } + } + } + catch + { + } + return true; + }, 0); + + if (hwndLogin == IntPtr.Zero) + { + return false; + } + + // Just for login WebUI chattering + Sleep(400, cts); + + var p = TaskContext.Instance().SystemInfo.CaptureAreaRect.GetCenterPoint(); + p.Add(new(0, 125)).Click(); + return true; + } + return false; + } + else + { + // Ignore and exit with OK + return true; + } } }