diff --git a/src/MeoAssistant/Task/Sub/ProcessTask.cpp b/src/MeoAssistant/Task/Sub/ProcessTask.cpp index dab9e4d9b8..ac66190844 100644 --- a/src/MeoAssistant/Task/Sub/ProcessTask.cpp +++ b/src/MeoAssistant/Task/Sub/ProcessTask.cpp @@ -210,10 +210,10 @@ bool ProcessTask::_run() info["details"] = json::object { { "task", cur_name }, - { "action", static_cast(m_cur_task_ptr->action) }, + { "action", enum_to_string(m_cur_task_ptr->action) }, { "exec_times", exec_times }, { "max_times", max_times }, - { "algorithm", static_cast(m_cur_task_ptr->algorithm) }, + { "algorithm", enum_to_string(m_cur_task_ptr->algorithm) }, }; callback(AsstMsg::SubTaskStart, info); diff --git a/src/MeoAssistant/TaskData.cpp b/src/MeoAssistant/TaskData.cpp index 06255fd78f..b56093dfed 100644 --- a/src/MeoAssistant/TaskData.cpp +++ b/src/MeoAssistant/TaskData.cpp @@ -181,7 +181,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 时才继承派生类成员 @@ -326,7 +325,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 6e9a1f2c6b..3a0cdef35b 100644 --- a/src/MeoAssistant/TaskData.h +++ b/src/MeoAssistant/TaskData.h @@ -166,55 +166,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..81d238e96c 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,45 @@ 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; + } + + inline std::string enum_to_string(AlgorithmType algo) + { + static const std::unordered_map algorithm_map = { + { AlgorithmType::Invalid, "Invalid" }, + { AlgorithmType::JustReturn, "JustReturn" }, + { AlgorithmType::MatchTemplate, "MatchTemplate" }, + { AlgorithmType::OcrDetect, "OcrDetect" }, + { AlgorithmType::Hash, "Hash" }, + }; + if (auto it = algorithm_map.find(algo); it != algorithm_map.end()) { + return it->second; + } + return "Invalid"; + } + enum class ProcessTaskAction { Invalid = 0, @@ -254,6 +286,52 @@ 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; + } + + inline std::string enum_to_string(ProcessTaskAction action) + { + static const std::unordered_map action_map = { + { ProcessTaskAction::Invalid, "Invalid" }, + { ProcessTaskAction::BasicClick, "BasicClick" }, + { ProcessTaskAction::ClickSelf, "ClickSelf" }, + { ProcessTaskAction::ClickRect, "ClickRect" }, + { ProcessTaskAction::ClickRand, "ClickRand" }, + { ProcessTaskAction::DoNothing, "DoNothing" }, + { ProcessTaskAction::Stop, "Stop" }, + { ProcessTaskAction::BasicSwipe, "BasicSwipe" }, + { ProcessTaskAction::SwipeToTheLeft, "SwipeToTheLeft" }, + { ProcessTaskAction::SwipeToTheRight, "SwipeToTheRight" }, + { ProcessTaskAction::SlowlySwipeToTheLeft, "SlowlySwipeToTheLeft" }, + { ProcessTaskAction::SlowlySwipeToTheRight, "SlowlySwipeToTheRight" }, + }; + if (auto it = action_map.find(action); it != action_map.end()) { + return it->second; + } + return "Invalid"; + } +} + +namespace asst +{ // 任务信息 struct TaskInfo { diff --git a/src/MeoAssistant/Utils/Logger.hpp b/src/MeoAssistant/Utils/Logger.hpp index 14751c0aac..4a089d0f7f 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" @@ -26,6 +27,8 @@ namespace asst { template concept has_stream_insertion_operator = requires { std::declval() << std::declval(); }; + template + concept enum_could_to_string = requires { asst::enum_to_string(std::declval()); }; // is_reference_wrapper template @@ -295,6 +298,9 @@ namespace asst #endif // END _WIN32 s << buff; } + else if constexpr (std::is_enum_v && enum_could_to_string) { + s << asst::enum_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