From 902783b8510922e50a38ccfa5ddbd5c246f0f88e Mon Sep 17 00:00:00 2001 From: MistEO Date: Mon, 7 Nov 2022 02:20:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=91=BD=E4=BB=A4=E6=94=AF=E6=8C=81min?= =?UTF-8?q?itouch=E7=82=B9=E5=87=BB=E5=92=8C=E6=BB=91=E5=8A=A8=EF=BC=88?= =?UTF-8?q?=E4=BD=86=E6=98=AF=E4=B8=8D=E7=9F=A5=E9=81=93=E4=B8=BA=E5=95=A5?= =?UTF-8?q?=E4=B8=8D=E8=B5=B7=E6=9D=A5=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/config.json | 16 +++++----- src/MeoAssistant/Controller.cpp | 53 +++++++++++++++++++++++++-------- src/MeoAssistant/Controller.h | 39 ++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 20 deletions(-) diff --git a/resource/config.json b/resource/config.json index 391f8d95c3..cfba5b3373 100644 --- a/resource/config.json +++ b/resource/config.json @@ -58,7 +58,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "BlueStacks": { "devices": "[Adb] devices", @@ -79,7 +79,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "MuMuEmulator": { "devices": "[Adb] devices", @@ -100,7 +100,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "LDPlayer": { "devices": "[Adb] devices", @@ -121,7 +121,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "Nox": { "devices": "[Adb] devices", @@ -142,7 +142,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "XYAZ": { "devices": "[Adb] devices", @@ -163,7 +163,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "WSA": { "devices": "[Adb] devices", @@ -185,7 +185,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" }, "Compatible": { "devices": "[Adb] devices", @@ -206,7 +206,7 @@ "stop": "[Adb] -s [AdbSerial] shell \"am force-stop `dumpsys activity activities 2>/dev/null | grep Activities= 2>/dev/null | grep -m 1 -i -o -E [^\\ ]*arknights[^/]*`\"", "pushMinitouch": "[Adb] -s [AdbSerial] push \"[minitouchLocalPath]\" \"/data/local/tmp/[minitouchWorkingFile]\"", "chmodMinitouch": "[Adb] -s [AdbSerial] shell chmod 700 \"/data/local/tmp/[minitouchWorkingFile]\"", - "callMinitouch": "[Adb] -s [AdbSerial] shell \"/data/local/tmp/[minitouchWorkingFile]\" [minitouchInput]" + "callMinitouch": "[Adb] -s [AdbSerial] shell 'echo \"[minitouchInput]\" | \"/data/local/tmp/[minitouchWorkingFile]\" -i'" } } } diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 81feeceb8c..99a00b3dce 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -831,9 +831,16 @@ bool asst::Controller::click_without_scale(const Point& p) if (p.x < 0 || p.x >= m_width || p.y < 0 || p.y >= m_height) { Log.error("click point out of range"); } - std::string cur_cmd = - utils::string_replace_all(m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } }); + std::string cur_cmd; + if (m_enable_minitouch) { + std::string minitouch_cmd = MinitouchCmd::down(p.x, p.y) + MinitouchCmd::up(); + cur_cmd = utils::string_replace_all(m_adb.call_minitouch, { { "[minitouchInput]", minitouch_cmd } }); + } + else { + cur_cmd = + utils::string_replace_all(m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } }); + } return call_command(cur_cmd).has_value(); } @@ -870,19 +877,41 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int y1 = std::clamp(y1, 0, m_height - 1); } - std::string cur_cmd = - utils::string_replace_all(m_adb.swipe, { - { "[x1]", std::to_string(x1) }, - { "[y1]", std::to_string(y1) }, - { "[x2]", std::to_string(x2) }, - { "[y2]", std::to_string(y2) }, - { "[duration]", duration <= 0 ? "" : std::to_string(duration) }, - }); + std::string cur_cmd; + if (m_enable_minitouch) { + std::string minitouch_cmd = MinitouchCmd::down(x1, x2); + if (duration == 0) { + duration = 1000; + } + constexpr int MoveInterval = MinitouchCmd::DefaultInterval; + int move_times = std::max(duration / MoveInterval, 1); + int x_step = (x2 - x1) / move_times; + int y_step = (y2 - y1) / move_times; + // TODO: 加点随机因子,或者改成中间快两头慢 + for (int times = 1; times < move_times; ++times) { + minitouch_cmd += MinitouchCmd::move(x1 + x_step * times, y1 + y_step * times); + } + minitouch_cmd += MinitouchCmd::move(x2, y2); + minitouch_cmd += MinitouchCmd::up(); + + cur_cmd = utils::string_replace_all(m_adb.call_minitouch, { { "[minitouchInput]", minitouch_cmd } }); + } + else { + cur_cmd = + utils::string_replace_all(m_adb.swipe, { + { "[x1]", std::to_string(x1) }, + { "[y1]", std::to_string(y1) }, + { "[x2]", std::to_string(x2) }, + { "[y2]", std::to_string(y2) }, + { "[duration]", duration <= 0 ? "" : std::to_string(duration) }, + }); + } + bool ret = call_command(cur_cmd).has_value(); // 额外的滑动:adb有bug,同样的参数,偶尔会划得非常远。额外做一个短程滑动,把之前的停下来 const auto& opt = Configer.get_options(); - if (extra_swipe && opt.adb_extra_swipe_duration > 0) { + if (!m_enable_minitouch && extra_swipe && opt.adb_extra_swipe_duration > 0) { std::string extra_cmd = utils::string_replace_all( m_adb.swipe, { { "[x1]", std::to_string(x2) }, @@ -1180,7 +1209,7 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a call_command(minitouch_cmd_rep(adb_cfg.push_minitouch)); call_command(minitouch_cmd_rep(adb_cfg.chmod_minitouch)); - m_adb.call_minitouch = minitouch_cmd_rep(adb_cfg.push_minitouch); + m_adb.call_minitouch = minitouch_cmd_rep(adb_cfg.call_minitouch); } // try to find the fastest way diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 7a29e57af0..b9d4874c9e 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -176,6 +176,45 @@ namespace asst mutable std::shared_mutex m_image_mutex; cv::Mat m_cache_image; + private: + struct MinitouchCmd + { + inline static constexpr int DefaultInterval = 50; + inline static std::string reset() { return "r\\n"; } + inline static std::string commit() { return "c\\n"; } + inline static std::string down(int x, int y, bool with_commit = true, bool with_wait = true, + int contact = 0, int pressure = 50) + { + std::stringstream ss; + ss << "d " << contact << " " << x << " " << y << " " << pressure << "\\n"; + if (with_commit) ss << commit(); + if (with_wait) ss << wait(); + return ss.str(); + } + inline static std::string move(int x, int y, bool with_commit = true, bool with_wait = true, + int contact = 0, int pressure = 50) + { + std::stringstream ss; + ss << "m " << contact << " " << x << " " << y << " " << pressure << "\\n"; + if (with_commit) ss << commit(); + if (with_wait) ss << wait(); + return ss.str(); + } + inline static std::string up(bool with_commit = true, int contact = 0) + { + std::stringstream ss; + ss << "u " << contact << "\\n"; + if (with_commit) ss << commit(); + return ss.str(); + } + inline static std::string wait(int ms = DefaultInterval) + { + std::stringstream ss; + ss << "w " << ms << "\\n"; + return ss.str(); + } + }; + private: #ifdef _WIN32 // for Windows socket