From 1a0add784de6162736bc41e1cf64b44fcf77684f Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 18 Jul 2021 02:40:57 +0800 Subject: [PATCH] try to supoort MuMu --- MeoAssistance/include/Assistance.h | 4 +- MeoAssistance/include/AsstCaller.h | 1 + MeoAssistance/include/AsstDef.h | 13 ++++++ MeoAssistance/include/Configer.h | 16 ++++++- MeoAssistance/include/WinMacro.h | 6 ++- MeoAssistance/src/Assistance.cpp | 22 ++++++---- MeoAssistance/src/AsstCaller.cpp | 16 ++++++- MeoAssistance/src/Configer.cpp | 32 +++++++++++--- MeoAssistance/src/WinMacro.cpp | 67 ++++++++++++++++++++++------- MeoAsstGui/MainWindow.xaml | 1 + MeoAsstGui/MainWindow.xaml.cs | 15 +++++++ meojson | 2 +- resource/PrtsErrorConfirm.png | Bin 392165 -> 405199 bytes resource/config.json | 60 +++++++++++++++++++++++++- 14 files changed, 217 insertions(+), 38 deletions(-) diff --git a/MeoAssistance/include/Assistance.h b/MeoAssistance/include/Assistance.h index 03bd5d9dca..8234503580 100644 --- a/MeoAssistance/include/Assistance.h +++ b/MeoAssistance/include/Assistance.h @@ -22,10 +22,10 @@ namespace asst { std::optional setSimulator(const std::string & simulator_name = std::string()); void start(const std::string & task); - void stop(); + void stop(bool block = true); bool setParam(const std::string& type, const std::string& param, const std::string& value); - + std::optional getParam(const std::string& type, const std::string& param); private: static void workingProc(Assistance* pThis); diff --git a/MeoAssistance/include/AsstCaller.h b/MeoAssistance/include/AsstCaller.h index 3bb90ccb6b..f6d016a420 100644 --- a/MeoAssistance/include/AsstCaller.h +++ b/MeoAssistance/include/AsstCaller.h @@ -12,6 +12,7 @@ extern __declspec(dllexport) bool AsstCatchSimulator(asst::Assistance* p_asst); extern __declspec(dllexport) void AsstStart(asst::Assistance* p_asst, const char* task); extern __declspec(dllexport) void AsstStop(asst::Assistance* p_asst); extern __declspec(dllexport) bool AsstSetParam(asst::Assistance* p_asst, const char* type, const char* param, const char* value); +extern __declspec(dllexport) bool AsstGetParam(asst::Assistance* p_asst, const char* type, const char* param, char * buffer, int buffer_size); #ifdef __cplusplus } diff --git a/MeoAssistance/include/AsstDef.h b/MeoAssistance/include/AsstDef.h index 30bc9be0d6..72a89dabba 100644 --- a/MeoAssistance/include/AsstDef.h +++ b/MeoAssistance/include/AsstDef.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -110,4 +111,16 @@ namespace asst { { DebugPrint("ERR", std::forward(args)...); } + + static std::string replace_all_distinct(const std::string& src, const std::string& old_value, const std::string& new_value) + { + std::string str = src; + for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()) { + if ((pos = str.find(old_value, pos)) != std::string::npos) + str.replace(pos, old_value.length(), new_value); + else + break; + } + return str; + } } \ No newline at end of file diff --git a/MeoAssistance/include/Configer.h b/MeoAssistance/include/Configer.h index 7f9e8d9a84..8d80f4e0a3 100644 --- a/MeoAssistance/include/Configer.h +++ b/MeoAssistance/include/Configer.h @@ -3,6 +3,8 @@ #include #include #include +#include + #include "AsstDef.h" namespace asst { @@ -10,14 +12,21 @@ namespace asst { std::string className; std::string windowName; }; + struct AdbCmd { + std::string path; + std::string connect; + std::string click; + }; struct SimulatorInfo { std::vector window; std::vector view; std::vector control; + bool is_adb = false; + AdbCmd adb; int width = 0; int height = 0; - int xOffset = 0; - int yOffset = 0; + int x_offset = 0; + int y_offset = 0; }; enum class TaskType { @@ -58,6 +67,9 @@ namespace asst { static std::unordered_map m_handles; static bool setParam(const std::string& type, const std::string& param, const std::string& value); + static std::optional getParam(const std::string& type, const std::string& param); + + static void clearExecTimes(); private: Configer() = default; diff --git a/MeoAssistance/include/WinMacro.h b/MeoAssistance/include/WinMacro.h index 1582106dcb..5d00a256c9 100644 --- a/MeoAssistance/include/WinMacro.h +++ b/MeoAssistance/include/WinMacro.h @@ -30,10 +30,12 @@ namespace asst { const std::string m_simulator_name; const HandleType m_handle_type; HWND m_handle = NULL; + bool m_is_adb = false; + std::string m_click_cmd; std::minstd_rand m_rand_engine; int m_width = 0; int m_height = 0; - int m_xOffset = 0; - int m_yOffset = 0; + int m_x_offset = 0; + int m_y_offset = 0; }; } diff --git a/MeoAssistance/src/Assistance.cpp b/MeoAssistance/src/Assistance.cpp index a30e30683d..f6a0ce3e43 100644 --- a/MeoAssistance/src/Assistance.cpp +++ b/MeoAssistance/src/Assistance.cpp @@ -65,7 +65,7 @@ std::optional Assistance::setSimulator(const std::string& simulator else { ret = create_handles(simulator_name); } - if (ret && m_pWindow->resizeWindow()) { + if (ret && m_pWindow->showWindow() && m_pWindow->resizeWindow() ) { m_inited = true; return cor_name; } @@ -89,13 +89,16 @@ void Assistance::start(const std::string& task) m_condvar.notify_one(); } -void Assistance::stop() +void Assistance::stop(bool block) { - std::unique_lock lock(m_mutex); - + std::unique_lock lock; + if (block) { + lock = std::unique_lock(m_mutex); + } m_thread_running = false; m_next_tasks.clear(); m_pIder->clear_cache(); + Configer::clearExecTimes(); } bool Assistance::setParam(const std::string& type, const std::string& param, const std::string& value) @@ -103,6 +106,11 @@ bool Assistance::setParam(const std::string& type, const std::string& param, con return Configer::setParam(type, param, value); } +std::optional Assistance::getParam(const std::string& type, const std::string& param) +{ + return Configer::getParam(type, param); +} + void Assistance::workingProc(Assistance* pThis) { while (!pThis->m_thread_exit) { @@ -141,8 +149,7 @@ void Assistance::workingProc(Assistance* pThis) } if (task.exec_times >= task.max_times) { DebugTraceInfo("Reached limit, Stop."); - pThis->m_thread_running = false; - pThis->m_next_tasks.clear(); + pThis->stop(false); continue; } pThis->m_pCtrl->clickRange(matched_rect); @@ -155,8 +162,7 @@ void Assistance::workingProc(Assistance* pThis) break; case TaskType::Stop: DebugTrace("TaskType is Stop"); - pThis->m_thread_running = false; - pThis->m_next_tasks.clear(); + pThis->stop(false); continue; break; default: diff --git a/MeoAssistance/src/AsstCaller.cpp b/MeoAssistance/src/AsstCaller.cpp index dda2cb4e6c..efea7880ce 100644 --- a/MeoAssistance/src/AsstCaller.cpp +++ b/MeoAssistance/src/AsstCaller.cpp @@ -1,5 +1,6 @@ #include "AsstCaller.h" +#include asst::Assistance* CreateAsst() { @@ -54,5 +55,18 @@ bool AsstSetParam(asst::Assistance* p_asst, const char* type, const char* param, return false; } - p_asst->setParam(type, param, value); + return p_asst->setParam(type, param, value); } + +bool AsstGetParam(asst::Assistance* p_asst, const char* type, const char* param, char * buffer, int buffer_size) +{ + if (p_asst == NULL) { + return false; + } + auto ret = p_asst->getParam(type, param); + if (!ret) { + return false; + } + strcpy_s(buffer, buffer_size, ret.value().c_str()); + return true; +} \ No newline at end of file diff --git a/MeoAssistance/src/Configer.cpp b/MeoAssistance/src/Configer.cpp index 90586abe05..9758944ce3 100644 --- a/MeoAssistance/src/Configer.cpp +++ b/MeoAssistance/src/Configer.cpp @@ -75,13 +75,13 @@ bool Configer::reload() DebugTraceError("Task:", name, "error:", type); return false; } - if (task_json.as_object().exist("maxTimes")) { + if (task_json.exist("maxTimes")) { task_info.max_times = task_json["maxTimes"].as_integer(); } - if (task_json.as_object().exist("preDelay")) { + if (task_json.exist("preDelay")) { task_info.pre_delay = task_json["preDelay"].as_integer(); } - if (task_json.as_object().exist("rearDelay")) { + if (task_json.exist("rearDelay")) { task_info.rear_delay = task_json["rearDelay"].as_integer(); } @@ -119,10 +119,17 @@ bool Configer::reload() handle_info.windowName = info["window"].as_string(); simulator_info.control.emplace_back(handle_info); } + if (simulator_json.exist("adbControl")) { + simulator_info.is_adb = true; + // meojson的bug,暂时没空修,先转个字符串 + simulator_info.adb.path = replace_all_distinct(simulator_json["adbControl"]["path"].as_string(), "\\\\", "\\"); + simulator_info.adb.connect = simulator_json["adbControl"]["connect"].as_string(); + simulator_info.adb.click = simulator_json["adbControl"]["click"].as_string(); + } simulator_info.width = simulator_json["width"].as_integer(); simulator_info.height = simulator_json["height"].as_integer(); - simulator_info.xOffset = simulator_json["xOffset"].as_integer(); - simulator_info.yOffset = simulator_json["yOffset"].as_integer(); + simulator_info.x_offset = simulator_json["xOffset"].as_integer(); + simulator_info.y_offset = simulator_json["yOffset"].as_integer(); m_handles.emplace(name, simulator_info); } @@ -171,4 +178,19 @@ bool Configer::setParam(const std::string& type, const std::string& param, const m_tasks[param].max_times = std::stoi(value); } return true; +} + +std::optional Configer::getParam(const std::string& type, const std::string& param) +{ + if (type == "task.execTimes" && m_tasks.find(param) != m_tasks.cend()) { + return std::to_string(m_tasks.at(param).exec_times); + } + return std::nullopt; +} + +void Configer::clearExecTimes() +{ + for (auto&& t : m_tasks) { + t.second.exec_times = 0; + } } \ No newline at end of file diff --git a/MeoAssistance/src/WinMacro.cpp b/MeoAssistance/src/WinMacro.cpp index e3651da858..28d475e22d 100644 --- a/MeoAssistance/src/WinMacro.cpp +++ b/MeoAssistance/src/WinMacro.cpp @@ -40,8 +40,9 @@ bool WinMacro::findHandle() handle_vec = info.view; break; case HandleType::Control: - m_xOffset = info.xOffset; - m_yOffset = info.yOffset; + m_is_adb = info.is_adb; + m_x_offset = info.x_offset; + m_y_offset = info.y_offset; handle_vec = info.control; break; default: @@ -77,7 +78,29 @@ bool WinMacro::findHandle() window_wbuff = NULL; } } + if (m_is_adb && m_handle != NULL) { + DWORD pid = 0; + ::GetWindowThreadProcessId(m_handle, &pid); + HANDLE handle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + LPSTR path_buff = new CHAR[MAX_PATH]; + memset(path_buff, 0, MAX_PATH); + DWORD buff_size = MAX_PATH; + QueryFullProcessImageNameA(handle, 0, path_buff, &buff_size); + std::string adb_dir = path_buff; + if (path_buff != NULL) { + delete[] path_buff; + path_buff = NULL; + } + size_t pos = adb_dir.find_last_of('\\') + 1; + adb_dir = adb_dir.substr(0, pos); + adb_dir = '"' + replace_all_distinct(info.adb.path, "[EmulatorPath]", adb_dir) + '"'; + std::string connect_cmd = adb_dir + info.adb.connect; + int ret = system(connect_cmd.c_str()); + DebugTrace("Call", connect_cmd, "—— ret", ret); + + m_click_cmd = adb_dir + info.adb.click; + } DebugTrace("Handle:", m_handle, "Name:", m_simulator_name, "Type:", static_cast(m_handle_type)); if (m_handle != NULL) { @@ -111,7 +134,7 @@ bool WinMacro::showWindow() return false; } - return ::ShowWindow(m_handle, SW_SHOW); + return ::ShowWindow(m_handle, SW_RESTORE); } bool WinMacro::hideWindow() @@ -163,17 +186,29 @@ bool WinMacro::click(const Point& p) return false; } - int x = (p.x + m_xOffset) / getScreenScale(); - int y = (p.y + m_yOffset) / getScreenScale(); - DebugTrace("click, raw:", p.x, p.y, "cor:", x, y); - LPARAM lparam = MAKELPARAM(x, y); + if (m_is_adb) { + int x = (p.x + m_x_offset); + int y = (p.y + m_y_offset); + std::string cur_cmd = replace_all_distinct(m_click_cmd, "[x]", std::to_string(x)); + cur_cmd = replace_all_distinct(cur_cmd, "[y]", std::to_string(y)); + int ret = system(cur_cmd.c_str()); + DebugTrace("Call", cur_cmd, "—— ret", ret); + return !ret; + } + else { + int x = (p.x + m_x_offset) / getScreenScale(); + int y = (p.y + m_y_offset) / getScreenScale(); + DebugTrace("Click, raw:", p.x, p.y, "cor:", x, y); - ::SendMessage(m_handle, WM_LBUTTONDOWN, MK_LBUTTON, lparam); - ::SendMessage(m_handle, WM_LBUTTONUP, 0, lparam); + LPARAM lparam = MAKELPARAM(x, y); - return true; + ::SendMessage(m_handle, WM_LBUTTONDOWN, MK_LBUTTON, lparam); + ::SendMessage(m_handle, WM_LBUTTONUP, 0, lparam); + + return true; + } } bool WinMacro::clickRange(const Rect& rect) @@ -242,12 +277,12 @@ cv::Mat WinMacro::getImage(const Rect& rect) DeleteDC(memDC); ReleaseDC(m_handle, pDC); -/* -#ifdef _DEBUG - std::string filename = GetCurrentDir() + "\\print.bmp"; - cv::imwrite(filename, dst_mat); -#endif -*/ + /* + #ifdef _DEBUG + std::string filename = GetCurrentDir() + "\\print.bmp"; + cv::imwrite(filename, dst_mat); + #endif + */ return dst_mat; } \ No newline at end of file diff --git a/MeoAsstGui/MainWindow.xaml b/MeoAsstGui/MainWindow.xaml index db3b8d9a9b..18718970df 100644 --- a/MeoAsstGui/MainWindow.xaml +++ b/MeoAsstGui/MainWindow.xaml @@ -15,5 +15,6 @@