From 644d820315fe5a7be5f456d825777d855d4c94bb Mon Sep 17 00:00:00 2001 From: Horror Proton <107091537+horror-proton@users.noreply.github.com> Date: Wed, 7 Feb 2024 19:09:56 +0800 Subject: [PATCH] fix: fix typo and undefined behavior of minmax --- src/MaaCore/Controller/AdbController.cpp | 36 +++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/MaaCore/Controller/AdbController.cpp b/src/MaaCore/Controller/AdbController.cpp index 3dd4e187bc..fc939b705c 100644 --- a/src/MaaCore/Controller/AdbController.cpp +++ b/src/MaaCore/Controller/AdbController.cpp @@ -458,7 +458,7 @@ bool asst::AdbController::screencap(cv::Mat& image_payload, bool allow_reconnect break; } // 记录截图耗时,每10次截图回传一次最值+平均值 - m_screencap_duration.emplace_back(screencap_ret ? m_last_command_duration : INT_MAX); // 记录截图耗时 + m_screencap_duration.emplace_back(screencap_ret ? m_last_command_duration : LLONG_MAX); // 记录截图耗时 ++m_screencap_time; if (m_screencap_duration.size() > 30) { @@ -466,23 +466,25 @@ bool asst::AdbController::screencap(cv::Mat& image_payload, bool allow_reconnect } if (m_screencap_time > 9) { // 每 10 次截图计算一次平均耗时 m_screencap_time = 0; - auto filted_duration = m_screencap_duration | views::filter([](long long num) { return num < INT_MAX; }); + auto filtered_duration = + m_screencap_duration | views::filter([](long long num) { return num < LLONG_MAX; }); // 过滤后的有效截图用时次数 - auto filted_count = m_screencap_duration.size() - ranges::count(m_screencap_duration, INT_MAX); - auto [screencap_cost_min, screencap_cost_max] = ranges::minmax(filted_duration); - json::value info = json::object { - { "uuid", m_uuid }, - { "what", "ScreencapCost" }, - { "details", - json::object { - { "min", screencap_cost_min }, - { "max", screencap_cost_max }, - { "avg", filted_count > 0 - ? std::accumulate(filted_duration.begin(), filted_duration.end(), 0ll) / filted_count - : -1 }, - } }, - }; - callback(AsstMsg::ConnectionInfo, info); + auto filtered_count = m_screencap_duration.size() - ranges::count(m_screencap_duration, LLONG_MAX); + if (filtered_count != 0) { + auto [screencap_cost_min, screencap_cost_max] = ranges::minmax(filtered_duration); + json::value info = json::object { + { "uuid", m_uuid }, + { "what", "ScreencapCost" }, + { "details", + json::object { + { "min", screencap_cost_min }, + { "max", screencap_cost_max }, + { "avg", + std::accumulate(filtered_duration.begin(), filtered_duration.end(), 0LL) / filtered_count }, + } }, + }; + callback(AsstMsg::ConnectionInfo, info); + } } return screencap_ret; }