From fd4dfbf6ce8744a9ab0cc7740c25249b9df60a2e Mon Sep 17 00:00:00 2001 From: zzyyyl Date: Mon, 24 Oct 2022 15:42:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E8=BD=BD=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=20`AlgorithmType`,=20`ProcessTaskAction`=20?= =?UTF-8?q?=E7=9A=84=20std::to=5Fstring(),=20=E5=B9=B6=E5=9C=A8=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E4=B8=8E=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20std::to=5Fstring()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Task/Sub/ProcessTask.cpp | 4 +- src/MeoAssistant/TaskData.cpp | 2 - src/MeoAssistant/TaskData.h | 49 ---------- src/MeoAssistant/Utils/AsstTypes.h | 113 ++++++++++++++++++++-- src/MeoAssistant/Utils/Logger.hpp | 4 + src/MeoAssistant/Utils/StringMisc.hpp | 1 + 6 files changed, 111 insertions(+), 62 deletions(-) diff --git a/src/MeoAssistant/Task/Sub/ProcessTask.cpp b/src/MeoAssistant/Task/Sub/ProcessTask.cpp index 650d9cd67d..2937b198f8 100644 --- a/src/MeoAssistant/Task/Sub/ProcessTask.cpp +++ b/src/MeoAssistant/Task/Sub/ProcessTask.cpp @@ -156,10 +156,10 @@ bool ProcessTask::_run() info["details"] = json::object { { "task", cur_name }, - { "action", static_cast(m_cur_task_ptr->action) }, + { "action", std::to_string(m_cur_task_ptr->action) }, { "exec_times", exec_times }, { "max_times", max_times }, - { "algorithm", static_cast(m_cur_task_ptr->algorithm) }, + { "algorithm", std::to_string(m_cur_task_ptr->algorithm) }, }; callback(AsstMsg::SubTaskStart, info); diff --git a/src/MeoAssistant/TaskData.cpp b/src/MeoAssistant/TaskData.cpp index 1bfc492a08..62a6287300 100644 --- a/src/MeoAssistant/TaskData.cpp +++ b/src/MeoAssistant/TaskData.cpp @@ -89,7 +89,6 @@ std::shared_ptr asst::TaskData::generate_task_info(const std::st std::shared_ptr default_derived_ptr = default_ptr; if (auto opt = task_json.find("algorithm")) { std::string algorithm_str = opt.value(); - utils::tolowers(algorithm_str); algorithm = get_algorithm_type(algorithm_str); if (default_ptr->algorithm != algorithm) { // 相同 algorithm 时才继承派生类成员 @@ -234,7 +233,6 @@ bool asst::TaskData::append_base_task_info(std::shared_ptr task_info_p } if (auto opt = task_json.find("action")) { std::string action = opt.value(); - utils::tolowers(action); task_info_ptr->action = get_action_type(action); if (task_info_ptr->action == ProcessTaskAction::Invalid) [[unlikely]] { Log.error("Unknown action:", action, ", Task:", name); diff --git a/src/MeoAssistant/TaskData.h b/src/MeoAssistant/TaskData.h index 80f483553c..7d0c48d693 100644 --- a/src/MeoAssistant/TaskData.h +++ b/src/MeoAssistant/TaskData.h @@ -205,55 +205,6 @@ namespace asst virtual bool parse(const json::value& json) override; private: - static AlgorithmType get_algorithm_type(std::string_view algorithm_str) - { - if (algorithm_str == "matchtemplate") { - return AlgorithmType::MatchTemplate; - } - else if (algorithm_str == "justreturn") { - return AlgorithmType::JustReturn; - } - else if (algorithm_str == "ocrdetect") { - return AlgorithmType::OcrDetect; - } - else if (algorithm_str == "hash") { - return AlgorithmType::Hash; - } - return AlgorithmType::Invalid; - } - - static ProcessTaskAction get_action_type(std::string_view action_str) - { - if (action_str == "clickself") { - return ProcessTaskAction::ClickSelf; - } - else if (action_str == "clickrand") { - return ProcessTaskAction::ClickRand; - } - else if (action_str == "donothing" || action_str.empty()) { - return ProcessTaskAction::DoNothing; - } - else if (action_str == "stop") { - return ProcessTaskAction::Stop; - } - else if (action_str == "clickrect") { - return ProcessTaskAction::ClickRect; - } - else if (action_str == "swipetotheleft") { - return ProcessTaskAction::SwipeToTheLeft; - } - else if (action_str == "swipetotheright") { - return ProcessTaskAction::SwipeToTheRight; - } - else if (action_str == "slowlyswipetotheleft") { - return ProcessTaskAction::SlowlySwipeToTheLeft; - } - else if (action_str == "slowlyswipetotheright") { - return ProcessTaskAction::SlowlySwipeToTheRight; - } - return ProcessTaskAction::Invalid; - } - #ifdef ASST_DEBUG bool syntax_check(const std::string& task_name, const json::value& task_json); #endif diff --git a/src/MeoAssistant/Utils/AsstTypes.h b/src/MeoAssistant/Utils/AsstTypes.h index b6eca19910..b842231589 100644 --- a/src/MeoAssistant/Utils/AsstTypes.h +++ b/src/MeoAssistant/Utils/AsstTypes.h @@ -9,6 +9,8 @@ #include #include +#include "Utils/StringMisc.hpp" + #ifndef NOMINMAX #define NOMINMAX #endif @@ -175,15 +177,6 @@ namespace asst }; using TextRectProc = std::function; - enum class AlgorithmType - { - Invalid = -1, - JustReturn, - MatchTemplate, - OcrDetect, - Hash - }; - struct MatchRect { MatchRect() = default; @@ -238,6 +231,30 @@ namespace std namespace asst { + enum class AlgorithmType + { + Invalid = -1, + JustReturn, + MatchTemplate, + OcrDetect, + Hash + }; + + inline AlgorithmType get_algorithm_type(std::string algorithm_str) + { + utils::tolowers(algorithm_str); + static const std::unordered_map algorithm_map = { + { "matchtemplate", AlgorithmType::MatchTemplate }, + { "justreturn", AlgorithmType::JustReturn }, + { "ocrdetect", AlgorithmType::OcrDetect }, + { "hash", AlgorithmType::Hash }, + }; + if (algorithm_map.contains(algorithm_str)) { + return algorithm_map.at(algorithm_str); + } + return AlgorithmType::Invalid; + } + enum class ProcessTaskAction { Invalid = 0, @@ -254,6 +271,84 @@ namespace asst SlowlySwipeToTheRight = SwipeToTheRight | 8 // 慢慢的往右划一下 }; + inline ProcessTaskAction get_action_type(std::string action_str) + { + utils::tolowers(action_str); + static const std::unordered_map action_map = { + { "clickself", ProcessTaskAction::ClickSelf }, + { "clickrand", ProcessTaskAction::ClickRand }, + { "", ProcessTaskAction::DoNothing }, + { "donothing", ProcessTaskAction::DoNothing }, + { "stop", ProcessTaskAction::Stop }, + { "clickrect", ProcessTaskAction::ClickRect }, + { "swipetotheleft", ProcessTaskAction::SwipeToTheLeft }, + { "swipetotheright", ProcessTaskAction::SwipeToTheRight }, + { "slowlyswipetotheleft", ProcessTaskAction::SlowlySwipeToTheLeft }, + { "slowlyswipetotheright", ProcessTaskAction::SlowlySwipeToTheRight }, + }; + if (auto it = action_map.find(action_str); it != action_map.end()) { + return it->second; + } + return ProcessTaskAction::Invalid; + } +} + +namespace std +{ + inline std::string to_string(asst::AlgorithmType algo) + { + static const std::unordered_map algorithm_map = { + { asst::AlgorithmType::Invalid, "Invalid" }, + { asst::AlgorithmType::JustReturn, "JustReturn" }, + { asst::AlgorithmType::MatchTemplate, "MatchTemplate" }, + { asst::AlgorithmType::OcrDetect, "OcrDetect" }, + { asst::AlgorithmType::Hash, "Hash" }, + }; + if (auto it = algorithm_map.find(algo); it != algorithm_map.end()) { + return it->second; + } + return "Invalid"; + } + + inline std::string to_string(asst::ProcessTaskAction action) + { + static const std::unordered_map action_map = { + { asst::ProcessTaskAction::Invalid, "Invalid" }, + { asst::ProcessTaskAction::BasicClick, "BasicClick" }, + { asst::ProcessTaskAction::ClickSelf, "ClickSelf" }, + { asst::ProcessTaskAction::ClickRect, "ClickRect" }, + { asst::ProcessTaskAction::ClickRand, "ClickRand" }, + { asst::ProcessTaskAction::DoNothing, "DoNothing" }, + { asst::ProcessTaskAction::Stop, "Stop" }, + { asst::ProcessTaskAction::BasicSwipe, "BasicSwipe" }, + { asst::ProcessTaskAction::SwipeToTheLeft, "SwipeToTheLeft" }, + { asst::ProcessTaskAction::SwipeToTheRight, "SwipeToTheRight" }, + { asst::ProcessTaskAction::SlowlySwipeToTheLeft, "SlowlySwipeToTheLeft" }, + { asst::ProcessTaskAction::SlowlySwipeToTheRight, "SlowlySwipeToTheRight" }, + }; + if (auto it = action_map.find(action); it != action_map.end()) { + return it->second; + } + return "Invalid"; + } + /* + template + requires requires() { asst::to_string(std::declval()); } + std::string to_string(const T& type) + { + return asst::to_string(type); + } + template + requires requires() { std::declval().to_string(); } + std::string to_string(const T& type) + { + return type.to_string(); + } + */ +} + +namespace asst +{ // 任务信息 struct TaskInfo { diff --git a/src/MeoAssistant/Utils/Logger.hpp b/src/MeoAssistant/Utils/Logger.hpp index 14751c0aac..5be0e8b3e9 100644 --- a/src/MeoAssistant/Utils/Logger.hpp +++ b/src/MeoAssistant/Utils/Logger.hpp @@ -10,6 +10,7 @@ #include #include "AsstRanges.hpp" +#include "AsstTypes.h" #include "Locale.hpp" #include "Meta.hpp" #include "Platform.hpp" @@ -295,6 +296,9 @@ namespace asst #endif // END _WIN32 s << buff; } + else if constexpr (std::is_enum_v) { + s << std::to_string(std::forward(v)); + } else if constexpr (has_stream_insertion_operator) { s << std::forward(v); } diff --git a/src/MeoAssistant/Utils/StringMisc.hpp b/src/MeoAssistant/Utils/StringMisc.hpp index 3b145b6f43..1479514f99 100644 --- a/src/MeoAssistant/Utils/StringMisc.hpp +++ b/src/MeoAssistant/Utils/StringMisc.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include