Skip to content

Commit

Permalink
refactor: 重构 adb argv 并为所有参数添加默认值 (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO authored Nov 28, 2023
2 parents cc04ce4 + c0e8a02 commit decf8aa
Show file tree
Hide file tree
Showing 27 changed files with 214 additions and 403 deletions.
14 changes: 7 additions & 7 deletions sample/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ int main([[maybe_unused]] int argc, char** argv)
{
MaaToolKitInit();

// auto controller_handle = create_adb_controller();
auto controller_handle = create_win32_controller();
auto controller_handle = create_adb_controller();
// auto controller_handle = create_win32_controller();
auto ctrl_id = MaaControllerPostConnection(controller_handle);

auto resource_handle = MaaResourceCreate(nullptr, nullptr);
Expand All @@ -46,12 +46,12 @@ int main([[maybe_unused]] int argc, char** argv)
MaaToolKitUninit();
};

// if (!MaaInited(maa_handle)) {
// std::cout << "Failed to init MAA" << std::endl;
if (!MaaInited(maa_handle)) {
std::cout << "Failed to init MAA" << std::endl;

// destroy();
// return -1;
//}
destroy();
return -1;
}

register_my_recognizer(maa_handle);

Expand Down
4 changes: 0 additions & 4 deletions source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ MaaControlUnitHandle MaaAdbControlUnitCreate( //
switch (screencap_type) {
case MaaAdbControllerType_Screencap_FastestWay:
LogInfo << "screencap_type: ScreencapFastestWay";
if (!std::filesystem::exists(minicap_path)) {
LogError << "minicap path not exists" << VAR(minicap_path);
return nullptr;
}
screencap_unit = std::make_shared<ScreencapFastestWay>(minicap_path);
break;
case MaaAdbControllerType_Screencap_RawByNetcat:
Expand Down
13 changes: 3 additions & 10 deletions source/MaaAdbControlUnit/Base/ArgvWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,19 @@ struct ArgvWrapper

Argv argv;

bool parse(const json::value& value);
bool parse(const json::array& arr);
Argv gen(const std::map<string, string>& replacement) const;
};

template <typename Argv>
requires IsSomeKindOfStringArray<Argv> && CheckArgv<Argv>
bool ArgvWrapper<Argv>::parse(const json::value& value)
bool ArgvWrapper<Argv>::parse(const json::array& arr)
{
if (!value.is_array()) {
return false;
}

const auto& arr = value.as_array();
if (MAA_RNS::ranges::any_of(arr, [](const json::value& val) { return !val.is_string(); })) {
return false;
}

argv.clear();
argv.reserve(arr.size());
MAA_RNS::ranges::transform(arr, std::back_inserter(argv), [](const json::value& val) { return val.as_string(); });
argv = arr.to_vector<string>();
return true;
}

Expand Down
17 changes: 4 additions & 13 deletions source/MaaAdbControlUnit/Base/UnitBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,12 @@ void UnitBase::merge_replacement(Argv::replacement argv_replace, bool _override)
}
}

bool UnitBase::parse_argv(const std::string& key, const json::value& config, Argv& argv)
bool UnitBase::parse_argv(const std::string& key, const json::value& config, const json::array& default_argv,
Argv& argv)
{
auto aopt = config.find<json::object>("command");
if (!aopt) {
LogError << "Cannot find argv entry";
return false;
}

auto opt = aopt->find<json::value>(key);
if (!opt) {
LogError << "Cannot find key" << VAR(key);
return false;
}
auto jargv = config.get("command", key, default_argv);

if (!argv.parse(*opt)) {
if (!argv.parse(jargv)) {
LogError << "Parse config failed:" << VAR(key);
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion source/MaaAdbControlUnit/Base/UnitBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class UnitBase
virtual void merge_replacement(Argv::replacement argv_replace, bool _override = true);

protected:
static bool parse_argv(const std::string& key, const json::value& config, /*out*/ Argv& argv);
static bool parse_argv(const std::string& key, const json::value& config, const json::array& default_argv,
/*out*/ Argv& argv);

std::optional<std::string> command(const Argv::value& cmd, bool recv_by_socket = false, int64_t timeout = 20000);

Expand Down
10 changes: 9 additions & 1 deletion source/MaaAdbControlUnit/General/Activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ MAA_CTRL_UNIT_NS_BEGIN

bool Activity::parse(const json::value& config)
{
return parse_argv("StartApp", config, start_app_argv_) && parse_argv("StopApp", config, stop_app_argv_);
static const json::array kDefaultStartAppArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "am start -n {INTENT}",
};
static const json::array kDefaultStopAppArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "am force-stop {INTENT}",
};

return parse_argv("StartApp", config, kDefaultStartAppArgv, start_app_argv_) &&
parse_argv("StopApp", config, kDefaultStopAppArgv, stop_app_argv_);
}

bool Activity::start_app(const std::string& intent)
Expand Down
13 changes: 12 additions & 1 deletion source/MaaAdbControlUnit/General/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ MAA_CTRL_UNIT_NS_BEGIN

bool Connection::parse(const json::value& config)
{
return parse_argv("Connect", config, connect_argv_) && parse_argv("KillServer", config, kill_server_argv_);
static const json::array kDefaultConnectArgv = {
"{ADB}",
"connect",
"{ADB_SERIAL}",
};
static const json::array kDefaultKillServerArgv = {
"{ADB}",
"kill-server",
};

return parse_argv("Connect", config, kDefaultConnectArgv, connect_argv_) &&
parse_argv("KillServer", config, kDefaultKillServerArgv, kill_server_argv_);
}

bool Connection::connect()
Expand Down
15 changes: 13 additions & 2 deletions source/MaaAdbControlUnit/General/DeviceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ MAA_CTRL_UNIT_NS_BEGIN

bool DeviceInfo::parse(const json::value& config)
{
return parse_argv("UUID", config, uuid_argv_) && parse_argv("Resolution", config, resolution_argv_) &&
parse_argv("Orientation", config, orientation_argv_);
static const json::array kDefaultUuidArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "settings get secure android_id",
};
static const json::array kDefaultResolutionArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "dumpsys window displays | grep -o -E cur=+[^\\ ]+ | grep -o -E [0-9]+",
};
static const json::array kDefaultOrientationArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "dumpsys input | grep SurfaceOrientation | grep -m 1 -o -E [0-9]",
};

