mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-18 10:10:45 +08:00
完善连手机的功能,增加USB调试接口
This commit is contained in:
@@ -13,7 +13,10 @@ extern "C" {
|
||||
MEOAPI_PORT asst::Assistance* MEO_CALL AsstCreate();
|
||||
MEOAPI_PORT asst::Assistance* MEO_CALL AsstCreateEx(AsstCallback callback, void* custom_arg);
|
||||
void MEOAPI AsstDestory(asst::Assistance* p_asst);
|
||||
|
||||
bool MEOAPI AsstCatchDefault(asst::Assistance* p_asst);
|
||||
bool MEOAPI AsstCatchEmulator(asst::Assistance* p_asst);
|
||||
bool MEOAPI AsstCatchUSB(asst::Assistance* p_asst);
|
||||
bool MEOAPI AsstCatchRemote(asst::Assistance* p_asst, const char* address);
|
||||
|
||||
bool MEOAPI AsstStartSanity(asst::Assistance* p_asst);
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
{
|
||||
"version": "0.5",
|
||||
"version": "0.6",
|
||||
"options": {
|
||||
"connectType" : 0,
|
||||
"connectType_Doc": "连接类型:0-连接电脑上的模拟器,1-连接USB连接的安卓设备,2-连接局域网中的安卓设备。默认0",
|
||||
"connectRemoteAddress": "",
|
||||
"connectRemoteAddress_Doc": "连接局域网中的安卓设备的地址,仅在connectType为2时生效。格式举例:192.168.1.123:5678",
|
||||
"taskDelay": 1000,
|
||||
"taskDelay_Doc": "识别的延迟:越快识别频率越快,但会增加CPU消耗。单位毫秒,默认1000",
|
||||
"printWindow": true,
|
||||
"printWindow_Doc": "截图功能:开启后每次结算界面会截图到screenshot目录下。true-开启,false-关闭,默认true",
|
||||
"printWindowDelay": 3000,
|
||||
"printWindowDelay_Doc": "截图延时:每次到结算界面,掉落物品不是一次性出来的,有个动画,所以需要等一会再截图。单位毫秒,默认3000",
|
||||
"printWindowDelay_Doc": "截图延时:每次到结算界面,掉落物品不是一次性出来的,有个动画,所以需要等一会再截图。单位毫秒,默认3000,仅在printWindow为true时生效",
|
||||
"controlDelayRange": [
|
||||
0,
|
||||
0
|
||||
@@ -66,6 +70,23 @@
|
||||
"displayRegex": "cur=%dx%d",
|
||||
"screencap": "[Adb] -s [Address] exec-out screencap -p"
|
||||
}
|
||||
},
|
||||
"USB": {
|
||||
"handle": {
|
||||
"class": "USB Class",
|
||||
"window": "USB Window",
|
||||
"Doc": "这俩不是用来捕获窗口的,是为了兼容现有的捕获模拟器的字段,随手写的,这两个字段实际不会使用到"
|
||||
},
|
||||
"adb": {
|
||||
"Doc": "USB连接暂时不兼容多设备的情况",
|
||||
"path": "[ExecDir]platform-tools\\adb.exe",
|
||||
"connect": "[Adb] devices",
|
||||
"click": "[Adb] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"displayRegex": "cur=%dx%d",
|
||||
"screencap": "[Adb] exec-out screencap -p"
|
||||
}
|
||||
}
|
||||
},
|
||||
"ocrReplace_Doc": "下面为常见的文字识别错误纠正,不需要修改",
|
||||
|
||||
@@ -133,6 +133,23 @@ Assistance::~Assistance()
|
||||
}
|
||||
}
|
||||
|
||||
bool asst::Assistance::catch_default()
|
||||
{
|
||||
DebugTraceFunction;
|
||||
|
||||
switch (Configer::get_instance().m_options.connect_type)
|
||||
{
|
||||
case ConnectType::Emulator:
|
||||
return catch_emulator();
|
||||
case ConnectType::USB:
|
||||
return catch_usb();
|
||||
case ConnectType::Remote:
|
||||
return catch_remote(Configer::get_instance().m_options.connect_remote_address);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Assistance::catch_emulator(const std::string& emulator_name)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
@@ -162,6 +179,24 @@ bool Assistance::catch_emulator(const std::string& emulator_name)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool asst::Assistance::catch_usb()
|
||||
{
|
||||
DebugTraceFunction;
|
||||
|
||||
stop();
|
||||
|
||||
bool ret = false;
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
|
||||
EmulatorInfo remote_info = Configer::get_instance().m_handles["USB"];
|
||||
|
||||
ret = m_controller_ptr->try_capture(remote_info, true);
|
||||
|
||||
m_inited = ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool asst::Assistance::catch_remote(const std::string& address)
|
||||
{
|
||||
DebugTraceFunction;
|
||||
|
||||
@@ -26,8 +26,12 @@ namespace asst {
|
||||
Assistance(AsstCallback callback = nullptr, void* callback_arg = nullptr);
|
||||
~Assistance();
|
||||
|
||||
// 根据配置文件,决定捕获模拟器、USB 还是远程设备
|
||||
bool catch_default();
|
||||
// 捕获模拟器
|
||||
bool catch_emulator(const std::string& emulator_name = std::string());
|
||||
// 捕获usb设备
|
||||
bool catch_usb();
|
||||
// 捕获远程地址(安卓手机)
|
||||
bool catch_remote(const std::string& address);
|
||||
|
||||
|
||||
@@ -60,6 +60,21 @@ void AsstDestory(asst::Assistance* p_asst)
|
||||
p_asst = NULL;
|
||||
}
|
||||
|
||||
bool AsstCatchDefault(asst::Assistance* p_asst)
|
||||
{
|
||||
if (p_asst == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto&& ret = p_asst->catch_default();
|
||||
if (ret) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool AsstCatchEmulator(asst::Assistance* p_asst)
|
||||
{
|
||||
if (p_asst == NULL) {
|
||||
@@ -75,6 +90,21 @@ bool AsstCatchEmulator(asst::Assistance* p_asst)
|
||||
}
|
||||
}
|
||||
|
||||
bool AsstCatchUSB(asst::Assistance* p_asst)
|
||||
{
|
||||
if (p_asst == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto&& ret = p_asst->catch_usb();
|
||||
if (ret) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool AsstCatchRemote(asst::Assistance* p_asst, const char* address)
|
||||
{
|
||||
if (p_asst == NULL) {
|
||||
|
||||
@@ -156,7 +156,15 @@ namespace asst {
|
||||
bool cache = false; // 是否使用缓存(直方图),false时就一直用模板匹配。默认为true
|
||||
};
|
||||
|
||||
enum class ConnectType {
|
||||
Emulator,
|
||||
USB,
|
||||
Remote
|
||||
};
|
||||
|
||||
struct Options {
|
||||
ConnectType connect_type; // 连接类型
|
||||
std::string connect_remote_address; // 连接局域网中的安卓设备的地址,仅在connectType为Remote时生效
|
||||
int task_delay = 0; // 任务间延时:越快操作越快,但会增加CPU消耗
|
||||
int control_delay_lower = 0; // 点击随机延时下限:每次点击操作会进行随机延时
|
||||
int control_delay_upper = 0; // 点击随机延时上限:每次点击操作会进行随机延时
|
||||
|
||||
@@ -16,6 +16,8 @@ bool asst::Configer::parse(const json::value& json)
|
||||
|
||||
const json::value& options_json = json.at("options");
|
||||
{
|
||||
m_options.connect_type = static_cast<ConnectType>(options_json.at("connectType").as_integer());
|
||||
m_options.connect_remote_address = options_json.at("connectRemoteAddress").as_string();
|
||||
m_options.task_delay = options_json.at("taskDelay").as_integer();
|
||||
//m_options.identify_cache = options_json.at("identifyCache").as_boolean();
|
||||
m_options.control_delay_lower = options_json.at("controlDelayRange")[0].as_integer();
|
||||
|
||||
@@ -30,13 +30,18 @@ bool OpenRecruitTask::run()
|
||||
m_callback(AsstMsg::TaskStart, task_start_json, m_callback_arg);
|
||||
|
||||
const cv::Mat& image = m_controller_ptr->get_image();
|
||||
if (image.empty()) {
|
||||
m_callback(AsstMsg::ImageIsEmpty, task_start_json, m_callback_arg);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 设置招募时间9小时 */
|
||||
auto set_time_foo = [&](bool flag) -> void {
|
||||
if (!flag) {
|
||||
return;
|
||||
}
|
||||
const static cv::Rect identify_area(340, 140, 510, 160);
|
||||
const static cv::Rect identify_area = make_rect<cv::Rect>(
|
||||
m_controller_ptr->shaped_correct(Rect(340, 140, 510, 160)));
|
||||
auto&& [algorithm, score, second_confirm_rect] =
|
||||
m_identify_ptr->find_image(image(identify_area), "RecruitTime");
|
||||
|
||||
@@ -49,7 +54,8 @@ bool OpenRecruitTask::run()
|
||||
};
|
||||
std::future<void> set_time_future = std::async(std::launch::async, set_time_foo, m_set_time);
|
||||
|
||||
const static cv::Rect identify_area(360, 340, 520, 160);
|
||||
const static cv::Rect identify_area = make_rect<cv::Rect>(
|
||||
m_controller_ptr->shaped_correct(Rect(360, 320, 520, 200)));
|
||||
/* Find all text */
|
||||
std::vector<TextArea> all_text_area = ocr_detect(image(identify_area));
|
||||
|
||||
|
||||
@@ -156,12 +156,12 @@ std::shared_ptr<TaskInfo> ProcessTask::match_image(Rect* matched_rect)
|
||||
double hist_threshold = process_task_info_ptr->hist_threshold;
|
||||
double add_cache_thres = process_task_info_ptr->cache ? templ_threshold : Identify::NotAddCache;
|
||||
cv::Mat identify_image;
|
||||
const auto& identify_area = task_info_ptr->identify_area;
|
||||
const auto& identify_area = m_controller_ptr->shaped_correct(task_info_ptr->identify_area);
|
||||
if (identify_area.width == 0) {
|
||||
identify_image = cur_image;
|
||||
}
|
||||
else {
|
||||
identify_image = cur_image(make_rect<cv::Rect>(task_info_ptr->identify_area));
|
||||
identify_image = cur_image(make_rect<cv::Rect>(identify_area));
|
||||
}
|
||||
auto&& [algorithm, score, temp_rect] = m_identify_ptr->find_image(identify_image, task_name, add_cache_thres);
|
||||
rect = std::move(temp_rect);
|
||||
@@ -193,12 +193,12 @@ std::shared_ptr<TaskInfo> ProcessTask::match_image(Rect* matched_rect)
|
||||
std::shared_ptr<OcrTaskInfo> ocr_task_info_ptr =
|
||||
std::dynamic_pointer_cast<OcrTaskInfo>(task_info_ptr);
|
||||
cv::Mat identify_image;
|
||||
const auto& identify_area = task_info_ptr->identify_area;
|
||||
const auto& identify_area = m_controller_ptr->shaped_correct(task_info_ptr->identify_area);
|
||||
if (identify_area.width == 0) {
|
||||
identify_image = cur_image;
|
||||
}
|
||||
else {
|
||||
identify_image = cur_image(make_rect<cv::Rect>(task_info_ptr->identify_area));
|
||||
identify_image = cur_image(make_rect<cv::Rect>(identify_area));
|
||||
}
|
||||
std::vector<TextArea> all_text_area = ocr_detect(identify_image);
|
||||
std::vector<TextArea> match_result;
|
||||
|
||||
@@ -176,4 +176,5 @@ bool asst::TaskConfiger::parse(const json::value& json)
|
||||
|
||||
m_all_tasks_info.emplace(name, task_info_ptr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -67,6 +67,24 @@ asst::WinMacro::~WinMacro()
|
||||
::CloseHandle(m_pipe_child_write);
|
||||
}
|
||||
|
||||
Rect asst::WinMacro::shaped_correct(const Rect& rect) const
|
||||
{
|
||||
if (rect.width == 0 || rect.height == 0) {
|
||||
return rect;
|
||||
}
|
||||
// 明日方舟在异形屏上,有的地方是按比例缩放的,有的地方又是直接位移。没法整,这里简单粗暴一点截一个长条
|
||||
Rect dst = rect;
|
||||
if (m_scale_size.first != Configer::WindowWidthDefault) { // 说明是宽屏
|
||||
dst.x = 0;
|
||||
dst.width = m_scale_size.first - 1;
|
||||
}
|
||||
else if (m_scale_size.second != Configer::WindowHeightDefault) { // 说明是偏方形屏
|
||||
dst.y = 0;
|
||||
dst.height = m_scale_size.second - 1;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
void asst::WinMacro::pipe_working_proc()
|
||||
{
|
||||
DebugTraceFunction;
|
||||
@@ -307,7 +325,7 @@ int asst::WinMacro::push_cmd(const std::string& cmd)
|
||||
return ++m_push_id;
|
||||
}
|
||||
|
||||
void asst::WinMacro::wait(unsigned id)
|
||||
void asst::WinMacro::wait(unsigned id) const noexcept
|
||||
{
|
||||
while (id > m_completed_id) {
|
||||
std::this_thread::yield();
|
||||
|
||||
@@ -38,11 +38,14 @@ namespace asst {
|
||||
int swipe(const Rect& r1, const Rect& r2, int duration = 0, bool block = true);
|
||||
int swipe_without_scale(const Point& p1, const Point& p2, int duration = 0, bool block = true);
|
||||
int swipe_without_scale(const Rect& r1, const Rect& r2, int duration = 0, bool block = true);
|
||||
|
||||
// 异形屏矫正
|
||||
Rect shaped_correct(const Rect& rect) const;
|
||||
private:
|
||||
void pipe_working_proc();
|
||||
std::vector<unsigned char> call_command(const std::string& cmd);
|
||||
int push_cmd(const std::string& cmd);
|
||||
void wait(unsigned id);
|
||||
void wait(unsigned id) const noexcept;
|
||||
void screencap();
|
||||
|
||||
Point rand_point_in_rect(const Rect& rect);
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace MeoAsstGui
|
||||
|
||||
[DllImport("MeoAssistance.dll")] static private extern void AsstDestory(IntPtr ptr);
|
||||
|
||||
[DllImport("MeoAssistance.dll")] static private extern bool AsstCatchEmulator(IntPtr ptr);
|
||||
[DllImport("MeoAssistance.dll")] static private extern bool AsstCatchDefault(IntPtr ptr);
|
||||
|
||||
[DllImport("MeoAssistance.dll")] static private extern bool AsstStartProcessTask(IntPtr ptr, string task);
|
||||
|
||||
@@ -237,9 +237,9 @@ namespace MeoAsstGui
|
||||
AsstStop(_ptr);
|
||||
}
|
||||
|
||||
public bool AsstCatchEmulator()
|
||||
public bool AsstCatchDefault()
|
||||
{
|
||||
return AsstCatchEmulator(_ptr);
|
||||
return AsstCatchDefault(_ptr);
|
||||
}
|
||||
|
||||
public bool AsstStartSanity()
|
||||
|
||||
@@ -186,7 +186,7 @@ namespace MeoAsstGui
|
||||
public void StartSanity()
|
||||
{
|
||||
var asstProxy = _container.Get<AsstProxy>();
|
||||
bool catched = asstProxy.AsstCatchEmulator();
|
||||
bool catched = asstProxy.AsstCatchDefault();
|
||||
CatchStatus = "捕获模拟器窗口:" + catched;
|
||||
if (!asstProxy.AsstStartSanity())
|
||||
{
|
||||
@@ -202,7 +202,7 @@ namespace MeoAsstGui
|
||||
public void Visit()
|
||||
{
|
||||
var asstProxy = _container.Get<AsstProxy>();
|
||||
bool catched = asstProxy.AsstCatchEmulator();
|
||||
bool catched = asstProxy.AsstCatchDefault();
|
||||
CatchStatus = "捕获模拟器窗口:" + catched;
|
||||
if (!asstProxy.AsstStartVisit())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user