From d1bd37c785263da4effaed26a30ff6674012883f Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Jul 2021 16:36:56 +0800 Subject: [PATCH] restruct Assistance --- MeoAssistance/AssDef.h | 14 ++++-- MeoAssistance/Assistance.cpp | 77 ++++++++++++++++++----------- MeoAssistance/Assistance.h | 16 ++++-- MeoAssistance/Identify.cpp | 4 ++ MeoAssistance/Identify.h | 16 ++++++ MeoAssistance/MeoAssistance.vcxproj | 2 + MeoAssistance/WinMacro.cpp | 74 ++++++++++++++++----------- MeoAssistance/WinMacro.h | 12 ++--- 8 files changed, 144 insertions(+), 71 deletions(-) create mode 100644 MeoAssistance/Identify.cpp create mode 100644 MeoAssistance/Identify.h diff --git a/MeoAssistance/AssDef.h b/MeoAssistance/AssDef.h index 3cb2d62d58..48d8c88be1 100644 --- a/MeoAssistance/AssDef.h +++ b/MeoAssistance/AssDef.h @@ -1,11 +1,19 @@ #pragma once -#include - namespace MeoAssistance { enum class SimulatorType { - BlueStacks + BlueStacks = 0x100 + }; + enum class HandleType + { + View = 1, + Control = 2, + Window = 4, + + BlueStacksView = 0x100 | 1, + BlueStacksControl = 0x100 | 2, + BlueStacksWindow = 0x100 | 4 }; struct Point diff --git a/MeoAssistance/Assistance.cpp b/MeoAssistance/Assistance.cpp index 749f2d573a..0250d7f50a 100644 --- a/MeoAssistance/Assistance.cpp +++ b/MeoAssistance/Assistance.cpp @@ -32,56 +32,40 @@ bool Assistance::setSimulatorType(SimulatorType type) { stop(); - std::unique_lock lock(m_queue_mutex); - std::queue empty; - m_click_queue.swap(empty); - m_pWinmarco = std::make_shared(type); - return m_pWinmarco->findHandle(); + std::unique_lock lock(m_tasks_mutex); + std::queue empty; + m_tasks.swap(empty); + int int_type = static_cast(type); + m_pCtrl = std::make_shared(static_cast(int_type | static_cast(HandleType::Control))); + m_pWindow = std::make_shared(static_cast(int_type | static_cast(HandleType::Window))); + m_pView = std::make_shared(static_cast(int_type | static_cast(HandleType::View))); + return m_pCtrl->findHandle() && m_pWindow->findHandle() && m_pView->findHandle(); } void Assistance::start() { - std::unique_lock lock(m_queue_mutex); + std::unique_lock lock(m_tasks_mutex); m_control_running = true; m_identify_running = true; - m_control_cv.notify_one(); - m_identify_cv.notify_one(); + m_control_cv.notify_all(); + m_identify_cv.notify_all(); } void Assistance::stop() { - std::unique_lock lock(m_queue_mutex); + std::unique_lock lock(m_tasks_mutex); m_control_running = false; m_identify_running = false; } -void Assistance::control_function(Assistance* pThis) -{ - while (!pThis->m_control_exit) { - std::unique_lock lock(pThis->m_queue_mutex); - - if (pThis->m_control_running && !pThis->m_click_queue.empty()) { - auto point = pThis->m_click_queue.front(); - pThis->m_click_queue.pop(); - lock.unlock(); - - pThis->m_pWinmarco->clickRange(point); - } - else { - pThis->m_control_cv.wait(lock); - } - } -} - void Assistance::identify_function(Assistance* pThis) { while (!pThis->m_identify_exit) { - std::unique_lock lock(pThis->m_queue_mutex); + std::unique_lock lock(pThis->m_tasks_mutex); if (pThis->m_identify_running) { - - pThis->m_click_queue.emplace(1060, 600, 0, 0); + pThis->m_tasks.emplace(Task::StartButton1); pThis->m_control_cv.notify_all(); lock.unlock(); @@ -91,4 +75,37 @@ void Assistance::identify_function(Assistance* pThis) pThis->m_identify_cv.wait(lock); } } +} + +void Assistance::control_function(Assistance* pThis) +{ + pThis->m_pWindow->resizeWindow(1200, 720); + + while (!pThis->m_control_exit) { + std::unique_lock lock(pThis->m_tasks_mutex); + if (pThis->m_control_running && !pThis->m_tasks.empty()) { + const Task task = pThis->m_tasks.front(); + pThis->m_tasks.pop(); + lock.unlock(); + + pThis->run_task(task); + } + else { + pThis->m_control_cv.wait(lock); + } + } +} + +bool Assistance::run_task(Task task) +{ + switch (task) + { + case MeoAssistance::Assistance::Task::StartButton1: + return m_pCtrl->click({ 1060, 600 }); + break; + case MeoAssistance::Assistance::Task::StartButton2: + break; + default: + break; + } } \ No newline at end of file diff --git a/MeoAssistance/Assistance.h b/MeoAssistance/Assistance.h index bf3b152e3c..a964e4ba64 100644 --- a/MeoAssistance/Assistance.h +++ b/MeoAssistance/Assistance.h @@ -13,6 +13,10 @@ namespace MeoAssistance { class Assistance { + enum class Task { + StartButton1, + StartButton2 + }; public: Assistance(); ~Assistance(); @@ -23,12 +27,16 @@ namespace MeoAssistance { void stop(); private: - static void control_function(Assistance* pThis); // 控制线程,消费者 static void identify_function(Assistance* pThis); // 识别线程,生产者 + static void control_function(Assistance* pThis); // 控制线程,消费者 - std::shared_ptr m_pWinmarco = nullptr; - std::queue m_click_queue; - std::mutex m_queue_mutex; + bool run_task(Task task); + + std::shared_ptr m_pCtrl = nullptr; + std::shared_ptr m_pWindow = nullptr; + std::shared_ptr m_pView = nullptr; + std::queue m_tasks; + std::mutex m_tasks_mutex; std::thread m_control_thread; std::thread m_identify_thread; diff --git a/MeoAssistance/Identify.cpp b/MeoAssistance/Identify.cpp new file mode 100644 index 0000000000..d4b22324bb --- /dev/null +++ b/MeoAssistance/Identify.cpp @@ -0,0 +1,4 @@ +#include "Identify.h" + +using namespace MeoAssistance; + diff --git a/MeoAssistance/Identify.h b/MeoAssistance/Identify.h new file mode 100644 index 0000000000..4fd7f385e2 --- /dev/null +++ b/MeoAssistance/Identify.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace MeoAssistance { + + class WinMacro; + + class Identify + { + public: + Identify(); + ~Identify(); + private: + }; +} diff --git a/MeoAssistance/MeoAssistance.vcxproj b/MeoAssistance/MeoAssistance.vcxproj index 548a4e8419..1f2bacc2dd 100644 --- a/MeoAssistance/MeoAssistance.vcxproj +++ b/MeoAssistance/MeoAssistance.vcxproj @@ -142,10 +142,12 @@ + + diff --git a/MeoAssistance/WinMacro.cpp b/MeoAssistance/WinMacro.cpp index 5d92874b4d..f5c60391d6 100644 --- a/MeoAssistance/WinMacro.cpp +++ b/MeoAssistance/WinMacro.cpp @@ -13,57 +13,75 @@ using namespace MeoAssistance; -WinMacro::WinMacro(SimulatorType type) - : m_simulator_type(type), +WinMacro::WinMacro(HandleType type) + : m_handle_type(type), m_rand_engine(time(NULL)) { - srand(time(NULL)); } bool WinMacro::findHandle() { - switch (m_simulator_type) { - case SimulatorType::BlueStacks: { - m_view_handle = ::FindWindow(L"BS2CHINAUI", L"BlueStacks App Player"); - HWND temp_handle = ::FindWindowEx(m_view_handle, NULL, L"BS2CHINAUI", L"HOSTWND"); - m_control_handle = ::FindWindowEx(temp_handle, NULL, L"WindowsForms10.Window.8.app.0.34f5582_r6_ad1", L"BlueStacks Android PluginAndroid"); - // ::FindWindowEx(m_view_handle, NULL, L"BlueStacksApp", L"_ctl.Window"); + switch (m_handle_type) { + case HandleType::BlueStacksControl: { + HWND temp_handle = ::FindWindow(L"BS2CHINAUI", L"BlueStacks App Player"); + temp_handle = ::FindWindowEx(temp_handle, NULL, L"BS2CHINAUI", L"HOSTWND"); + m_handle = ::FindWindowEx(temp_handle, NULL, L"WindowsForms10.Window.8.app.0.34f5582_r6_ad1", L"BlueStacks Android PluginAndroid"); + } break; + case HandleType::BlueStacksView: + case HandleType::BlueStacksWindow: + m_handle = ::FindWindow(L"BS2CHINAUI", L"BlueStacks App Player"); + break; + default: + std::cerr << "handle type error! " << static_cast(m_handle_type) << std::endl; + break; + } #ifdef _DEBUG - std::cout << "view handle: " << m_view_handle << std::endl; - std::cout << "control handle: " << m_control_handle << std::endl; + std::cout << "handle: " << m_handle << std::endl; #endif - if (m_view_handle != NULL && m_control_handle != NULL) { - MoveWindow(m_view_handle, 0, 0, 1200, 720, true); - return true; - } - else { - return false; - } - } break; - default: - std::cerr << "simulator type error! " << static_cast(m_simulator_type) << std::endl; - break; + if (m_handle != NULL) { + return true; + } + else { + return false; } } -void WinMacro::click(Point p) +bool WinMacro::resizeWindow(int width, int height) { + if (!(static_cast(m_handle_type) & static_cast(HandleType::Window))) { + return false; + } + + return ::MoveWindow(m_handle, 0, 0, width, height, true); +} + +bool WinMacro::click(Point p) +{ + if (!(static_cast(m_handle_type) & static_cast(HandleType::Control))) { + return false; + } + #ifdef _DEBUG std::cout << "click: " << p.x << ", " << p.y << std::endl; #endif LPARAM lparam = MAKELPARAM(p.x, p.y); - ::SendMessage(m_control_handle, WM_LBUTTONDOWN, MK_LBUTTON, lparam); - ::SendMessage(m_control_handle, WM_LBUTTONUP, 0, lparam); + ::SendMessage(m_handle, WM_LBUTTONDOWN, MK_LBUTTON, lparam); + ::SendMessage(m_handle, WM_LBUTTONUP, 0, lparam); + + return true; } -void WinMacro::clickRange(PointRange pr) +bool WinMacro::clickRange(PointRange pr) { - int x = 0, y = 0; + if (!(static_cast(m_handle_type) & static_cast(HandleType::Control))) { + return false; + } + int x = 0, y = 0; if (pr.right == 0) { x = pr.left; } @@ -80,5 +98,5 @@ void WinMacro::clickRange(PointRange pr) int y = y_rand(m_rand_engine) + pr.right; } - click({ x, y }); + return click({ x, y }); } \ No newline at end of file diff --git a/MeoAssistance/WinMacro.h b/MeoAssistance/WinMacro.h index 1f4618c48e..a0cec8b225 100644 --- a/MeoAssistance/WinMacro.h +++ b/MeoAssistance/WinMacro.h @@ -10,17 +10,17 @@ namespace MeoAssistance { class WinMacro { public: - WinMacro(SimulatorType type); + WinMacro(HandleType type); ~WinMacro() = default; bool findHandle(); - void click(Point p); - void clickRange(PointRange pr); + bool resizeWindow(int Width, int Height); + bool click(Point p); + bool clickRange(PointRange pr); private: - SimulatorType m_simulator_type; - HWND m_view_handle; - HWND m_control_handle; + HandleType m_handle_type; + HWND m_handle; std::minstd_rand m_rand_engine; }; }