return parse_argv("UUID", config, kDefaultUuidArgv, uuid_argv_) &&
parse_argv("Resolution", config, kDefaultResolutionArgv, resolution_argv_) &&
parse_argv("Orientation", config, kDefaultOrientationArgv, orientation_argv_);
}

std::optional<std::string> DeviceInfo::request_uuid()
Expand Down
7 changes: 6 additions & 1 deletion source/MaaAdbControlUnit/General/DeviceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ MAA_CTRL_UNIT_NS_BEGIN

bool DeviceList::parse(const json::value& config)
{
return parse_argv("Devices", config, devices_argv_);
static const json::array kDefaultDevicesArgv = {
"{ADB}",
"devices",
};

return parse_argv("Devices", config, kDefaultDevicesArgv, devices_argv_);
}

std::optional<std::vector<std::string>> DeviceList::request_devices()
Expand Down
35 changes: 8 additions & 27 deletions source/MaaAdbControlUnit/Input/MaatouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,8 @@ MAA_CTRL_UNIT_NS_BEGIN

bool MaatouchInput::parse(const json::value& config)
{
auto popt = config.find<json::object>("prebuilt");
if (!popt) {
LogError << "Cannot find entry prebuilt";
return false;
}

auto mopt = popt->find<json::object>("maatouch");
if (!mopt) {
LogError << "Cannot find entry prebuilt.maatouch";
return false;
}

{
auto opt = mopt->find<json::value>("package");
if (!opt) {
LogError << "Cannot find entry prebuilt.maatouch.package";
return false;
}

if (!opt->is_string()) {
return false;
}

package_name_ = opt->as_string();
}
static const std::string kDefaultPackage = "com.shxyke.MaaTouch.App";
package_name_ = config.get("prebuilt", "maatouch", "package", kDefaultPackage);

return invoke_app_->parse(config);
}
Expand Down Expand Up @@ -80,8 +57,12 @@ bool MaatouchInput::press_key(int key)
return false;
}

bool ret = shell_handler_->write(MAA_FMT::format("k {} d\nc\n", key)) &&
shell_handler_->write(MAA_FMT::format("k {} u\nc\n", key));
// https://github.com/openstf/minitouch#writable-to-the-socket
static constexpr std::string_view kKeyDownFormat = "k {} d\nc\n";
static constexpr std::string_view kKeyUpFormat = "k {} u\nc\n";

bool ret = shell_handler_->write(MAA_FMT::format(kKeyDownFormat, key)) &&
shell_handler_->write(MAA_FMT::format(kKeyUpFormat, key));

