diff --git a/sample/cpp/main.cpp b/sample/cpp/main.cpp index 9f30f6976..1b1e7091b 100644 --- a/sample/cpp/main.cpp +++ b/sample/cpp/main.cpp @@ -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); @@ -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); diff --git a/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp b/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp index 2de180318..967367412 100644 --- a/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp +++ b/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp @@ -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(minicap_path); break; case MaaAdbControllerType_Screencap_RawByNetcat: diff --git a/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp b/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp index fbac0c27a..dc4d0603a 100644 --- a/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp +++ b/source/MaaAdbControlUnit/Base/ArgvWrapper.hpp @@ -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& replacement) const; }; template requires IsSomeKindOfStringArray && CheckArgv -bool ArgvWrapper::parse(const json::value& value) +bool ArgvWrapper::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(); return true; } diff --git a/source/MaaAdbControlUnit/Base/UnitBase.cpp b/source/MaaAdbControlUnit/Base/UnitBase.cpp index 3a3906525..8cf6d5544 100644 --- a/source/MaaAdbControlUnit/Base/UnitBase.cpp +++ b/source/MaaAdbControlUnit/Base/UnitBase.cpp @@ -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("command"); - if (!aopt) { - LogError << "Cannot find argv entry"; - return false; - } - - auto opt = aopt->find(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; } diff --git a/source/MaaAdbControlUnit/Base/UnitBase.h b/source/MaaAdbControlUnit/Base/UnitBase.h index 121d1693a..3cb60a48a 100644 --- a/source/MaaAdbControlUnit/Base/UnitBase.h +++ b/source/MaaAdbControlUnit/Base/UnitBase.h @@ -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 command(const Argv::value& cmd, bool recv_by_socket = false, int64_t timeout = 20000); diff --git a/source/MaaAdbControlUnit/General/Activity.cpp b/source/MaaAdbControlUnit/General/Activity.cpp index 6973af887..8db63a8d3 100644 --- a/source/MaaAdbControlUnit/General/Activity.cpp +++ b/source/MaaAdbControlUnit/General/Activity.cpp @@ -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) diff --git a/source/MaaAdbControlUnit/General/Connection.cpp b/source/MaaAdbControlUnit/General/Connection.cpp index b235ead71..a39a38853 100644 --- a/source/MaaAdbControlUnit/General/Connection.cpp +++ b/source/MaaAdbControlUnit/General/Connection.cpp @@ -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() diff --git a/source/MaaAdbControlUnit/General/DeviceInfo.cpp b/source/MaaAdbControlUnit/General/DeviceInfo.cpp index eb6634a92..8413284a2 100644 --- a/source/MaaAdbControlUnit/General/DeviceInfo.cpp +++ b/source/MaaAdbControlUnit/General/DeviceInfo.cpp @@ -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 DeviceInfo::request_uuid() diff --git a/source/MaaAdbControlUnit/General/DeviceList.cpp b/source/MaaAdbControlUnit/General/DeviceList.cpp index 0f34ac8b7..53c0d294f 100644 --- a/source/MaaAdbControlUnit/General/DeviceList.cpp +++ b/source/MaaAdbControlUnit/General/DeviceList.cpp @@ -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> DeviceList::request_devices() diff --git a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp index 91eb25d42..55b5432b0 100644 --- a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp @@ -11,31 +11,8 @@ MAA_CTRL_UNIT_NS_BEGIN bool MaatouchInput::parse(const json::value& config) { - auto popt = config.find("prebuilt"); - if (!popt) { - LogError << "Cannot find entry prebuilt"; - return false; - } - - auto mopt = popt->find("maatouch"); - if (!mopt) { - LogError << "Cannot find entry prebuilt.maatouch"; - return false; - } - - { - auto opt = mopt->find("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); } @@ -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"; diff --git a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp index 02265e1fe..b5851dbdb 100644 --- a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp @@ -11,40 +11,16 @@ MAA_CTRL_UNIT_NS_BEGIN bool MinitouchInput::parse(const json::value& config) { - auto popt = config.find("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("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("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(); return invoke_app_->parse(config); } @@ -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; } diff --git a/source/MaaAdbControlUnit/Input/MtouchHelper.cpp b/source/MaaAdbControlUnit/Input/MtouchHelper.cpp index 48d4b1c02..8f006c51f 100644 --- a/source/MaaAdbControlUnit/Input/MtouchHelper.cpp +++ b/source/MaaAdbControlUnit/Input/MtouchHelper.cpp @@ -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"; @@ -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; @@ -147,7 +147,7 @@ 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"; } @@ -155,11 +155,11 @@ 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, 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"; @@ -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"; @@ -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"; @@ -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"; diff --git a/source/MaaAdbControlUnit/Input/MtouchHelper.h b/source/MaaAdbControlUnit/Input/MtouchHelper.h index 0ee1b3b25..fd86eb91b 100644 --- a/source/MaaAdbControlUnit/Input/MtouchHelper.h +++ b/source/MaaAdbControlUnit/Input/MtouchHelper.h @@ -24,6 +24,11 @@ class MtouchHelper : public TouchInputBase virtual std::pair screen_to_touch(int x, int y) = 0; virtual std::pair 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 shell_handler_ = nullptr; int screen_width_ = 0; diff --git a/source/MaaAdbControlUnit/Input/TapInput.cpp b/source/MaaAdbControlUnit/Input/TapInput.cpp index 38f9cb157..9920e47ba 100644 --- a/source/MaaAdbControlUnit/Input/TapInput.cpp +++ b/source/MaaAdbControlUnit/Input/TapInput.cpp @@ -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) @@ -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) diff --git a/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp b/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp index f883da3c7..4cbd9f3dc 100644 --- a/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp +++ b/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp @@ -8,9 +8,39 @@ MAA_CTRL_UNIT_NS_BEGIN bool InvokeApp::parse(const json::value& config) { - return parse_argv("Abilist", config, abilist_argv_) && parse_argv("SDK", config, sdk_argv_) && - parse_argv("PushBin", config, push_bin_argv_) && parse_argv("ChmodBin", config, chmod_bin_argv_) && - parse_argv("InvokeBin", config, invoke_bin_argv_) && parse_argv("InvokeApp", config, invoke_app_argv_); + static const json::array kDefaultAbilistArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "shell", "getprop ro.product.cpu.abilist | tr -d '\n\r'", + }; + static const json::array kDefaultSdkArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "shell", "getprop ro.build.version.sdk | tr -d '\n\r'", + }; + static const json::array kDefaultPushBinArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "push", "{BIN_PATH}", "/data/local/tmp/{BIN_WORKING_FILE}", + }; + static const json::array kDefaultChmodBinArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "shell", "chmod 700 \"/data/local/tmp/{BIN_WORKING_FILE}\"", + }; + static const json::array kDefaultInvokeBinArgv = { + "{ADB}", + "-s", + "{ADB_SERIAL}", + "shell", + "export LD_LIBRARY_PATH=/data/local/tmp/; \"/data/local/tmp/{BIN_WORKING_FILE}\" {BIN_EXTRA_PARAMS} 2>&1", + }; + static const json::array kDefaultInvokeAppArgv = { + "{ADB}", + "-s", + "{ADB_SERIAL}", + "shell", + "export CLASSPATH=\"/data/local/tmp/{APP_WORKING_FILE}\"; app_process /data/local/tmp {PACKAGE_NAME}", + }; + + return parse_argv("Abilist", config, kDefaultAbilistArgv, abilist_argv_) && + parse_argv("SDK", config, kDefaultSdkArgv, sdk_argv_) && + parse_argv("PushBin", config, kDefaultPushBinArgv, push_bin_argv_) && + parse_argv("ChmodBin", config, kDefaultChmodBinArgv, chmod_bin_argv_) && + parse_argv("InvokeBin", config, kDefaultInvokeBinArgv, invoke_bin_argv_) && + parse_argv("InvokeApp", config, kDefaultInvokeAppArgv, invoke_app_argv_); } bool InvokeApp::init(const std::string& force_temp) diff --git a/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp index e139aba15..042203d95 100644 --- a/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp +++ b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp @@ -218,11 +218,6 @@ bool ControlUnitMgr::parse(const json::value& config) { bool ret = true; - if (config.get("NotParse", false)) { - LogWarn << "NotParse"; - return true; - } - ret &= connection_.parse(config); ret &= device_info_.parse(config); ret &= activity_.parse(config); diff --git a/source/MaaAdbControlUnit/Screencap/Encode.cpp b/source/MaaAdbControlUnit/Screencap/Encode.cpp index 1091f5432..6b9ce7ab0 100644 --- a/source/MaaAdbControlUnit/Screencap/Encode.cpp +++ b/source/MaaAdbControlUnit/Screencap/Encode.cpp @@ -6,7 +6,11 @@ MAA_CTRL_UNIT_NS_BEGIN bool ScreencapEncode::parse(const json::value& config) { - return parse_argv("ScreencapEncode", config, screencap_encode_argv_); + static const json::array kDefaultScreencapEncodeArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "exec-out", "screencap -p", + }; + + return parse_argv("ScreencapEncode", config, kDefaultScreencapEncodeArgv, screencap_encode_argv_); } bool ScreencapEncode::init(int swidth, int sheight) diff --git a/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp b/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp index 0de81fa63..6adcbedb8 100644 --- a/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp +++ b/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp @@ -9,8 +9,16 @@ MAA_CTRL_UNIT_NS_BEGIN bool ScreencapEncodeToFileAndPull::parse(const json::value& config) { - return parse_argv("ScreencapEncodeToFile", config, screencap_encode_to_file_argv_) && - parse_argv("PullFile", config, pull_file_argv_); + static const json::array kDefaultScreencapEncodeToFileArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "shell", "screencap -p > \"/data/local/tmp/{TEMP_FILE}\"", + }; + static const json::array kDefaultPullFileArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "pull", "/data/local/tmp/{TEMP_FILE}", "{DST_PATH}" + }; + + return parse_argv("ScreencapEncodeToFile", config, kDefaultScreencapEncodeToFileArgv, + screencap_encode_to_file_argv_) && + parse_argv("PullFile", config, kDefaultPullFileArgv, pull_file_argv_); } bool ScreencapEncodeToFileAndPull::init(int swidth, int sheight) diff --git a/source/MaaAdbControlUnit/Screencap/FastestWay.cpp b/source/MaaAdbControlUnit/Screencap/FastestWay.cpp index 27ad8c77e..d9a7d3b78 100644 --- a/source/MaaAdbControlUnit/Screencap/FastestWay.cpp +++ b/source/MaaAdbControlUnit/Screencap/FastestWay.cpp @@ -15,10 +15,18 @@ ScreencapFastestWay::ScreencapFastestWay(const std::filesystem::path& minicap_pa { Method::RawWithGzip, std::make_shared() }, { Method::Encode, std::make_shared() }, { Method::EncodeToFileAndPull, std::make_shared() }, - { Method::MinicapDirect, std::make_shared(minicap_path) }, - { Method::MinicapStream, std::make_shared(minicap_path) }, }; + if (std::filesystem::exists(minicap_path)) { + units_.merge(decltype(units_) { + { Method::MinicapDirect, std::make_shared(minicap_path) }, + { Method::MinicapStream, std::make_shared(minicap_path) }, + }); + } + else { + LogWarn << "minicap path not exists" << VAR(minicap_path); + } + for (auto pair : units_) { children_.emplace_back(pair.second); } diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp index 1b7911157..e939e9341 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp @@ -11,63 +11,23 @@ MAA_CTRL_UNIT_NS_BEGIN bool MinicapBase::parse(const json::value& config) { - auto popt = config.find("prebuilt"); - if (!popt) { - LogError << "Cannot find entry prebuilt"; + static const json::array kDefaultArch = { + "x86", + "armeabi-v7a", + "armeabi", + }; + json::array jarch = config.get("prebuilt", "minicap", "arch", kDefaultArch); + + if (MAA_RNS::ranges::any_of(jarch, [](const json::value& val) { return !val.is_string(); })) { return false; } + arch_list_ = jarch.to_vector(); - auto mopt = popt->find("minicap"); - if (!mopt) { - LogError << "Cannot find entry prebuilt.minicap"; - return false; - } - - { - auto opt = mopt->find("arch"); - if (!opt) { - LogError << "Cannot find entry prebuilt.minicap.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(); }); - } - - { - auto opt = mopt->find("sdk"); - if (!opt) { - LogError << "Cannot find entry prebuilt.minicap.sdk"; - 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_number(); })) { - return false; - } - - sdk_list_.clear(); - sdk_list_.reserve(arr.size()); - MAA_RNS::ranges::transform(arr, std::back_inserter(sdk_list_), - [](const json::value& val) { return val.as_integer(); }); - } + static const json::array kDefaultSdk = { + 31, 29, 28, 27, 26, 25, 24, 23, 22, 21, 19, 18, 17, 16, 15, 14, + }; + json::array jsdk = config.get("prebuilt", "minicap", "sdk", kDefaultSdk); + sdk_list_ = jsdk.to_vector(); return binary_->parse(config) && library_->parse(config); } diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp index 370b186d7..97bf12347 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp @@ -17,7 +17,11 @@ MinicapStream::~MinicapStream() bool MinicapStream::parse(const json::value& config) { - return MinicapBase::parse(config) && parse_argv("ForwardSocket", config, forward_argv_); + static const json::array kDefaultForwardArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "forward", "tcp:{FOWARD_PORT}", "localabstract:{LOCAL_SOCKET}", + }; + + return MinicapBase::parse(config) && parse_argv("ForwardSocket", config, kDefaultForwardArgv, forward_argv_); } bool MinicapStream::init(int swidth, int sheight) diff --git a/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp b/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp index b37b62095..cdf751018 100644 --- a/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp +++ b/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp @@ -6,8 +6,16 @@ MAA_CTRL_UNIT_NS_BEGIN bool ScreencapRawByNetcat::parse(const json::value& config) { - return parse_argv("ScreencapRawByNetcat", config, screencap_raw_by_netcat_argv_) && - parse_argv("NetcatAddress", config, netcat_address_argv_); + static const json::array kDefaultScreencapRawByNetcatArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "exec-out", "screencap | nc -w 3 {NETCAT_ADDRESS} {NETCAT_PORT}", + }; + static const json::array kDefaultNetcatAddressArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "shell", "cat /proc/net/arp | grep : ", + }; + + return parse_argv("ScreencapRawByNetcat", config, kDefaultScreencapRawByNetcatArgv, + screencap_raw_by_netcat_argv_) && + parse_argv("NetcatAddress", config, kDefaultNetcatAddressArgv, netcat_address_argv_); } bool ScreencapRawByNetcat::init(int swidth, int sheight) diff --git a/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp b/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp index 4eefe7835..29206b1f2 100644 --- a/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp +++ b/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp @@ -6,7 +6,11 @@ MAA_CTRL_UNIT_NS_BEGIN bool ScreencapRawWithGzip::parse(const json::value& config) { - return parse_argv("ScreencapRawWithGzip", config, screencap_raw_with_gzip_argv_); + static const json::array kDefaultScreencapRawWithGzipArgv = { + "{ADB}", "-s", "{ADB_SERIAL}", "exec-out", "screencap | gzip -1", + }; + + return parse_argv("ScreencapRawWithGzip", config, kDefaultScreencapRawWithGzipArgv, screencap_raw_with_gzip_argv_); } bool ScreencapRawWithGzip::init(int swidth, int sheight) diff --git a/source/MaaToolKit/AdbDevice/AdbConfigDef.hpp b/source/MaaToolKit/AdbDevice/AdbConfigDef.hpp deleted file mode 100644 index 74597085e..000000000 --- a/source/MaaToolKit/AdbDevice/AdbConfigDef.hpp +++ /dev/null @@ -1,205 +0,0 @@ -#pragma once - -#include "Conf/Conf.h" - -#include - -#include - -MAA_TOOLKIT_DEVICE_NS_BEGIN - -using namespace json::literals; -inline static const json::value kAdbConfig = R"( -{ - "prebuilt": { - "minicap": { - "arch": [ - "x86", - "armeabi-v7a", - "armeabi" - ], - "sdk": [ - 31, 29, 28, 27, 26, 25, 24, 23, 22, 21, 19, 18, 17, 16, 15, 14 - ] - }, - "minitouch": { - "arch": [ - "x86_64", - "x86", - "arm64-v8a", - "armeabi-v7a", - "armeabi" - ] - }, - "maatouch": { - "package": "com.shxyke.MaaTouch.App" - } - }, - "command": { - "Devices": [ - "{ADB}", - "devices" - ], - "Connect": [ - "{ADB}", - "connect", - "{ADB_SERIAL}" - ], - "KillServer": [ - "{ADB}", - "kill-server" - ], - "UUID": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "settings get secure android_id" - ], - "Resolution": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "dumpsys window displays | grep -o -E cur=+[^\\ ]+ | grep -o -E [0-9]+" - ], - "StartApp": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "am start -n {INTENT}" - ], - "StopApp": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "am force-stop {INTENT}" - ], - "Click": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "input tap {X} {Y}" - ], - "Swipe": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "input swipe {X1} {Y1} {X2} {Y2} {DURATION}" - ], - "PressKey": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "input keyevent {KEY}" - ], - "ForwardSocket": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "forward", - "tcp:{FOWARD_PORT}", - "localabstract:{LOCAL_SOCKET}" - ], - "NetcatAddress": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "cat /proc/net/arp | grep : " - ], - "ScreencapRawByNetcat": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "exec-out", - "screencap | nc -w 3 {NETCAT_ADDRESS} {NETCAT_PORT}" - ], - "ScreencapRawWithGzip": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "exec-out", - "screencap | gzip -1" - ], - "ScreencapEncode": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "exec-out", - "screencap -p" - ], - "ScreencapEncodeToFile": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "screencap -p > \"/data/local/tmp/{TEMP_FILE}\"" - ], - "PullFile": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "pull", - "/data/local/tmp/{TEMP_FILE}", - "{DST_PATH}" - ], - "Abilist": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "getprop ro.product.cpu.abilist | tr -d '\n\r'" - ], - "SDK": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "getprop ro.build.version.sdk | tr -d '\n\r'" - ], - "Orientation": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "dumpsys input | grep SurfaceOrientation | grep -m 1 -o -E [0-9]" - ], - "PushBin": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "push", - "{BIN_PATH}", - "/data/local/tmp/{BIN_WORKING_FILE}" - ], - "ChmodBin": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "chmod 700 \"/data/local/tmp/{BIN_WORKING_FILE}\"" - ], - "InvokeBin": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "export LD_LIBRARY_PATH=/data/local/tmp/; \"/data/local/tmp/{BIN_WORKING_FILE}\" {BIN_EXTRA_PARAMS} 2>&1" - ], - "InvokeApp": [ - "{ADB}", - "-s", - "{ADB_SERIAL}", - "shell", - "export CLASSPATH=\"/data/local/tmp/{APP_WORKING_FILE}\"; app_process /data/local/tmp {PACKAGE_NAME}" - ] - } -})"_json; - -MAA_TOOLKIT_DEVICE_NS_END diff --git a/source/MaaToolKit/AdbDevice/DeviceMgrMacOS.cpp b/source/MaaToolKit/AdbDevice/DeviceMgrMacOS.cpp index 695084366..6cd3d7e86 100644 --- a/source/MaaToolKit/AdbDevice/DeviceMgrMacOS.cpp +++ b/source/MaaToolKit/AdbDevice/DeviceMgrMacOS.cpp @@ -5,7 +5,6 @@ #include #include -#include "AdbConfigDef.hpp" #include "Utils/Logger.h" #include "Utils/Platform.h" #include "Utils/Ranges.hpp" @@ -39,12 +38,12 @@ std::vector DeviceMgrMacOS::find_device_impl() const auto& constant = kEmulators.at(e.name); std::filesystem::path adb_path = get_adb_path(constant, e.process.pid); - auto serials = request_adb_serials(adb_path, kAdbConfig); + auto serials = request_adb_serials(adb_path, json::value()); serials.insert(serials.end(), constant.adb_common_serials.begin(), constant.adb_common_serials.end()); // Deduplication auto set = std::set(serials.begin(), serials.end()); serials.assign(set.begin(), set.end()); - serials = check_available_adb_serials(adb_path, serials, kAdbConfig); + serials = check_available_adb_serials(adb_path, serials, json::value()); for (const std::string& ser : serials) { Device device; @@ -52,7 +51,7 @@ std::vector DeviceMgrMacOS::find_device_impl() device.adb_path = path_to_utf8_string(adb_path); device.adb_serial = ser; // TODO: 根据设备情况使用不同的配置 - device.adb_config = kAdbConfig.to_string(); + device.adb_config = json::object().to_string(); device.adb_controller_type = check_adb_controller_type(device.adb_path, device.adb_serial, device.adb_config); result.emplace_back(std::move(device)); @@ -66,14 +65,14 @@ std::vector DeviceMgrMacOS::find_device_with_adb_impl(std::string_view a { std::vector result; - auto serials = request_adb_serials(path(adb_path), kAdbConfig); + auto serials = request_adb_serials(path(adb_path), json::value()); for (const std::string& ser : serials) { Device device; device.name = adb_path; device.adb_path = adb_path; device.adb_serial = ser; - device.adb_config = kAdbConfig.to_string(); + device.adb_config = json::object().to_string(); device.adb_controller_type = check_adb_controller_type(device.adb_path, device.adb_serial, device.adb_config); result.emplace_back(std::move(device)); } diff --git a/source/MaaToolKit/AdbDevice/DeviceMgrWin32.cpp b/source/MaaToolKit/AdbDevice/DeviceMgrWin32.cpp index ba16b1676..2fb0a6091 100644 --- a/source/MaaToolKit/AdbDevice/DeviceMgrWin32.cpp +++ b/source/MaaToolKit/AdbDevice/DeviceMgrWin32.cpp @@ -9,7 +9,6 @@ #include -#include "AdbConfigDef.hpp" #include "Utils/Logger.h" #include "Utils/Platform.h" #include "Utils/Ranges.hpp" @@ -68,12 +67,12 @@ std::vector DeviceMgrWin32::find_device_impl() const auto& constant = kEmulators.at(e.name); std::filesystem::path adb_path = get_adb_path(constant, e.process.pid); - auto serials = request_adb_serials(adb_path, kAdbConfig); + auto serials = request_adb_serials(adb_path, json::value()); serials.insert(serials.end(), constant.adb_common_serials.begin(), constant.adb_common_serials.end()); // Deduplication auto set = std::set(serials.begin(), serials.end()); serials.assign(set.begin(), set.end()); - serials = check_available_adb_serials(adb_path, serials, kAdbConfig); + serials = check_available_adb_serials(adb_path, serials, json::value()); for (const std::string& ser : serials) { Device device; @@ -81,7 +80,7 @@ std::vector DeviceMgrWin32::find_device_impl() device.adb_path = path_to_utf8_string(adb_path); device.adb_serial = ser; // TODO: 根据设备情况使用不同的配置 - device.adb_config = kAdbConfig.to_string(); + device.adb_config = json::object().to_string(); device.adb_controller_type = check_adb_controller_type(device.adb_path, device.adb_serial, device.adb_config); result.emplace_back(std::move(device)); @@ -95,14 +94,14 @@ std::vector DeviceMgrWin32::find_device_with_adb_impl(std::string_view a { std::vector result; - auto serials = request_adb_serials(path(adb_path), kAdbConfig); + auto serials = request_adb_serials(path(adb_path), json::value()); for (const std::string& ser : serials) { Device device; device.name = adb_path; device.adb_path = adb_path; device.adb_serial = ser; - device.adb_config = kAdbConfig.to_string(); + device.adb_config = json::object().to_string(); device.adb_controller_type = check_adb_controller_type(device.adb_path, device.adb_serial, device.adb_config); result.emplace_back(std::move(device)); } diff --git a/test/dlopen/main.cpp b/test/dlopen/main.cpp index af9544c46..58ed811c7 100644 --- a/test/dlopen/main.cpp +++ b/test/dlopen/main.cpp @@ -5,9 +5,10 @@ int main() { #ifdef WITH_ADB_CONTROLLER + auto adb_type = MaaAdbControllerType_Input_Preset_Adb | MaaAdbControllerType_Screencap_FastestWay; std::cout << "********** AdbControlUnitLibraryHolder::create_control_unit **********" << std::endl; - auto adb_handle = MAA_NS::AdbControlUnitLibraryHolder::create_control_unit( - "adb", "127.0.0.1:5555", 0, R"({"NotParse":true})", "./", nullptr, nullptr); + auto adb_handle = MAA_NS::AdbControlUnitLibraryHolder::create_control_unit("adb", "127.0.0.1:5555", adb_type, "{}", + "./", nullptr, nullptr); if (!adb_handle) { std::cerr << "Failed to create adb control unit" << std::endl; return -1; @@ -15,9 +16,11 @@ int main() #endif #ifdef WITH_WIN32_CONTROLLER + auto win32_type = MaaWin32ControllerType_Touch_SendMessage | MaaWin32ControllerType_Key_SendMessage | + MaaWin32ControllerType_Screencap_GDI; std::cout << "********** Win32ControlUnitLibraryHolder::create_control_unit **********" << std::endl; - auto win32_handle = - MAA_NS::Win32ControlUnitLibraryHolder::create_control_unit(FindWindowA(nullptr, nullptr), 0, nullptr, nullptr); + auto win32_handle = MAA_NS::Win32ControlUnitLibraryHolder::create_control_unit(FindWindowA(nullptr, nullptr), + win32_type, nullptr, nullptr); if (!win32_handle) { std::cerr << "Failed to create win32 control unit" << std::endl; return -1; @@ -26,7 +29,8 @@ int main() #ifdef WITH_THRIFT_CONTROLLER std::cout << "********** ThriftControlUnitLibraryHolder::create_control_unit **********" << std::endl; - auto thrift_handle = MAA_NS::ThriftControlUnitLibraryHolder::create_control_unit(1, "127.0.0.1", 8080, "{}"); + auto thrift_handle = MAA_NS::ThriftControlUnitLibraryHolder::create_control_unit(MaaThriftControllerType_Socket, + "127.0.0.1", 8080, "{}"); if (!thrift_handle) { std::cerr << "Failed to create thrift control unit" << std::endl; return -1; @@ -35,7 +39,8 @@ int main() #ifdef WITH_DBG_CONTROLLER std::cout << "********** DbgControlUnitLibraryHolder::create_control_unit **********" << std::endl; - auto dbg_handle = MAA_NS::DbgControlUnitLibraryHolder::create_control_unit(1, "./"); + auto dbg_handle = + MAA_NS::DbgControlUnitLibraryHolder::create_control_unit(MaaDbgControllerType_CarouselImage, "./"); if (!dbg_handle) { std::cerr << "Failed to create dbg control unit" << std::endl; return -1;