From 155f7bc79a5630dc747fffb1b5dfc39b44beba9d Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 1 Aug 2021 02:22:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E9=87=8D=E6=9E=84=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E7=BA=BF=E7=A8=8B=EF=BC=8C=E4=B8=B4=E6=97=B6=E4=BF=9D?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MeoAssistance/MeoAssistance.vcxproj | 2 + MeoAssistance/MeoAssistance.vcxproj.filters | 6 + MeoAssistance/include/Assistance.h | 15 +- MeoAssistance/include/AsstDef.h | 25 +- MeoAssistance/include/Configer.h | 8 +- MeoAssistance/include/Identify.h | 2 +- MeoAssistance/include/Task.h | 87 +++++++ MeoAssistance/src/Assistance.cpp | 252 +++++++------------- MeoAssistance/src/Configer.cpp | 26 +- MeoAssistance/src/Identify.cpp | 4 +- MeoAssistance/src/Task.cpp | 219 +++++++++++++++++ MeoAssistance/src/WinMacro.cpp | 2 +- resource/config.json | 22 +- 13 files changed, 448 insertions(+), 222 deletions(-) create mode 100644 MeoAssistance/include/Task.h create mode 100644 MeoAssistance/src/Task.cpp diff --git a/MeoAssistance/MeoAssistance.vcxproj b/MeoAssistance/MeoAssistance.vcxproj index 29c2d71cf2..8b72f84b00 100644 --- a/MeoAssistance/MeoAssistance.vcxproj +++ b/MeoAssistance/MeoAssistance.vcxproj @@ -28,6 +28,7 @@ + @@ -38,6 +39,7 @@ + diff --git a/MeoAssistance/MeoAssistance.vcxproj.filters b/MeoAssistance/MeoAssistance.vcxproj.filters index 904727b4d8..4e878ad2b8 100644 --- a/MeoAssistance/MeoAssistance.vcxproj.filters +++ b/MeoAssistance/MeoAssistance.vcxproj.filters @@ -51,6 +51,9 @@ 头文件 + + 头文件 + @@ -74,6 +77,9 @@ 源文件 + + 源文件 + diff --git a/MeoAssistance/include/Assistance.h b/MeoAssistance/include/Assistance.h index b194ffd2a5..20d4bd6fea 100644 --- a/MeoAssistance/include/Assistance.h +++ b/MeoAssistance/include/Assistance.h @@ -6,11 +6,13 @@ #include #include #include +#include #include "AsstPort.h" #include "AsstDef.h" #include "Configer.h" #include "RecruitConfiger.h" +#include "Task.h" namespace cv { class Mat; @@ -46,17 +48,19 @@ namespace asst { open_recruit(const std::vector& required_level, bool set_time = true); private: static void working_proc(Assistance* pThis); + static void task_callback(TaskMsg msg, const std::string& detail_json = std::string()); cv::Mat get_format_image(); void set_control_scale(int cur_width, int cur_height); + void clear_exec_times(); // for debug bool find_text_and_click(const std::string& text, bool block = true); - std::shared_ptr m_pWindow = nullptr; - std::shared_ptr m_pView = nullptr; - std::shared_ptr m_pCtrl = nullptr; - std::shared_ptr m_pIder = nullptr; + std::shared_ptr m_window_ptr = nullptr; + std::shared_ptr m_view_ptr = nullptr; + std::shared_ptr m_control_ptr = nullptr; + std::shared_ptr m_identify_ptr = nullptr; bool m_inited = false; std::thread m_working_thread; @@ -64,8 +68,9 @@ namespace asst { std::condition_variable m_condvar; bool m_thread_exit = false; bool m_thread_running = false; - std::vector m_next_tasks; + std::queue> m_tasks_queue; + std::unordered_map m_all_tasks_info; Configer m_configer; RecruitConfiger m_recruit_configer; }; diff --git a/MeoAssistance/include/AsstDef.h b/MeoAssistance/include/AsstDef.h index b461172509..c58575ab0c 100644 --- a/MeoAssistance/include/AsstDef.h +++ b/MeoAssistance/include/AsstDef.h @@ -27,15 +27,14 @@ namespace asst { enum class TaskType { Invalid = 0, - BasicClick = 1, - DoNothing = 2, - Stop = 4, - PrintWindow, - ClickSelf = 8 | BasicClick, - ClickRect = 16 | BasicClick, - ClickRand = 32 | BasicClick, - Ocr = 64, - OcrAndClick = Ocr | BasicClick + BasicClick = 0x100, + ClickSelf = BasicClick | 1, // ģλ + ClickRect = BasicClick | 2, // ָ + ClickRand = BasicClick | 4, // + DoNothing = 0x200, // ʲô + Stop = 0x400, // ֹͣ߳ + PrintWindow = 0x800, // ͼ + OpenRecruit = 0x1000 // ļ }; static bool operator&(const TaskType& lhs, const TaskType& rhs) { @@ -51,7 +50,8 @@ namespace asst { {TaskType::ClickRand, "ClickRand"}, {TaskType::DoNothing, "DoNothing"}, {TaskType::Stop, "Stop"}, - {TaskType::PrintWindow, "PrintWindow"} + {TaskType::PrintWindow, "PrintWindow"}, + {TaskType::OpenRecruit, "OpenRecruit"} }; return os << _type_name.at(task); } @@ -147,9 +147,10 @@ namespace asst { }; struct TaskInfo { + std::string name; // std::string template_filename; // ƥģͼƬļ - double threshold = 0; // ģƥֵ - double cache_threshold = 0; // ֱͼȽֵ + double templ_threshold = 0; // ģƥֵ + double hist_threshold = 0; // ֱͼȽֵ TaskType type = TaskType::Invalid; // std::vector next; // һܵб int exec_times = 0; // ִ˶ٴ diff --git a/MeoAssistance/include/Configer.h b/MeoAssistance/include/Configer.h index 73dc391b91..2a521d8e32 100644 --- a/MeoAssistance/include/Configer.h +++ b/MeoAssistance/include/Configer.h @@ -22,19 +22,17 @@ namespace asst { bool set_param(const std::string& type, const std::string& param, const std::string& value); std::optional get_param(const std::string& type, const std::string& param); - void clear_exec_times(); - Configer& operator=(const Configer& rhs) = default; Configer& operator=(Configer&& rhs) noexcept = default; constexpr static int DefaultWindowWidth = 1280; constexpr static int DefaultWindowHeight = 720; - constexpr static double DefaultThreshold = 0.9; - constexpr static double DefaultCacheThreshold = 0.9; + constexpr static double Defaulttempl_threshold = 0.9; + constexpr static double DefaultCachetempl_threshold = 0.9; std::string m_version; Options m_options; - std::unordered_map m_tasks; + std::unordered_map m_all_tasks_info; std::unordered_map m_handles; std::unordered_map m_ocr_replace; diff --git a/MeoAssistance/include/Identify.h b/MeoAssistance/include/Identify.h index 9ee22aae83..353f982b62 100644 --- a/MeoAssistance/include/Identify.h +++ b/MeoAssistance/include/Identify.h @@ -27,7 +27,7 @@ namespace asst { bool add_image(const std::string& name, const std::string& path); // return tuple< algorithmType, suitability, matched asst::rect> - std::tuple find_image(const cv::Mat& image, const std::string& templ, double threshold = 0.99); + std::tuple find_image(const cv::Mat& image, const std::string& templ, double templ_threshold = 0.99); void clear_cache(); diff --git a/MeoAssistance/include/Task.h b/MeoAssistance/include/Task.h new file mode 100644 index 0000000000..0c897b27be --- /dev/null +++ b/MeoAssistance/include/Task.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include + +#include "AsstDef.h" + +namespace cv +{ + class Mat; +} + +namespace asst { + + class WinMacro; + class Identify; + class Configer; + + enum class TaskMsg { + /* Error Msg */ + PtrIsNull, + ImageIsEmpty, + WindowMinimized, + /* Info Msg */ + TaskMatched, + ReachedLimit, + ReadyToSleep, + TaskCompleted, + MissionStop + /* Trace Msg */ + // Todo + }; + + typedef void (*TaskCallback)(TaskMsg msg, const std::string& detail_json); + + class AbstractTask + { + public: + AbstractTask(TaskCallback callback); + ~AbstractTask() = default; + AbstractTask(const AbstractTask&) = default; + AbstractTask(AbstractTask&&) = default; + + virtual void set_ptr( + std::shared_ptr window_ptr, + std::shared_ptr view_ptr, + std::shared_ptr control_ptr, + std::shared_ptr identify_ptr); + virtual bool run() = 0; + + virtual void set_exit_flag(bool* exit_flag); + protected: + virtual cv::Mat get_format_image(); + virtual bool set_control_scale(int cur_width, int cur_height); + virtual void sleep(unsigned millisecond); + + std::shared_ptr m_window_ptr = nullptr; + std::shared_ptr m_view_ptr = nullptr; + std::shared_ptr m_control_ptr = nullptr; + std::shared_ptr m_identify_ptr = nullptr; + + TaskCallback m_callback = NULL; + bool* m_exit_flag = NULL; + }; + + class MatchTask : public AbstractTask + { + public: + MatchTask(TaskCallback callback, + std::unordered_map* all_tasks_ptr, + Configer* configer_ptr); + + virtual bool run() override; + + virtual void set_tasks(const std::vector& cur_tasks_name) { m_cur_tasks_name = cur_tasks_name; } + + protected: + bool match_image(TaskInfo * task_info, asst::Rect* matched_rect = NULL); + void exec_click_task(TaskInfo& task, const asst::Rect& matched_rect); + + std::unordered_map* m_all_tasks_ptr = NULL; + Configer* m_configer_ptr = NULL; + std::vector m_cur_tasks_name; + }; +} diff --git a/MeoAssistance/src/Assistance.cpp b/MeoAssistance/src/Assistance.cpp index 5d0705a040..971c6df04b 100644 --- a/MeoAssistance/src/Assistance.cpp +++ b/MeoAssistance/src/Assistance.cpp @@ -5,6 +5,8 @@ #include "Identify.h" #include "Logger.hpp" #include "AsstAux.h" +#include "Task.h" +#include "json.h" #include @@ -18,17 +20,18 @@ Assistance::Assistance() DebugTraceFunction; m_configer.load(GetResourceDir() + "config.json"); + m_all_tasks_info = std::move(m_configer.m_all_tasks_info); m_recruit_configer.load(GetResourceDir() + "operInfo.json"); - m_pIder = std::make_shared(); - for (const auto& [name, info] : m_configer.m_tasks) + m_identify_ptr = std::make_shared(); + for (const auto& [name, info] : m_all_tasks_info) { - m_pIder->add_image(name, GetResourceDir() + "template\\" + info.template_filename); + m_identify_ptr->add_image(name, GetResourceDir() + "template\\" + info.template_filename); } - m_pIder->set_use_cache(m_configer.m_options.identify_cache); + m_identify_ptr->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_identify_ptr->set_ocr_param(m_configer.m_options.ocr_gpu_index, m_configer.m_options.ocr_thread_number); + m_identify_ptr->ocr_init_models(GetResourceDir() + "OcrLiteNcnn\\models\\"); m_working_thread = std::thread(working_proc, this); } @@ -37,8 +40,8 @@ Assistance::~Assistance() { DebugTraceFunction; - //if (m_pWindow != nullptr) { - // m_pWindow->showWindow(); + //if (m_window_ptr != nullptr) { + // m_window_ptr->showWindow(); //} m_thread_exit = true; @@ -57,10 +60,10 @@ std::optional Assistance::catch_emulator(const std::string& emulato 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(); + m_window_ptr = std::make_shared(info, HandleType::Window); + m_view_ptr = std::make_shared(info, HandleType::View); + m_control_ptr = std::make_shared(info, HandleType::Control); + return m_window_ptr->captured() && m_view_ptr->captured() && m_control_ptr->captured(); }; bool ret = false; @@ -82,7 +85,7 @@ std::optional Assistance::catch_emulator(const std::string& emulato else { // ָģ ret = create_handles(m_configer.m_handles[emulator_name]); } - if (ret && m_pWindow->showWindow() && m_pWindow->resizeWindow()) { + if (ret && m_window_ptr->showWindow() && m_window_ptr->resizeWindow()) { m_inited = true; return cor_name; } @@ -96,18 +99,18 @@ 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(); + clear_exec_times(); + m_identify_ptr->clear_cache(); + + auto task_ptr = std::make_shared(task_callback, &m_all_tasks_info, &m_configer); + task_ptr->set_tasks({ task }); + m_tasks_queue.emplace(task_ptr); - m_pIder->clear_cache(); - m_next_tasks.clear(); - m_next_tasks.emplace_back(task); m_thread_running = true; m_condvar.notify_one(); } @@ -120,11 +123,12 @@ void Assistance::stop(bool block) std::unique_lock lock; if (block) { // ⲿ lock = std::unique_lock(m_mutex); - m_configer.clear_exec_times(); + clear_exec_times(); } m_thread_running = false; - m_next_tasks.clear(); - m_pIder->clear_cache(); + std::queue> empty; + m_tasks_queue.swap(empty); + m_identify_ptr->clear_cache(); } bool Assistance::set_param(const std::string& type, const std::string& param, const std::string& value) @@ -187,7 +191,7 @@ bool asst::Assistance::find_text_and_click(const std::string& text, bool block) lock = std::unique_lock(m_mutex); } const cv::Mat& image = get_format_image(); - std::optional&& result = m_pIder->find_text(image, text); + std::optional&& result = m_identify_ptr->find_text(image, text); if (!result) { DebugTrace("Cannot found", Utf8ToGbk(text)); @@ -195,7 +199,7 @@ bool asst::Assistance::find_text_and_click(const std::string& text, bool block) } set_control_scale(image.cols, image.rows); - return m_pCtrl->click(result.value()); + return m_control_ptr->click(result.value()); } std::optional, OperCombs>>> @@ -213,7 +217,7 @@ std::optional, OperCombs>>> start("RecruitTime"); } - std::vector