From 556acc7a8cc8ca92b9524a9a5b7644aeb599d0ca Mon Sep 17 00:00:00 2001 From: MistEO Date: Thu, 30 Nov 2023 13:53:41 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96adb=20control=20unit?= =?UTF-8?q?=20=E9=83=A8=E5=88=86=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/MaaAdbControlUnit/Base/ArgvWrapper.hpp | 4 +- source/MaaAdbControlUnit/Invoke/InvokeApp.cpp | 7 ++- source/MaaAdbControlUnit/Platform/BoostIO.cpp | 48 +++++++++++-------- source/include/Utils/StringMisc.hpp | 13 +++-- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp b/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp index dc4d0603a..db154a61a 100644 --- a/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp +++ b/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp @@ -31,7 +31,7 @@ template requires IsSomeKindOfStringArray && CheckArgv bool ArgvWrapper::parse(const json::array& arr) { - if (MAA_RNS::ranges::any_of(arr, [](const json::value& val) { return !val.is_string(); })) { + if (!MAA_RNS::ranges::all_of(arr, [](const json::value& val) { return val.is_string(); })) { return false; } @@ -45,7 +45,7 @@ Argv ArgvWrapper::gen(const std::map& replacement) const { auto argv_dup = argv; for (auto& s : argv_dup) { - s = string_replace_all(s, replacement); + string_replace_all_(s, replacement); } return argv_dup; } diff --git a/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp b/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp index 4b5e65483..b046b62c1 100644 --- a/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp +++ b/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp @@ -73,8 +73,13 @@ std::optional InvokeApp::sdk() if (!cmd_ret) { return std::nullopt; } + std::string& ret = *cmd_ret; + string_trim_(ret); - return atoi(cmd_ret.value().c_str()); + if (!MAA_RNS::ranges::all_of(ret, [](char c) { return std::isdigit(c); })) { + return std::nullopt; + } + return std::stoi(ret); } bool InvokeApp::push(const std::filesystem::path& path) diff --git a/source/MaaAdbControlUnit/Platform/BoostIO.cpp b/source/MaaAdbControlUnit/Platform/BoostIO.cpp index 3f2c38f4c..a370087e9 100644 --- a/source/MaaAdbControlUnit/Platform/BoostIO.cpp +++ b/source/MaaAdbControlUnit/Platform/BoostIO.cpp @@ -30,17 +30,15 @@ int BoostIO::call_command(const std::vector& cmd, bool recv_by_sock return -1; } - auto exec = boost::process::search_path(cmd[0]); + auto exec = boost::process::search_path(cmd.front()); if (!std::filesystem::exists(exec)) { - LogError << "path not exists" << VAR(exec) << VAR(cmd[0]); + LogError << "path not exists" << VAR(exec) << VAR(cmd.front()); return -1; } - // TODO: 想办法直接把cmd的后面塞进args - std::vector rcmd(cmd.begin() + 1, cmd.end()); + auto args = boost::process::args(std::vector(cmd.begin() + 1, cmd.end())); boost::process::ipstream pout; - boost::process::child proc(exec, boost::process::args(rcmd), - boost::process::std_in pout, + boost::process::child proc(exec, args, boost::process::std_in pout, boost::process::std_err > boost::process::null BOOST_CREATE_NO_WINDOW); const auto start_time = std::chrono::steady_clock::now(); @@ -114,19 +112,31 @@ std::shared_ptr BoostIO::tcp(const std::string& target, unsigned shor std::shared_ptr BoostIO::interactive_shell(const std::vector& cmd, bool want_stderr) { - // TODO: 想办法直接把cmd的后面塞进args - std::vector rcmd(cmd.begin() + 1, cmd.end()); - - std::shared_ptr pin(new boost::process::opstream); - std::shared_ptr pout(new boost::process::ipstream); - - std::shared_ptr proc( - want_stderr ? new boost::process::child(boost::process::search_path(cmd[0]), boost::process::args(rcmd), - boost::process::std_in<*pin, boost::process::std_err> * - pout BOOST_CREATE_NO_WINDOW) - : new boost::process::child(boost::process::search_path(cmd[0]), boost::process::args(rcmd), - boost::process::std_in<*pin, boost::process::std_out> * - pout BOOST_CREATE_NO_WINDOW)); + if (cmd.empty()) { + LogError << "cmd is empty"; + return nullptr; + } + + auto pin = std::make_shared(); + auto pout = std::make_shared(); + + std::shared_ptr proc = nullptr; + + auto exec = boost::process::search_path(cmd.front()); + if (!std::filesystem::exists(exec)) { + LogError << "path not exists" << VAR(exec) << VAR(cmd.front()); + return nullptr; + } + auto args = boost::process::args(std::vector(cmd.begin() + 1, cmd.end())); + + if (want_stderr) { + proc = std::make_shared( + exec, args, boost::process::std_in<*pin, boost::process::std_err> * pout BOOST_CREATE_NO_WINDOW); + } + else { + proc = std::make_shared( + exec, args, boost::process::std_in<*pin, boost::process::std_out> * pout BOOST_CREATE_NO_WINDOW); + } return std::make_shared(pout, pin, proc); } diff --git a/source/include/Utils/StringMisc.hpp b/source/include/Utils/StringMisc.hpp index 7f50fbc8f..7f5b7199e 100644 --- a/source/include/Utils/StringMisc.hpp +++ b/source/include/Utils/StringMisc.hpp @@ -53,6 +53,15 @@ inline constexpr void string_replace_all_(StringT& str, } } +template +requires IsSomeKindOfString +inline constexpr void string_replace_all_(StringT& str, const std::map& replace_map) +{ + for (const auto& [from, to] : replace_map) { + string_replace_all_(str, from, to); + } +} + #ifdef MAA_USE_RANGES_RANGE_V3 // workaround for P2210R2 template @@ -109,9 +118,7 @@ requires IsSomeKindOfString const std::map& replace_map) { StringT result = str; - for (const auto& [from, to] : replace_map) { - string_replace_all_(result, from, to); - } + string_replace_all_(result, replace_map); return result; }