Skip to content

Commit

Permalink
perf: 优化adb control unit 部分实现
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 30, 2023
1 parent ced0426 commit 556acc7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
4 changes: 2 additions & 2 deletions source/MaaAdbControlUnit/Base/ArgvWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template <typename Argv>
requires IsSomeKindOfStringArray<Argv> && CheckArgv<Argv>
bool ArgvWrapper<Argv>::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;
}

Expand All @@ -45,7 +45,7 @@ Argv ArgvWrapper<Argv>::gen(const std::map<string, string>& 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;
}
Expand Down
7 changes: 6 additions & 1 deletion source/MaaAdbControlUnit/Invoke/InvokeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ std::optional<int> 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)
Expand Down
48 changes: 29 additions & 19 deletions source/MaaAdbControlUnit/Platform/BoostIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ int BoostIO::call_command(const std::vector<std::string>& 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<std::string> 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<boost::process::null, boost::process::std_out> pout,
boost::process::child proc(exec, args, boost::process::std_in<boost::process::null, boost::process::std_out> pout,
boost::process::std_err > boost::process::null BOOST_CREATE_NO_WINDOW);

const auto start_time = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -114,19 +112,31 @@ std::shared_ptr<IOHandler> BoostIO::tcp(const std::string& target, unsigned shor

std::shared_ptr<IOHandler> BoostIO::interactive_shell(const std::vector<std::string>& cmd, bool want_stderr)
{
// TODO: 想办法直接把cmd的后面塞进args
std::vector<std::string> rcmd(cmd.begin() + 1, cmd.end());

std::shared_ptr<boost::process::opstream> pin(new boost::process::opstream);
std::shared_ptr<boost::process::ipstream> pout(new boost::process::ipstream);

std::shared_ptr<boost::process::child> 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<boost::process::opstream>();
auto pout = std::make_shared<boost::process::ipstream>();

std::shared_ptr<boost::process::child> 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<boost::process::child>(
exec, args, boost::process::std_in<*pin, boost::process::std_err> * pout BOOST_CREATE_NO_WINDOW);
}
else {
proc = std::make_shared<boost::process::child>(
exec, args, boost::process::std_in<*pin, boost::process::std_out> * pout BOOST_CREATE_NO_WINDOW);
}

return std::make_shared<IOHandlerBoostStream>(pout, pin, proc);
}
Expand Down
13 changes: 10 additions & 3 deletions source/include/Utils/StringMisc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ inline constexpr void string_replace_all_(StringT& str,
}
}

template <typename StringT>
requires IsSomeKindOfString<StringT>
inline constexpr void string_replace_all_(StringT& str, const std::map<StringT, StringT>& 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 <MAA_RNS::ranges::forward_range Rng>
Expand Down Expand Up @@ -109,9 +118,7 @@ requires IsSomeKindOfString<StringT>
const std::map<StringT, StringT>& 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;
}

Expand Down

0 comments on commit 556acc7

Please sign in to comment.