From 2829455fa259e5ca73800e77a3d02bfd6a75fde9 Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Wed, 18 Sep 2024 00:41:30 +0800 Subject: [PATCH] feat: capture fresh image (#10460) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 使用 AsstAsyncScreencap 的实时截图 --------- Co-authored-by: uye <99072975+ABA2396@users.noreply.github.com> * feat: 增加空阻塞任务以便调试 * chore: 增加帧率显示 --------- Co-authored-by: zzyyyl --- resource/tasks.json | 5 +++ src/MaaCore/Assistant.h | 2 +- src/MaaWpfGui/Main/AsstProxy.cs | 9 ++++ src/MaaWpfGui/Services/MaaService.cs | 3 ++ .../ViewModels/UI/RecognizerViewModel.cs | 44 +++++++++++++------ src/MaaWpfGui/Views/UI/RecognizerView.xaml | 22 ++++++++-- 6 files changed, 67 insertions(+), 18 deletions(-) diff --git a/resource/tasks.json b/resource/tasks.json index b36941452a..bca77475eb 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -1,4 +1,9 @@ { + "Block": { + "algorithm": "JustReturn", + "postDelay": 1000, + "next": ["#self"] + }, "SlowlySwipeToTheLeft": { "algorithm": "JustReturn", "action": "Swipe", diff --git a/src/MaaCore/Assistant.h b/src/MaaCore/Assistant.h index 4d61b9a450..dbaca7c1c6 100644 --- a/src/MaaCore/Assistant.h +++ b/src/MaaCore/Assistant.h @@ -99,7 +99,7 @@ namespace asst public: std::shared_ptr ctrler() const { return m_ctrler; } std::shared_ptr status() const { return m_status; } - bool need_exit() const { return m_thread_idle; } + bool need_exit() const { return m_thread_idle && m_running; } private: void append_callback(AsstMsg msg, const json::value& detail); diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index 795fca5cb3..206a87121d 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -194,6 +194,12 @@ namespace MaaWpfGui.Main return AsstGetImage(_handle); } + public BitmapImage? AsstGetFreshImage() + { + MaaService.AsstAsyncScreencap(_handle, true); + return AsstGetImage(_handle); + } + private readonly MaaService.CallbackDelegate _callback; /// @@ -2599,6 +2605,9 @@ namespace MaaWpfGui.Main ["task_names"] = new JArray { once ? "GachaOnce" : "GachaTenTimes", + + // TEST + // "Block", }, }; AsstTaskId id = AsstAppendTaskWithEncoding("Custom", taskParams); diff --git a/src/MaaWpfGui/Services/MaaService.cs b/src/MaaWpfGui/Services/MaaService.cs index f2f0308bea..4e071c3a8e 100644 --- a/src/MaaWpfGui/Services/MaaService.cs +++ b/src/MaaWpfGui/Services/MaaService.cs @@ -60,6 +60,9 @@ namespace MaaWpfGui.Services [DllImport("MaaCore.dll")] public static extern bool AsstStop(AsstHandle handle); + [DllImport("MaaCore.dll")] + public static extern unsafe Int32 AsstAsyncScreencap(AsstHandle handle, bool block); + [DllImport("MaaCore.dll")] public static extern unsafe ulong AsstGetImage(AsstHandle handle, byte* buff, ulong buffSize); diff --git a/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs b/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs index c2a9f54f81..8a7e495a1a 100644 --- a/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/RecognizerViewModel.cs @@ -747,6 +747,14 @@ namespace MaaWpfGui.ViewModels.UI set => SetAndNotify(ref _gachaInfo, value); } + private double _gachaScreenFpf; + + public double GachaScreenFpf + { + get => _gachaScreenFpf; + set => SetAndNotify(ref _gachaScreenFpf, value); + } + // xaml 中用到了 // ReSharper disable once UnusedMember.Global public void GachaOnce() @@ -782,15 +790,13 @@ namespace MaaWpfGui.ViewModels.UI return; } - _gachaImageTimer.Interval = TimeSpan.FromMilliseconds(500); + _gachaImageTimer.Interval = TimeSpan.FromMilliseconds(10); _gachaImageTimer.Tick += RefreshGachaImage; _gachaImageTimer.Start(); } private readonly DispatcherTimer _gachaImageTimer = new(); - private static readonly object _lock = new(); - private BitmapImage? _gachaImage; public BitmapImage? GachaImage @@ -799,21 +805,31 @@ namespace MaaWpfGui.ViewModels.UI set => SetAndNotify(ref _gachaImage, value); } + private DateTime _lastTipUpdateTime = DateTime.MinValue; + private DateTime _lastFpsUpdateTime = DateTime.MinValue; + private int _frameCount; + private void RefreshGachaImage(object? sender, EventArgs? e) { - lock (_lock) + GachaImage = Instances.AsstProxy.AsstGetFreshImage(); + + var now = DateTime.Now; + _frameCount++; + if ((now - _lastFpsUpdateTime).TotalSeconds >= 1) { - var image = Instances.AsstProxy.AsstGetImage(); - if (GachaImage.IsEqual(image)) - { - return; - } - - GachaImage = image; - - var rd = new Random(); - GachaInfo = LocalizationHelper.GetString("GachaTip" + rd.Next(1, 18).ToString()); + GachaScreenFpf = _frameCount / (now - _lastFpsUpdateTime).TotalSeconds; + _frameCount = 0; + _lastFpsUpdateTime = now; } + + if ((now - _lastTipUpdateTime).TotalSeconds < 5) + { + return; + } + + var rd = new Random(); + GachaInfo = LocalizationHelper.GetString("GachaTip" + rd.Next(1, 18)); + _lastTipUpdateTime = now; } // DO NOT CHANGE diff --git a/src/MaaWpfGui/Views/UI/RecognizerView.xaml b/src/MaaWpfGui/Views/UI/RecognizerView.xaml index 87c806d0f4..3f6f0fc9e8 100644 --- a/src/MaaWpfGui/Views/UI/RecognizerView.xaml +++ b/src/MaaWpfGui/Views/UI/RecognizerView.xaml @@ -358,12 +358,28 @@ Text="{Binding GachaInfo}" TextWrapping="Wrap" Visibility="{c:Binding !GachaShowDisclaimer}" /> - + Visibility="{c:Binding !GachaShowDisclaimer}"> + + + + +