From d8df4e753e28a3bf2b6b41ccea3b0c3e6211e762 Mon Sep 17 00:00:00 2001 From: Alex Gu Date: Sun, 19 Apr 2026 10:18:27 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20macOS=20PlayTools/SCK=20=E5=87=A0?= =?UTF-8?q?=E5=A4=84=E5=B0=8F=E4=BF=AE=E6=AD=A3=20(#16276)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: macOS 相关几处小修正 - PlayToolsController: screencap_rgba/screencap_bgr/toucher_commit 调用 open() 时未检查返回值,连接失败时继续操作会写坏的 socket - PlayToolsController: start_game() 日志写成了 InputText,应该是 StartGame - OcrPack: macOS 分支日志里残留了多余的 "rec" 字样 - MacSCKHelper: 析构时 stopCapture 是异步的,没等回调完成就释放了 stream/output/queue,可能踩到野指针;改用信号量等停完再释放 --- src/MaaCore/Config/Miscellaneous/OcrPack.cpp | 2 +- src/MaaCore/Controller/MacSCKHelper.mm | 13 ++++++++++++- src/MaaCore/Controller/PlayToolsController.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/MaaCore/Config/Miscellaneous/OcrPack.cpp b/src/MaaCore/Config/Miscellaneous/OcrPack.cpp index df9d4638b8..f4f25a0310 100644 --- a/src/MaaCore/Config/Miscellaneous/OcrPack.cpp +++ b/src/MaaCore/Config/Miscellaneous/OcrPack.cpp @@ -204,7 +204,7 @@ bool asst::OcrPack::check_and_load() rec_option.UseCpu(); det_option.SetCpuThreadNum(cpu_threads); rec_option.SetCpuThreadNum(cpu_threads); - Log.info("FastDeploy macOS mode with rec", cpu_threads, "CPU threads"); + Log.info("FastDeploy macOS mode with", cpu_threads, "CPU threads"); #else det_option.UseCpu(); rec_option.UseCpu(); diff --git a/src/MaaCore/Controller/MacSCKHelper.mm b/src/MaaCore/Controller/MacSCKHelper.mm index ebe160ab7a..063923dcb5 100644 --- a/src/MaaCore/Controller/MacSCKHelper.mm +++ b/src/MaaCore/Controller/MacSCKHelper.mm @@ -82,7 +82,18 @@ struct asst::MacSCKHelper::Impl { asst::MacSCKHelper::Impl::~Impl() { if (m_stream) { - [m_stream stopCaptureWithCompletionHandler:nil]; + auto sem = dispatch_semaphore_create(0); + [m_stream stopCaptureWithCompletionHandler:^(NSError* _Nullable error) { + if (error) { + Log.error("Error stopping capture:", error.localizedDescription.UTF8String); + } + dispatch_semaphore_signal(sem); + }]; + + dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC); + dispatch_semaphore_wait(sem, timeout); + dispatch_release(sem); + [m_stream release]; m_stream = nil; } diff --git a/src/MaaCore/Controller/PlayToolsController.cpp b/src/MaaCore/Controller/PlayToolsController.cpp index 1b06725ff3..0c7d91a655 100644 --- a/src/MaaCore/Controller/PlayToolsController.cpp +++ b/src/MaaCore/Controller/PlayToolsController.cpp @@ -96,7 +96,7 @@ bool asst::PlayToolsController::screencap(cv::Mat& image_payload, bool allow_rec bool asst::PlayToolsController::screencap_rgba(cv::Mat& image_payload, bool allow_reconnect [[maybe_unused]]) { - open(); + if (!open()) return false; uint32_t image_size = 0; try { @@ -131,7 +131,7 @@ bool asst::PlayToolsController::screencap_rgba(cv::Mat& image_payload, bool allo bool asst::PlayToolsController::screencap_bgr(cv::Mat& image_payload, bool allow_reconnect [[maybe_unused]]) { - open(); + if (!open()) return false; std::array header; try { @@ -169,7 +169,7 @@ bool asst::PlayToolsController::screencap_bgr(cv::Mat& image_payload, bool allow bool asst::PlayToolsController::start_game(const std::string& client_type [[maybe_unused]]) { - Log.info("InputText is not supported on iOS"); + Log.info("StartGame is not supported on PlayTools"); return true; } @@ -423,7 +423,7 @@ bool asst::PlayToolsController::fetch_screen_res() bool asst::PlayToolsController::toucher_commit(const TouchPhase phase, const Point& p, const int delay) { - open(); + if (!open()) return false; uint16_t x = socket_ops::host_to_network_short(static_cast(p.x)); uint16_t y = socket_ops::host_to_network_short(static_cast(p.y)); uint8_t payload[5] = { static_cast(phase), 0, 0, 0, 0 };