From eba5c715c2eaea508c6c0d21572bc5be4529be10 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 19 Feb 2022 01:17:08 +0800 Subject: [PATCH 1/7] =?UTF-8?q?refactor.=E5=AE=8C=E6=88=90=20Controller=20?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=9A=84=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/回调消息协议.md | 11 +- include/AsstCaller.h | 5 +- resource/config.json | 229 +++--------- src/MeoAssistant/AbstractImageAnalyzer.cpp | 5 - src/MeoAssistant/AbstractImageAnalyzer.h | 1 - src/MeoAssistant/AbstractTask.cpp | 8 +- src/MeoAssistant/AbstractTask.h | 4 + src/MeoAssistant/Assistant.cpp | 88 +---- src/MeoAssistant/Assistant.h | 13 +- src/MeoAssistant/AsstCaller.cpp | 36 +- src/MeoAssistant/AsstDef.h | 49 --- src/MeoAssistant/AutoRecruitTask.cpp | 4 +- src/MeoAssistant/Controller.cpp | 348 ++++++------------ src/MeoAssistant/Controller.h | 77 ++-- src/MeoAssistant/CreditShoppingTask.cpp | 12 +- src/MeoAssistant/GeneralConfiger.cpp | 41 +-- src/MeoAssistant/GeneralConfiger.h | 38 +- src/MeoAssistant/InfrastAbstractTask.cpp | 16 +- src/MeoAssistant/InfrastDormTask.cpp | 4 +- src/MeoAssistant/InfrastInfoTask.cpp | 2 +- src/MeoAssistant/InfrastOfficeTask.cpp | 2 +- src/MeoAssistant/InfrastPowerTask.cpp | 2 +- src/MeoAssistant/InfrastProductionTask.cpp | 14 +- src/MeoAssistant/InfrastReceptionTask.cpp | 16 +- src/MeoAssistant/MatchImageAnalyzer.cpp | 1 - src/MeoAssistant/MeoAssistant.vcxproj | 2 - src/MeoAssistant/MeoAssistant.vcxproj.filters | 6 - src/MeoAssistant/OcrImageAnalyzer.cpp | 1 - src/MeoAssistant/ProcessTask.cpp | 14 +- src/MeoAssistant/RecruitTask.cpp | 6 +- src/MeoAssistant/Resource.cpp | 9 - src/MeoAssistant/Resource.h | 10 - .../RoguelikeBattleTaskPlugin.cpp | 18 +- src/MeoAssistant/RoguelikeFormationTask.cpp | 6 +- src/MeoAssistant/StageDropsTaskPlugin.cpp | 2 +- src/MeoAssistant/UserConfiger.cpp | 48 --- src/MeoAssistant/UserConfiger.h | 41 --- tools/TestCaller/main.cpp | 2 +- 38 files changed, 371 insertions(+), 820 deletions(-) delete mode 100644 src/MeoAssistant/UserConfiger.cpp delete mode 100644 src/MeoAssistant/UserConfiger.h diff --git a/docs/回调消息协议.md b/docs/回调消息协议.md index 4e2f02d3eb..fe7e5282a1 100644 --- a/docs/回调消息协议.md +++ b/docs/回调消息协议.md @@ -48,13 +48,20 @@ Todo ```jsonc { "what": string, // 错误类型 - "details": object, // 错误详情 + "why": string, // 错误原因 + "details": object // 错误详情 } ``` ### ConnectionError -Todo +```jsonc +{ + "what": string, // 错误类型 + "why": string, // 错误原因 + "details": object, // 错误详情 +} +``` ### AllTasksCompleted diff --git a/include/AsstCaller.h b/include/AsstCaller.h index afa0b5eafa..450e808ddb 100644 --- a/include/AsstCaller.h +++ b/include/AsstCaller.h @@ -16,10 +16,7 @@ extern "C" { ASSTAPI_PORT asst::Assistant* ASST_CALL AsstCreateEx(const char* dirname, AsstCallback callback, void* custom_arg); void ASSTAPI AsstDestroy(asst::Assistant* p_asst); - bool ASSTAPI AsstCatchDefault(asst::Assistant* p_asst); - bool ASSTAPI AsstCatchEmulator(asst::Assistant* p_asst); - bool ASSTAPI AsstCatchCustom(asst::Assistant* p_asst, const char* address); - bool ASSTAPI AsstCatchFake(asst::Assistant* p_asst); + bool ASSTAPI AsstConnect(asst::Assistant* p_asst, const char* adb_path, const char* address, const char* config); bool ASSTAPI AsstAppendStartUp(asst::Assistant* p_asst); bool ASSTAPI AsstAppendFight(asst::Assistant* p_asst, const char* stage, int max_mecidine, int max_stone, int max_times); diff --git a/resource/config.json b/resource/config.json index 679c2e6412..c9d778e4ac 100644 --- a/resource/config.json +++ b/resource/config.json @@ -24,183 +24,76 @@ "emulator_Doc": "下面的和模拟器窗口捕获逻辑有关,不需要修改", "emulator": { "BlueStacks": { - "handle": { - "class": "BS2CHINAUI", - "window": "BlueStacks App Player" - }, - "adb": { - "path": "[EmulatorPath]Engine\\ProgramFiles\\HD-Adb.exe", - "addresses": [ - "127.0.0.1:5555" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", - "release": "[Adb] kill-server" - } - }, - "BlueStacksInternational": { - "handle": { - "class": "", - "window": "BlueStacks" - }, - "adb": { - "path": "[EmulatorPath]HD-Adb.exe", - "addresses": [ - "127.0.0.1:5555" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", - "release": "[Adb] kill-server" - } + "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", + "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", + "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", + "release": "[Adb] kill-server" }, "MuMuEmulator": { - "handle": { - "class": "Qt5QWindowIcon", - "window": "明日方舟 - MuMu模拟器" - }, - "adb": { - "path": "[EmulatorPath]..\\vmonitor\\bin\\adb_server.exe", - "addresses": [ - "127.0.0.1:7555" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", - "release": "[Adb] kill-server" - } + "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", + "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", + "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", + "release": "[Adb] kill-server" }, "LDPlayer": { - "handle": { - "class": "LDPlayerMainFrame", - "window": "" - }, - "adb": { - "path": "[EmulatorPath]adb.exe", - "addresses": [ - "127.0.0.1:5555", - "emulator-5554" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] shell screencap -p", - "release": "[Adb] kill-server" - } + "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", + "screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap | gzip -1\"", + "screencapEncode": "[Adb] -s [Address] shell screencap -p", + "release": "[Adb] kill-server" }, "Nox": { - "handle": { - "class": "Qt5QWindowIcon", - "window": "夜神模拟器" - }, - "adb": { - "path": "[EmulatorPath]nox_adb.exe", - "addresses": [ - "127.0.0.1:62001", - "127.0.0.1:59865" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", - "release": "[Adb] kill-server" - } - }, - "NoxInternational": { - "handle": { - "class": "Qt5QWindowIcon", - "window": "NoxPlayer" - }, - "adb": { - "path": "[EmulatorPath]nox_adb.exe", - "addresses": [ - "127.0.0.1:62001", - "127.0.0.1:59865" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", - "release": "[Adb] kill-server" - } + "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", + "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", + "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", + "release": "[Adb] kill-server" }, "XYAZ": { - "handle": { - "class": "Qt5QWindowIcon", - "window": "逍遥模拟器" - }, - "adb": { - "path": "[EmulatorPath]adb.exe", - "addresses": [ - "127.0.0.1:21503" - ], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] shell screencap -p", - "release": "[Adb] kill-server" - } + "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", + "screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap | gzip -1\"", + "screencapEncode": "[Adb] -s [Address] shell screencap -p", + "release": "[Adb] kill-server" }, - "Custom": { - "handle": { - "class": "Custom Class", - "window": "Custom Window", - "Doc": "这俩不是用来捕获窗口的,是为了兼容现有的捕获模拟器的字段,随手写的,这两个字段实际不会使用到" - }, - "adb": { - "Doc": "1.请将Adb解压到'path'中的路径(或者修改'path');2.请修改addresses为指定地址,若不填写,则使用devices自动查找", - "path": "[ExecDir]platform-tools\\adb.exe", - "addresses": [], - "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", - "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", - "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", - "release": "[Adb] kill-server" - } + "General": { + "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", + "screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"", + "screencapEncode": "[Adb] -s [Address] exec-out screencap -p", + "release": "[Adb] kill-server" } } } diff --git a/src/MeoAssistant/AbstractImageAnalyzer.cpp b/src/MeoAssistant/AbstractImageAnalyzer.cpp index 8ce18a24f8..a43a86762d 100644 --- a/src/MeoAssistant/AbstractImageAnalyzer.cpp +++ b/src/MeoAssistant/AbstractImageAnalyzer.cpp @@ -41,11 +41,6 @@ void asst::AbstractImageAnalyzer::set_roi(const Rect& roi) noexcept m_roi = empty_rect_to_full(roi, m_image); } -void asst::AbstractImageAnalyzer::correct_roi() noexcept -{ - m_roi = Ctrler.shaped_correct(m_roi); -} - asst::Rect asst::AbstractImageAnalyzer::empty_rect_to_full(const Rect& rect, const cv::Mat image) noexcept { return rect.empty() ? Rect(0, 0, image.cols, image.rows) : rect; diff --git a/src/MeoAssistant/AbstractImageAnalyzer.h b/src/MeoAssistant/AbstractImageAnalyzer.h index f86fb4b86f..db187e6577 100644 --- a/src/MeoAssistant/AbstractImageAnalyzer.h +++ b/src/MeoAssistant/AbstractImageAnalyzer.h @@ -20,7 +20,6 @@ namespace asst virtual void set_roi(const Rect& roi) noexcept; virtual bool analyze() = 0; - void correct_roi() noexcept; AbstractImageAnalyzer& operator=(const AbstractImageAnalyzer&) = delete; AbstractImageAnalyzer& operator=(AbstractImageAnalyzer&&) = delete; diff --git a/src/MeoAssistant/AbstractTask.cpp b/src/MeoAssistant/AbstractTask.cpp index 75155edbfa..0423bd6f9b 100644 --- a/src/MeoAssistant/AbstractTask.cpp +++ b/src/MeoAssistant/AbstractTask.cpp @@ -58,6 +58,12 @@ AbstractTask& asst::AbstractTask::set_retry_times(int times) noexcept return *this; } +AbstractTask& asst::AbstractTask::set_ctrler(std::shared_ptr ctrler) noexcept +{ + m_ctrler = ctrler; + return *this; +} + void asst::AbstractTask::clear_plugin() noexcept { m_plugins.clear(); @@ -164,6 +170,6 @@ void asst::AbstractTask::click_return_button() Rect ReturnButtonRect = return_task_ptr->specific_rect; - Ctrler.click(ReturnButtonRect); + m_ctrler->click(ReturnButtonRect); sleep(return_task_ptr->rear_delay); } diff --git a/src/MeoAssistant/AbstractTask.h b/src/MeoAssistant/AbstractTask.h index 326044c713..96da644455 100644 --- a/src/MeoAssistant/AbstractTask.h +++ b/src/MeoAssistant/AbstractTask.h @@ -17,6 +17,8 @@ namespace asst class AbstractTaskPlugin; using TaskPluginPtr = std::shared_ptr; + class Controller; + class AbstractTask { public: @@ -29,6 +31,7 @@ namespace asst AbstractTask& set_exit_flag(bool* exit_flag) noexcept; AbstractTask& set_retry_times(int times) noexcept; + AbstractTask& set_ctrler(std::shared_ptr ctrler) noexcept; template std::shared_ptr regiseter_plugin() @@ -66,5 +69,6 @@ namespace asst mutable json::value m_basic_info_cache; std::set m_plugins; + std::shared_ptr m_ctrler = nullptr; }; } diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index ccb55786fe..f45f52ca5d 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -37,7 +37,6 @@ Assistant::Assistant(std::string dirname, AsstCallback callback, void* callback_ m_callback_arg(callback_arg) { Logger::set_dirname(m_dirname); - Controller::set_dirname(m_dirname); LogTraceFunction; @@ -57,6 +56,8 @@ Assistant::Assistant(std::string dirname, AsstCallback callback, void* callback_ throw error; } + m_ctrler = std::make_shared(task_callback, (void*)this); + m_working_thread = std::thread(std::bind(&Assistant::working_proc, this)); m_msg_thread = std::thread(std::bind(&Assistant::msg_proc, this)); } @@ -78,85 +79,17 @@ Assistant::~Assistant() } } -bool asst::Assistant::catch_default() +bool asst::Assistant::connect(const std::string& adb_path, const std::string& address, const std::string& config) { LogTraceFunction; - auto& opt = Resrc.cfg().get_options(); - switch (opt.connect_type) { - case ConnectType::Emulator: - return catch_emulator(); - case ConnectType::Custom: - return catch_custom(); - default: - return false; - } -} - -bool Assistant::catch_emulator(const std::string& emulator_name) -{ - LogTraceFunction; - - stop(); -#ifdef _WIN32 - bool ret = false; - //std::string cor_name = emulator_name; - auto& cfg = Resrc.cfg(); - std::unique_lock lock(m_mutex); - // 自动匹配模拟器,逐个找 - if (emulator_name.empty()) { - for (const auto& [name, info] : cfg.get_emulators_info()) { - ret = Ctrler.try_capture(info); - if (ret) { - //cor_name = name; - break; - } - } - } - else { // 指定的模拟器 - auto& info = cfg.get_emulators_info().at(emulator_name); - ret = Ctrler.try_capture(info); - } + stop(false); + bool ret = m_ctrler->connect(adb_path, address, config.empty() ? "General" : config); m_inited = ret; return ret; -#else // Not supported catch emulator in Linux - return false; -#endif -} - -bool asst::Assistant::catch_custom(const std::string& address) -{ - LogTraceFunction; - - stop(); - - bool ret = false; - auto& cfg = Resrc.cfg(); - - std::unique_lock lock(m_mutex); - - EmulatorInfo remote_info = cfg.get_emulators_info().at("Custom"); - if (!address.empty()) { - remote_info.adb.addresses.push_back(address); - } - - ret = Ctrler.try_capture(remote_info, true); - - m_inited = ret; - return ret; -} - -bool asst::Assistant::catch_fake() -{ - LogTraceFunction; - - stop(); - - m_inited = true; - return true; } bool asst::Assistant::append_start_up() @@ -497,12 +430,18 @@ void asst::Assistant::set_penguin_id(const std::string& id) std::vector asst::Assistant::get_image() const { - return Ctrler.get_image_encode(); + if (!m_inited) { + return std::vector(); + } + return m_ctrler->get_image_encode(); } bool asst::Assistant::ctrler_click(int x, int y, bool block) { - Ctrler.click(Point(x, y), block); + if (!m_inited) { + return false; + } + m_ctrler->click(Point(x, y), block); return true; } @@ -568,6 +507,7 @@ void Assistant::working_proc() } task_ptr->set_exit_flag(&m_thread_idle); + task_ptr->set_ctrler(m_ctrler); bool ret = task_ptr->run(); if (cur_taskchain != next_taskchain) { diff --git a/src/MeoAssistant/Assistant.h b/src/MeoAssistant/Assistant.h index 13d1054f92..59cb297411 100644 --- a/src/MeoAssistant/Assistant.h +++ b/src/MeoAssistant/Assistant.h @@ -24,6 +24,7 @@ namespace asst { class Controller; class Identify; + class Controller; class Assistant { @@ -31,14 +32,8 @@ namespace asst Assistant(std::string dirname, AsstCallback callback = nullptr, void* callback_arg = nullptr); ~Assistant(); - // 根据配置文件,决定捕获模拟器、USB 还是远程设备 - bool catch_default(); - // 捕获模拟器 - bool catch_emulator(const std::string& emulator_name = std::string()); - // 捕获自定义设备 - bool catch_custom(const std::string& address = std::string()); - // 不实际进行捕获,调试用接口 - bool catch_fake(); + // 连接adb + bool connect(const std::string& adb_path, const std::string& address, const std::string& config); // 添加开始游戏的任务(进入到主界面) bool append_start_up(); @@ -106,6 +101,8 @@ namespace asst bool m_inited = false; std::string m_dirname; + std::shared_ptr m_ctrler; + bool m_thread_exit = false; std::queue> m_tasks_queue; AsstCallback m_callback = nullptr; diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index b575d2518e..8dc7c779cf 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -89,45 +89,13 @@ void AsstDestroy(asst::Assistant* p_asst) _callback = nullptr; } -bool AsstCatchDefault(asst::Assistant* p_asst) +bool AsstConnect(asst::Assistant* p_asst, const char* adb_path, const char* address, const char* config) { if (p_asst == nullptr) { return false; } - return p_asst->catch_default(); -} - -bool AsstCatchEmulator(asst::Assistant* p_asst) -{ - if (p_asst == nullptr) { - return false; - } - - return p_asst->catch_emulator(); -} - -bool AsstCatchCustom(asst::Assistant* p_asst, const char* address) -{ - if (p_asst == nullptr) { - return false; - } - - return p_asst->catch_custom(address); -} - -bool AsstCatchFake(asst::Assistant* p_asst) -{ -#ifdef ASST_DEBUG - if (p_asst == nullptr) { - return false; - } - - return p_asst->catch_fake(); -#else - (void*)p_asst; - return false; -#endif // ASST_DEBUG + return p_asst->connect(adb_path, address, config ? config : std::string()); } bool ASSTAPI AsstAppendStartUp(asst::Assistant* p_asst) diff --git a/src/MeoAssistant/AsstDef.h b/src/MeoAssistant/AsstDef.h index de26dced7e..042b08f7e4 100644 --- a/src/MeoAssistant/AsstDef.h +++ b/src/MeoAssistant/AsstDef.h @@ -256,53 +256,4 @@ namespace asst std::pair mask_range; // 掩码的二值化范围 bool bound = false; // 是否裁剪周围黑边 }; - - struct HandleInfo - { - std::string class_name; - std::string window_name; - }; - - struct AdbCmd - { - std::string path; - std::vector addresses; // 会优先尝试连接addresses中的地址,若均失败,则会使用devices获取地址 - std::string devices; - std::string address_regex; - std::string connect; - std::string click; - std::string swipe; - std::string display; - std::string display_format; - std::string screencap_raw_with_gzip; - std::string screencap_encode; - std::string release; - //std::string pullscreen; - int display_width = 0; - int display_height = 0; - - enum class ScreencapEndOfLine - { - UnknownYet, - CRLF, - LF, - CR - } screencap_end_of_line = ScreencapEndOfLine::UnknownYet; - - enum class ScreencapMethod - { - UnknownYet, - Default, - RawWithGzip, - Encode - } screencap_method = ScreencapMethod::UnknownYet; - }; - - struct EmulatorInfo - { - std::string name; - HandleInfo handle; - AdbCmd adb; - std::string path; - }; } diff --git a/src/MeoAssistant/AutoRecruitTask.cpp b/src/MeoAssistant/AutoRecruitTask.cpp index 38f766ae86..46a889e376 100644 --- a/src/MeoAssistant/AutoRecruitTask.cpp +++ b/src/MeoAssistant/AutoRecruitTask.cpp @@ -82,7 +82,7 @@ bool asst::AutoRecruitTask::analyze_start_buttons() OcrImageAnalyzer start_analyzer; start_analyzer.set_task_info("StartRecruit"); - auto image = Ctrler.get_image(); + auto image = m_ctrler->get_image(); start_analyzer.set_image(image); if (!start_analyzer.analyze()) { return false; @@ -100,7 +100,7 @@ bool asst::AutoRecruitTask::recruit_index(size_t index) return false; } Rect button = m_start_buttons.at(index).rect; - Ctrler.click(button); + m_ctrler->click(button); sleep(delay); return calc_and_recruit(); diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 5e5f80a747..673ff61a9f 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -21,10 +21,11 @@ #include "AsstDef.h" #include "Logger.hpp" #include "Resource.h" -#include "UserConfiger.h" -asst::Controller::Controller() - : m_rand_engine(static_cast(time(nullptr))) +asst::Controller::Controller(AsstCallback callback, void* callback_arg) + : m_callback(callback), + m_callback_arg(callback_arg), + m_rand_engine(static_cast(time(nullptr))) { LogTraceFunction; @@ -87,7 +88,7 @@ asst::Controller::~Controller() #else if (true) { #endif - call_command(m_emulator_info.adb.release); + call_command(m_adb.release); } #ifdef _WIN32 @@ -103,67 +104,6 @@ asst::Controller::~Controller() #endif } -bool asst::Controller::connect_adb(const std::string & address) -{ - LogTraceScope("connect_adb " + address); - - std::string connect_cmd = utils::string_replace_all( - utils::string_replace_all(m_emulator_info.adb.connect, "[Adb]", m_emulator_info.adb.path), - "[Address]", address); - auto connect_ret = call_command(connect_cmd); - // 端口即使错误,命令仍然会返回0,TODO 对connect_result进行判断 - if (!connect_ret) { - return false; - } - - std::string display_cmd = utils::string_replace_all( - utils::string_replace_all(m_emulator_info.adb.display, "[Adb]", m_emulator_info.adb.path), - "[Address]", address); - auto display_ret = call_command(display_cmd); - if (!display_ret) { - return false; - } - auto& display_result = display_ret.value(); - std::string display_pipe_str( - std::make_move_iterator(display_result.begin()), - std::make_move_iterator(display_result.end())); - int size_value1 = 0; - int size_value2 = 0; -#ifdef _MSC_VER - sscanf_s(display_pipe_str.c_str(), m_emulator_info.adb.display_format.c_str(), &size_value1, &size_value2); -#else - sscanf(display_pipe_str.c_str(), m_emulator_info.adb.display_format.c_str(), &size_value1, &size_value2); -#endif - // 为了防止抓取句柄的时候手机是竖屏的(还没进游戏),这里取大的值为宽,小的为高 - // 总不能有人竖屏玩明日方舟吧(? - m_emulator_info.adb.display_width = (std::max)(size_value1, size_value2); - m_emulator_info.adb.display_height = (std::min)(size_value1, size_value2); - - constexpr double DefaultRatio = - static_cast(WindowWidthDefault) / static_cast(WindowHeightDefault); - double cur_ratio = static_cast(m_emulator_info.adb.display_width) / static_cast(m_emulator_info.adb.display_height); - - if (cur_ratio >= DefaultRatio // 说明是宽屏或默认16:9,按照高度计算缩放 - || std::fabs(cur_ratio - DefaultRatio) < DoubleDiff) { - int scale_width = static_cast(cur_ratio * WindowHeightDefault); - m_scale_size = std::make_pair(scale_width, WindowHeightDefault); - m_control_scale = static_cast(m_emulator_info.adb.display_height) / static_cast(WindowHeightDefault); - } - else { // 否则可能是偏正方形的屏幕,按宽度计算 - int scale_height = static_cast(WindowWidthDefault / cur_ratio); - m_scale_size = std::make_pair(WindowWidthDefault, scale_height); - m_control_scale = static_cast(m_emulator_info.adb.display_width) / static_cast(WindowWidthDefault); - } - - m_emulator_info.adb.click = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.click, "[Adb]", m_emulator_info.adb.path), "[Address]", address); - m_emulator_info.adb.swipe = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.swipe, "[Adb]", m_emulator_info.adb.path), "[Address]", address); - m_emulator_info.adb.screencap_raw_with_gzip = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.screencap_raw_with_gzip, "[Adb]", m_emulator_info.adb.path), "[Address]", address); - m_emulator_info.adb.screencap_encode = utils::string_replace_all(utils::string_replace_all(m_emulator_info.adb.screencap_encode, "[Adb]", m_emulator_info.adb.path), "[Address]", address); - m_emulator_info.adb.release = utils::string_replace_all(m_emulator_info.adb.release, "[Adb]", m_emulator_info.adb.path); - - return true; -} - asst::Rect asst::Controller::shaped_correct(const Rect & rect) const { if (rect.empty() @@ -256,148 +196,6 @@ void asst::Controller::pipe_working_proc() } } -void asst::Controller::set_dirname(std::string dirname) noexcept -{ - m_dirname = std::move(dirname); -} - -bool asst::Controller::try_capture(const EmulatorInfo & info, bool without_handle) -{ - LogTraceScope("try_capture | " + info.name); - - const HandleInfo& handle_info = info.handle; - - std::string adb_path; - if (!without_handle) { // 使用模拟器自带的adb -#ifdef _WIN32 // Only support Windows - // 转成宽字符的 - wchar_t* class_wbuff = nullptr; - if (!handle_info.class_name.empty()) { - int class_len = static_cast(handle_info.class_name.size() + 1U) * 2; - class_wbuff = new wchar_t[class_len]; - ::MultiByteToWideChar(CP_UTF8, 0, handle_info.class_name.c_str(), -1, class_wbuff, class_len); - } - wchar_t* window_wbuff = nullptr; - if (!handle_info.window_name.empty()) { - int window_len = static_cast(handle_info.window_name.size() + 1U) * 2; - window_wbuff = new wchar_t[window_len]; - memset(window_wbuff, 0, window_len); - ::MultiByteToWideChar(CP_UTF8, 0, handle_info.window_name.c_str(), -1, window_wbuff, window_len); - } - // 查找窗口句柄 - HWND window_handle = nullptr; - window_handle = ::FindWindowExW(window_handle, nullptr, class_wbuff, window_wbuff); - - if (class_wbuff != nullptr) { - delete[] class_wbuff; - class_wbuff = nullptr; - } - if (window_wbuff != nullptr) { - delete[] window_wbuff; - window_wbuff = nullptr; - } - if (window_handle == nullptr) { - return false; - } - - std::string emulator_path; - if (info.path.empty()) { - // 获取模拟器窗口句柄对应的进程句柄 - DWORD process_id = 0; - ::GetWindowThreadProcessId(window_handle, &process_id); - HANDLE process_handle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_id); - - // 获取模拟器程序所在路径 - LPSTR path_buff = new CHAR[MAX_PATH]; - memset(path_buff, 0, MAX_PATH); - DWORD path_size = MAX_PATH; - QueryFullProcessImageNameA(process_handle, 0, path_buff, &path_size); - emulator_path = std::string(path_buff); - if (path_buff != nullptr) { - delete[] path_buff; - path_buff = nullptr; - } - if (emulator_path.empty()) { - return false; - } - Resrc.cfg().set_emulator_path(info.name, emulator_path); - Resrc.user().set_emulator_path(info.name, emulator_path); - } - else { - emulator_path = info.path; - } - - // 到这一步说明句柄和权限没问题了,接下来就是adb的事情了 - m_emulator_info = info; - Log.trace("Handle:", window_handle, "Name:", m_emulator_info.name); - - adb_path = emulator_path.substr(0, emulator_path.find_last_of('\\') + 1); - adb_path = '"' + utils::string_replace_all(m_emulator_info.adb.path, "[EmulatorPath]", adb_path) + '"'; - adb_path = utils::string_replace_all(adb_path, "[ExecDir]", m_dirname); -#else - Log.error("Capture handle is only supported on Windows!"); - return false; -#endif - } - else { // 使用辅助自带的标准adb - m_emulator_info = info; -#ifdef _WIN32 - adb_path = '"' + utils::string_replace_all(m_emulator_info.adb.path, "[ExecDir]", m_dirname) + '"'; -#else - adb_path = m_emulator_info.adb.path; -#endif - } - - m_emulator_info.adb.path = std::move(adb_path); - - // 优先使用addresses里指定的地址 - for (const std::string& address : info.adb.addresses) { - if (connect_adb(address)) { - return true; - } - } - - // 若指定地址都没连上,再尝试用devices查找地址 - std::string devices_cmd = utils::string_replace_all(m_emulator_info.adb.devices, "[Adb]", m_emulator_info.adb.path); - auto devices_ret = call_command(devices_cmd); - if (!devices_ret) { - return false; - } - auto& devices_result = devices_ret.value(); - 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"); - - 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("device address", address); - if (address.empty()) { - return false; - } - - return connect_adb(address); -} - -//void asst::Controller::set_idle(bool flag) -//{ -// LogTraceFunction; -// -// m_thread_idle = flag; -// if (!flag) { -// // 开始前,立即截一张图,保证第一张图片非空 -// //screencap(); -// -// m_cmd_condvar.notify_one(); -// } -//} - std::optional> asst::Controller::call_command(const std::string & cmd) { LogTraceFunction; @@ -581,7 +379,7 @@ bool asst::Controller::screencap() { LogTraceFunction; - auto& adb = m_emulator_info.adb; + auto& adb = m_adb; DecodeFunc decode_raw_with_gzip = [&](const std::vector& data) -> bool { auto unzip_data = gzip::decompress(data.data(), data.size()); @@ -589,7 +387,7 @@ bool asst::Controller::screencap() if (unzip_data.empty()) { return false; } - size_t std_size = adb.display_height * adb.display_width * 4; + size_t std_size = m_width * m_height * 4; if (unzip_data.size() < std_size) { return false; } @@ -603,11 +401,7 @@ bool asst::Controller::screencap() if (is_all_zero) { return false; } - cv::Mat temp = cv::Mat( - adb.display_height, - adb.display_width, - CV_8UC4, - unzip_data.data() + header_size); + cv::Mat temp = cv::Mat(m_width, m_height, CV_8UC4, unzip_data.data() + header_size); if (temp.empty()) { return false; } @@ -628,26 +422,26 @@ bool asst::Controller::screencap() }; switch (adb.screencap_method) { - case AdbCmd::ScreencapMethod::UnknownYet: + case AdbProperty::ScreencapMethod::UnknownYet: { if (screencap(adb.screencap_raw_with_gzip, decode_raw_with_gzip)) { - adb.screencap_method = AdbCmd::ScreencapMethod::RawWithGzip; + adb.screencap_method = AdbProperty::ScreencapMethod::RawWithGzip; return true; } else if (screencap(adb.screencap_encode, decode_encode)) { - adb.screencap_method = AdbCmd::ScreencapMethod::Encode; + adb.screencap_method = AdbProperty::ScreencapMethod::Encode; return true; } else { return false; } } - case AdbCmd::ScreencapMethod::RawWithGzip: + case AdbProperty::ScreencapMethod::RawWithGzip: { return screencap(adb.screencap_raw_with_gzip, decode_raw_with_gzip); } break; - case AdbCmd::ScreencapMethod::Encode: + case AdbProperty::ScreencapMethod::Encode: { return screencap(adb.screencap_encode, decode_encode); } @@ -659,7 +453,7 @@ bool asst::Controller::screencap() bool asst::Controller::screencap(const std::string & cmd, DecodeFunc decode_func) { - auto& adb = m_emulator_info.adb; + auto& adb = m_adb; auto ret = call_command(cmd); if (!ret || ret.value().empty()) { @@ -668,25 +462,25 @@ bool asst::Controller::screencap(const std::string & cmd, DecodeFunc decode_func } auto data = std::move(ret).value(); - if (adb.screencap_end_of_line == AdbCmd::ScreencapEndOfLine::CRLF) { + if (adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::CRLF) { convert_lf(data); } if (decode_func(data)) { - if (adb.screencap_end_of_line == AdbCmd::ScreencapEndOfLine::UnknownYet) { - adb.screencap_end_of_line = AdbCmd::ScreencapEndOfLine::LF; + if (adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) { + adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::LF; } return true; } else { Log.info("data is not empty, but image is empty"); - if (adb.screencap_end_of_line == AdbCmd::ScreencapEndOfLine::UnknownYet) { + if (adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) { Log.info("try to cvt lf"); convert_lf(data); if (decode_func(data)) { - adb.screencap_end_of_line = AdbCmd::ScreencapEndOfLine::CRLF; + adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::CRLF; return true; } else { @@ -727,10 +521,10 @@ int asst::Controller::click(const Rect & rect, bool block) int asst::Controller::click_without_scale(const Point & p, bool block) { - if (p.x < 0 || p.x >= m_emulator_info.adb.display_width || p.y < 0 || p.y >= m_emulator_info.adb.display_height) { + if (p.x < 0 || p.x >= m_width || p.y < 0 || p.y >= m_height) { Log.error("click point out of range"); } - std::string cur_cmd = utils::string_replace_all(m_emulator_info.adb.click, "[x]", std::to_string(p.x)); + std::string cur_cmd = utils::string_replace_all(m_adb.click, "[x]", std::to_string(p.x)); cur_cmd = utils::string_replace_all(cur_cmd, "[y]", std::to_string(p.y)); int id = push_cmd(cur_cmd); if (block) { @@ -762,10 +556,10 @@ int asst::Controller::swipe(const Rect & r1, const Rect & r2, int duration, bool int asst::Controller::swipe_without_scale(const Point & p1, const Point & p2, int duration, bool block, int extra_delay, bool extra_swipe) { - if (p1.x < 0 || p1.x >= m_emulator_info.adb.display_width || p1.y < 0 || p1.y >= m_emulator_info.adb.display_height || p2.x < 0 || p2.x >= m_emulator_info.adb.display_width || p2.y < 0 || p2.y >= m_emulator_info.adb.display_height) { + if (p1.x < 0 || p1.x >= m_width || p1.y < 0 || p1.y >= m_height || p2.x < 0 || p2.x >= m_width || p2.y < 0 || p2.y >= m_height) { Log.error("swipe point out of range"); } - std::string cur_cmd = utils::string_replace_all(m_emulator_info.adb.swipe, "[x1]", std::to_string(p1.x)); + std::string cur_cmd = utils::string_replace_all(m_adb.swipe, "[x1]", std::to_string(p1.x)); cur_cmd = utils::string_replace_all(cur_cmd, "[y1]", std::to_string(p1.y)); cur_cmd = utils::string_replace_all(cur_cmd, "[x2]", std::to_string(p2.x)); cur_cmd = utils::string_replace_all(cur_cmd, "[y2]", std::to_string(p2.y)); @@ -783,7 +577,7 @@ int asst::Controller::swipe_without_scale(const Point & p1, const Point & p2, in // 额外的滑动:adb有bug,同样的参数,偶尔会划得非常远。额外做一个短程滑动,把之前的停下来 if (extra_swipe && extra_swipe_duration >= 0) { - std::string extra_cmd = utils::string_replace_all(m_emulator_info.adb.swipe, "[x1]", std::to_string(p2.x)); + std::string extra_cmd = utils::string_replace_all(m_adb.swipe, "[x1]", std::to_string(p2.x)); extra_cmd = utils::string_replace_all(extra_cmd, "[y1]", std::to_string(p2.y)); int end_x = 0, end_y = 0; if (p2.x != p1.x) { @@ -819,6 +613,100 @@ int asst::Controller::swipe_without_scale(const Rect & r1, const Rect & r2, int return swipe_without_scale(rand_point_in_rect(r1), rand_point_in_rect(r2), duration, block, extra_delay, extra_swipe); } +bool asst::Controller::connect(const std::string & adb_path, const std::string & address, const std::string & config) +{ + LogTraceFunction; + + auto adb_ret = Resrc.cfg().get_adb_cfg(config); + if (!adb_ret) { + json::value info = json::object{ + {"what", "ConnectFailed"}, + {"why", "ConfigNotFound"}, + {"details", json::object{}} + }; + m_callback(AsstMsg::ConnectionError, info, m_callback_arg); + return false; + } + const auto adb_cfg = std::move(adb_ret.value()); + + auto cmd_replace = [&](const std::string& cfg_cmd) -> std::string { + return utils::string_replace_all( + utils::string_replace_all(cfg_cmd, "[Adb]", adb_path), "[Address]", address); + }; + + /* connect */ + { + auto connect_ret = call_command(cmd_replace(adb_cfg.connect)); + // 端口即使错误,命令仍然会返回0,TODO 对connect_result进行判断 + if (!connect_ret) { + json::value info = json::object{ + {"what", "ConnectFailed"}, + {"why", "Connection command failed to exec"}, + {"details", json::object{}} + }; + m_callback(AsstMsg::ConnectionError, info, m_callback_arg); + return false; + } + } + + /* display */ + { + auto display_ret = call_command(cmd_replace(adb_cfg.display)); + if (!display_ret) { + json::value info = json::object{ + {"what", "ConnectFailed"}, + {"why", "Display command failed to exec"}, + {"details", json::object{}} + }; + m_callback(AsstMsg::ConnectionError, info, m_callback_arg); + return false; + } + + auto& display_result = display_ret.value(); + std::string display_pipe_str( + std::make_move_iterator(display_result.begin()), + std::make_move_iterator(display_result.end())); + int size_value1 = 0; + int size_value2 = 0; +#ifdef _MSC_VER + sscanf_s(display_pipe_str.c_str(), adb_cfg.display_format.c_str(), &size_value1, &size_value2); +#else + sscanf(display_pipe_str.c_str(), adb_cfg.display_format.c_str(), &size_value1, &size_value2); +#endif + // 为了防止抓取句柄的时候手机是竖屏的(还没进游戏),这里取大的值为宽,小的为高 + // 总不能有人竖屏玩明日方舟吧(? + m_width = (std::max)(size_value1, size_value2); + m_height = (std::min)(size_value1, size_value2); + } + + /* calc ratio */ + { + constexpr double DefaultRatio = + static_cast(WindowWidthDefault) / static_cast(WindowHeightDefault); + double cur_ratio = static_cast(m_width) / static_cast(m_height); + + if (cur_ratio >= DefaultRatio // 说明是宽屏或默认16:9,按照高度计算缩放 + || std::fabs(cur_ratio - DefaultRatio) < DoubleDiff) { + int scale_width = static_cast(cur_ratio * WindowHeightDefault); + m_scale_size = std::make_pair(scale_width, WindowHeightDefault); + m_control_scale = static_cast(m_height) / static_cast(WindowHeightDefault); + } + else { // 否则可能是偏正方形的屏幕,按宽度计算 + int scale_height = static_cast(WindowWidthDefault / cur_ratio); + m_scale_size = std::make_pair(WindowWidthDefault, scale_height); + m_control_scale = static_cast(m_width) / static_cast(WindowWidthDefault); + } + } + + m_adb.click = cmd_replace(adb_cfg.click); + m_adb.swipe = cmd_replace(adb_cfg.swipe); + m_adb.screencap_raw_with_gzip = cmd_replace(adb_cfg.screencap_raw_with_gzip); + m_adb.screencap_encode = cmd_replace(adb_cfg.screencap_encode); + m_adb.release = cmd_replace(adb_cfg.release); + + return true; +} + cv::Mat asst::Controller::get_image() { // 有些模拟器adb偶尔会莫名其妙截图失败,多试几次 diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 0c06954a60..1c05617dee 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -18,25 +18,20 @@ #include #include "AsstDef.h" +#include "AsstMsg.h" namespace asst { class Controller { public: + Controller(AsstCallback callback, void* callback_arg); Controller(const Controller&) = delete; Controller(Controller&&) = delete; ~Controller(); - static Controller& get_instance() - { - static Controller unique_instance; - return unique_instance; - } + bool connect(const std::string& adb_path, const std::string& address, const std::string& config); - static void set_dirname(std::string dirname) noexcept; - - bool try_capture(const EmulatorInfo& info, bool without_handle = false); cv::Mat get_image(); std::vector get_image_encode(); @@ -62,10 +57,6 @@ namespace asst Controller& operator=(Controller&&) = delete; private: - Controller(); - - bool connect_adb(const std::string& address); - void pipe_working_proc(); std::optional> call_command(const std::string& cmd); int push_cmd(const std::string& cmd); @@ -82,19 +73,10 @@ namespace asst // 转换data中所有的crlf为lf:有些模拟器自带的adb,exec-out输出的\n,会被替换成\r\n,导致解码错误,所以这里转一下回来(点名批评mumu) static void convert_lf(std::vector& data); - inline static std::string m_dirname; + AsstCallback m_callback; + void* m_callback_arg = nullptr; - bool m_thread_exit = false; - //bool m_thread_idle = true; - std::thread m_cmd_thread; - std::mutex m_cmd_queue_mutex; - std::condition_variable m_cmd_condvar; - std::queue m_cmd_queue; - std::atomic m_completed_id = 0; - unsigned m_push_id = 0; // push_id的自增总是伴随着queue的push,肯定是要上锁的,所以没必要原子 - - mutable std::shared_mutex m_image_mutex; - cv::Mat m_cache_image; + std::minstd_rand m_rand_engine; constexpr static int PipeBuffSize = 4 * 1024 * 1024; // 管道缓冲区大小 std::unique_ptr m_pipe_buffer = nullptr; @@ -113,12 +95,49 @@ namespace asst int m_child = 0; #endif - EmulatorInfo m_emulator_info; - std::minstd_rand m_rand_engine; + struct AdbProperty + { + /* command */ + std::string click; + std::string swipe; + + std::string screencap_raw_with_gzip; + std::string screencap_encode; + std::string release; + + /* propertities */ + enum class ScreencapEndOfLine + { + UnknownYet, + CRLF, + LF, + CR + } screencap_end_of_line = ScreencapEndOfLine::UnknownYet; + + enum class ScreencapMethod + { + UnknownYet, + Default, + RawWithGzip, + Encode + } screencap_method = ScreencapMethod::UnknownYet; + } m_adb; + std::pair m_scale_size; double m_control_scale = 1.0; - }; + int m_width = 0; + int m_height = 0; - //static auto& ctrler = Controller::get_instance(); -#define Ctrler Controller::get_instance() + mutable std::shared_mutex m_image_mutex; + cv::Mat m_cache_image; + + bool m_thread_exit = false; + //bool m_thread_idle = true; + std::mutex m_cmd_queue_mutex; + std::condition_variable m_cmd_condvar; + std::queue m_cmd_queue; + std::atomic m_completed_id = 0; + unsigned m_push_id = 0; // push_id的自增总是伴随着queue的push,肯定是要上锁的,所以没必要原子 + std::thread m_cmd_thread; + }; } diff --git a/src/MeoAssistant/CreditShoppingTask.cpp b/src/MeoAssistant/CreditShoppingTask.cpp index 706de412fc..702e84a89a 100644 --- a/src/MeoAssistant/CreditShoppingTask.cpp +++ b/src/MeoAssistant/CreditShoppingTask.cpp @@ -13,7 +13,7 @@ bool asst::CreditShoppingTask::_run() { - const cv::Mat image = Ctrler.get_image(); + const cv::Mat image = m_ctrler->get_image(); CreditShopImageAnalyzer shop_analyzer(image); if (!shop_analyzer.analyze()) { @@ -25,7 +25,7 @@ bool asst::CreditShoppingTask::_run() if (need_exit()) { return false; } - Ctrler.click(commodity); + m_ctrler->click(commodity); static int pre_delay = 1000; static int rare_delay = 1000; @@ -37,7 +37,7 @@ bool asst::CreditShoppingTask::_run() if (buy_it_rect.empty()) { const auto buy_it_task_ptr = Task.get("CreditShop-BuyIt"); - const cv::Mat buy_image = Ctrler.get_image(); + const cv::Mat buy_image = m_ctrler->get_image(); MatchImageAnalyzer buy_it_analyzer(buy_image); buy_it_analyzer.set_task_info(buy_it_task_ptr); if (!buy_it_analyzer.analyze()) { @@ -52,11 +52,11 @@ bool asst::CreditShoppingTask::_run() if (need_exit()) { return false; } - Ctrler.click(buy_it_rect); + m_ctrler->click(buy_it_rect); sleep(rare_delay); // 识别是否信用不足无法购买 - const cv::Mat prompt_image = Ctrler.get_image(); + const cv::Mat prompt_image = m_ctrler->get_image(); OcrImageAnalyzer prompt_analyzer(prompt_image); prompt_analyzer.set_task_info("CreditShop-NoMoney"); @@ -66,7 +66,7 @@ bool asst::CreditShoppingTask::_run() } // 这里随便点一下都行,把购买完弹出来物品的界面取消掉 - Ctrler.click(buy_it_rect); + m_ctrler->click(buy_it_rect); sleep(rare_delay); } return true; diff --git a/src/MeoAssistant/GeneralConfiger.cpp b/src/MeoAssistant/GeneralConfiger.cpp index 74399af223..0e498cf7e9 100644 --- a/src/MeoAssistant/GeneralConfiger.cpp +++ b/src/MeoAssistant/GeneralConfiger.cpp @@ -30,34 +30,23 @@ bool asst::GeneralConfiger::parse(const json::value& json) } } - for (const auto& [name, emulator_json] : json.at("emulator").as_object()) { - EmulatorInfo emulator_info; - emulator_info.name = name; + for (const auto& [name, cfg_json] : json.at("emulator").as_object()) { + AdbCfg adb; - const json::object& handle_json = emulator_json.at("handle").as_object(); - emulator_info.handle.class_name = handle_json.at("class").as_string(); - emulator_info.handle.window_name = handle_json.at("window").as_string(); + adb.devices = cfg_json.at("devices").as_string(); + adb.address_regex = cfg_json.at("addressRegex").as_string(); + adb.connect = cfg_json.at("connect").as_string(); + adb.click = cfg_json.at("click").as_string(); + adb.swipe = cfg_json.at("swipe").as_string(); + adb.display = cfg_json.at("display").as_string(); + adb.display_format = cfg_json.at("displayFormat").as_string(); + adb.screencap_raw_with_gzip = cfg_json.at("screencapRawWithGzip").as_string(); + adb.screencap_encode = cfg_json.at("screencapEncode").as_string(); + adb.release = cfg_json.at("release").as_string(); + //adb.pullscreen = cfg_json.at("pullscreen").as_string(); - const json::object& adb_json = emulator_json.at("adb").as_object(); - emulator_info.adb.path = adb_json.at("path").as_string(); - - for (const json::value& address_json : adb_json.at("addresses").as_array()) { - emulator_info.adb.addresses.emplace_back(address_json.as_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_format = adb_json.at("displayFormat").as_string(); - emulator_info.adb.screencap_raw_with_gzip = adb_json.at("screencapRawWithGzip").as_string(); - emulator_info.adb.screencap_encode = adb_json.at("screencapEncode").as_string(); - emulator_info.adb.release = adb_json.at("release").as_string(); - //emulator_info.adb.pullscreen = adb_json.at("pullscreen").as_string(); - - m_emulators_info.emplace(name, std::move(emulator_info)); + m_adb_cfg.emplace(name, std::move(adb)); } return true; -} \ No newline at end of file +} diff --git a/src/MeoAssistant/GeneralConfiger.h b/src/MeoAssistant/GeneralConfiger.h index 7e3cecc28d..02c11af251 100644 --- a/src/MeoAssistant/GeneralConfiger.h +++ b/src/MeoAssistant/GeneralConfiger.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "AsstDef.h" @@ -45,6 +46,23 @@ namespace asst AipOcrCfg aip_ocr; // 百度 OCR API 的配置 }; + struct AdbCfg + { + /* format */ + std::string address_regex; + std::string display_format; + + /* command */ + std::string devices; + std::string connect; + std::string click; + std::string swipe; + std::string display; + std::string screencap_raw_with_gzip; + std::string screencap_encode; + std::string release; + }; + class GeneralConfiger : public AbstractConfiger { public: @@ -62,29 +80,27 @@ namespace asst { return m_options; } - const std::unordered_map& get_emulators_info() const noexcept + std::optional get_adb_cfg(const std::string& name) const { - return m_emulators_info; + if (auto iter = m_adb_cfg.find(name); + iter != m_adb_cfg.cend()) { + return iter->second; + } + else { + return std::nullopt; + } } void set_options(Options opt) noexcept { m_options = std::move(opt); } - void set_emulator_info(std::string name, EmulatorInfo emu) - { - m_emulators_info.emplace(std::move(name), std::move(emu)); - } - void set_emulator_path(std::string name, std::string path) - { - m_emulators_info[name].path = path; - } protected: virtual bool parse(const json::value& json) override; std::string m_version; Options m_options; - std::unordered_map m_emulators_info; + std::unordered_map m_adb_cfg; }; } diff --git a/src/MeoAssistant/InfrastAbstractTask.cpp b/src/MeoAssistant/InfrastAbstractTask.cpp index e53412d7fc..ff7dffdd60 100644 --- a/src/MeoAssistant/InfrastAbstractTask.cpp +++ b/src/MeoAssistant/InfrastAbstractTask.cpp @@ -74,7 +74,7 @@ bool asst::InfrastAbstractTask::on_run_fails() bool asst::InfrastAbstractTask::enter_facility(int index) { - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); InfrastFacilityImageAnalyzer analyzer(image); analyzer.set_to_be_analyzed({ facility_name() }); @@ -91,7 +91,7 @@ bool asst::InfrastAbstractTask::enter_facility(int index) m_cur_facility_index = index; callback(AsstMsg::SubTaskExtraInfo, basic_info_with_what("EnterFacility")); - Ctrler.click(rect); + m_ctrler->click(rect); const auto enter_task_ptr = Task.get("InfrastEnterFacility"); sleep(enter_task_ptr->rear_delay); @@ -115,10 +115,10 @@ void asst::InfrastAbstractTask::async_swipe_of_operlist(bool reverse) static int duration = Task.get("InfrastOperListSwipeBegin")->pre_delay; if (!reverse) { - m_last_swipe_id = Ctrler.swipe(begin_rect, end_rect, duration, false, 0, true); + m_last_swipe_id = m_ctrler->swipe(begin_rect, end_rect, duration, false, 0, true); } else { - m_last_swipe_id = Ctrler.swipe(end_rect, begin_rect, duration, false, 0, true); + m_last_swipe_id = m_ctrler->swipe(end_rect, begin_rect, duration, false, 0, true); } } @@ -127,7 +127,7 @@ void asst::InfrastAbstractTask::await_swipe() LogTraceFunction; static int extra_delay = Task.get("InfrastOperListSwipeBegin")->rear_delay; - Ctrler.wait(m_last_swipe_id); + m_ctrler->wait(m_last_swipe_id); sleep(extra_delay); } @@ -174,7 +174,7 @@ void asst::InfrastAbstractTask::swipe_to_the_left_of_operlist(int loop_times) if (need_exit()) { return; } - Ctrler.swipe(end_rect, begin_rect, duration, true, 0, false); + m_ctrler->swipe(end_rect, begin_rect, duration, true, 0, false); } sleep(extra_delay); } @@ -187,7 +187,7 @@ void asst::InfrastAbstractTask::swipe_to_the_left_of_main_ui() static int duration = Task.get("InfrastOperListSwipeToTheLeftBegin")->pre_delay; static int extra_delay = Task.get("InfrastOperListSwipeToTheLeftBegin")->rear_delay; - Ctrler.swipe(end_rect, begin_rect, duration, true, extra_delay, false); + m_ctrler->swipe(end_rect, begin_rect, duration, true, extra_delay, false); } void asst::InfrastAbstractTask::swipe_to_the_right_of_main_ui() @@ -198,5 +198,5 @@ void asst::InfrastAbstractTask::swipe_to_the_right_of_main_ui() static int duration = Task.get("InfrastOperListSwipeToTheLeftBegin")->pre_delay; static int extra_delay = Task.get("InfrastOperListSwipeToTheLeftBegin")->rear_delay; - Ctrler.swipe(begin_rect, end_rect, duration, true, extra_delay, false); + m_ctrler->swipe(begin_rect, end_rect, duration, true, extra_delay, false); } diff --git a/src/MeoAssistant/InfrastDormTask.cpp b/src/MeoAssistant/InfrastDormTask.cpp index 0c4703b9fa..24dc048edf 100644 --- a/src/MeoAssistant/InfrastDormTask.cpp +++ b/src/MeoAssistant/InfrastDormTask.cpp @@ -42,7 +42,7 @@ bool asst::InfrastDormTask::opers_choose() if (need_exit()) { return false; } - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); InfrastOperImageAnalyzer oper_analyzer(image); const int without_skill = InfrastOperImageAnalyzer::All ^ InfrastOperImageAnalyzer::Skill; oper_analyzer.set_to_be_calced(without_skill); @@ -75,7 +75,7 @@ bool asst::InfrastDormTask::opers_choose() case infrast::SmileyType::Distract: // 干员没有被选择的情况下,且不在工作,就进驻宿舍 if (oper.selected == false && oper.doing != infrast::Doing::Working) { - Ctrler.click(oper.rect); + m_ctrler->click(oper.rect); if (++num_of_selected >= max_num_of_opers()) { Log.trace("num_of_selected:", num_of_selected, ", just break"); break; diff --git a/src/MeoAssistant/InfrastInfoTask.cpp b/src/MeoAssistant/InfrastInfoTask.cpp index 738ca62715..75300dcd42 100644 --- a/src/MeoAssistant/InfrastInfoTask.cpp +++ b/src/MeoAssistant/InfrastInfoTask.cpp @@ -9,7 +9,7 @@ bool asst::InfrastInfoTask::_run() { swipe_to_the_left_of_main_ui(); - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); InfrastFacilityImageAnalyzer analyzer(image); analyzer.set_to_be_analyzed({ "Mfg", "Trade", "Power", "Dorm" }); diff --git a/src/MeoAssistant/InfrastOfficeTask.cpp b/src/MeoAssistant/InfrastOfficeTask.cpp index 62f8779cd5..441f405b52 100644 --- a/src/MeoAssistant/InfrastOfficeTask.cpp +++ b/src/MeoAssistant/InfrastOfficeTask.cpp @@ -40,7 +40,7 @@ bool asst::InfrastOfficeTask::_run() need_shift = true; // TODO,这里有个bug,全部干员中的selected,和当前的,不一定是同一个页面 // 不过目前没影响,反正滑动到最前面了,selected的一定是在最前面 - //Ctrler.click(find_iter->rect); + //m_ctrler->click(find_iter->rect); //sleep(300); break; case infrast::WorkMode::Extreme: // TODO diff --git a/src/MeoAssistant/InfrastPowerTask.cpp b/src/MeoAssistant/InfrastPowerTask.cpp index 44d2eff2cb..cd1bd9ff50 100644 --- a/src/MeoAssistant/InfrastPowerTask.cpp +++ b/src/MeoAssistant/InfrastPowerTask.cpp @@ -51,7 +51,7 @@ bool asst::InfrastPowerTask::_run() need_shift = true; // TODO,这里有个bug,全部干员中的selected,和当前的,不一定是同一个页面 // 不过目前没影响,反正滑动到最前面了,selected的一定是在最前面 - //Ctrler.click(find_iter->rect); + //m_ctrler->click(find_iter->rect); //sleep(300); break; case infrast::WorkMode::Extreme: // TODO diff --git a/src/MeoAssistant/InfrastProductionTask.cpp b/src/MeoAssistant/InfrastProductionTask.cpp index 2ff385bd87..951b4c9518 100644 --- a/src/MeoAssistant/InfrastProductionTask.cpp +++ b/src/MeoAssistant/InfrastProductionTask.cpp @@ -59,11 +59,11 @@ bool asst::InfrastProductionTask::shift_facility_list() callback(AsstMsg::SubTaskExtraInfo, basic_info_with_what("EnterFacility")); } - Ctrler.click(tab); + m_ctrler->click(tab); sleep(tab_task_ptr->rear_delay); /* 识别当前制造/贸易站有没有添加干员按钮,没有就不换班 */ - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); add_analyzer.set_image(image); if (!add_analyzer.analyze()) { Log.info("no add button, just continue"); @@ -105,7 +105,7 @@ bool asst::InfrastProductionTask::shift_facility_list() } /* 进入干员选择页面 */ - Ctrler.click(add_button); + m_ctrler->click(add_button); sleep(add_task_ptr->rear_delay); for (int i = 0; i <= OperSelectRetryTimes; ++i) { @@ -180,7 +180,7 @@ bool asst::InfrastProductionTask::opers_detect_with_swipe() size_t asst::InfrastProductionTask::opers_detect() { LogTraceFunction; - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); InfrastOperImageAnalyzer oper_analyzer(image); oper_analyzer.set_facility(facility_name()); @@ -463,7 +463,7 @@ bool asst::InfrastProductionTask::opers_choose() if (need_exit()) { return false; } - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); InfrastOperImageAnalyzer oper_analyzer(image); oper_analyzer.set_facility(facility_name()); @@ -537,7 +537,7 @@ bool asst::InfrastProductionTask::opers_choose() // 但是如果当前设施只有一个位置,即不存在“上次循环”的情况,说明是清除干员按钮没点到 } else { - Ctrler.click(find_iter->rect); + m_ctrler->click(find_iter->rect); } { auto avlb_iter = std::find_if( @@ -620,7 +620,7 @@ bool asst::InfrastProductionTask::facility_list_detect() LogTraceFunction; m_facility_list_tabs.clear(); - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); MultiMatchImageAnalyzer mm_analyzer(image); mm_analyzer.set_task_info("InfrastFacilityListTab" + facility_name()); diff --git a/src/MeoAssistant/InfrastReceptionTask.cpp b/src/MeoAssistant/InfrastReceptionTask.cpp index f6d3f7af25..f464f697d4 100644 --- a/src/MeoAssistant/InfrastReceptionTask.cpp +++ b/src/MeoAssistant/InfrastReceptionTask.cpp @@ -61,7 +61,7 @@ bool asst::InfrastReceptionTask::use_clue() proc_clue_vacancy(); } - cv::Mat image = Ctrler.get_image(); + cv::Mat image = m_ctrler->get_image(); // 所有的空位分析一次,看看还缺哪些线索 InfrastClueVacancyImageAnalyzer vacancy_analyzer(image); @@ -94,7 +94,7 @@ bool asst::InfrastReceptionTask::proc_clue_vacancy() "No1", "No2", "No3", "No4", "No5", "No6", "No7" }; - cv::Mat image = Ctrler.get_image(); + cv::Mat image = m_ctrler->get_image(); for (const std::string& clue : clue_suffix) { if (need_exit()) { return false; @@ -107,18 +107,18 @@ bool asst::InfrastReceptionTask::proc_clue_vacancy() } // 点开线索的空位 Rect vacancy = vacancy_analyzer.get_vacancy().cbegin()->second; - Ctrler.click(vacancy); + m_ctrler->click(vacancy); int delay = Task.get(clue_vacancy + clue)->rear_delay; sleep(delay); // 识别右边列表中的线索,然后用最底下的那个(一般都是剩余时间最短的) //swipe_to_the_bottom_of_clue_list_on_the_right(); - image = Ctrler.get_image(); + image = m_ctrler->get_image(); InfrastClueImageAnalyzer clue_analyzer(image); if (!clue_analyzer.analyze()) { continue; } - Ctrler.click(clue_analyzer.get_result().back().first); + m_ctrler->click(clue_analyzer.get_result().back().first); sleep(delay); } return true; @@ -140,14 +140,14 @@ bool asst::InfrastReceptionTask::send_clue() bool asst::InfrastReceptionTask::shift() { LogTraceFunction; - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); MatchImageAnalyzer add_analyzer(image); const auto raw_task_ptr = Task.get("InfrastAddOperator" + facility_name() + m_work_mode_name); switch (raw_task_ptr->algorithm) { case AlgorithmType::JustReturn: if (raw_task_ptr->action == ProcessTaskAction::ClickRect) { - Ctrler.click(raw_task_ptr->specific_rect); + m_ctrler->click(raw_task_ptr->specific_rect); } break; case AlgorithmType::MatchTemplate: { @@ -156,7 +156,7 @@ bool asst::InfrastReceptionTask::shift() if (!add_analyzer.analyze()) { return true; } - Ctrler.click(add_analyzer.get_result().rect); + m_ctrler->click(add_analyzer.get_result().rect); } break; default: break; diff --git a/src/MeoAssistant/MatchImageAnalyzer.cpp b/src/MeoAssistant/MatchImageAnalyzer.cpp index 8217a5004a..9af95d6a1b 100644 --- a/src/MeoAssistant/MatchImageAnalyzer.cpp +++ b/src/MeoAssistant/MatchImageAnalyzer.cpp @@ -73,7 +73,6 @@ void asst::MatchImageAnalyzer::set_task_info(MatchTaskInfo task_info) noexcept } else { set_roi(task_info.roi); - correct_roi(); } } bool asst::MatchImageAnalyzer::match_templ(const cv::Mat templ) diff --git a/src/MeoAssistant/MeoAssistant.vcxproj b/src/MeoAssistant/MeoAssistant.vcxproj index 4c918f0d72..db85c68b84 100644 --- a/src/MeoAssistant/MeoAssistant.vcxproj +++ b/src/MeoAssistant/MeoAssistant.vcxproj @@ -69,7 +69,6 @@ - @@ -126,7 +125,6 @@ - diff --git a/src/MeoAssistant/MeoAssistant.vcxproj.filters b/src/MeoAssistant/MeoAssistant.vcxproj.filters index 397c7be3a8..4248c60706 100644 --- a/src/MeoAssistant/MeoAssistant.vcxproj.filters +++ b/src/MeoAssistant/MeoAssistant.vcxproj.filters @@ -117,9 +117,6 @@ 头文件\Resource - - 头文件\Resource - 头文件\Task @@ -284,9 +281,6 @@ 源文件\Resource - - 源文件\Resource - 源文件\Task diff --git a/src/MeoAssistant/OcrImageAnalyzer.cpp b/src/MeoAssistant/OcrImageAnalyzer.cpp index 0818ef69d1..cf151c2dcc 100644 --- a/src/MeoAssistant/OcrImageAnalyzer.cpp +++ b/src/MeoAssistant/OcrImageAnalyzer.cpp @@ -91,7 +91,6 @@ void asst::OcrImageAnalyzer::set_task_info(OcrTaskInfo task_info) noexcept m_replace = std::move(task_info.replace_map); set_roi(task_info.roi); - correct_roi(); auto& cache_roi = task_info.region_of_appeared; if (task_info.cache && !cache_roi.empty()) { m_roi = cache_roi; diff --git a/src/MeoAssistant/ProcessTask.cpp b/src/MeoAssistant/ProcessTask.cpp index 14ec064d63..032dbcf88c 100644 --- a/src/MeoAssistant/ProcessTask.cpp +++ b/src/MeoAssistant/ProcessTask.cpp @@ -1,4 +1,4 @@ -#include "ProcessTask.h" +#include "ProcessTask.h" #include #include @@ -93,7 +93,7 @@ bool ProcessTask::_run() cur_task_ptr = front_task_ptr; } else { - const auto image = Ctrler.get_image(); + const auto image = m_ctrler->get_image(); ProcessTaskImageAnalyzer analyzer(image, m_cur_tasks_name); if (!analyzer.analyze()) { return false; @@ -156,7 +156,7 @@ bool ProcessTask::_run() exec_click_task(rect); break; case ProcessTaskAction::ClickRand: { - auto&& [width, height] = Ctrler.get_scale_size(); + auto&& [width, height] = m_ctrler->get_scale_size(); const Rect full_rect(0, 0, width, height); exec_click_task(full_rect); } break; @@ -207,12 +207,12 @@ bool ProcessTask::_run() void ProcessTask::exec_click_task(const Rect& matched_rect) { - Ctrler.click(matched_rect); + m_ctrler->click(matched_rect); } void asst::ProcessTask::exec_swipe_task(ProcessTaskAction action) { - const auto&& [width, height] = Ctrler.get_scale_size(); + const auto&& [width, height] = m_ctrler->get_scale_size(); const static Rect right_rect( static_cast(width * 0.8), @@ -228,10 +228,10 @@ void asst::ProcessTask::exec_swipe_task(ProcessTaskAction action) switch (action) { case asst::ProcessTaskAction::SwipeToTheLeft: - Ctrler.swipe(left_rect, right_rect); + m_ctrler->swipe(left_rect, right_rect); break; case asst::ProcessTaskAction::SwipeToTheRight: - Ctrler.swipe(right_rect, left_rect); + m_ctrler->swipe(right_rect, left_rect); break; default: // 走不到这里,TODO 报个错 break; diff --git a/src/MeoAssistant/RecruitTask.cpp b/src/MeoAssistant/RecruitTask.cpp index 6ced9d0965..3feb9bc288 100644 --- a/src/MeoAssistant/RecruitTask.cpp +++ b/src/MeoAssistant/RecruitTask.cpp @@ -16,7 +16,7 @@ bool RecruitTask::_run() m_has_special_tag = false; m_has_refresh = false; - const cv::Mat image = Ctrler.get_image(); + const cv::Mat image = m_ctrler->get_image(); RecruitImageAnalyzer analyzer(image); if (!analyzer.analyze()) { @@ -26,7 +26,7 @@ bool RecruitTask::_run() if (m_set_time) { for (const auto& rect : analyzer.get_set_time_rect()) { - Ctrler.click(rect, false); + m_ctrler->click(rect, false); } } const std::vector& all_tags = analyzer.get_tags_result(); @@ -198,7 +198,7 @@ bool RecruitTask::_run() for (const TextRect& text_area : all_tags) { if (std::find(final_tags_name.cbegin(), final_tags_name.cend(), text_area.text) != final_tags_name.cend()) { - Ctrler.click(text_area.rect, true); + m_ctrler->click(text_area.rect, true); } } diff --git a/src/MeoAssistant/Resource.cpp b/src/MeoAssistant/Resource.cpp index 8907427858..4ed2ee8c3a 100644 --- a/src/MeoAssistant/Resource.cpp +++ b/src/MeoAssistant/Resource.cpp @@ -16,7 +16,6 @@ bool asst::Resource::load(const std::string& dir) constexpr static const char* RecruitCfgFilename = "recruit.json"; constexpr static const char* ItemCfgFilename = "item_index.json"; constexpr static const char* InfrastCfgFilename = "infrast.json"; - constexpr static const char* UserCfgFilename = "../user.json"; constexpr static const char* OcrResourceFilename = "PaddleOCR"; constexpr static const char* PenguinResourceFilename = "penguin-stats-recognize"; constexpr static const char* TilesCalcResourceFilename = "Arknights-Tile-Pos"; @@ -44,14 +43,6 @@ bool asst::Resource::load(const std::string& dir) return false; } - if (!m_user_cfg_unique_ins.load(dir + UserCfgFilename)) { - m_last_error = std::string(UserCfgFilename) + ": " + m_user_cfg_unique_ins.get_last_error(); - return false; - } - /* 根据用户配置,覆盖原有的部分配置 */ - for (const auto& [name, path] : m_user_cfg_unique_ins.get_emulator_path()) { - m_general_cfg_unique_ins.set_emulator_path(name, path); - } const auto& opt = m_general_cfg_unique_ins.get_options(); /* 加载模板图片资源 */ diff --git a/src/MeoAssistant/Resource.h b/src/MeoAssistant/Resource.h index b803056ba2..5b4fc9ddd7 100644 --- a/src/MeoAssistant/Resource.h +++ b/src/MeoAssistant/Resource.h @@ -13,7 +13,6 @@ #include "RecruitConfiger.h" #include "TaskData.h" #include "TemplResource.h" -#include "UserConfiger.h" #include "TilePack.h" namespace asst @@ -51,10 +50,6 @@ namespace asst { return m_infrast_cfg_unique_ins; } - UserConfiger& user() noexcept - { - return m_user_cfg_unique_ins; - } OcrPack& ocr() noexcept { return m_ocr_pack_unique_ins; @@ -88,10 +83,6 @@ namespace asst { return m_infrast_cfg_unique_ins; } - const UserConfiger& user() const noexcept - { - return m_user_cfg_unique_ins; - } const OcrPack& ocr() const noexcept { return m_ocr_pack_unique_ins; @@ -116,7 +107,6 @@ namespace asst RecruitConfiger m_recruit_cfg_unique_ins; ItemConfiger m_item_cfg_unique_ins; InfrastConfiger m_infrast_cfg_unique_ins; - UserConfiger m_user_cfg_unique_ins; OcrPack m_ocr_pack_unique_ins; PenguinPack m_penguin_pack_unique_ins; TilePack m_tile_pack_unique_ins; diff --git a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp index 0f5165cc83..d289fbb634 100644 --- a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp @@ -64,7 +64,7 @@ bool asst::RoguelikeBattleTaskPlugin::get_stage_info() constexpr int StageNameRetryTimes = 50; for (int i = 0; i != StageNameRetryTimes; ++i) { - cv::Mat image = Ctrler.get_image(); + cv::Mat image = m_ctrler->get_image(); OcrImageAnalyzer name_analyzer(image); name_analyzer.set_task_info(stage_name_task_ptr); if (!name_analyzer.analyze()) { @@ -94,7 +94,7 @@ bool asst::RoguelikeBattleTaskPlugin::get_stage_info() if (calced) { #ifdef ASST_DEBUG auto normal_tiles = tile.calc(m_stage_name, false); - cv::Mat draw = Ctrler.get_image(); + cv::Mat draw = m_ctrler->get_image(); for (const auto& [point, info] : normal_tiles) { using TileKey = TilePack::TileKey; static const std::unordered_map TileKeyMapping = { @@ -133,7 +133,7 @@ bool asst::RoguelikeBattleTaskPlugin::auto_battle() using Oper = asst::BattleImageAnalyzer::Oper; - BattleImageAnalyzer battle_analyzer(Ctrler.get_image()); + BattleImageAnalyzer battle_analyzer(m_ctrler->get_image()); if (!battle_analyzer.analyze()) { return false; } @@ -195,7 +195,7 @@ bool asst::RoguelikeBattleTaskPlugin::auto_battle() if (!oper_found) { return true; } - Ctrler.click(opt_oper.rect); + m_ctrler->click(opt_oper.rect); sleep(use_oper_task_ptr->pre_delay); // 将干员拖动到场上 @@ -234,11 +234,11 @@ bool asst::RoguelikeBattleTaskPlugin::auto_battle() Point placed_loc = get_placed(loc); Point placed_point = m_side_tile_info.at(placed_loc).pos; #ifdef ASST_DEBUG - auto image = Ctrler.get_image(); + auto image = m_ctrler->get_image(); cv::circle(image, cv::Point(placed_point.x, placed_point.y), 10, cv::Scalar(0, 0, 255), -1); #endif Rect placed_rect(placed_point.x, placed_point.y, 1, 1); - Ctrler.swipe(opt_oper.rect, placed_rect, swipe_oper_task_ptr->pre_delay); + m_ctrler->swipe(opt_oper.rect, placed_rect, swipe_oper_task_ptr->pre_delay); sleep(use_oper_task_ptr->rear_delay); // 计算往哪边拖动(干员朝向) @@ -255,7 +255,7 @@ bool asst::RoguelikeBattleTaskPlugin::auto_battle() end_point.y = std::max(0, end_point.y); end_point.y = std::min(end_point.y, WindowHeightDefault); - Ctrler.swipe(placed_point, end_point, swipe_oper_task_ptr->rear_delay); + m_ctrler->swipe(placed_point, end_point, swipe_oper_task_ptr->rear_delay); m_used_tiles.emplace(placed_loc); m_used_opers = true; @@ -272,7 +272,7 @@ bool asst::RoguelikeBattleTaskPlugin::speed_up() bool asst::RoguelikeBattleTaskPlugin::use_skill(const asst::Rect& rect) { - Ctrler.click(rect); + m_ctrler->click(rect); ProcessTask task(*this, { "BattleUseSkillBegin" }); task.set_retry_times(0); @@ -292,7 +292,7 @@ void asst::RoguelikeBattleTaskPlugin::clear() //asst::Rect asst::RoguelikeBattleTaskPlugin::get_placed_by_cv() //{ -// BattlePerspectiveImageAnalyzer placed_analyzer(Ctrler.get_image()); +// BattlePerspectiveImageAnalyzer placed_analyzer(m_ctrler->get_image()); // placed_analyzer.set_src_homes(m_home_cache); // if (!placed_analyzer.analyze()) { // return Rect(); diff --git a/src/MeoAssistant/RoguelikeFormationTask.cpp b/src/MeoAssistant/RoguelikeFormationTask.cpp index 98a9915917..ae23a8ba4e 100644 --- a/src/MeoAssistant/RoguelikeFormationTask.cpp +++ b/src/MeoAssistant/RoguelikeFormationTask.cpp @@ -8,7 +8,7 @@ bool asst::RoguelikeFormationTaskPlugin::verify(AsstMsg msg, const json::value& details) const { if (msg != AsstMsg::SubTaskCompleted - || details.get("subtask", std::string()) != "ProcessTask") { + || details.get("subtask", std::string()) != "ProcessTask") { return false; } @@ -22,7 +22,7 @@ bool asst::RoguelikeFormationTaskPlugin::verify(AsstMsg msg, const json::value& bool asst::RoguelikeFormationTaskPlugin::_run() { - RoguelikeFormationImageAnalyzer formation_analyzer(Ctrler.get_image()); + RoguelikeFormationImageAnalyzer formation_analyzer(m_ctrler->get_image()); if (!formation_analyzer.analyze()) { return false; } @@ -31,7 +31,7 @@ bool asst::RoguelikeFormationTaskPlugin::_run() if (oper.selected) { continue; } - Ctrler.click(oper.rect); + m_ctrler->click(oper.rect); } return true; diff --git a/src/MeoAssistant/StageDropsTaskPlugin.cpp b/src/MeoAssistant/StageDropsTaskPlugin.cpp index 178c9568bb..5cfe246aaa 100644 --- a/src/MeoAssistant/StageDropsTaskPlugin.cpp +++ b/src/MeoAssistant/StageDropsTaskPlugin.cpp @@ -64,7 +64,7 @@ bool asst::StageDropsTaskPlugin::recognize_drops() if (need_exit()) { return false; } - cv::Mat image = Ctrler.get_image(); + cv::Mat image = m_ctrler->get_image(); std::string res = Resrc.penguin().recognize(image); Log.trace("Results of penguin recognition:\n", res); m_cur_drops = json::parse(res).value(); diff --git a/src/MeoAssistant/UserConfiger.cpp b/src/MeoAssistant/UserConfiger.cpp deleted file mode 100644 index 853ca4cb8f..0000000000 --- a/src/MeoAssistant/UserConfiger.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "UserConfiger.h" - -#include - -#include - -constexpr static const char* EmulatorPathKey = "emulatorPath"; - -bool asst::UserConfiger::load(const std::string& filename) -{ - AbstractConfiger::load(filename); - m_filename = filename; - return true; -} - -bool asst::UserConfiger::set_emulator_path(const std::string& name, const std::string& path) -{ - m_emulators_path.emplace(name, path); - return save(); -} - -bool asst::UserConfiger::parse(const json::value& json) -{ - if (json.contains(EmulatorPathKey)) { - for (const auto& [name, path] : json.at(EmulatorPathKey).as_object()) { - m_emulators_path.emplace(name, path.as_string()); - } - } - m_raw_json = json; - - return true; -} - -bool asst::UserConfiger::save() -{ - for (const auto& [name, path] : m_emulators_path) { - m_raw_json[EmulatorPathKey][name] = path; - } - - std::ofstream ofs(m_filename, std::ios::out); - if (!ofs.is_open()) { - return false; - } - ofs << m_raw_json.format(); - ofs.close(); - - return true; -} \ No newline at end of file diff --git a/src/MeoAssistant/UserConfiger.h b/src/MeoAssistant/UserConfiger.h deleted file mode 100644 index 08364af540..0000000000 --- a/src/MeoAssistant/UserConfiger.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include "AbstractConfiger.h" - -#include -#include - -namespace asst -{ - class UserConfiger : public AbstractConfiger - { - public: - virtual ~UserConfiger() = default; - virtual bool load(const std::string& filename) override; - - bool set_emulator_path(const std::string& name, const std::string& path); - const std::string& get_emulator_path(const std::string& name) const noexcept - { - if (auto iter = m_emulators_path.find(name); - iter != m_emulators_path.cend()) { - return iter->second; - } - else { - static const std::string empty; - return empty; - } - } - const std::unordered_map& get_emulator_path() const noexcept - { - return m_emulators_path; - } - - protected: - virtual bool parse(const json::value& json) override; - bool save(); - - json::value m_raw_json; - std::string m_filename; - std::unordered_map m_emulators_path; - }; -} diff --git a/tools/TestCaller/main.cpp b/tools/TestCaller/main.cpp index 2ac73278a0..ac6b8c2983 100644 --- a/tools/TestCaller/main.cpp +++ b/tools/TestCaller/main.cpp @@ -18,7 +18,7 @@ int main(int argc, char** argv) return -1; } //auto ret = AsstCatchCustom(ptr, "127.0.0.1:5555"); - auto ret = AsstCatchDefault(ptr); + auto ret = AsstConnect(ptr, "adb", "127.0.0.1:5555", nullptr); if (!ret) { std::cout << "connect failed" << std::endl; if (ptr) { From b2baed17b181ec0cbbadf08aaa2fe693b33a7d0e Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 19 Feb 2022 21:59:20 +0800 Subject: [PATCH 2/7] =?UTF-8?q?fix.=E4=BF=AE=E5=A4=8D=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=90=8E=E7=9A=84=20controller=20=E6=88=AA=E5=9B=BE=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/AbstractTask.cpp | 1 + src/MeoAssistant/Assistant.cpp | 2 +- src/MeoAssistant/Controller.cpp | 133 +++++++++++----------- src/MeoAssistant/Controller.h | 2 +- src/MeoAssistant/StageDropsTaskPlugin.cpp | 2 +- 5 files changed, 69 insertions(+), 71 deletions(-) diff --git a/src/MeoAssistant/AbstractTask.cpp b/src/MeoAssistant/AbstractTask.cpp index 0423bd6f9b..d7326f8f56 100644 --- a/src/MeoAssistant/AbstractTask.cpp +++ b/src/MeoAssistant/AbstractTask.cpp @@ -148,6 +148,7 @@ void asst::AbstractTask::callback(AsstMsg msg, const json::value& detail) { for (TaskPluginPtr plugin : m_plugins) { plugin->set_exit_flag(m_exit_flag); + plugin->set_ctrler(m_ctrler); plugin->set_task_ptr(this); if (!plugin->verify(msg, detail)) { diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index f45f52ca5d..bb99b5e71b 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -221,7 +221,7 @@ bool asst::Assistant::append_recruit(unsigned max_times, const std::vector& if (!m_inited) { return false; } - static const std::string TaskChain = "Recruit"; + constexpr static const char* TaskChain = "Recruit"; auto recruit_task_ptr = std::make_shared(task_callback, (void*)this, TaskChain); recruit_task_ptr->set_max_times(max_times) diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 673ff61a9f..a8b98752cb 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -104,58 +104,58 @@ asst::Controller::~Controller() #endif } -asst::Rect asst::Controller::shaped_correct(const Rect & rect) const -{ - if (rect.empty() - || m_scale_size.first == 0 - || m_scale_size.second == 0) { - return rect; - } - // 明日方舟在异形屏上,有的地方是按比例缩放的,有的地方又是直接位移。没法整,这里简单粗暴一点截一个长条 - Rect dst = rect; - if (m_scale_size.first != WindowWidthDefault) { // 说明是宽屏 - if (rect.width <= WindowWidthDefault / 2) { - if (rect.x + rect.width <= WindowWidthDefault / 2) { // 整个矩形都在左半边 - dst.x = 0; - dst.width = m_scale_size.first / 2; - } - else if (rect.x >= WindowWidthDefault / 2) { // 整个矩形都在右半边 - dst.x = m_scale_size.first / 2; - dst.width = m_scale_size.first / 2; - } - else { // 整个矩形横跨了中线 - dst.x = 0; - dst.width = m_scale_size.first; - } - } - else { - dst.x = 0; - dst.width = m_scale_size.first; - } - } - else if (m_scale_size.second != WindowHeightDefault) { // 说明是偏方形屏 - if (rect.height <= WindowHeightDefault / 2) { - if (rect.y + rect.height <= WindowHeightDefault / 2) { // 整个矩形都在上半边 - dst.y = 0; - dst.height = m_scale_size.second / 2; - } - else if (rect.y >= WindowHeightDefault / 2) { // 整个矩形都在下半边 - dst.y = m_scale_size.second / 2; - dst.height = m_scale_size.second / 2; // 整个矩形横跨了中线 - } - else { - dst.y = 0; - dst.height = m_scale_size.second; - } - } - - else { - dst.y = 0; - dst.height = m_scale_size.second; - } - } - return dst; -} +//asst::Rect asst::Controller::shaped_correct(const Rect & rect) const +//{ +// if (rect.empty() +// || m_scale_size.first == 0 +// || m_scale_size.second == 0) { +// return rect; +// } +// // 明日方舟在异形屏上,有的地方是按比例缩放的,有的地方又是直接位移。没法整,这里简单粗暴一点截一个长条 +// Rect dst = rect; +// if (m_scale_size.first != WindowWidthDefault) { // 说明是宽屏 +// if (rect.width <= WindowWidthDefault / 2) { +// if (rect.x + rect.width <= WindowWidthDefault / 2) { // 整个矩形都在左半边 +// dst.x = 0; +// dst.width = m_scale_size.first / 2; +// } +// else if (rect.x >= WindowWidthDefault / 2) { // 整个矩形都在右半边 +// dst.x = m_scale_size.first / 2; +// dst.width = m_scale_size.first / 2; +// } +// else { // 整个矩形横跨了中线 +// dst.x = 0; +// dst.width = m_scale_size.first; +// } +// } +// else { +// dst.x = 0; +// dst.width = m_scale_size.first; +// } +// } +// else if (m_scale_size.second != WindowHeightDefault) { // 说明是偏方形屏 +// if (rect.height <= WindowHeightDefault / 2) { +// if (rect.y + rect.height <= WindowHeightDefault / 2) { // 整个矩形都在上半边 +// dst.y = 0; +// dst.height = m_scale_size.second / 2; +// } +// else if (rect.y >= WindowHeightDefault / 2) { // 整个矩形都在下半边 +// dst.y = m_scale_size.second / 2; +// dst.height = m_scale_size.second / 2; // 整个矩形横跨了中线 +// } +// else { +// dst.y = 0; +// dst.height = m_scale_size.second; +// } +// } +// +// else { +// dst.y = 0; +// dst.height = m_scale_size.second; +// } +// } +// return dst; +//} std::pair asst::Controller::get_scale_size() const noexcept { @@ -379,8 +379,6 @@ bool asst::Controller::screencap() { LogTraceFunction; - auto& adb = m_adb; - DecodeFunc decode_raw_with_gzip = [&](const std::vector& data) -> bool { auto unzip_data = gzip::decompress(data.data(), data.size()); Log.trace("unzip data size:", unzip_data.size()); @@ -401,7 +399,7 @@ bool asst::Controller::screencap() if (is_all_zero) { return false; } - cv::Mat temp = cv::Mat(m_width, m_height, CV_8UC4, unzip_data.data() + header_size); + cv::Mat temp(m_height, m_width, CV_8UC4, unzip_data.data() + header_size); if (temp.empty()) { return false; } @@ -421,15 +419,15 @@ bool asst::Controller::screencap() return true; }; - switch (adb.screencap_method) { + switch (m_adb.screencap_method) { case AdbProperty::ScreencapMethod::UnknownYet: { - if (screencap(adb.screencap_raw_with_gzip, decode_raw_with_gzip)) { - adb.screencap_method = AdbProperty::ScreencapMethod::RawWithGzip; + if (screencap(m_adb.screencap_raw_with_gzip, decode_raw_with_gzip)) { + m_adb.screencap_method = AdbProperty::ScreencapMethod::RawWithGzip; return true; } - else if (screencap(adb.screencap_encode, decode_encode)) { - adb.screencap_method = AdbProperty::ScreencapMethod::Encode; + else if (screencap(m_adb.screencap_encode, decode_encode)) { + m_adb.screencap_method = AdbProperty::ScreencapMethod::Encode; return true; } else { @@ -438,12 +436,12 @@ bool asst::Controller::screencap() } case AdbProperty::ScreencapMethod::RawWithGzip: { - return screencap(adb.screencap_raw_with_gzip, decode_raw_with_gzip); + return screencap(m_adb.screencap_raw_with_gzip, decode_raw_with_gzip); } break; case AdbProperty::ScreencapMethod::Encode: { - return screencap(adb.screencap_encode, decode_encode); + return screencap(m_adb.screencap_encode, decode_encode); } break; } @@ -453,7 +451,6 @@ bool asst::Controller::screencap() bool asst::Controller::screencap(const std::string & cmd, DecodeFunc decode_func) { - auto& adb = m_adb; auto ret = call_command(cmd); if (!ret || ret.value().empty()) { @@ -462,25 +459,25 @@ bool asst::Controller::screencap(const std::string & cmd, DecodeFunc decode_func } auto data = std::move(ret).value(); - if (adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::CRLF) { + if (m_adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::CRLF) { convert_lf(data); } if (decode_func(data)) { - if (adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) { - adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::LF; + if (m_adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) { + m_adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::LF; } return true; } else { Log.info("data is not empty, but image is empty"); - if (adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) { + if (m_adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) { Log.info("try to cvt lf"); convert_lf(data); if (decode_func(data)) { - adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::CRLF; + m_adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::CRLF; return true; } else { diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 1c05617dee..04de3d7f8b 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -50,7 +50,7 @@ namespace asst void wait(unsigned id) const noexcept; // 异形屏矫正 - Rect shaped_correct(const Rect& rect) const; + //Rect shaped_correct(const Rect& rect) const; std::pair get_scale_size() const noexcept; Controller& operator=(const Controller&) = delete; diff --git a/src/MeoAssistant/StageDropsTaskPlugin.cpp b/src/MeoAssistant/StageDropsTaskPlugin.cpp index 5cfe246aaa..0c0991dd77 100644 --- a/src/MeoAssistant/StageDropsTaskPlugin.cpp +++ b/src/MeoAssistant/StageDropsTaskPlugin.cpp @@ -64,7 +64,7 @@ bool asst::StageDropsTaskPlugin::recognize_drops() if (need_exit()) { return false; } - cv::Mat image = m_ctrler->get_image(); + const cv::Mat image = m_ctrler->get_image(); std::string res = Resrc.penguin().recognize(image); Log.trace("Results of penguin recognition:\n", res); m_cur_drops = json::parse(res).value(); From a72ac6c0055cd6656774e43e7bff4ed5127aec7a Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 19 Feb 2022 22:36:45 +0800 Subject: [PATCH 3/7] =?UTF-8?q?feat.=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?python=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/AsstCaller.h | 3 ++ src/MeoAssistant/AsstCaller.cpp | 10 ++++++ src/MeoAssistant/Logger.hpp | 5 +++ src/Python/interface.py | 55 ++++++++++++++++----------------- src/Python/sample.py | 2 +- 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/include/AsstCaller.h b/include/AsstCaller.h index 450e808ddb..cb5b7a22d0 100644 --- a/include/AsstCaller.h +++ b/include/AsstCaller.h @@ -41,6 +41,9 @@ extern "C" { bool ASSTAPI AsstCtrlerClick(asst::Assistant* p_asst, int x, int y, bool block); ASSTAPI_PORT const char* ASST_CALL AsstGetVersion(); + + void ASSTAPI AsstLog(asst::Assistant* p_asst, const char* level, const char* message); + #ifdef __cplusplus } #endif diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index 8dc7c779cf..fdb49ff277 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -8,6 +8,7 @@ #include "Assistant.h" #include "AsstDef.h" #include "Version.h" +#include "Logger.hpp" #if 0 #if _MSC_VER @@ -262,6 +263,15 @@ const char* AsstGetVersion() return asst::Version; } +void AsstLog(asst::Assistant* p_asst, const char* level, const char* message) +{ + if (p_asst == nullptr) { + return; + } + + asst::Log.log_with_custom_level(level, message); +} + bool AsstAppendDebug(asst::Assistant* p_asst) { if (p_asst == nullptr) { diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 95697ac18a..bd92be8360 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -61,6 +61,11 @@ namespace asst std::string_view level = "ERR"; log(level, std::forward(args)...); } + template + inline void log_with_custom_level(std::string_view level, Args&&... args) + { + log(level, std::forward(args)...); + } void flush() { std::unique_lock trace_lock(m_trace_mutex); diff --git a/src/Python/interface.py b/src/Python/interface.py index a3b614a002..87a6d301b2 100644 --- a/src/Python/interface.py +++ b/src/Python/interface.py @@ -38,29 +38,19 @@ class Asst: def __del__(self): self.__lib.AsstDestroy(self.__ptr) - def catch_default(self) -> bool: + def connect(self, adb_path: str, address: str, config: str = 'General'): """ - 连接配置文件中的默认选项(模拟器or安卓手机) + 连接设备 + + :params: + ``adb_path``: adb 程序的路径 + ``address``: adb 地址+端口 + ``config``: adb 配置,可参考 resource/config.json :return: 是否连接成功 """ - return self.__lib.AsstCatchDefault(self.__ptr) - - def catch_emulator(self) -> bool: - """ - 连接模拟器 - - :return: 是否连接成功 - """ - return self.__lib.AsstCatchEmulator(self.__ptr) - - def catch_custom(self, address: str) -> bool: - """ - 连接自定义设备 - - :return: 是否连接成功 - """ - return self.__lib.AsstCatchCustom(self.__ptr, address.encode('utf-8')) + return self.__lib.AsstConnect(self.__ptr, + adb_path.encode('utf-8'), address.encode('utf-8'), config.encode('utf-8')) def append_fight(self, stage: str, max_medicine: int, max_stone: int, max_times: int) -> bool: """ @@ -194,6 +184,17 @@ class Asst: return self.__lib.AsstSetPenguinId(self.__ptr, id.encode('utf-8')) + def log(self, level: str, message: str): + ''' + 打印日志 + + :params: + ``level``: 日志等级标签 + ``message``: 日志内容 + ''' + + return self.__lib.AsstLog(self.__ptr, level.encode('utf-8'), message.encode('utf-8')) + def get_version(self) -> str: """ 获取DLL版本号 @@ -209,15 +210,9 @@ class Asst: self.__lib.AsstDestroy.argtypes = (ctypes.c_void_p,) - self.__lib.AsstCatchDefault.restype = ctypes.c_bool - self.__lib.AsstCatchDefault.argtypes = (ctypes.c_void_p,) - - self.__lib.AsstCatchEmulator.restype = ctypes.c_bool - self.__lib.AsstCatchEmulator.argtypes = (ctypes.c_void_p,) - - self.__lib.AsstCatchCustom.restype = ctypes.c_bool - self.__lib.AsstCatchCustom.argtypes = ( - ctypes.c_void_p, ctypes.c_char_p,) + self.__lib.AsstConnect.restype = ctypes.c_bool + self.__lib.AsstConnect.argtypes = ( + ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,) self.__lib.AsstAppendFight.restype = ctypes.c_bool self.__lib.AsstAppendFight.argtypes = ( @@ -261,3 +256,7 @@ class Asst: ctypes.c_void_p, ctypes.c_char_p) self.__lib.AsstGetVersion.restype = ctypes.c_char_p + + self.__lib.AsstLog.restype = ctypes.c_none + self.__lib.AsstLog.argtypes = ( + ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p) diff --git a/src/Python/sample.py b/src/Python/sample.py index 7555af268a..a6ecb0617c 100644 --- a/src/Python/sample.py +++ b/src/Python/sample.py @@ -17,7 +17,7 @@ if __name__ == "__main__": print('version', asst.get_version()) - if asst.catch_custom('127.0.0.1:7555'): + if asst.connect('adb', '127.0.0.1:5555'): print('连接成功') else: print('连接失败') From 18217f4bed43e5c664cdb8c6310d0b1e00e4b652 Mon Sep 17 00:00:00 2001 From: MistEO Date: Mon, 21 Feb 2022 23:30:54 +0800 Subject: [PATCH 4/7] feat. JNI --- CMakeLists.txt | 10 ++++++ src/Java/jni/meoasst_jni.cpp | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/Java/jni/meoasst_jni.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 99248b9d0c..901c4cec36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 2.8) project(MeoAssistantArknights) +option(BUILD_JNI "build jni" OFF) option(BUILD_TEST "build a demo" OFF) include_directories(include 3rdparty/include) @@ -34,6 +35,15 @@ endif () target_link_libraries(MeoAssistant ${OpenCV} ${PaddleOCR_LIB} ${Penguin_LIB} ${ZLIB}) +if (BUILD_JNI) + find_package(JNI REQUIRED) + include_directories(${JNI_INCLUDE_DIRS}) + + set(JNI_SRC src/Java/jni/meoasst_jni.cpp) + add_library(MeoAssistantJni SHARED ${JNI_SRC}) + target_link_libraries(MeoAssistantJni MeoAssistant) +endif(BUILD_JNI) + if (BUILD_TEST) add_executable(test tools/TestCaller/main.cpp) target_link_libraries(test MeoAssistant) diff --git a/src/Java/jni/meoasst_jni.cpp b/src/Java/jni/meoasst_jni.cpp new file mode 100644 index 0000000000..49ac223eb5 --- /dev/null +++ b/src/Java/jni/meoasst_jni.cpp @@ -0,0 +1,62 @@ +#include + +#ifdef __ANDROID__ +#include + +#define LOG "maa_jni" +#define LOG_D(...) __android_log_print(ANDROID_LOG_DEBUG, LOG, __VA_ARGS__) +#define LOG_I(...) __android_log_print(ANDROID_LOG_INFO, LOG, __VA_ARGS__) +#define LOG_W(...) __android_log_print(ANDROID_LOG_WARN, LOG, __VA_ARGS__) +#define LOG_E(...) __android_log_print(ANDROID_LOG_ERROR, LOG, __VA_ARGS__) +#define LOG_F(...) __android_log_print(ANDROID_LOG_FATAL, LOG, __VA_ARGS__) +#else +#define LOG_D(...) +#define LOG_I(...) +#define LOG_W(...) +#define LOG_E(...) +#define LOG_F(...) +#endif + +#include "AsstCaller.h" + +#ifdef __cplusplus +extern "C" { +#endif + + JNIEXPORT jlong JNICALL Java_com_maa_create(JNIEnv* env, jclass c, jstring jstr_dirname) + { + LOG_D(__FUNCTION__); + + if (env == NULL) { + return (jlong)NULL; + } + + const char* cstr_dirname = NULL; + if (jstr_dirname) { + cstr_dirname = env->GetStringUTFChars(jstr_dirname, NULL); + } + + asst::Assistant* p_asst = AsstCreate(cstr_dirname); + LOG_D("after AsstCreate, p_asst = %p", p_asst); + + if (cstr_dirname) { + env->ReleaseStringUTFChars(jstr_dirname, cstr_dirname); + } + + return (jlong)p_asst; + } + + JNIEXPORT void JNICALL Java_com_maa_destroy(JNIEnv* env, jclass c, jlong jlong_asst) + { + LOG_D(__FUNCTION__); + + if (env == NULL) { + return; + } + + AsstDestroy((asst::Assistant*)jlong_asst); + } + +#ifdef __cplusplus +} +#endif From 5257ee212fdd04e615a8a95bc6f19415e6f441d6 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 22 Feb 2022 00:38:32 +0800 Subject: [PATCH 5/7] =?UTF-8?q?feat.=E6=96=B0=E5=A2=9E=20java=20sample=20?= =?UTF-8?q?=EF=BC=88=E4=BD=86=E5=A5=BD=E5=83=8F=E8=BF=98=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Java/.gitignore | 2 ++ src/Java/com/meo/asst/libmaa.java | 12 ++++++++++++ src/Java/jni/meoasst_jni.cpp | 4 ++-- src/Java/sample.java | 8 ++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/Java/.gitignore create mode 100644 src/Java/com/meo/asst/libmaa.java create mode 100644 src/Java/sample.java diff --git a/src/Java/.gitignore b/src/Java/.gitignore new file mode 100644 index 0000000000..8e9e794863 --- /dev/null +++ b/src/Java/.gitignore @@ -0,0 +1,2 @@ +*.class +*.jar diff --git a/src/Java/com/meo/asst/libmaa.java b/src/Java/com/meo/asst/libmaa.java new file mode 100644 index 0000000000..cba63927fe --- /dev/null +++ b/src/Java/com/meo/asst/libmaa.java @@ -0,0 +1,12 @@ +package com.meo.asst; + +public class libmaa { + + static { + // System.loadLibrary("MeoAssistant"); + System.loadLibrary("MeoAssistantJni"); + } + + public static native long create(String dirname); + public static native void destroy(long handle); +} diff --git a/src/Java/jni/meoasst_jni.cpp b/src/Java/jni/meoasst_jni.cpp index 49ac223eb5..773dd7dcdd 100644 --- a/src/Java/jni/meoasst_jni.cpp +++ b/src/Java/jni/meoasst_jni.cpp @@ -23,7 +23,7 @@ extern "C" { #endif - JNIEXPORT jlong JNICALL Java_com_maa_create(JNIEnv* env, jclass c, jstring jstr_dirname) + JNIEXPORT jlong JNICALL Java_com_meo_asst_libmaa_create(JNIEnv* env, jclass c, jstring jstr_dirname) { LOG_D(__FUNCTION__); @@ -46,7 +46,7 @@ extern "C" { return (jlong)p_asst; } - JNIEXPORT void JNICALL Java_com_maa_destroy(JNIEnv* env, jclass c, jlong jlong_asst) + JNIEXPORT void JNICALL Java_com_meo_asst_libmaa_destroy(JNIEnv* env, jclass c, jlong jlong_asst) { LOG_D(__FUNCTION__); diff --git a/src/Java/sample.java b/src/Java/sample.java new file mode 100644 index 0000000000..0f8c6f1109 --- /dev/null +++ b/src/Java/sample.java @@ -0,0 +1,8 @@ +import com.meo.asst.libmaa; + +public class sample { + public static void main(String[] args) { + long handle = libmaa.create("D:\\Code\\MeoAssistantArknights\\x64\\Release"); + libmaa.destroy(handle); + } +} From 037e11c75d7cedace543ba1f291193a2d328697c Mon Sep 17 00:00:00 2001 From: StupidBh <2623615250@qq.com> Date: Sat, 26 Feb 2022 18:10:08 +0800 Subject: [PATCH 6/7] Boring revision --- src/MeoAssistant/Assistant.cpp | 4 ++-- src/MeoAssistant/AsstUtils.hpp | 20 +++++++++++++------ src/MeoAssistant/BattleImageAnalyzer.cpp | 2 +- src/MeoAssistant/HashImageAnalyzer.cpp | 4 ++-- src/MeoAssistant/InfrastControlTask.cpp | 3 +-- src/MeoAssistant/InfrastOfficeTask.cpp | 3 +-- src/MeoAssistant/InfrastPowerTask.cpp | 3 +-- src/MeoAssistant/InfrastProductionTask.cpp | 13 ++++-------- src/MeoAssistant/OcrImageAnalyzer.cpp | 2 +- src/MeoAssistant/RecruitTask.cpp | 4 ++-- .../RoguelikeFormationImageAnalyzer.cpp | 2 +- src/MeoAssistant/TemplResource.cpp | 3 +-- 12 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index bb99b5e71b..c5f3852ca6 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -493,12 +493,12 @@ void Assistant::working_proc() std::unique_lock lock(m_mutex); if (!m_thread_idle && !m_tasks_queue.empty()) { - auto task_ptr = m_tasks_queue.front(); + const auto& task_ptr = m_tasks_queue.front(); m_tasks_queue.pop(); std::string cur_taskchain = task_ptr->get_task_chain(); std::string next_taskchain = m_tasks_queue.empty() ? std::string() : m_tasks_queue.front()->get_task_chain(); - json::value callback_json = json::object{ + json::value callback_json = json::object { { "taskchain", cur_taskchain }, { "pre_taskchain", pre_taskchain } }; diff --git a/src/MeoAssistant/AsstUtils.hpp b/src/MeoAssistant/AsstUtils.hpp index b1b6a65157..64a5079574 100644 --- a/src/MeoAssistant/AsstUtils.hpp +++ b/src/MeoAssistant/AsstUtils.hpp @@ -88,10 +88,14 @@ namespace asst memset(str, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, nullptr, nullptr); std::string strTemp = str; - if (wstr) + if (wstr) { delete[] wstr; - if (str) + wstr = nullptr; + } + if (str) { delete[] str; + str = nullptr; + } return strTemp; #else // Don't fucking use gbk in linux! return gbk_str; @@ -104,17 +108,21 @@ namespace asst const char* src_str = utf8_str.c_str(); int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0); wchar_t* wszGBK = new wchar_t[len + 1]; - memset(wszGBK, 0, len * 2 + 2); + memset(wszGBK, 0, len * 2LLU + 2LLU); MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, nullptr, 0, nullptr, nullptr); char* szGBK = new char[len + 1]; - memset(szGBK, 0, len + 1); + memset(szGBK, 0, len + 1LLU); WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, nullptr, nullptr); std::string strTemp(szGBK); - if (wszGBK) + if (wszGBK) { delete[] wszGBK; - if (szGBK) + wszGBK = nullptr; + } + if (szGBK) { delete[] szGBK; + szGBK = nullptr; + } return strTemp; #else // Don't fucking use gbk in linux! return utf8_str; diff --git a/src/MeoAssistant/BattleImageAnalyzer.cpp b/src/MeoAssistant/BattleImageAnalyzer.cpp index 8f244cef4c..90fc1b6c6e 100644 --- a/src/MeoAssistant/BattleImageAnalyzer.cpp +++ b/src/MeoAssistant/BattleImageAnalyzer.cpp @@ -294,7 +294,7 @@ bool asst::BattleImageAnalyzer::hp_analyze() range_upper = cv::Scalar(h_u, s_u, v_u); std::unordered_map num_hashs; for (auto&& num : NumName) { - auto hashs_vec = std::dynamic_pointer_cast( + const auto &hashs_vec = std::dynamic_pointer_cast( Task.get("BattleHp" + num))->hashs; for (size_t i = 0; i != hashs_vec.size(); ++i) { num_hashs.emplace(num + "_" + std::to_string(i), hashs_vec.at(i)); diff --git a/src/MeoAssistant/HashImageAnalyzer.cpp b/src/MeoAssistant/HashImageAnalyzer.cpp index c61aee7b4c..ed6318df5c 100644 --- a/src/MeoAssistant/HashImageAnalyzer.cpp +++ b/src/MeoAssistant/HashImageAnalyzer.cpp @@ -1,4 +1,4 @@ -#include "HashImageAnalyzer.h" +#include "HashImageAnalyzer.h" #include "AsstUtils.hpp" #include "Logger.hpp" @@ -163,7 +163,7 @@ int asst::HashImageAnalyzer::hamming(std::string hash1, std::string hash2) unsigned long long x = strtoull(hash1.substr(i, 16).c_str(), nullptr, 16) ^ strtoull(hash2.substr(i, 16).c_str(), nullptr, 16); while (x) { - dist++; + ++dist; x = x & (x - 1); } } diff --git a/src/MeoAssistant/InfrastControlTask.cpp b/src/MeoAssistant/InfrastControlTask.cpp index db2540389f..09d2fa6c1b 100644 --- a/src/MeoAssistant/InfrastControlTask.cpp +++ b/src/MeoAssistant/InfrastControlTask.cpp @@ -26,8 +26,7 @@ bool asst::InfrastControlTask::_run() swipe_to_the_left_of_operlist(); optimal_calc(); - bool ret = opers_choose(); - if (!ret) { + if (!opers_choose()) { m_all_available_opers.clear(); swipe_to_the_left_of_operlist(); continue; diff --git a/src/MeoAssistant/InfrastOfficeTask.cpp b/src/MeoAssistant/InfrastOfficeTask.cpp index 441f405b52..5b6fef9bf0 100644 --- a/src/MeoAssistant/InfrastOfficeTask.cpp +++ b/src/MeoAssistant/InfrastOfficeTask.cpp @@ -51,8 +51,7 @@ bool asst::InfrastOfficeTask::_run() } if (need_shift) { optimal_calc(); - bool ret = opers_choose(); - if (!ret) { + if (!opers_choose()) { m_all_available_opers.clear(); swipe_to_the_left_of_operlist(); continue; diff --git a/src/MeoAssistant/InfrastPowerTask.cpp b/src/MeoAssistant/InfrastPowerTask.cpp index cd1bd9ff50..fbcc9ba619 100644 --- a/src/MeoAssistant/InfrastPowerTask.cpp +++ b/src/MeoAssistant/InfrastPowerTask.cpp @@ -62,8 +62,7 @@ bool asst::InfrastPowerTask::_run() } if (need_shift) { optimal_calc(); - bool ret = opers_choose(); - if (!ret) { + if (!opers_choose()) { m_all_available_opers.clear(); swipe_to_the_left_of_operlist(); continue; diff --git a/src/MeoAssistant/InfrastProductionTask.cpp b/src/MeoAssistant/InfrastProductionTask.cpp index 951b4c9518..3b9813d3c9 100644 --- a/src/MeoAssistant/InfrastProductionTask.cpp +++ b/src/MeoAssistant/InfrastProductionTask.cpp @@ -37,10 +37,7 @@ void asst::InfrastProductionTask::set_product(std::string product_name) noexcept bool asst::InfrastProductionTask::shift_facility_list() { LogTraceFunction; - if (!facility_list_detect()) { - return false; - } - if (need_exit()) { + if (!facility_list_detect() || need_exit()) { return false; } const auto tab_task_ptr = Task.get("InfrastFacilityListTab" + facility_name()); @@ -124,8 +121,7 @@ bool asst::InfrastProductionTask::shift_facility_list() opers_detect(); } optimal_calc(); - bool ret = opers_choose(); - if (!ret) { + if (!opers_choose()) { m_all_available_opers.clear(); swipe_to_the_left_of_operlist(2); continue; @@ -254,7 +250,7 @@ bool asst::InfrastProductionTask::optimal_calc() std::unordered_map skills_num; for (int i = 0; i != m_all_available_opers.size(); ++i) { - auto comb = all_avaliable_combs.at(i); + const auto& comb = all_avaliable_combs.at(i); bool out_of_num = false; for (auto&& skill : comb.skills) { @@ -451,7 +447,6 @@ bool asst::InfrastProductionTask::opers_choose() LogTraceFunction; bool has_error = false; - int count = 0; auto& facility_info = Resrc.infrast().get_facility_info(facility_name()); int cur_max_num_of_opers = facility_info.max_num_of_opers - m_cur_num_of_lokced_opers; @@ -495,7 +490,7 @@ bool asst::InfrastProductionTask::opers_choose() }); cur_all_opers.erase(remove_iter, cur_all_opers.end()); Log.trace("after mood filter, opers size:", cur_all_opers.size()); - + int count = 0; for (auto opt_iter = m_optimal_combs.begin(); opt_iter != m_optimal_combs.end();) { Log.trace("to find", opt_iter->skills.begin()->names.front()); auto find_iter = std::find_if( diff --git a/src/MeoAssistant/OcrImageAnalyzer.cpp b/src/MeoAssistant/OcrImageAnalyzer.cpp index cf151c2dcc..8164b8eacd 100644 --- a/src/MeoAssistant/OcrImageAnalyzer.cpp +++ b/src/MeoAssistant/OcrImageAnalyzer.cpp @@ -48,7 +48,7 @@ bool asst::OcrImageAnalyzer::analyze() preds_vec.emplace_back(m_pred); TextRectProc all_pred = [&](TextRect& tr) -> bool { - for (auto pred : preds_vec) { + for (const auto &pred : preds_vec) { if (pred && !pred(tr)) { return false; } diff --git a/src/MeoAssistant/RecruitTask.cpp b/src/MeoAssistant/RecruitTask.cpp index 3feb9bc288..50ea4df009 100644 --- a/src/MeoAssistant/RecruitTask.cpp +++ b/src/MeoAssistant/RecruitTask.cpp @@ -44,7 +44,7 @@ bool RecruitTask::_run() } json::value info = basic_info(); info["what"] = "RecruitTagsDetected"; - info["details"] = json::object{ + info["details"] = json::object { { "tags", json::array(all_tags_json_vector) } }; callback(AsstMsg::SubTaskExtraInfo, info); @@ -56,7 +56,7 @@ bool RecruitTask::_run() auto special_iter = std::find_first_of(SpecialTags.cbegin(), SpecialTags.cend(), all_tags_name.cbegin(), all_tags_name.cend()); if (special_iter != SpecialTags.cend()) { info["what"] = "RecruitSpecialTag"; - info["details"] = json::object{ + info["details"] = json::object { { "tag", *special_iter } }; callback(AsstMsg::SubTaskExtraInfo, info); diff --git a/src/MeoAssistant/RoguelikeFormationImageAnalyzer.cpp b/src/MeoAssistant/RoguelikeFormationImageAnalyzer.cpp index 9dc5d0dc6b..478b1f2bc9 100644 --- a/src/MeoAssistant/RoguelikeFormationImageAnalyzer.cpp +++ b/src/MeoAssistant/RoguelikeFormationImageAnalyzer.cpp @@ -13,7 +13,7 @@ bool asst::RoguelikeFormationImageAnalyzer::analyze() return false; } opers_analyzer.sort_result(); - const auto all_opers = opers_analyzer.get_result(); + const auto& all_opers = opers_analyzer.get_result(); for (const MatchRect& oper_mr : all_opers) { Oper oper; oper.rect = oper_mr.rect; diff --git a/src/MeoAssistant/TemplResource.cpp b/src/MeoAssistant/TemplResource.cpp index 4566327ada..3c22eaa893 100644 --- a/src/MeoAssistant/TemplResource.cpp +++ b/src/MeoAssistant/TemplResource.cpp @@ -40,8 +40,7 @@ const cv::Mat asst::TemplResource::get_templ(const std::string& key) const noexc return iter->second; } else { - const static cv::Mat empty; - return empty; + return cv::Mat(); } } From 92af41c2d5f8ebfff02fd74610c89e3189a29ded Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 26 Feb 2022 18:57:25 +0800 Subject: [PATCH 7/7] =?UTF-8?q?fix.=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Assistant.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index c5f3852ca6..49ce1f5488 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -493,7 +493,7 @@ void Assistant::working_proc() std::unique_lock lock(m_mutex); if (!m_thread_idle && !m_tasks_queue.empty()) { - const auto& task_ptr = m_tasks_queue.front(); + const auto task_ptr = m_tasks_queue.front(); m_tasks_queue.pop(); std::string cur_taskchain = task_ptr->get_task_chain();