if (!ret) {
LogError << "failed to write";
Expand Down
40 changes: 9 additions & 31 deletions source/MaaAdbControlUnit/Input/MinitouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,16 @@ MAA_CTRL_UNIT_NS_BEGIN

bool MinitouchInput::parse(const json::value& config)
{
auto popt = config.find<json::object>("prebuilt");
if (!popt) {
LogError << "Cannot find entry prebuilt";
return false;
}
static const json::array kDefaultArch = {
"x86_64", "x86", "arm64-v8a", "armeabi-v7a", "armeabi",
};
json::array jarch = config.get("prebuilt", "minitouch", "arch", kDefaultArch);

auto mopt = popt->find<json::object>("minitouch");
if (!mopt) {
LogError << "Cannot find entry prebuilt.minitouch";
if (MAA_RNS::ranges::any_of(jarch, [](const json::value& val) { return !val.is_string(); })) {
return false;
}

{
auto opt = mopt->find<json::value>("arch");
if (!opt) {
LogError << "Cannot find entry prebuilt.minitouch.arch";
return false;
}

const auto& value = *opt;
if (!value.is_array()) {
return false;
}

const auto& arr = value.as_array();
if (MAA_RNS::ranges::any_of(arr, [](const json::value& val) { return !val.is_string(); })) {
return false;
}

arch_list_.clear();
arch_list_.reserve(arr.size());
MAA_RNS::ranges::transform(arr, std::back_inserter(arch_list_),
[](const json::value& val) { return val.as_string(); });
}
arch_list_ = jarch.to_vector<std::string>();

return invoke_app_->parse(config);
}
Expand Down Expand Up @@ -85,7 +61,9 @@ bool MinitouchInput::set_wh(int swidth, int sheight, int orientation)
{
LogFunc << VAR(swidth) << VAR(sheight) << VAR(orientation);

shell_handler_ = invoke_app_->invoke_bin("-i");
// https://github.com/openstf/minitouch#running
static const std::string kMinitouchUseStdin = "-i";
shell_handler_ = invoke_app_->invoke_bin(kMinitouchUseStdin);
if (!shell_handler_) {
return false;
}
Expand Down
18 changes: 9 additions & 9 deletions source/MaaAdbControlUnit/Input/MtouchHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ bool MtouchHelper::click(int x, int y)

LogInfo << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));
bool ret = shell_handler_->write(MAA_FMT::format(kDownFormat, 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format(kUpFormat, 0));

if (!ret) {
LogError << "failed to write";
Expand Down Expand Up @@ -130,7 +130,7 @@ bool MtouchHelper::swipe(int x1, int y1, int x2, int y2, int duration)
auto start = std::chrono::steady_clock::now();
auto now = start;
bool ret = true;
ret &= shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x1, touch_y1, press_));
ret &= shell_handler_->write(MAA_FMT::format(kDownFormat, 0, touch_x1, touch_y1, press_));
if (!ret) {
LogError << "write error";
return false;
Expand All @@ -147,19 +147,19 @@ bool MtouchHelper::swipe(int x1, int y1, int x2, int y2, int duration)
std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();

ret &= shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", 0, tx, ty, press_));
ret &= shell_handler_->write(MAA_FMT::format(kMoveFormat, 0, tx, ty, press_));
if (!ret) {
LogWarn << "write error";
}
}

std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", 0, touch_x2, touch_y2, press_));
ret &= shell_handler_->write(MAA_FMT::format(kMoveFormat, 0, touch_x2, touch_y2, press_));

std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));
ret &= shell_handler_->write(MAA_FMT::format(kUpFormat, 0));

if (!ret) {
LogError << "failed to write";
Expand All @@ -180,7 +180,7 @@ bool MtouchHelper::touch_down(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format(kDownFormat, contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
Expand All @@ -201,7 +201,7 @@ bool MtouchHelper::touch_move(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format(kMoveFormat, contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
Expand All @@ -220,7 +220,7 @@ bool MtouchHelper::touch_up(int contact)

LogInfo << VAR(contact);

bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));
bool ret = shell_handler_->write(MAA_FMT::format(kUpFormat, contact));

if (!ret) {
LogError << "failed to write";
Expand Down
5 changes: 5 additions & 0 deletions source/MaaAdbControlUnit/Input/MtouchHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class MtouchHelper : public TouchInputBase
virtual std::pair<int, int> screen_to_touch(int x, int y) = 0;
virtual std::pair<int, int> screen_to_touch(double x, double y) = 0;

// https://github.com/openstf/minitouch#writable-to-the-socket
static constexpr std::string_view kDownFormat = "d {} {} {} {}\nc\n";
static constexpr std::string_view kMoveFormat = "m {} {} {} {}\nc\n";
static constexpr std::string_view kUpFormat = "u {}\nc\n";

std::shared_ptr<IOHandler> shell_handler_ = nullptr;

int screen_width_ = 0;
Expand Down
16 changes: 14 additions & 2 deletions source/MaaAdbControlUnit/Input/TapInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ MAA_CTRL_UNIT_NS_BEGIN

bool TapTouchInput::parse(const json::value& config)
{
return parse_argv("Click", config, click_argv_) && parse_argv("Swipe", config, swipe_argv_);
static const json::array kDefaultClickArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "input tap {X} {Y}",
};
static const json::array kDefaultSwipeArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "input swipe {X1} {Y1} {X2} {Y2} {DURATION}",
};

return parse_argv("Click", config, kDefaultClickArgv, click_argv_) &&
parse_argv("Swipe", config, kDefaultSwipeArgv, swipe_argv_);
}

bool TapTouchInput::init(int swidth, int sheight, int orientation)
Expand Down Expand Up @@ -71,7 +79,11 @@ bool TapTouchInput::touch_up(int contact)

bool TapKeyInput::parse(const json::value& config)
{
return parse_argv("PressKey", config, press_key_argv_);
static const json::array kDefaultPressKeyArgv = {
"{ADB}", "-s", "{ADB_SERIAL}", "shell", "input keyevent {KEY}",
};

return parse_argv("PressKey", config, kDefaultPressKeyArgv, press_key_argv_);
}

bool TapKeyInput::press_key(int key)
Expand Down
Loading

0 comments on commit decf8aa

Please sign in to comment.