From ff01ce3af47f8ecda203a4b669cb19498dd9be56 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 17 Jul 2021 00:03:21 +0800 Subject: [PATCH] update configer interface and GUI --- MeoAssistance/include/Assistance.h | 3 +- MeoAssistance/include/AsstCaller.h | 1 + MeoAssistance/include/Configer.h | 2 ++ MeoAssistance/include/Identify.h | 1 + MeoAssistance/src/Assistance.cpp | 8 +++-- MeoAssistance/src/AsstCaller.cpp | 48 ++++++++++++++++++++---------- MeoAssistance/src/Configer.cpp | 40 ++++++++++++++++++++++++- MeoAssistance/src/Identify.cpp | 5 ++++ MeoAsstGui/MainWindow.xaml | 6 ++-- MeoAsstGui/MainWindow.xaml.cs | 31 +++++++++++++++++++ resource/config.json | 3 +- 11 files changed, 123 insertions(+), 25 deletions(-) diff --git a/MeoAssistance/include/Assistance.h b/MeoAssistance/include/Assistance.h index da54aefabe..8f5fc9bb0a 100644 --- a/MeoAssistance/include/Assistance.h +++ b/MeoAssistance/include/Assistance.h @@ -23,7 +23,8 @@ namespace asst { void start(const std::string & task); void stop(); - void setParam(const std::string& param, const std::string& param_value); + + bool setParam(const std::string& type, const std::string& param, const std::string& value); private: static void workingProc(Assistance* pThis); diff --git a/MeoAssistance/include/AsstCaller.h b/MeoAssistance/include/AsstCaller.h index 689b6af63c..3bb90ccb6b 100644 --- a/MeoAssistance/include/AsstCaller.h +++ b/MeoAssistance/include/AsstCaller.h @@ -11,6 +11,7 @@ extern __declspec(dllexport) void DestoryAsst(asst::Assistance* p_asst); extern __declspec(dllexport) bool AsstCatchSimulator(asst::Assistance* p_asst); extern __declspec(dllexport) void AsstStart(asst::Assistance* p_asst, const char* task); extern __declspec(dllexport) void AsstStop(asst::Assistance* p_asst); +extern __declspec(dllexport) bool AsstSetParam(asst::Assistance* p_asst, const char* type, const char* param, const char* value); #ifdef __cplusplus } diff --git a/MeoAssistance/include/Configer.h b/MeoAssistance/include/Configer.h index b543aa5352..785eb2cbec 100644 --- a/MeoAssistance/include/Configer.h +++ b/MeoAssistance/include/Configer.h @@ -53,6 +53,8 @@ namespace asst { static Options m_options; static std::unordered_map m_tasks; static std::unordered_map m_handles; + + static bool setParam(const std::string& type, const std::string& param, const std::string& value); private: Configer() = default; diff --git a/MeoAssistance/include/Identify.h b/MeoAssistance/include/Identify.h index 1a0caf8606..155d6b8fa7 100644 --- a/MeoAssistance/include/Identify.h +++ b/MeoAssistance/include/Identify.h @@ -23,6 +23,7 @@ namespace asst { // return tuple< algorithmType, suitability, scaled asst::rect> std::tuple findImage(const cv::Mat& image, const std::string& templ, double threshold = 0.99); + void clear_cache(); private: cv::Mat image2Hist(const cv::Mat& src); double imageHistComp(const cv::Mat& src, const cv::MatND& hist); diff --git a/MeoAssistance/src/Assistance.cpp b/MeoAssistance/src/Assistance.cpp index 817f48f0ba..809d22c6b0 100644 --- a/MeoAssistance/src/Assistance.cpp +++ b/MeoAssistance/src/Assistance.cpp @@ -82,6 +82,7 @@ void Assistance::start(const std::string& task) } std::unique_lock lock(m_mutex); + m_pIder->clear_cache(); m_next_tasks.clear(); m_next_tasks.emplace_back(task); m_thread_running = true; @@ -94,11 +95,12 @@ void Assistance::stop() m_thread_running = false; m_next_tasks.clear(); + m_pIder->clear_cache(); } -void Assistance::setParam(const std::string& param, const std::string& param_value) +bool Assistance::setParam(const std::string& type, const std::string& param, const std::string& value) { - + return Configer::setParam(type, param, value); } void Assistance::workingProc(Assistance* pThis) @@ -169,4 +171,4 @@ void Assistance::workingProc(Assistance* pThis) pThis->m_condvar.wait(lock); } } -} \ No newline at end of file +} diff --git a/MeoAssistance/src/AsstCaller.cpp b/MeoAssistance/src/AsstCaller.cpp index 35cd0a132e..dda2cb4e6c 100644 --- a/MeoAssistance/src/AsstCaller.cpp +++ b/MeoAssistance/src/AsstCaller.cpp @@ -8,35 +8,51 @@ asst::Assistance* CreateAsst() void DestoryAsst(asst::Assistance* p_asst) { - if (p_asst != NULL) { - delete p_asst; - p_asst = NULL; + if (p_asst == NULL) { + return; } + + delete p_asst; + p_asst = NULL; } bool AsstCatchSimulator(asst::Assistance* p_asst) { - if (p_asst != NULL) { - auto ret = p_asst->setSimulator(); - if (ret) { - return true; - } - else { - return false; - } + if (p_asst == NULL) { + return false; + } + + auto ret = p_asst->setSimulator(); + if (ret) { + return true; + } + else { + return false; } } void AsstStart(asst::Assistance* p_asst, const char* task) { - if (p_asst != NULL) { - p_asst->start(task); + if (p_asst == NULL) { } + + p_asst->start(task); } void AsstStop(asst::Assistance* p_asst) { - if (p_asst != NULL) { - p_asst->stop(); + if (p_asst == NULL) { + return; } -} \ No newline at end of file + + p_asst->stop(); +} + +bool AsstSetParam(asst::Assistance* p_asst, const char* type, const char* param, const char* value) +{ + if (p_asst == NULL) { + return false; + } + + p_asst->setParam(type, param, value); +} diff --git a/MeoAssistance/src/Configer.cpp b/MeoAssistance/src/Configer.cpp index ace30e44fe..18362d4b42 100644 --- a/MeoAssistance/src/Configer.cpp +++ b/MeoAssistance/src/Configer.cpp @@ -66,6 +66,9 @@ bool Configer::reload() DebugTraceError("Task: %s 's type error: %s", name.c_str(), type.c_str()); return false; } + if (task_json.as_object().exist("maxTimes")) { + task_info.max_times = task_json["maxTimes"].as_integer(); + } auto next_arr = task_json["next"].as_array(); for (auto&& name : next_arr) { task_info.next.emplace_back(name.as_string()); @@ -115,6 +118,41 @@ bool Configer::reload() return true; } +bool Configer::setParam(const std::string& type, const std::string& param, const std::string& value) +{ + if (type == "task.type") { + if (m_tasks.find(param) == m_tasks.cend()) { + return false; + } + auto& task_info = m_tasks[param]; + std::string type = value; + std::transform(type.begin(), type.end(), type.begin(), std::tolower); + if (type == "clickself") { + task_info.type = TaskType::ClickSelf; + } + else if (type == "clickrand") { + task_info.type = TaskType::ClickRand; + } + else if (type == "donothing" || type.empty()) { + task_info.type = TaskType::DoNothing; + } + else if (type == "stop") { + task_info.type = TaskType::Stop; + } + else { + DebugTraceError("Task: %s 's type error: %s", param.c_str(), type.c_str()); + return false; + } + } + else if (type == "task.maxTimes") { + if (m_tasks.find(param) == m_tasks.cend()) { + return false; + } + m_tasks[param].max_times = std::stoi(value); + } + return true; +} + std::string Configer::getCurDir() { if (m_curDir.empty()) { @@ -129,4 +167,4 @@ std::string Configer::getCurDir() std::string Configer::getResDir() { return getCurDir() + "resource\\"; -} \ No newline at end of file +} diff --git a/MeoAssistance/src/Identify.cpp b/MeoAssistance/src/Identify.cpp index 412a94ecad..70fc907e24 100644 --- a/MeoAssistance/src/Identify.cpp +++ b/MeoAssistance/src/Identify.cpp @@ -91,6 +91,11 @@ std::tuple Identify::findImage(const Mat& cur, const st } } +void Identify::clear_cache() +{ + m_cache_map.clear(); +} + /* std::pair Identify::findImageWithFile(const cv::Mat& cur, const std::string& filename) { diff --git a/MeoAsstGui/MainWindow.xaml b/MeoAsstGui/MainWindow.xaml index 7e6e287d01..b8eb6edd6a 100644 --- a/MeoAsstGui/MainWindow.xaml +++ b/MeoAsstGui/MainWindow.xaml @@ -7,9 +7,9 @@ mc:Ignorable="d" Title="MeoAssistance-明日方舟辅助" Height="450" Width="730"> - - - + + +