diff --git a/src/MeoAssistant/AbstractImageAnalyzer.cpp b/src/MeoAssistant/AbstractImageAnalyzer.cpp index 3e9abf52a3..d25f5e3b9f 100644 --- a/src/MeoAssistant/AbstractImageAnalyzer.cpp +++ b/src/MeoAssistant/AbstractImageAnalyzer.cpp @@ -68,10 +68,7 @@ asst::Rect asst::AbstractImageAnalyzer::empty_rect_to_full(const Rect& rect, con bool asst::AbstractImageAnalyzer::save_img() { std::string stem = utils::get_format_time(); - stem = utils::string_replace_all_batch(stem, { - { ":", "-" }, - { " ", "_" }, - }); + stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } }); std::filesystem::create_directory("debug"); bool ret = cv::imwrite("debug/" + stem + "_raw.png", m_image); diff --git a/src/MeoAssistant/AbstractTask.cpp b/src/MeoAssistant/AbstractTask.cpp index 89042e9433..8a3c1abad0 100644 --- a/src/MeoAssistant/AbstractTask.cpp +++ b/src/MeoAssistant/AbstractTask.cpp @@ -191,7 +191,7 @@ void asst::AbstractTask::click_return_button() void asst::AbstractTask::save_image() { std::string stem = utils::get_format_time(); - stem = utils::string_replace_all_batch(stem, { { ":", "-" }, { " ", "_" } }); + stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } }); std::filesystem::create_directory("debug"); cv::imwrite("debug/" + stem + "_raw.png", m_ctrler->get_image()); } diff --git a/src/MeoAssistant/AsstUtils.hpp b/src/MeoAssistant/AsstUtils.hpp index 25c72df0a2..5c4e7d52a4 100644 --- a/src/MeoAssistant/AsstUtils.hpp +++ b/src/MeoAssistant/AsstUtils.hpp @@ -33,11 +33,14 @@ namespace asst::utils template requires ranges::range && ranges::range - dst_t view_cast(const src_t& src) + dst_t view_cast(src_t&& src) { return dst_t(src.begin(), src.end()); } + template + using pair_of_string_view = std::pair, std::basic_string_view>; + template inline void _string_replace_all(std::basic_string& str, std::basic_string_view old_value, std::basic_string_view new_value) @@ -51,8 +54,22 @@ namespace asst::utils } template - requires std::is_constructible_v, old_value_t> && - std::is_constructible_v, new_value_t> + requires std::convertible_to> && + std::convertible_to> + inline void _string_replace_all(std::basic_string& str, old_value_t&& old_value, new_value_t&& new_value) + { + _string_replace_all(str, { old_value }, { new_value }); + } + + template + inline void _string_replace_all(std::basic_string& str, const pair_of_string_view& replace_pair) + { + _string_replace_all(str, replace_pair.first, replace_pair.second); + } + + template + requires std::convertible_to> && + std::convertible_to> inline std::basic_string string_replace_all(const std::basic_string& src, old_value_t&& old_value, new_value_t&& new_value) { @@ -61,30 +78,35 @@ namespace asst::utils return str; } - template , - typename new_value_t = std::basic_string_view> - requires std::is_constructible_v, old_value_t> && - std::is_constructible_v, new_value_t> - inline std::basic_string string_replace_all_batch( - const std::basic_string& src, - std::initializer_list>&& replace_pairs) + template + inline std::basic_string string_replace_all(const std::basic_string& src, + const pair_of_string_view& replace_pair) { std::basic_string str = src; - for (auto&& [old_value, new_value] : replace_pairs) { - _string_replace_all(str, { old_value }, { new_value }); + _string_replace_all(str, replace_pair); + return str; + } + + template + inline std::basic_string string_replace_all( + const std::basic_string& src, std::initializer_list>&& replace_pairs) + { + std::basic_string str = src; + for (const auto& [old_value, new_value] : replace_pairs) { + _string_replace_all(str, old_value, new_value); } return str; } template - [[deprecated]] inline std::basic_string string_replace_all_batch(const std::basic_string& src, - const map_t& replace_pairs) requires std::derived_from> && std::derived_from> + [[deprecated]] inline std::basic_string string_replace_all(const std::basic_string& src, + const map_t& replace_pairs) { std::basic_string str = src; for (const auto& [old_value, new_value] : replace_pairs) { - _string_replace_all(str, { old_value }, { new_value }); + _string_replace_all(str, old_value, new_value); } return str; } diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index b61f1c69d2..b87d69998f 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -796,8 +796,8 @@ int asst::Controller::click_without_scale(const Point& p, bool block) if (p.x < 0 || p.x >= m_width || p.y < 0 || p.y >= m_height) { Log.error("click point out of range"); } - std::string cur_cmd = utils::string_replace_all_batch( - m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } }); + std::string cur_cmd = + utils::string_replace_all(m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } }); int id = push_cmd(cur_cmd); if (block) { wait(id); @@ -842,20 +842,20 @@ int asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int y1 = std::clamp(y1, 0, m_height - 1); } - std::string cur_cmd = utils::string_replace_all_batch( - m_adb.swipe, { - { "[x1]", std::to_string(x1) }, - { "[y1]", std::to_string(y1) }, - { "[x2]", std::to_string(x2) }, - { "[y2]", std::to_string(y2) }, - { "[duration]", duration <= 0 ? "" : std::to_string(duration) }, - }); + std::string cur_cmd = + utils::string_replace_all(m_adb.swipe, { + { "[x1]", std::to_string(x1) }, + { "[y1]", std::to_string(y1) }, + { "[x2]", std::to_string(x2) }, + { "[y2]", std::to_string(y2) }, + { "[duration]", duration <= 0 ? "" : std::to_string(duration) }, + }); int id = 0; // 额外的滑动:adb有bug,同样的参数,偶尔会划得非常远。额外做一个短程滑动,把之前的停下来 const auto& opt = Configer.get_options(); if (extra_swipe && opt.adb_extra_swipe_duration > 0) { - std::string extra_cmd = utils::string_replace_all_batch( + std::string extra_cmd = utils::string_replace_all( m_adb.swipe, { { "[x1]", std::to_string(x2) }, { "[y1]", std::to_string(y2) }, @@ -926,13 +926,13 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a // 里面的值每次执行命令后可能更新,所以要用 lambda 拿最新的 auto cmd_replace = [&](const std::string& cfg_cmd) -> std::string { - return utils::string_replace_all_batch(cfg_cmd, { - { "[Adb]", adb_path }, - { "[AdbSerial]", address }, - { "[DisplayId]", display_id }, - { "[NcPort]", std::to_string(nc_port) }, - { "[NcAddress]", nc_address }, - }); + return utils::string_replace_all(cfg_cmd, { + { "[Adb]", adb_path }, + { "[AdbSerial]", address }, + { "[DisplayId]", display_id }, + { "[NcPort]", std::to_string(nc_port) }, + { "[NcAddress]", nc_address }, + }); }; /* connect */ diff --git a/src/MeoAssistant/ReportDataTask.cpp b/src/MeoAssistant/ReportDataTask.cpp index 8ebe13c3f1..2e568b7231 100644 --- a/src/MeoAssistant/ReportDataTask.cpp +++ b/src/MeoAssistant/ReportDataTask.cpp @@ -129,7 +129,7 @@ asst::http::Response asst::ReportDataTask::escape_and_request(const std::string& { LogTraceFunction; - std::string body_escape = utils::string_replace_all_batch(m_body, { { "\"", "\\\"" } }); + std::string body_escape = utils::string_replace_all(m_body, { { "\"", "\\\"" } }); #ifdef _WIN32 std::string body_escapes = utils::utf8_to_unicode_escape(body_escape); @@ -138,7 +138,7 @@ asst::http::Response asst::ReportDataTask::escape_and_request(const std::string& #endif std::string cmd_line = - utils::string_replace_all_batch(format, { { "[body]", body_escapes }, { "[extra]", m_extra_param } }); + utils::string_replace_all(format, { { "[body]", body_escapes }, { "[extra]", m_extra_param } }); Log.info("request:\n", cmd_line); http::Response response = utils::callcmd(cmd_line); diff --git a/src/MeoAssistant/StageDropsImageAnalyzer.cpp b/src/MeoAssistant/StageDropsImageAnalyzer.cpp index a0faf0a045..14d4347030 100644 --- a/src/MeoAssistant/StageDropsImageAnalyzer.cpp +++ b/src/MeoAssistant/StageDropsImageAnalyzer.cpp @@ -20,7 +20,7 @@ bool asst::StageDropsImageAnalyzer::analyze() #ifdef ASST_DEBUG std::string stem = utils::get_format_time(); - stem = utils::string_replace_all_batch(stem, { { ":", "-" }, { " ", "_" } }); + stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } }); std::filesystem::create_directory("debug"); cv::imwrite("debug/" + stem + "_raw.png", m_image); #endif