From 8f1298caf24a12935eb90cf901989be05e7da751 Mon Sep 17 00:00:00 2001 From: MistEO Date: Thu, 29 Jul 2021 21:35:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=8C=E5=B0=BE=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MeoAssistance/src/Assistance.cpp | 782 +++++++++++++++---------------- 1 file changed, 391 insertions(+), 391 deletions(-) diff --git a/MeoAssistance/src/Assistance.cpp b/MeoAssistance/src/Assistance.cpp index 34aecfff35..d4acfca51c 100644 --- a/MeoAssistance/src/Assistance.cpp +++ b/MeoAssistance/src/Assistance.cpp @@ -1,210 +1,210 @@ -#include "Assistance.h" - -#include "WinMacro.h" -#include "Configer.h" -#include "Identify.h" -#include "Logger.hpp" -#include "AsstAux.h" - -#include - -#include -#include - -using namespace asst; - -Assistance::Assistance() -{ - DebugTraceFunction; - - m_configer.load(GetResourceDir() + "config.json"); - m_recruit_configer.load(GetResourceDir() + "operInfo.json"); - - m_pIder = std::make_shared(); - for (const auto& [name, info] : m_configer.m_tasks) - { - m_pIder->add_image(name, GetResourceDir() + "template\\" + info.template_filename); - } - m_pIder->set_use_cache(m_configer.m_options.identify_cache); - - m_pIder->set_ocr_param(m_configer.m_options.ocr_gpu_index, m_configer.m_options.ocr_thread_number); - m_pIder->ocr_init_models(GetResourceDir() + "OcrLiteNcnn\\models\\"); - - m_working_thread = std::thread(working_proc, this); -} - -Assistance::~Assistance() -{ - DebugTraceFunction; - - //if (m_pWindow != nullptr) { - // m_pWindow->showWindow(); - //} - - m_thread_exit = true; - m_thread_running = false; - m_condvar.notify_all(); - - if (m_working_thread.joinable()) { - m_working_thread.join(); - } -} - -std::optional Assistance::catch_emulator(const std::string& emulator_name) -{ - DebugTraceFunction; - - stop(); - - auto create_handles = [&](const EmulatorInfo& info) -> bool { - m_pWindow = std::make_shared(info, HandleType::Window); - m_pView = std::make_shared(info, HandleType::View); - m_pCtrl = std::make_shared(info, HandleType::Control); - return m_pWindow->captured() && m_pView->captured() && m_pCtrl->captured(); - }; - - bool ret = false; - std::string cor_name = emulator_name; - - std::unique_lock lock(m_mutex); - - // 自动匹配模拟器,逐个找 - if (emulator_name.empty()) { - for (const auto& [name, info] : m_configer.m_handles) - { - ret = create_handles(info); - if (ret) { - cor_name = name; - break; - } - } - } - else { // 指定的模拟器 - ret = create_handles(m_configer.m_handles[emulator_name]); - } - if (ret && m_pWindow->showWindow() && m_pWindow->resizeWindow()) { - m_inited = true; - return cor_name; - } - else { - m_inited = false; - return std::nullopt; - } -} - -void Assistance::start(const std::string& task) -{ - DebugTraceFunction; - DebugTrace("Start |", task); - - - if (m_thread_running || !m_inited) { - return; - } - - std::unique_lock lock(m_mutex); - m_configer.clear_exec_times(); - - m_pIder->clear_cache(); - m_next_tasks.clear(); - m_next_tasks.emplace_back(task); - m_thread_running = true; - m_condvar.notify_one(); -} - -void Assistance::stop(bool block) -{ - DebugTraceFunction; - DebugTrace("Stop |", block ? "block" : "non block"); - - std::unique_lock lock; - if (block) { // 外部调用 - lock = std::unique_lock(m_mutex); - m_configer.clear_exec_times(); - } - m_thread_running = false; - m_next_tasks.clear(); - m_pIder->clear_cache(); -} - -bool Assistance::set_param(const std::string& type, const std::string& param, const std::string& value) -{ - DebugTraceFunction; - DebugTrace("SetParam |", type, param, value); - - std::unique_lock lock(m_mutex); - return m_configer.set_param(type, param, value); -} - -std::optional Assistance::get_param(const std::string& type, const std::string& param) -{ - // DebugTraceFunction; - if (type == "status") { - if (param == "running") { - return std::to_string(m_thread_running); - } - else { - return std::nullopt; - } - } - - std::unique_lock lock(m_mutex); - return m_configer.get_param(type, param); -} - -bool asst::Assistance::print_window(const std::string& filename, bool block) -{ - DebugTraceFunction; - DebugTrace("print_window |", block ? "block" : "non block"); - - std::unique_lock lock; - if (block) { // 外部调用 - lock = std::unique_lock(m_mutex); - } - - const cv::Mat& image = get_format_image(); - // 保存的截图额外再裁剪掉一圈,不然企鹅物流识别不出来 - int offset = m_configer.m_options.print_window_crop_offset; - cv::Rect rect(offset, offset, image.cols - offset * 2, image.rows - offset * 2); - bool ret = cv::imwrite(filename.c_str(), image(rect)); - - if (ret) { - DebugTraceInfo("PrintWindow to", filename); - } - else { - DebugTraceError("PrintWindow error", filename); - } - return ret; -} - -bool asst::Assistance::find_text_and_click(const std::string& text, bool block) -{ - DebugTraceFunction; - DebugTrace("find_text_and_click |", Utf8ToGbk(text), block ? "block" : "non block"); - - std::unique_lock lock; - if (block) { // 外部调用 - lock = std::unique_lock(m_mutex); - } - const cv::Mat& image = get_format_image(); - std::optional&& result = m_pIder->find_text(image, text); - - if (!result) { - DebugTrace("Cannot found", Utf8ToGbk(text)); - return false; - } - - set_control_scale(image.cols, image.rows); - return m_pCtrl->click(result.value()); +#include "Assistance.h" + +#include "WinMacro.h" +#include "Configer.h" +#include "Identify.h" +#include "Logger.hpp" +#include "AsstAux.h" + +#include + +#include +#include + +using namespace asst; + +Assistance::Assistance() +{ + DebugTraceFunction; + + m_configer.load(GetResourceDir() + "config.json"); + m_recruit_configer.load(GetResourceDir() + "operInfo.json"); + + m_pIder = std::make_shared(); + for (const auto& [name, info] : m_configer.m_tasks) + { + m_pIder->add_image(name, GetResourceDir() + "template\\" + info.template_filename); + } + m_pIder->set_use_cache(m_configer.m_options.identify_cache); + + m_pIder->set_ocr_param(m_configer.m_options.ocr_gpu_index, m_configer.m_options.ocr_thread_number); + m_pIder->ocr_init_models(GetResourceDir() + "OcrLiteNcnn\\models\\"); + + m_working_thread = std::thread(working_proc, this); +} + +Assistance::~Assistance() +{ + DebugTraceFunction; + + //if (m_pWindow != nullptr) { + // m_pWindow->showWindow(); + //} + + m_thread_exit = true; + m_thread_running = false; + m_condvar.notify_all(); + + if (m_working_thread.joinable()) { + m_working_thread.join(); + } +} + +std::optional Assistance::catch_emulator(const std::string& emulator_name) +{ + DebugTraceFunction; + + stop(); + + auto create_handles = [&](const EmulatorInfo& info) -> bool { + m_pWindow = std::make_shared(info, HandleType::Window); + m_pView = std::make_shared(info, HandleType::View); + m_pCtrl = std::make_shared(info, HandleType::Control); + return m_pWindow->captured() && m_pView->captured() && m_pCtrl->captured(); + }; + + bool ret = false; + std::string cor_name = emulator_name; + + std::unique_lock lock(m_mutex); + + // 自动匹配模拟器,逐个找 + if (emulator_name.empty()) { + for (const auto& [name, info] : m_configer.m_handles) + { + ret = create_handles(info); + if (ret) { + cor_name = name; + break; + } + } + } + else { // 指定的模拟器 + ret = create_handles(m_configer.m_handles[emulator_name]); + } + if (ret && m_pWindow->showWindow() && m_pWindow->resizeWindow()) { + m_inited = true; + return cor_name; + } + else { + m_inited = false; + return std::nullopt; + } +} + +void Assistance::start(const std::string& task) +{ + DebugTraceFunction; + DebugTrace("Start |", task); + + + if (m_thread_running || !m_inited) { + return; + } + + std::unique_lock lock(m_mutex); + m_configer.clear_exec_times(); + + m_pIder->clear_cache(); + m_next_tasks.clear(); + m_next_tasks.emplace_back(task); + m_thread_running = true; + m_condvar.notify_one(); +} + +void Assistance::stop(bool block) +{ + DebugTraceFunction; + DebugTrace("Stop |", block ? "block" : "non block"); + + std::unique_lock lock; + if (block) { // 外部调用 + lock = std::unique_lock(m_mutex); + m_configer.clear_exec_times(); + } + m_thread_running = false; + m_next_tasks.clear(); + m_pIder->clear_cache(); +} + +bool Assistance::set_param(const std::string& type, const std::string& param, const std::string& value) +{ + DebugTraceFunction; + DebugTrace("SetParam |", type, param, value); + + std::unique_lock lock(m_mutex); + return m_configer.set_param(type, param, value); +} + +std::optional Assistance::get_param(const std::string& type, const std::string& param) +{ + // DebugTraceFunction; + if (type == "status") { + if (param == "running") { + return std::to_string(m_thread_running); + } + else { + return std::nullopt; + } + } + + std::unique_lock lock(m_mutex); + return m_configer.get_param(type, param); +} + +bool asst::Assistance::print_window(const std::string& filename, bool block) +{ + DebugTraceFunction; + DebugTrace("print_window |", block ? "block" : "non block"); + + std::unique_lock lock; + if (block) { // 外部调用 + lock = std::unique_lock(m_mutex); + } + + const cv::Mat& image = get_format_image(); + // 保存的截图额外再裁剪掉一圈,不然企鹅物流识别不出来 + int offset = m_configer.m_options.print_window_crop_offset; + cv::Rect rect(offset, offset, image.cols - offset * 2, image.rows - offset * 2); + bool ret = cv::imwrite(filename.c_str(), image(rect)); + + if (ret) { + DebugTraceInfo("PrintWindow to", filename); + } + else { + DebugTraceError("PrintWindow error", filename); + } + return ret; +} + +bool asst::Assistance::find_text_and_click(const std::string& text, bool block) +{ + DebugTraceFunction; + DebugTrace("find_text_and_click |", Utf8ToGbk(text), block ? "block" : "non block"); + + std::unique_lock lock; + if (block) { // 外部调用 + lock = std::unique_lock(m_mutex); + } + const cv::Mat& image = get_format_image(); + std::optional&& result = m_pIder->find_text(image, text); + + if (!result) { + DebugTrace("Cannot found", Utf8ToGbk(text)); + return false; + } + + set_control_scale(image.cols, image.rows); + return m_pCtrl->click(result.value()); } void asst::Assistance::find_and_clac_tags(bool need_click) { - DebugTraceFunction; - - const cv::Mat& image = get_format_image(); - set_control_scale(image.cols, image.rows); - + DebugTraceFunction; + + const cv::Mat& image = get_format_image(); + set_control_scale(image.cols, image.rows); + std::vector