From 175c3960530047ad7d6f223014f0b88a89292ea8 Mon Sep 17 00:00:00 2001 From: Constrat <56174894+Constrat@users.noreply.github.com.> Date: Sun, 12 May 2024 11:10:43 +0200 Subject: [PATCH 1/5] fix: return specific values for auto starting methods --- src/MaaCore/Controller/PlayToolsController.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MaaCore/Controller/PlayToolsController.cpp b/src/MaaCore/Controller/PlayToolsController.cpp index 8bfb5a3596..80b364adeb 100644 --- a/src/MaaCore/Controller/PlayToolsController.cpp +++ b/src/MaaCore/Controller/PlayToolsController.cpp @@ -50,12 +50,12 @@ const std::string& asst::PlayToolsController::get_uuid() const size_t asst::PlayToolsController::get_pipe_data_size() const noexcept { - return size_t(); + return 170; } size_t asst::PlayToolsController::get_version() const noexcept { - return size_t(); + return 9; } bool asst::PlayToolsController::screencap( From 32d06df501bf8e536fc757e096186e0e889dff1c Mon Sep 17 00:00:00 2001 From: Constrat <56174894+Constrat@users.noreply.github.com.> Date: Sun, 12 May 2024 12:07:29 +0200 Subject: [PATCH 2/5] chore: Add get_controller_type() to discern Android / PlayTools --- src/MaaCore/Controller/Controller.cpp | 4 + src/MaaCore/Controller/Controller.h | 2 + .../Miscellaneous/StartGameTaskPlugin.cpp | 79 ++++++++++--------- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/MaaCore/Controller/Controller.cpp b/src/MaaCore/Controller/Controller.cpp index 8c7bff0afc..cefd4b4890 100644 --- a/src/MaaCore/Controller/Controller.cpp +++ b/src/MaaCore/Controller/Controller.cpp @@ -49,6 +49,10 @@ size_t asst::Controller::get_version() const noexcept return m_controller->get_version(); } +asst::ControllerType asst::Controller::get_controller_type() const noexcept +{ + return m_controller_type; +} std::pair asst::Controller::get_scale_size() const noexcept { diff --git a/src/MaaCore/Controller/Controller.h b/src/MaaCore/Controller/Controller.h index b476f9a2e1..55d2fb6b89 100644 --- a/src/MaaCore/Controller/Controller.h +++ b/src/MaaCore/Controller/Controller.h @@ -52,6 +52,8 @@ public: size_t get_version() const noexcept; + ControllerType get_controller_type() const noexcept; + cv::Mat get_image(bool raw = false); cv::Mat get_image_cache() const; bool screencap(bool allow_reconnect = false); diff --git a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp index c2d22f4823..b5be460291 100644 --- a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp +++ b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp @@ -9,48 +9,53 @@ bool StartGameTaskPlugin::_run() if (m_client_type.empty()) { return false; } - - // check for android version, because it leads to different magic values - // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8966#issuecomment-2094369694 - if (ctrler()->get_version() > 8) { - do { - if (!ctrler()->start_game(m_client_type)) { - return false; - } - - // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8961#issue-2277568882 - // 167: magic value needs to be >164 but <172 (as that's max) - if (ctrler()->get_pipe_data_size() > 167) { - return true; - } - - sleep(1500); - - } while (true); + // check for MAC / iOS + if (ctrler()->get_controller_type() == ControllerType::MacPlayTools) { + return ctrler()->start_game(m_client_type); } else { - do { - if (!ctrler()->start_game(m_client_type)) { - return false; - } - - // 145: needs to be >85 but <153 (as that's max) - // magic value lowered because of different android version - if (ctrler()->get_pipe_data_size() > 145) { - // As there's no way to know (in Android < 8) if Ark has correctly started - // we run the command a few more times to be sure - for (auto _ = 3; _--;) { - sleep(1500); - if (!ctrler()->start_game(m_client_type)) { - return false; - } + // check for android version, because it leads to different magic values + // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8966#issuecomment-2094369694 + if (ctrler()->get_version() > 8) { + do { + if (!ctrler()->start_game(m_client_type)) { + return false; } - return true; - } - sleep(1500); + // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8961#issue-2277568882 + // 167: magic value needs to be >164 but <172 (as that's max) + if (ctrler()->get_pipe_data_size() > 167) { + return true; + } - } while (true); + sleep(1500); + + } while (true); + } + else { + do { + if (!ctrler()->start_game(m_client_type)) { + return false; + } + + // 145: needs to be >85 but <153 (as that's max) + // magic value lowered because of different android version + if (ctrler()->get_pipe_data_size() > 145) { + // As there's no way to know (in Android < 8) if Ark has correctly started + // we run the command a few more times to be sure + for (auto _ = 3; _--;) { + sleep(1500); + if (!ctrler()->start_game(m_client_type)) { + return false; + } + } + return true; + } + + sleep(1500); + + } while (true); + } } } From 63f3b90f1c35c2612590713ff691844f6d8a678f Mon Sep 17 00:00:00 2001 From: Constrat <56174894+Constrat@users.noreply.github.com.> Date: Sun, 12 May 2024 16:02:29 +0200 Subject: [PATCH 3/5] revert: PlayToolsController returns --- src/MaaCore/Controller/PlayToolsController.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MaaCore/Controller/PlayToolsController.cpp b/src/MaaCore/Controller/PlayToolsController.cpp index 80b364adeb..8bfb5a3596 100644 --- a/src/MaaCore/Controller/PlayToolsController.cpp +++ b/src/MaaCore/Controller/PlayToolsController.cpp @@ -50,12 +50,12 @@ const std::string& asst::PlayToolsController::get_uuid() const size_t asst::PlayToolsController::get_pipe_data_size() const noexcept { - return 170; + return size_t(); } size_t asst::PlayToolsController::get_version() const noexcept { - return 9; + return size_t(); } bool asst::PlayToolsController::screencap( From b42f010e9dd90cbc70b3dff9ab81b0ef3e3f45fa Mon Sep 17 00:00:00 2001 From: Constrat <56174894+Constrat@users.noreply.github.com.> Date: Sun, 12 May 2024 16:03:37 +0200 Subject: [PATCH 4/5] perf: optimized variable usage + limited for + general rewriting for readability --- .../Miscellaneous/StartGameTaskPlugin.cpp | 73 ++++++++----------- .../Task/Miscellaneous/StartGameTaskPlugin.h | 34 +++++---- 2 files changed, 50 insertions(+), 57 deletions(-) diff --git a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp index b5be460291..643ea5e494 100644 --- a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp +++ b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp @@ -4,59 +4,50 @@ using namespace asst; +bool StartGameTaskPlugin::start_game_with_retries(size_t pipe_data_size_limit, bool newer_android) + const +{ + int extra_runs = 0; + for (int i = 0; i < 30; ++i) { + if (!ctrler()->start_game(m_client_type)) { + return false; + } + + if (ctrler()->get_pipe_data_size() > pipe_data_size_limit) { + if (newer_android || ++extra_runs > 3) { + return true; + } + } + + sleep(1500); + } + + return false; +} + bool StartGameTaskPlugin::_run() { if (m_client_type.empty()) { return false; } + // check for MAC / iOS if (ctrler()->get_controller_type() == ControllerType::MacPlayTools) { return ctrler()->start_game(m_client_type); } - else { - // check for android version, because it leads to different magic values - // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8966#issuecomment-2094369694 - if (ctrler()->get_version() > 8) { - do { - if (!ctrler()->start_game(m_client_type)) { - return false; - } - // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8961#issue-2277568882 - // 167: magic value needs to be >164 but <172 (as that's max) - if (ctrler()->get_pipe_data_size() > 167) { - return true; - } + // check for android version, because it leads to different magic values + // >8: 167: magic value needs to be >164 but <172 (as that's max) + // <=8: 145: needs to be >85 but <153 (as that's max) + // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8966#issuecomment-2094369694 + // https://github.com/MaaAssistantArknights/MaaAssistantArknights/pull/8961#issue-2277568882 + int pipe_data_size_limit = (ctrler()->get_version() > 8) ? 167 : 145; - sleep(1500); + // In Android 9 and above, there's a way to check if the game started correctly + // so no need for limited tries + bool newer_android = (ctrler()->get_version() > 8); - } while (true); - } - else { - do { - if (!ctrler()->start_game(m_client_type)) { - return false; - } - - // 145: needs to be >85 but <153 (as that's max) - // magic value lowered because of different android version - if (ctrler()->get_pipe_data_size() > 145) { - // As there's no way to know (in Android < 8) if Ark has correctly started - // we run the command a few more times to be sure - for (auto _ = 3; _--;) { - sleep(1500); - if (!ctrler()->start_game(m_client_type)) { - return false; - } - } - return true; - } - - sleep(1500); - - } while (true); - } - } + return start_game_with_retries(pipe_data_size_limit, newer_android); } StartGameTaskPlugin& StartGameTaskPlugin::set_client_type(std::string client_type) noexcept diff --git a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.h b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.h index a980335b4f..047a44cd22 100644 --- a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.h +++ b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.h @@ -5,26 +5,28 @@ namespace asst { - inline static const std::string ClientTypeKey = "client_type"; +inline static const std::string ClientTypeKey = "client_type"; - class StartGameTaskPlugin : public AbstractTaskPlugin +class StartGameTaskPlugin : public AbstractTaskPlugin +{ +public: + using AbstractTaskPlugin::AbstractTaskPlugin; + virtual ~StartGameTaskPlugin() noexcept override = default; + + virtual bool verify(AsstMsg msg, const json::value& details) const override { - public: - using AbstractTaskPlugin::AbstractTaskPlugin; - virtual ~StartGameTaskPlugin() noexcept override = default; + std::ignore = msg; + std::ignore = details; + return true; + } - virtual bool verify(AsstMsg msg, const json::value& details) const override - { - std::ignore = msg; - std::ignore = details; - return true; - } + StartGameTaskPlugin& set_client_type(std::string client_type) noexcept; - StartGameTaskPlugin& set_client_type(std::string client_type) noexcept; +protected: + bool _run() override; - protected: - bool _run() override; + bool start_game_with_retries(size_t pipe_data_size_limit, bool newer_android) const; - std::string m_client_type = ""; - }; + std::string m_client_type = ""; +}; } From 7ab466a17beda8e2d539e4d23bbf707f505ab525 Mon Sep 17 00:00:00 2001 From: Constrat <56174894+Constrat@users.noreply.github.com> Date: Tue, 14 May 2024 08:12:04 +0000 Subject: [PATCH 5/5] feat: Stop implementation --- src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp index 643ea5e494..484402e351 100644 --- a/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp +++ b/src/MaaCore/Task/Miscellaneous/StartGameTaskPlugin.cpp @@ -9,6 +9,10 @@ bool StartGameTaskPlugin::start_game_with_retries(size_t pipe_data_size_limit, b { int extra_runs = 0; for (int i = 0; i < 30; ++i) { + if (need_exit()) { + return false; + } + if (!ctrler()->start_game(m_client_type)) { return false; }