From 040eded10535578e68e864d1582c02a334e0b13a Mon Sep 17 00:00:00 2001 From: shxyke Date: Mon, 27 Mar 2023 10:24:07 +0000 Subject: [PATCH] fix: set the params after controller created --- src/MaaCore/Controller/Controller.cpp | 30 +++++++++++++++++++++++++-- src/MaaCore/Controller/Controller.h | 4 ++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/MaaCore/Controller/Controller.cpp b/src/MaaCore/Controller/Controller.cpp index 6e900a9b05..58cb52da7f 100644 --- a/src/MaaCore/Controller/Controller.cpp +++ b/src/MaaCore/Controller/Controller.cpp @@ -64,6 +64,18 @@ void asst::Controller::callback(AsstMsg msg, const json::value& details) } } +#define CHECK_EXIST(object, return_type) \ + if (!object) { \ + Log.error(#object, " is not inited"); \ + return return_type; \ + } + +void asst::Controller::sync_params() +{ + CHECK_EXIST(m_controller, ); + m_controller->set_swipe_with_pause(m_swipe_with_pause); +} + cv::Mat asst::Controller::get_resized_image_cache() const { const static cv::Size d_size(m_scale_size.first, m_scale_size.second); @@ -80,50 +92,59 @@ cv::Mat asst::Controller::get_resized_image_cache() const bool asst::Controller::start_game(const std::string& client_type) { + CHECK_EXIST(m_controller, false); return m_controller->start_game(client_type); } bool asst::Controller::stop_game() { + CHECK_EXIST(m_controller, false); return m_controller->stop_game(); } bool asst::Controller::click(const Point& p) { + CHECK_EXIST(m_controller, false); return m_scale_proxy->click(p); } bool asst::Controller::click(const Rect& rect) { + CHECK_EXIST(m_controller, false); return m_scale_proxy->click(rect); } bool asst::Controller::swipe(const Point& p1, const Point& p2, int duration, bool extra_swipe, double slope_in, double slope_out, bool with_pause) { + CHECK_EXIST(m_controller, false); return m_scale_proxy->swipe(p1, p2, duration, extra_swipe, slope_in, slope_out, with_pause); } bool asst::Controller::swipe(const Rect& r1, const Rect& r2, int duration, bool extra_swipe, double slope_in, double slope_out, bool with_pause) { + CHECK_EXIST(m_controller, false); return m_scale_proxy->swipe(r1, r2, duration, extra_swipe, slope_in, slope_out, with_pause); } bool asst::Controller::inject_input_event(InputEvent& event) { - return m_scale_proxy->inject_input_event(event); + CHECK_EXIST(m_controller, false); + return m_controller->inject_input_event(event); } bool asst::Controller::press_esc() { LogTraceFunction; + CHECK_EXIST(m_controller, false); return m_controller->press_esc(); } asst::ControlFeat::Feat asst::Controller::support_features() { + CHECK_EXIST(m_controller, ControlFeat::NONE); return m_controller->support_features(); } @@ -141,6 +162,8 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a return false; } + sync_params(); + m_uuid = m_controller->get_uuid(); // try to find the fastest way @@ -182,6 +205,7 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a bool asst::Controller::inited() noexcept { + CHECK_EXIST(m_controller, false); return m_controller->inited(); } @@ -204,7 +228,8 @@ void asst::Controller::set_touch_mode(const TouchMode& mode) noexcept void asst::Controller::set_swipe_with_pause(bool enable) noexcept { - m_controller->set_swipe_with_pause(enable); + m_swipe_with_pause = enable; + sync_params(); } void asst::Controller::set_adb_lite_enabled(bool enable) noexcept @@ -271,6 +296,7 @@ cv::Mat asst::Controller::get_image_cache() const bool asst::Controller::screencap(bool allow_reconnect) { + CHECK_EXIST(m_controller, false); std::unique_lock image_lock(m_image_mutex); return m_controller->screencap(m_cache_image, allow_reconnect); } diff --git a/src/MaaCore/Controller/Controller.h b/src/MaaCore/Controller/Controller.h index d7e6108b75..089e6d6495 100644 --- a/src/MaaCore/Controller/Controller.h +++ b/src/MaaCore/Controller/Controller.h @@ -76,6 +76,8 @@ namespace asst void clear_info() noexcept; void callback(AsstMsg msg, const json::value& details); + void sync_params(); + AsstCallback m_callback; std::minstd_rand m_rand_engine; @@ -94,6 +96,8 @@ namespace asst std::pair m_scale_size = { WindowWidthDefault, WindowHeightDefault }; + bool m_swipe_with_pause = false; + mutable std::shared_mutex m_image_mutex; cv::Mat m_cache_image; };