perf.优化ADB端口识别

This commit is contained in:
MistEO
2021-11-09 01:40:26 +08:00
parent 2e3fe35104
commit b2d737cf47
7 changed files with 200 additions and 198 deletions

View File

@@ -5,93 +5,93 @@
namespace json
{
static std::string unescape_string(std::string&& str)
{
std::string replace_str;
std::string escape_str = std::move(str);
static std::string unescape_string(std::string&& str)
{
std::string replace_str;
std::string escape_str = std::move(str);
for (size_t pos = 0; pos < escape_str.size(); ++pos)
{
switch (escape_str[pos]) {
case '\"':
replace_str = R"(\")";
break;
case '\\':
replace_str = R"(\\)";
break;
case '\b':
replace_str = R"(\b)";
break;
case '\f':
replace_str = R"(\f)";
break;
case '\n':
replace_str = R"(\n)";
break;
case '\r':
replace_str = R"(\r)";
break;
case '\t':
replace_str = R"(\t)";
break;
default:
continue;
break;
}
escape_str.replace(pos, 1, replace_str);
++pos;
}
return escape_str;
}
for (size_t pos = 0; pos < escape_str.size(); ++pos)
{
switch (escape_str[pos]) {
case '\"':
replace_str = R"(\")";
break;
case '\\':
replace_str = R"(\\)";
break;
case '\b':
replace_str = R"(\b)";
break;
case '\f':
replace_str = R"(\f)";
break;
case '\n':
replace_str = R"(\n)";
break;
case '\r':
replace_str = R"(\r)";
break;
case '\t':
replace_str = R"(\t)";
break;
default:
continue;
break;
}
escape_str.replace(pos, 1, replace_str);
++pos;
}
return escape_str;
}
static std::string unescape_string(const std::string& str)
{
return unescape_string(std::string(str));
}
static std::string unescape_string(const std::string& str)
{
return unescape_string(std::string(str));
}
static std::string escape_string(std::string&& str)
{
std::string escape_str = std::move(str);
static std::string escape_string(std::string&& str)
{
std::string escape_str = std::move(str);
for (size_t pos = 0; pos + 1 < escape_str.size(); ++pos)
{
if (escape_str[pos] != '\\') {
continue;
}
std::string replace_str;
switch (escape_str[pos+1]) {
case '"':
replace_str = "\"";
break;
case '\\':
replace_str = "\\";
break;
case 'b':
replace_str = "\b";
break;
case 'f':
replace_str = "\f";
break;
case 'n':
replace_str = "\n";
break;
case 'r':
replace_str = "\r";
break;
case 't':
replace_str = "\r";
break;
default:
return std::string();
break;
}
escape_str.replace(pos, 2, replace_str);
}
return escape_str;
}
for (size_t pos = 0; pos + 1 < escape_str.size(); ++pos)
{
if (escape_str[pos] != '\\') {
continue;
}
std::string replace_str;
switch (escape_str[pos + 1]) {
case '"':
replace_str = "\"";
break;
case '\\':
replace_str = "\\";
break;
case 'b':
replace_str = "\b";
break;
case 'f':
replace_str = "\f";
break;
case 'n':
replace_str = "\n";
break;
case 'r':
replace_str = "\r";
break;
case 't':
replace_str = "\t";
break;
default:
return std::string();
break;
}
escape_str.replace(pos, 2, replace_str);
}
return escape_str;
}
static std::string escape_string(const std::string& str)
{
return escape_string(std::string(str));
}
static std::string escape_string(const std::string& str)
{
return escape_string(std::string(str));
}
}

View File

