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] 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 = ""; +}; }