diff --git a/3rdparty/include/meojson/json_aux.h b/3rdparty/include/meojson/json_aux.h index 3cef03f856..ebae472459 100644 --- a/3rdparty/include/meojson/json_aux.h +++ b/3rdparty/include/meojson/json_aux.h @@ -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)); + } } \ No newline at end of file diff --git a/3rdparty/lib/libmeojson.lib b/3rdparty/lib/libmeojson.lib index 6d174836e4..aaf52f9162 100644 Binary files a/3rdparty/lib/libmeojson.lib and b/3rdparty/lib/libmeojson.lib differ diff --git a/resource/config.json b/resource/config.json index d21ac331e1..671fb4561c 100644 --- a/resource/config.json +++ b/resource/config.json @@ -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" } } } diff --git a/src/MeoAssistance/AsstDef.h b/src/MeoAssistance/AsstDef.h index 0741d48448..dcd6cb77f6 100644 --- a/src/MeoAssistance/AsstDef.h +++ b/src/MeoAssistance/AsstDef.h @@ -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; diff --git a/src/MeoAssistance/AsstUtils.hpp b/src/MeoAssistance/AsstUtils.hpp index 2f63e8a8b2..3e904c8c34 100644 --- a/src/MeoAssistance/AsstUtils.hpp +++ b/src/MeoAssistance/AsstUtils.hpp @@ -37,6 +37,25 @@ namespace asst { return str; } + static std::vector 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 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; diff --git a/src/MeoAssistance/Controller.cpp b/src/MeoAssistance/Controller.cpp index 17dab3c5d0..17a02671eb 100644 --- a/src/MeoAssistance/Controller.cpp +++ b/src/MeoAssistance/Controller.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -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); // 端口即使错误,命令仍然会返回0,TODO 对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(m_emulator_info.adb.display_width) / static_cast(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; } diff --git a/src/MeoAssistance/GeneralConfiger.cpp b/src/MeoAssistance/GeneralConfiger.cpp index 10b2118db8..856bff6851 100644 --- a/src/MeoAssistance/GeneralConfiger.cpp +++ b/src/MeoAssistance/GeneralConfiger.cpp @@ -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(); diff --git a/src/MeoAssistance/Version.h b/src/MeoAssistance/Version.h index 3100240a33..e95741d0fb 100644 --- a/src/MeoAssistance/Version.h +++ b/src/MeoAssistance/Version.h @@ -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"; } \ No newline at end of file