mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-19 18:47:55 +08:00
restruct Assistance
This commit is contained in:
@@ -1,11 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
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
|
||||
|
||||
@@ -32,56 +32,40 @@ bool Assistance::setSimulatorType(SimulatorType type)
|
||||
{
|
||||
stop();
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_queue_mutex);
|
||||
std::queue<PointRange> empty;
|
||||
m_click_queue.swap(empty);
|
||||
m_pWinmarco = std::make_shared<WinMacro>(type);
|
||||
return m_pWinmarco->findHandle();
|
||||
std::unique_lock<std::mutex> lock(m_tasks_mutex);
|
||||
std::queue<Task> empty;
|
||||
m_tasks.swap(empty);
|
||||
int int_type = static_cast<int>(type);
|
||||
m_pCtrl = std::make_shared<WinMacro>(static_cast<HandleType>(int_type | static_cast<int>(HandleType::Control)));
|
||||
m_pWindow = std::make_shared<WinMacro>(static_cast<HandleType>(int_type | static_cast<int>(HandleType::Window)));
|
||||
m_pView = std::make_shared<WinMacro>(static_cast<HandleType>(int_type | static_cast<int>(HandleType::View)));
|
||||
return m_pCtrl->findHandle() && m_pWindow->findHandle() && m_pView->findHandle();
|
||||
}
|
||||
|
||||
void Assistance::start()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_queue_mutex);
|
||||
std::unique_lock<std::mutex> 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<std::mutex> lock(m_queue_mutex);
|
||||
std::unique_lock<std::mutex> 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<std::mutex> 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<std::mutex> lock(pThis->m_queue_mutex);
|
||||
std::unique_lock<std::mutex> 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<std::mutex> 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;
|
||||
}
|
||||
}
|
||||
@@ -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<WinMacro> m_pWinmarco = nullptr;
|
||||
std::queue<PointRange> m_click_queue;
|
||||
std::mutex m_queue_mutex;
|
||||
bool run_task(Task task);
|
||||
|
||||
std::shared_ptr<WinMacro> m_pCtrl = nullptr;
|
||||
std::shared_ptr<WinMacro> m_pWindow = nullptr;
|
||||
std::shared_ptr<WinMacro> m_pView = nullptr;
|
||||
std::queue<Task> m_tasks;
|
||||
std::mutex m_tasks_mutex;
|
||||
|
||||
std::thread m_control_thread;
|
||||
std::thread m_identify_thread;
|
||||
|
||||
4
MeoAssistance/Identify.cpp
Normal file
4
MeoAssistance/Identify.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
#include "Identify.h"
|
||||
|
||||
using namespace MeoAssistance;
|
||||
|
||||
16
MeoAssistance/Identify.h
Normal file
16
MeoAssistance/Identify.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace MeoAssistance {
|
||||
|
||||
class WinMacro;
|
||||
|
||||
class Identify
|
||||
{
|
||||
public:
|
||||
Identify();
|
||||
~Identify();
|
||||
private:
|
||||
};
|
||||
}
|
||||
@@ -142,10 +142,12 @@
|
||||
<ItemGroup>
|
||||
<ClInclude Include="AssDef.h" />
|
||||
<ClInclude Include="Assistance.h" />
|
||||
<ClInclude Include="Identify.h" />
|
||||
<ClInclude Include="WinMacro.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Assistance.cpp" />
|
||||
<ClCompile Include="Identify.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="WinMacro.cpp" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -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<int>(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<int>(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<int>(m_handle_type) & static_cast<int>(HandleType::Window))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ::MoveWindow(m_handle, 0, 0, width, height, true);
|
||||
}
|
||||
|
||||
bool WinMacro::click(Point p)
|
||||
{
|
||||
if (!(static_cast<int>(m_handle_type) & static_cast<int>(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<int>(m_handle_type) & static_cast<int>(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 });
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user