mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-15 17:30:27 +08:00
perf. c++ code tidy
style. typo fixed
This commit is contained in:
@@ -87,8 +87,8 @@ asst::Assistant::TaskId asst::Assistant::append_task(const std::string& type, co
|
||||
#define ASST_ASSISTANT_APPEND_TASK_FROM_STRING_IF_BRANCH(TASK) \
|
||||
else if (type == TASK::TaskType) { ptr = std::make_shared<TASK>(task_callback, static_cast<void*>(this)); }
|
||||
|
||||
if (false) {}
|
||||
ASST_ASSISTANT_APPEND_TASK_FROM_STRING_IF_BRANCH(FightTask)
|
||||
if constexpr (false) {}
|
||||
ASST_ASSISTANT_APPEND_TASK_FROM_STRING_IF_BRANCH(FightTask)
|
||||
ASST_ASSISTANT_APPEND_TASK_FROM_STRING_IF_BRANCH(StartUpTask)
|
||||
ASST_ASSISTANT_APPEND_TASK_FROM_STRING_IF_BRANCH(CloseDownTask)
|
||||
ASST_ASSISTANT_APPEND_TASK_FROM_STRING_IF_BRANCH(AwardTask)
|
||||
|
||||
@@ -53,10 +53,9 @@ namespace asst::infrast
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
class hash<asst::infrast::Skill>
|
||||
struct hash<asst::infrast::Skill>
|
||||
{
|
||||
public:
|
||||
size_t operator()(const asst::infrast::Skill& skill) const
|
||||
size_t operator()(const asst::infrast::Skill& skill) const noexcept
|
||||
{
|
||||
return ::std::hash<std::string>()(skill.id);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace asst
|
||||
struct Point
|
||||
{
|
||||
Point() = default;
|
||||
~Point() = default;
|
||||
Point(const Point&) noexcept = default;
|
||||
Point(Point&&) noexcept = default;
|
||||
constexpr Point(int x, int y) : x(x), y(y) {}
|
||||
@@ -60,14 +61,15 @@ friend Point& operator Op##= (Point& val, const Point& opd) noexcept { val.x Op#
|
||||
static int dot(const Point& lhs, const Point& rhs) noexcept { return (lhs.x * rhs.x) + (lhs.y * rhs.y); }
|
||||
static double distance(const Point& lhs, const Point& rhs) noexcept
|
||||
{
|
||||
return std::sqrt(double(std::pow(rhs.x - lhs.x, 2) + std::pow(rhs.y - lhs.y, 2)));
|
||||
return std::sqrt(std::pow(rhs.x - lhs.x, 2) + std::pow(rhs.y - lhs.y, 2));
|
||||
}
|
||||
double length() const noexcept { return std::sqrt(double(dot(*this, *this))); }
|
||||
double length() const noexcept { return std::sqrt(static_cast<double>(dot(*this, *this))); }
|
||||
};
|
||||
|
||||
struct Rect
|
||||
{
|
||||
Rect() = default;
|
||||
~Rect() = default;
|
||||
Rect(const Rect&) noexcept = default;
|
||||
Rect(Rect&&) noexcept = default;
|
||||
Rect(int x, int y, int width, int height)
|
||||
@@ -83,9 +85,9 @@ friend Point& operator Op##= (Point& val, const Point& opd) noexcept { val.x Op#
|
||||
}
|
||||
Rect center_zoom(double scale, int max_width = INT_MAX, int max_height = INT_MAX) const
|
||||
{
|
||||
int half_width_scale = static_cast<int>(width * (1 - scale) / 2);
|
||||
int half_hight_scale = static_cast<int>(height * (1 - scale) / 2);
|
||||
Rect dst(x + half_width_scale, y + half_hight_scale,
|
||||
const int half_width_scale = static_cast<int>(width * (1 - scale) / 2);
|
||||
const int half_height_scale = static_cast<int>(height * (1 - scale) / 2);
|
||||
Rect dst(x + half_width_scale, y + half_height_scale,
|
||||
static_cast<int>(width * scale), static_cast<int>(height * scale));
|
||||
if (dst.x < 0) {
|
||||
dst.x = 0;
|
||||
@@ -134,6 +136,7 @@ friend Point& operator Op##= (Point& val, const Point& opd) noexcept { val.x Op#
|
||||
struct TextRect
|
||||
{
|
||||
TextRect() = default;
|
||||
~TextRect() = default;
|
||||
TextRect(const TextRect&) = default;
|
||||
TextRect(TextRect&&) noexcept = default;
|
||||
|
||||
@@ -172,6 +175,7 @@ friend Point& operator Op##= (Point& val, const Point& opd) noexcept { val.x Op#
|
||||
struct MatchRect
|
||||
{
|
||||
MatchRect() = default;
|
||||
~MatchRect() = default;
|
||||
MatchRect(const MatchRect&) = default;
|
||||
MatchRect(MatchRect&&) noexcept = default;
|
||||
|
||||
@@ -187,29 +191,26 @@ friend Point& operator Op##= (Point& val, const Point& opd) noexcept { val.x Op#
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
class hash<asst::Point>
|
||||
struct hash<asst::Point>
|
||||
{
|
||||
public:
|
||||
size_t operator()(const asst::Point& point) const
|
||||
size_t operator()(const asst::Point& point) const noexcept
|
||||
{
|
||||
return std::hash<int>()(point.x) ^ std::hash<int>()(point.y);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
class hash<asst::Rect>
|
||||
struct hash<asst::Rect>
|
||||
{
|
||||
public:
|
||||
size_t operator()(const asst::Rect& rect) const
|
||||
size_t operator()(const asst::Rect& rect) const noexcept
|
||||
{
|
||||
return std::hash<int>()(rect.x) ^ std::hash<int>()(rect.y) ^ std::hash<int>()(rect.width) ^ std::hash<int>()(rect.height);
|
||||
}
|
||||
};
|
||||
template <>
|
||||
class hash<asst::TextRect>
|
||||
struct hash<asst::TextRect>
|
||||
{
|
||||
public:
|
||||
size_t operator()(const asst::TextRect& tr) const
|
||||
size_t operator()(const asst::TextRect& tr) const noexcept
|
||||
{
|
||||
return std::hash<std::string>()(tr.text) ^ std::hash<asst::Rect>()(tr.rect);
|
||||
}
|
||||
@@ -237,7 +238,12 @@ namespace asst
|
||||
// 任务信息
|
||||
struct TaskInfo
|
||||
{
|
||||
TaskInfo() = default;
|
||||
virtual ~TaskInfo() = default;
|
||||
TaskInfo(const TaskInfo&) = default;
|
||||
TaskInfo(TaskInfo&&) noexcept = default;
|
||||
TaskInfo& operator=(const TaskInfo&) = default;
|
||||
TaskInfo& operator=(TaskInfo&&) noexcept = default;
|
||||
std::string name; // 任务名
|
||||
AlgorithmType algorithm = // 图像算法类型
|
||||
AlgorithmType::Invalid;
|
||||
@@ -262,7 +268,12 @@ namespace asst
|
||||
// 文字识别任务的信息
|
||||
struct OcrTaskInfo : public TaskInfo
|
||||
{
|
||||
OcrTaskInfo() = default;
|
||||
virtual ~OcrTaskInfo() override = default;
|
||||
OcrTaskInfo(const OcrTaskInfo&) = default;
|
||||
OcrTaskInfo(OcrTaskInfo&&) noexcept = default;
|
||||
OcrTaskInfo& operator=(const OcrTaskInfo&) = default;
|
||||
OcrTaskInfo& operator=(OcrTaskInfo&&) noexcept = default;
|
||||
std::vector<std::string> text; // 文字的容器,匹配到这里面任一个,就算匹配上了
|
||||
bool full_match = false; // 是否需要全匹配,否则搜索到子串就算匹配上了
|
||||
std::unordered_map<std::string, std::string>
|
||||
@@ -272,7 +283,12 @@ namespace asst
|
||||
// 图片匹配任务的信息
|
||||
struct MatchTaskInfo : public TaskInfo
|
||||
{
|
||||
MatchTaskInfo() = default;
|
||||
virtual ~MatchTaskInfo() override = default;
|
||||
MatchTaskInfo(const MatchTaskInfo&) = default;
|
||||
MatchTaskInfo(MatchTaskInfo&&) noexcept = default;
|
||||
MatchTaskInfo& operator=(const MatchTaskInfo&) = default;
|
||||
MatchTaskInfo& operator=(MatchTaskInfo&&) noexcept = default;
|
||||
std::string templ_name; // 匹配模板图片文件名
|
||||
double templ_threshold = 0; // 模板匹配阈值
|
||||
double special_threshold = 0; // 某些任务使用的特殊的阈值
|
||||
@@ -282,8 +298,13 @@ namespace asst
|
||||
// hash 计算任务的信息
|
||||
struct HashTaskInfo : public TaskInfo
|
||||
{
|
||||
HashTaskInfo() = default;
|
||||
virtual ~HashTaskInfo() override = default;
|
||||
std::vector<std::string> hashs; // 需要多个哈希值
|
||||
HashTaskInfo(const HashTaskInfo&) = default;
|
||||
HashTaskInfo(HashTaskInfo&&) noexcept = default;
|
||||
HashTaskInfo& operator=(const HashTaskInfo&) = default;
|
||||
HashTaskInfo& operator=(HashTaskInfo&&) noexcept = default;
|
||||
std::vector<std::string> hashes; // 需要多个哈希值
|
||||
int dist_threshold = 0; // 汉明距离阈值
|
||||
std::pair<int, int> mask_range; // 掩码的二值化范围
|
||||
bool bound = false; // 是否裁剪周围黑边
|
||||
|
||||
@@ -54,9 +54,9 @@ namespace asst::utils
|
||||
// 但不能是
|
||||
// initializer_list<pair<string_view, string_view>>
|
||||
// 等其它类型
|
||||
static_assert(std::is_base_of<typename map_t::value_type::first_type, std::string>::value,
|
||||
static_assert(std::is_base_of_v<typename map_t::value_type::first_type, std::string>,
|
||||
"type `map_t::value_type::first_type` is not allowed.");
|
||||
static_assert(std::is_base_of<typename map_t::value_type::second_type, std::string>::value,
|
||||
static_assert(std::is_base_of_v<typename map_t::value_type::second_type, std::string>,
|
||||
"type `map_t::value_type::second_type` is not allowed.");
|
||||
std::string str = src;
|
||||
for (const auto& [old_value, new_value] : replace_pairs) {
|
||||
|
||||
@@ -275,7 +275,7 @@ int asst::BattleImageAnalyzer::oper_cost_analyze(const Rect& roi)
|
||||
std::unordered_map<std::string, std::string> num_hashs;
|
||||
for (auto&& num : NumName) {
|
||||
auto hashs_vec = std::dynamic_pointer_cast<HashTaskInfo>(
|
||||
Task.get("BattleOperCost" + num))->hashs;
|
||||
Task.get("BattleOperCost" + num))->hashes;
|
||||
for (size_t i = 0; i != hashs_vec.size(); ++i) {
|
||||
num_hashs.emplace(num + "_" + std::to_string(i), hashs_vec.at(i));
|
||||
}
|
||||
@@ -418,7 +418,7 @@ bool asst::BattleImageAnalyzer::hp_analyze()
|
||||
std::unordered_map<std::string, std::string> num_hashs;
|
||||
for (auto&& num : NumName) {
|
||||
const auto& hashs_vec = std::dynamic_pointer_cast<HashTaskInfo>(
|
||||
Task.get("BattleHp" + num))->hashs;
|
||||
Task.get("BattleHp" + num))->hashes;
|
||||
for (size_t i = 0; i != hashs_vec.size(); ++i) {
|
||||
num_hashs.emplace(num + "_" + std::to_string(i), hashs_vec.at(i));
|
||||
}
|
||||
|
||||
@@ -282,10 +282,10 @@ std::optional<std::vector<uchar>> asst::Controller::call_command(const std::stri
|
||||
FD_SET(m_server_sock, &fdset);
|
||||
constexpr int TimeoutMilliseconds = 5000;
|
||||
timeval select_timeout = { TimeoutMilliseconds / 1000, (TimeoutMilliseconds % 1000) * 1000 };
|
||||
select(static_cast<int>(m_server_sock) + 1, &fdset, NULL, NULL, &select_timeout);
|
||||
select(static_cast<int>(m_server_sock) + 1, &fdset, nullptr, nullptr, &select_timeout);
|
||||
if (FD_ISSET(m_server_sock, &fdset)) {
|
||||
SOCKET client_sock = ::accept(m_server_sock, NULL, NULL);
|
||||
setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&TimeoutMilliseconds, sizeof(int));
|
||||
SOCKET client_sock = ::accept(m_server_sock, nullptr, nullptr);
|
||||
setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&TimeoutMilliseconds), sizeof(int));
|
||||
int recv_size = 0;
|
||||
do {
|
||||
recv_size = ::recv(client_sock, (char*)m_socket_buffer.get(), SocketBuffSize, NULL);
|
||||
@@ -371,7 +371,7 @@ std::optional<std::vector<uchar>> asst::Controller::call_command(const std::stri
|
||||
m_callback(AsstMsg::ConnectionInfo, reconnect_info, m_callback_arg);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||
auto reconnect_ret = call_command(m_adb.connect, 60 * 1000);
|
||||
auto reconnect_ret = call_command(m_adb.connect, 60LL * 1000);
|
||||
bool is_reconnect_success = false;
|
||||
if (reconnect_ret) {
|
||||
auto& reconnect_val = reconnect_ret.value();
|
||||
@@ -448,7 +448,7 @@ asst::Point asst::Controller::rand_point_in_rect(const Rect& rect)
|
||||
x = rect.x;
|
||||
}
|
||||
else {
|
||||
int x_rand = std::poisson_distribution<int>(rect.width / 2)(m_rand_engine);
|
||||
int x_rand = std::poisson_distribution<int>(rect.width / 2.)(m_rand_engine);
|
||||
|
||||
x = x_rand + rect.x;
|
||||
}
|
||||
@@ -457,7 +457,7 @@ asst::Point asst::Controller::rand_point_in_rect(const Rect& rect)
|
||||
y = rect.y;
|
||||
}
|
||||
else {
|
||||
int y_rand = std::poisson_distribution<int>(rect.height / 2)(m_rand_engine);
|
||||
int y_rand = std::poisson_distribution<int>(rect.height / 2.)(m_rand_engine);
|
||||
y = y_rand + rect.y;
|
||||
}
|
||||
|
||||
@@ -507,7 +507,7 @@ int asst::Controller::push_cmd(const std::string& cmd)
|
||||
std::unique_lock<std::mutex> lock(m_cmd_queue_mutex);
|
||||
m_cmd_queue.emplace(cmd);
|
||||
m_cmd_condvar.notify_one();
|
||||
return int(++m_push_id);
|
||||
return static_cast<int>(++m_push_id);
|
||||
}
|
||||
|
||||
std::optional<unsigned short> asst::Controller::try_to_init_socket(const std::string& local_address, unsigned short try_port, unsigned short try_times)
|
||||
@@ -751,7 +751,7 @@ bool asst::Controller::screencap(const std::string& cmd, const DecodeFunc& decod
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
Log.error("convert lf and retry decode falied!");
|
||||
Log.error("convert lf and retry decode failed!");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -959,7 +959,7 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a
|
||||
/* connect */
|
||||
{
|
||||
m_adb.connect = cmd_replace(adb_cfg.connect);
|
||||
auto connect_ret = call_command(m_adb.connect, 60 * 1000);
|
||||
auto connect_ret = call_command(m_adb.connect, 60LL * 1000);
|
||||
// 端口即使错误,命令仍然会返回0,TODO 对connect_result进行判断
|
||||
bool is_connect_success = false;
|
||||
if (connect_ret) {
|
||||
@@ -1204,7 +1204,7 @@ cv::Mat asst::Controller::get_image(bool raw)
|
||||
{
|
||||
if (m_scale_size.first == 0 || m_scale_size.second == 0) {
|
||||
Log.error("Unknown image size");
|
||||
return cv::Mat();
|
||||
return {};
|
||||
}
|
||||
|
||||
// 有些模拟器adb偶尔会莫名其妙截图失败,多试几次
|
||||
|
||||
@@ -129,7 +129,7 @@ namespace asst
|
||||
std::string start;
|
||||
std::string stop;
|
||||
|
||||
/* propertities */
|
||||
/* properties */
|
||||
enum class ScreencapEndOfLine
|
||||
{
|
||||
UnknownYet,
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace asst
|
||||
|
||||
Logger(const Logger&) = delete;
|
||||
Logger(Logger&&) = delete;
|
||||
Logger& operator=(const Logger&) = delete;
|
||||
Logger& operator=(Logger&&) = delete;
|
||||
|
||||
static Logger& get_instance()
|
||||
{
|
||||
@@ -96,12 +98,12 @@ namespace asst
|
||||
log_init_info();
|
||||
}
|
||||
|
||||
void check_filesize_and_remove()
|
||||
void check_filesize_and_remove() const
|
||||
{
|
||||
constexpr uintmax_t MaxLogSize = 4 * 1024 * 1024;
|
||||
constexpr uintmax_t MaxLogSize = 4ULL * 1024 * 1024;
|
||||
try {
|
||||
if (std::filesystem::exists(m_log_filename)) {
|
||||
uintmax_t log_size = std::filesystem::file_size(m_log_filename);
|
||||
const uintmax_t log_size = std::filesystem::file_size(m_log_filename);
|
||||
if (log_size >= MaxLogSize) {
|
||||
std::filesystem::rename(m_log_filename, m_log_bak_filename);
|
||||
}
|
||||
@@ -264,7 +266,7 @@ namespace asst
|
||||
class LoggerAux
|
||||
{
|
||||
public:
|
||||
LoggerAux(std::string func_name)
|
||||
explicit LoggerAux(std::string func_name)
|
||||
: m_func_name(std::move(func_name)),
|
||||
m_start_time(std::chrono::steady_clock::now())
|
||||
{
|
||||
@@ -272,11 +274,14 @@ namespace asst
|
||||
}
|
||||
~LoggerAux()
|
||||
{
|
||||
auto duration = std::chrono::steady_clock::now() - m_start_time;
|
||||
const auto duration = std::chrono::steady_clock::now() - m_start_time;
|
||||
Logger::get_instance().trace(m_func_name, "| leave,",
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(), "ms");
|
||||
}
|
||||
|
||||
LoggerAux(const LoggerAux&) = default;
|
||||
LoggerAux(LoggerAux&&) = default;
|
||||
LoggerAux& operator=(const LoggerAux&) = default;
|
||||
LoggerAux& operator=(LoggerAux&&) = default;
|
||||
private:
|
||||
std::string m_func_name;
|
||||
std::chrono::time_point<std::chrono::steady_clock> m_start_time;
|
||||
|
||||
@@ -80,7 +80,7 @@ bool asst::TaskData::parse(const json::value& json)
|
||||
{
|
||||
auto hash_task_info_ptr = std::make_shared<HashTaskInfo>();
|
||||
for (const json::value& hash : task_json.at("hash").as_array()) {
|
||||
hash_task_info_ptr->hashs.emplace_back(hash.as_string());
|
||||
hash_task_info_ptr->hashes.emplace_back(hash.as_string());
|
||||
}
|
||||
hash_task_info_ptr->dist_threshold = task_json.get("threshold", 0);
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace asst
|
||||
protected:
|
||||
TaskData() = default;
|
||||
|
||||
virtual bool parse(const json::value& json);
|
||||
virtual bool parse(const json::value& json) override;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<TaskInfo>> m_all_tasks_info;
|
||||
std::unordered_set<std::string> m_templ_required;
|
||||
|
||||
Reference in New Issue
Block a user