@@ -34,12 +34,14 @@
},
"adb": {
"path": "[EmulatorPath]Engine\\ProgramFiles\\HD-Adb.exe",
"connect": "[Adb] connect 127.0.0.1:5555",
"click": "[Adb] -s 127.0.0.1:5555 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:5555 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:5555 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:5555 exec-out screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] exec-out screencap -p"
}
},
"BlueStacksInternational": {
@@ -49,12 +51,14 @@
},
"adb": {
"path": "[EmulatorPath]HD-Adb.exe",
"connect": "[Adb] connect 127.0.0.1:5555",
"click": "[Adb] -s 127.0.0.1:5555 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:5555 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:5555 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:5555 exec-out screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] exec-out screencap -p"
}
},
"MuMuEmulator": {
@@ -64,42 +68,31 @@
},
"adb": {
"path": "[EmulatorPath]..\\vmonitor\\bin\\adb_server.exe",
"connect": "[Adb] connect 127.0.0.1:7555",
"click": "[Adb] -s 127.0.0.1:7555 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:7555 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:7555 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:7555 exec-out screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] exec-out screencap -p"
}
},
"LDPlayerLocal": {
"LDPlayer": {
"handle": {
"class": "LDPlayerMainFrame",
"window": ""
},
"adb": {
"path": "[EmulatorPath]adb.exe",
"connect": "[Adb] devices",
"click": "[Adb] -s emulator-5554 shell input tap [x] [y]",
"swipe": "[Adb] -s emulator-5554 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s emulator-5554 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s emulator-5554 shell screencap -p"
}
},
"LDPlayerRemote": {
"handle": {
"class": "LDPlayerMainFrame",
"window": ""
},
"adb": {
"path": "[EmulatorPath]adb.exe",
"connect": "[Adb] connect 127.0.0.1:5555",
"click": "[Adb] -s 127.0.0.1:5555 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:5555 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:5555 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:5555 shell screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] shell screencap -p"
}
},
"Nox": {
@@ -109,27 +102,14 @@
},
"adb": {
"path": "[EmulatorPath]nox_adb.exe",
"connect": "[Adb] connect 127.0.0.1:62001",
"click": "[Adb] -s 127.0.0.1:62001 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:62001 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:62001 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:62001 exec-out screencap -p"
}
},
"NoxPort2": {
"handle": {
"class": "Qt5QWindowIcon",
"window": "夜神模拟器"
},
"adb": {
"path": "[EmulatorPath]nox_adb.exe",
"connect": "[Adb] connect 127.0.0.1:59865",
"click": "[Adb] -s 127.0.0.1:59865 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:59865 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:59865 shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:59865 exec-out screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] exec-out screencap -p"
}
},
"XYAZ": {
@@ -139,12 +119,14 @@
},
"adb": {
"path": "[EmulatorPath]adb.exe",
"connect": "[Adb] connect 127.0.0.1:21503",
"click": "[Adb] -s 127.0.0.1:21503 shell input tap [x] [y]",
"swipe": "[Adb] -s 127.0.0.1:21503 shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s 127.0.0.1:21503 shell dumpsys window displays | grep init=",
"displayRegex": " init=%dx%d",
"screencap": "[Adb] -s 127.0.0.1:21503 shell screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init=",
"displayFormat": " init=%dx%d",
"screencap": "[Adb] -s [Address] shell screencap -p"
}
},
"Remote": {
@@ -155,11 +137,13 @@
},
"adb": {
"path": "[ExecDir]platform-tools\\adb.exe",
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] exec-out screencap -p"
}
},
@@ -172,12 +156,14 @@
"adb": {
"Doc": "USB连接暂时不兼容多设备的情况",
"path": "[ExecDir]platform-tools\\adb.exe",
"connect": "[Adb] devices",
"click": "[Adb] -d shell input tap [x] [y]",
"swipe": "[Adb] -d shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -d shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayRegex": "cur=%dx%d",
"screencap": "[Adb] -d exec-out screencap -p"
"devices": "[Adb] devices",
"addressRegex": "(.+)\tdevice",
"connect": "[Adb] connect [Address]",
"click": "[Adb] -s [Address] shell input tap [x] [y]",
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
"displayFormat": "cur=%dx%d",
"screencap": "[Adb] -s [Address] exec-out screencap -p"
}
}
}

View File

