Merge pull request #2412 from MaaAssistantArknights/feat/enum_to_string

feat: 为部分枚举类型增加与字符串类型的互转函数
This commit is contained in:
MistEO
2022-10-24 22:00:51 +08:00
committed by GitHub
6 changed files with 96 additions and 62 deletions

View File

@@ -210,10 +210,10 @@ bool ProcessTask::_run()
info["details"] = json::object {
{ "task", cur_name },
{ "action", static_cast<int>(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<int>(m_cur_task_ptr->algorithm) },
{ "algorithm", enum_to_string(m_cur_task_ptr->algorithm) },
};
callback(AsstMsg::SubTaskStart, info);

View File

@@ -181,7 +181,6 @@ std::shared_ptr<asst::TaskInfo> asst::TaskData::generate_task_info(const std::st
std::shared_ptr<TaskInfo> default_derived_ptr = default_ptr;
if (auto opt = task_json.find<std::string>("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<TaskInfo> task_info_p
}
if (auto opt = task_json.find<std::string>("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);

View File

@@ -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

View File

@@ -9,6 +9,8 @@
#include <unordered_set>
#include <vector>
#include "Utils/StringMisc.hpp"
#ifndef NOMINMAX
#define NOMINMAX
#endif
@@ -175,15 +177,6 @@ namespace asst
};
using TextRectProc = std::function<bool(TextRect&)>;
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<std::string_view, AlgorithmType> 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<AlgorithmType, std::string> 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<std::string, ProcessTaskAction> 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<ProcessTaskAction, std::string> 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
{

View File

@@ -10,6 +10,7 @@
#include <utility>
#include "AsstRanges.hpp"
#include "AsstTypes.h"
#include "Locale.hpp"
#include "Meta.hpp"
#include "Platform.hpp"
@@ -26,6 +27,8 @@ namespace asst
{
template <typename Stream, typename T>
concept has_stream_insertion_operator = requires { std::declval<Stream&>() << std::declval<T>(); };
template <typename T>
concept enum_could_to_string = requires { asst::enum_to_string(std::declval<T>()); };
// is_reference_wrapper
template <typename T>
@@ -295,6 +298,9 @@ namespace asst
#endif // END _WIN32
s << buff;
}
else if constexpr (std::is_enum_v<T> && enum_could_to_string<T>) {
s << asst::enum_to_string(std::forward<T>(v));
}
else if constexpr (has_stream_insertion_operator<Stream, T>) {
s << std::forward<T>(v);
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <locale>