@@ -208,11 +208,14 @@ namespace asst {
struct AdbCmd {
std::string path;
std::string devices;
std::string address_regex;
std::string address;
std::string connect;
std::string click;
std::string swipe;
std::string display;
std::string display_regex;
std::string display_format;
std::string screencap;
//std::string pullscreen;
int display_width = 0;

View File

@@ -37,6 +37,25 @@ namespace asst {
return str;
}
static std::vector<std::string> string_split(const std::string& str, const std::string& delimiter)
{
std::string::size_type pos1 = 0;
std::string::size_type pos2 = str.find(delimiter);
std::vector<std::string> result;
while (std::string::npos != pos2)
{
result.emplace_back(str.substr(pos1, pos2 - pos1));
pos1 = pos2 + delimiter.size();
pos2 = str.find(delimiter, pos1);
}
if (pos1 != str.length())
result.emplace_back(str.substr(pos1));
return result;
}
static std::string get_format_time()
{
SYSTEMTIME curtime;

View File

@@ -7,6 +7,7 @@
#include <utility>
#include <algorithm>
#include <chrono>
#include <regex>
#include <opencv2/opencv.hpp>
@@ -198,47 +199,37 @@ bool Controller::try_capture(const EmulatorInfo& info, bool without_handle)
adb_dir = '"' + utils::string_replace_all(m_emulator_info.adb.path, "[ExecDir]", utils::get_cur_dir()) + '"';
}
//// 针对雷电模拟器连接问题的专用补丁
//if (m_emulator_info.handle.class_name == "LDPlayerMainFrame") {
// log.trace("LDPlayer Connection Patch Activated.");
// std::string connect_cmd = utils::string_replace_all("[Adb] devices", "[Adb]", adb_dir);
// auto devices_result = call_command(connect_cmd);
// std::string devices_list(
// std::make_move_iterator(devices_result.begin()),
// std::make_move_iterator(devices_result.end()));
// // 查找连接的所有设备
// std::string device = {};
std::string devices_cmd = utils::string_replace_all(m_emulator_info.adb.devices, "[Adb]", adb_dir);
auto&& [devices_ret, devices_result] = call_command(devices_cmd);
if (!devices_ret) {
return false;
}
std::string devices_pipe_str(
std::make_move_iterator(devices_result.begin()),
std::make_move_iterator(devices_result.end()));
auto lines = utils::string_split(devices_pipe_str, "\r\n");
// if (devices_list.find("emulator") != devices_list.npos) {
// device = devices_list.substr(devices_list.find("emulator"), 13);
// m_emulator_info.adb.connect = "";
// }
// else if (devices_list.find("127") != devices_list.npos) {
// device = devices_list.substr(devices_list.find("127"), 14);
// }
std::string address;
const std::regex address_regex(m_emulator_info.adb.address_regex);
for (const std::string& line : lines) {
std::smatch smatch;
if (std::regex_match(line, smatch, address_regex)) {
address = smatch[1];
}
}
log.trace("address", address);
if (address.empty()) {
return false;
}
// if (device.empty()) {
// log.trace("no device detected.");
// return false;
// }
// log.trace("device detected: ", device);
// m_emulator_info.adb.connect = utils::string_replace_all(m_emulator_info.adb.connect, "127.0.0.1:5555", device);
// m_emulator_info.adb.click = utils::string_replace_all(m_emulator_info.adb.click, "-e", "-s " + device);
// m_emulator_info.adb.swipe = utils::string_replace_all(m_emulator_info.adb.swipe, "-e", "-s " + device);
// m_emulator_info.adb.display = utils::string_replace_all(m_emulator_info.adb.display, "-e", "-s " + device);
// m_emulator_info.adb.screencap = utils::string_replace_all(m_emulator_info.adb.screencap, "-e", "-s " + device);
//}
std::string connect_cmd = utils::string_replace_all(m_emulator_info.adb.connect, "[Adb]", adb_dir);
std::string connect_cmd = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.connect, "[Adb]", adb_dir), "[Address]", address);
auto&& [connect_ret, connect_result] = call_command(connect_cmd);
// 端口即使错误命令仍然会返回0TODO 对connect_result进行判断
if (!connect_ret) {
return false;
}
std::string display_cmd = utils::string_replace_all(m_emulator_info.adb.display, "[Adb]", adb_dir);
std::string display_cmd = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.display, "[Adb]", adb_dir), "[Address]", address);
auto&& [display_ret, display_result] = call_command(display_cmd);
if (!display_ret) {
return false;
@@ -248,7 +239,7 @@ bool Controller::try_capture(const EmulatorInfo& info, bool without_handle)
std::make_move_iterator(display_result.end()));
int size_value1 = 0;
int size_value2 = 0;
sscanf_s(display_pipe_str.c_str(), m_emulator_info.adb.display_regex.c_str(), &size_value1, &size_value2);
sscanf_s(display_pipe_str.c_str(), m_emulator_info.adb.display_format.c_str(), &size_value1, &size_value2);
// 为了防止抓取句柄的时候手机是竖屏的(还没进游戏),这里取大的值为宽,小的为高
// 总不能有人竖屏玩明日方舟吧(?
m_emulator_info.adb.display_width = (std::max)(size_value1, size_value2);
@@ -272,9 +263,9 @@ bool Controller::try_capture(const EmulatorInfo& info, bool without_handle)
m_control_scale = static_cast<double>(m_emulator_info.adb.display_width) / static_cast<double>(GeneralConfiger::WindowWidthDefault);
}
m_emulator_info.adb.click = utils::string_replace_all(m_emulator_info.adb.click, "[Adb]", adb_dir);
m_emulator_info.adb.swipe = utils::string_replace_all(m_emulator_info.adb.swipe, "[Adb]", adb_dir);
m_emulator_info.adb.screencap = utils::string_replace_all(m_emulator_info.adb.screencap, "[Adb]", adb_dir);
m_emulator_info.adb.click = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.click, "[Adb]", adb_dir), "[Address]", address);
m_emulator_info.adb.swipe = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.swipe, "[Adb]", adb_dir), "[Address]", address);
m_emulator_info.adb.screencap = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.screencap, "[Adb]", adb_dir), "[Address]", address);
return true;
}

View File

@@ -36,11 +36,14 @@ bool asst::GeneralConfiger::parse(const json::value& json)
const json::object& adb_json = emulator_json.at("adb").as_object();
emulator_info.adb.path = adb_json.at("path").as_string();
emulator_info.adb.address = adb_json.get("address", std::string());
emulator_info.adb.devices = adb_json.at("devices").as_string();
emulator_info.adb.address_regex = adb_json.at("addressRegex").as_string();
emulator_info.adb.connect = adb_json.at("connect").as_string();
emulator_info.adb.click = adb_json.at("click").as_string();
emulator_info.adb.swipe = adb_json.at("swipe").as_string();
emulator_info.adb.display = adb_json.at("display").as_string();
emulator_info.adb.display_regex = adb_json.at("displayRegex").as_string();
emulator_info.adb.display_format = adb_json.at("displayFormat").as_string();
emulator_info.adb.screencap = adb_json.at("screencap").as_string();
//emulator_info.adb.pullscreen = adb_json.at("pullscreen").as_string();

View File

@@ -1,5 +1,5 @@
#pragma once
namespace asst {
constexpr static const char* Version = "v1.3-beta.2";
constexpr static const char* Version = "v1.3-beta.3";
}