diff --git a/src/MaaCore/Config/TaskData.cpp b/src/MaaCore/Config/TaskData.cpp index ff5ef6e3ff..ab2a1c81f5 100644 --- a/src/MaaCore/Config/TaskData.cpp +++ b/src/MaaCore/Config/TaskData.cpp @@ -1,8 +1,8 @@ #include "TaskData.h" #include +#include #include -#include #ifdef ASST_DEBUG #include @@ -191,9 +191,9 @@ bool asst::TaskData::lazy_parse(const json::value& json) task->algorithm == AlgorithmType::OcrDetect) { for (const auto& [regex, new_str] : ocr_task->replace_map) { try { - std::regex _(regex); + boost::regex _(regex); } - catch (const std::regex_error& e) { + catch (const boost::regex_error& e) { Log.error("Task", name, "has invalid regex:", regex, ":", e.what()); validity = false; break; diff --git a/src/MaaCore/Controller/AdbController.cpp b/src/MaaCore/Controller/AdbController.cpp index 8982a5a125..f1cb74aafb 100644 --- a/src/MaaCore/Controller/AdbController.cpp +++ b/src/MaaCore/Controller/AdbController.cpp @@ -20,7 +20,7 @@ #include "Utils/Platform.hpp" #include "Utils/StringMisc.hpp" -#include +#include asst::AdbController::AdbController(const AsstCallback& callback, Assistant* inst, PlatformType type) : InstHelper(inst), @@ -913,8 +913,8 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string bool need_connect = true; if (devices_ret) { const auto& devices_str = devices_ret.value(); - const std::regex address_regex(m_adb.address_regex); - for (std::sregex_iterator iter(devices_str.begin(), devices_str.end(), address_regex), end; iter != end; + const boost::regex address_regex(m_adb.address_regex); + for (boost::sregex_iterator iter(devices_str.begin(), devices_str.end(), address_regex), end; iter != end; ++iter) { if (iter->size() > 1 && iter->str(1) == address) { need_connect = false; @@ -978,8 +978,8 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string std::string uuid_str = raw_output; std::erase_if(uuid_str, [](char c) { return !std::isdigit(c) && !std::isalpha(c); }); - std::regex hex_regex("^[0-9a-fA-F]{8,}$"); - if (uuid_str.empty() || !std::regex_match(uuid_str, hex_regex)) { + boost::regex hex_regex("^[0-9a-fA-F]{8,}$"); + if (uuid_str.empty() || !boost::regex_match(uuid_str, hex_regex)) { json::value info = get_info_json() | json::object { { "what", "ConnectFailed" }, { "why", "Uuid invalid or shell command not available" }, diff --git a/src/MaaCore/Controller/Controller.cpp b/src/MaaCore/Controller/Controller.cpp index 5014048fbe..fd4fafa715 100644 --- a/src/MaaCore/Controller/Controller.cpp +++ b/src/MaaCore/Controller/Controller.cpp @@ -2,7 +2,7 @@ #include "Utils/Platform.hpp" -#include +#include #include #include diff --git a/src/MaaCore/Controller/Platform/AdbLiteIO.cpp b/src/MaaCore/Controller/Platform/AdbLiteIO.cpp index 61d81cd7d3..f577707780 100644 --- a/src/MaaCore/Controller/Platform/AdbLiteIO.cpp +++ b/src/MaaCore/Controller/Platform/AdbLiteIO.cpp @@ -1,6 +1,6 @@ #include "AdbLiteIO.h" -#include +#include #include "Utils/Logger.hpp" @@ -22,18 +22,18 @@ std::optional asst::AdbLiteIO::call_command( // TODO: 实现 timeout,目前暂时忽略 std::ignore = timeout; std::ignore = start_time; - std::smatch match; + boost::smatch match; std::optional ret; - static const std::regex devices_regex(R"(^.+ devices$)"); - static const std::regex release_regex(R"(^.+ kill-server$)"); - static const std::regex connect_regex(R"(^.+ connect (\S+)$)"); - static const std::regex shell_regex(R"(^.+ -s \S+ shell (.+)$)"); - static const std::regex exec_regex(R"(^.+ -s \S+ exec-out (.+)$)"); - static const std::regex push_regex(R"#(^.+ -s \S+ push "(.+)" "(.+)"$)#"); + static const boost::regex devices_regex(R"(^.+ devices$)"); + static const boost::regex release_regex(R"(^.+ kill-server$)"); + static const boost::regex connect_regex(R"(^.+ connect (\S+)$)"); + static const boost::regex shell_regex(R"(^.+ -s \S+ shell (.+)$)"); + static const boost::regex exec_regex(R"(^.+ -s \S+ exec-out (.+)$)"); + static const boost::regex push_regex(R"#(^.+ -s \S+ push "(.+)" "(.+)"$)#"); // adb devices - if (std::regex_match(cmd, devices_regex)) { + if (boost::regex_match(cmd, devices_regex)) { try { pipe_data = adb::devices(); ret = 0; @@ -47,7 +47,7 @@ std::optional asst::AdbLiteIO::call_command( } // adb kill-server - if (std::regex_match(cmd, release_regex)) { + if (boost::regex_match(cmd, release_regex)) { try { adb::kill_server(); ret = 0; @@ -62,7 +62,7 @@ std::optional asst::AdbLiteIO::call_command( // adb connect // TODO: adb server 尚未实现,第一次连接需要执行一次 adb.exe 启动 daemon - if (std::regex_match(cmd, match, connect_regex)) { + if (boost::regex_match(cmd, match, connect_regex)) { m_adb_client = adb::client::create(match[1].str()); // TODO: compare address with existing (if any) try { @@ -79,7 +79,7 @@ std::optional asst::AdbLiteIO::call_command( } // adb shell - if (std::regex_match(cmd, match, shell_regex)) { + if (boost::regex_match(cmd, match, shell_regex)) { if (!m_adb_client) { Log.error("adb client not initialized"); ret = std::nullopt; @@ -102,7 +102,7 @@ std::optional asst::AdbLiteIO::call_command( } // adb exec-out - if (std::regex_match(cmd, match, exec_regex)) { + if (boost::regex_match(cmd, match, exec_regex)) { if (!m_adb_client) { Log.error("adb client not initialized"); ret = std::nullopt; @@ -125,7 +125,7 @@ std::optional asst::AdbLiteIO::call_command( } // adb push - if (std::regex_match(cmd, match, push_regex)) { + if (boost::regex_match(cmd, match, push_regex)) { if (!m_adb_client) { Log.error("adb client not initialized"); ret = std::nullopt; @@ -158,10 +158,10 @@ ret_exit: std::shared_ptr asst::AdbLiteIO::interactive_shell(const std::string& cmd) { - static const std::regex shell_regex(R"(^.+ -s \S+ shell (.+)$)"); - std::smatch match; + static const boost::regex shell_regex(R"(^.+ -s \S+ shell (.+)$)"); + boost::smatch match; - if (std::regex_match(cmd, match, shell_regex)) { + if (boost::regex_match(cmd, match, shell_regex)) { if (!m_adb_client) { Log.error("adb client not initialized"); return nullptr; diff --git a/src/MaaCore/Task/AbstractTask.cpp b/src/MaaCore/Task/AbstractTask.cpp index 4dd61af879..1ea56252e3 100644 --- a/src/MaaCore/Task/AbstractTask.cpp +++ b/src/MaaCore/Task/AbstractTask.cpp @@ -1,8 +1,8 @@ #include "AbstractTask.h" #include +#include #include -#include #include #include @@ -91,9 +91,9 @@ json::value asst::AbstractTask::basic_info() const std::string task_name; // typeid.name() 结果可能和编译器有关,所以这里使用正则尽可能保证结果正确。 // 但还是不能完全保证,如果不行的话建议 override - std::regex regex("[a-zA-Z]+Task"); - std::smatch match_obj; - if (std::regex_search(class_name, match_obj, regex)) { + boost::regex regex("[a-zA-Z]+Task"); + boost::smatch match_obj; + if (boost::regex_search(class_name, match_obj, regex)) { task_name = match_obj[0].str(); } else { diff --git a/src/MaaCore/Task/Fight/DrGrandetTaskPlugin.cpp b/src/MaaCore/Task/Fight/DrGrandetTaskPlugin.cpp index ee70f2114b..a627d3e90a 100644 --- a/src/MaaCore/Task/Fight/DrGrandetTaskPlugin.cpp +++ b/src/MaaCore/Task/Fight/DrGrandetTaskPlugin.cpp @@ -6,7 +6,7 @@ #include "Utils/Logger.hpp" #include "Vision/OCRer.h" -#include +#include bool asst::DrGrandetTaskPlugin::verify(AsstMsg msg, const json::value& details) const { @@ -74,9 +74,9 @@ int asst::DrGrandetTaskPlugin::analyze_time_left(const cv::Mat& image) } auto text = analyzer.get_result().front().text; - auto regex = std::regex(R"((\d):(\d?)(\d?))"); - std::smatch match; - if (!std::regex_search(text, match, regex)) { + auto regex = boost::regex(R"((\d):(\d?)(\d?))"); + boost::smatch match; + if (!boost::regex_search(text, match, regex)) { Log.warn(__FUNCTION__, "Unable to match time:", text); return -1; } diff --git a/src/MaaCore/Task/Fight/StageDropsTaskPlugin.cpp b/src/MaaCore/Task/Fight/StageDropsTaskPlugin.cpp index f6bf51e36a..6dcab3bfbf 100644 --- a/src/MaaCore/Task/Fight/StageDropsTaskPlugin.cpp +++ b/src/MaaCore/Task/Fight/StageDropsTaskPlugin.cpp @@ -1,7 +1,7 @@ #include "StageDropsTaskPlugin.h" +#include #include -#include #include #include "Common/AsstTypes.h" diff --git a/src/MaaCore/Task/Fight/StageNavigationTask.cpp b/src/MaaCore/Task/Fight/StageNavigationTask.cpp index 3861550be1..2f91bad856 100644 --- a/src/MaaCore/Task/Fight/StageNavigationTask.cpp +++ b/src/MaaCore/Task/Fight/StageNavigationTask.cpp @@ -1,6 +1,6 @@ #include "StageNavigationTask.h" -#include +#include #include "Config/TaskData.h" #include "Controller/Controller.h" @@ -23,9 +23,9 @@ bool asst::StageNavigationTask::set_stage_name(const std::string& stage_name) } m_is_directly = false; - static const std::regex stage_regex(R"(^([A-Za-z]{0,3})(\d{1,2})-(\d{1,2})(?:-?(\w+))*$)"); - std::smatch stage_sm; - if (!std::regex_match(stage_name, stage_sm, stage_regex)) { + static const boost::regex stage_regex(R"(^([A-Za-z]{0,3})(\d{1,2})-(\d{1,2})(?:-?(\w+))*$)"); + boost::smatch stage_sm; + if (!boost::regex_match(stage_name, stage_sm, stage_regex)) { Log.error("The stage name is not in invalid, or is not main line stage", stage_name); return false; } diff --git a/src/MaaCore/Task/Fight/StageQueueMissionCompletedTaskPlugin.h b/src/MaaCore/Task/Fight/StageQueueMissionCompletedTaskPlugin.h index 034c7c5e0a..1ec205cb2a 100644 --- a/src/MaaCore/Task/Fight/StageQueueMissionCompletedTaskPlugin.h +++ b/src/MaaCore/Task/Fight/StageQueueMissionCompletedTaskPlugin.h @@ -1,7 +1,7 @@ #pragma once #include "Task/AbstractTaskPlugin.h" -#include +#include #include "Config/Miscellaneous/StageDropsConfig.h" #include "Config/TaskData.h" diff --git a/src/MaaCore/Task/Infrast/InfrastAbstractTask.cpp b/src/MaaCore/Task/Infrast/InfrastAbstractTask.cpp index 84f52b506a..01d7ba9600 100644 --- a/src/MaaCore/Task/Infrast/InfrastAbstractTask.cpp +++ b/src/MaaCore/Task/Infrast/InfrastAbstractTask.cpp @@ -1,7 +1,7 @@ #include "InfrastAbstractTask.h" #include -#include +#include #include #include "Common/AsstMsg.h" @@ -47,9 +47,9 @@ std::string asst::InfrastAbstractTask::facility_name() const std::string class_name = typeid(*this).name(); // typeid.name() 结果可能和编译器有关,所以这里使用正则尽可能保证结果正确。 // 但还是不能完全保证,如果不行的话建议 override - std::regex regex("Infrast(.*)Task"); - std::smatch match_obj; - if (std::regex_search(class_name, match_obj, regex)) { + boost::regex regex("Infrast(.*)Task"); + boost::smatch match_obj; + if (boost::regex_search(class_name, match_obj, regex)) { m_facility_name_cache = match_obj[1].str(); } else { diff --git a/src/MaaCore/Task/Infrast/InfrastDormTask.cpp b/src/MaaCore/Task/Infrast/InfrastDormTask.cpp index fe388ca6cb..a1f592d54a 100644 --- a/src/MaaCore/Task/Infrast/InfrastDormTask.cpp +++ b/src/MaaCore/Task/Infrast/InfrastDormTask.cpp @@ -1,6 +1,6 @@ #include "InfrastDormTask.h" -#include +#include #include "Config/TaskData.h" #include "Controller/Controller.h" @@ -155,8 +155,8 @@ bool asst::InfrastDormTask::opers_choose(asst::infrast::CustomRoomConfig const& } std::string opertrust = trust_analyzer.get_result().text; - std::regex rule("[^0-9]"); // 只保留数字 - opertrust = std::regex_replace(opertrust, rule, ""); + boost::regex rule("[^0-9]"); // 只保留数字 + opertrust = boost::regex_replace(opertrust, rule, ""); Log.trace("opertrust:", opertrust); bool if_opertrust_not_full = false; @@ -188,8 +188,8 @@ bool asst::InfrastDormTask::opers_choose(asst::infrast::CustomRoomConfig const& } std::string facilityname = facility_analyzer.get_result().text; - std::regex rule2("[^BF0-9]"); // 只保留B、F和数字 - facilityname = std::regex_replace(facilityname, rule2, ""); + boost::regex rule2("[^BF0-9]"); // 只保留B、F和数字 + facilityname = boost::regex_replace(facilityname, rule2, ""); Log.trace("facilityname:<" + facilityname + ">"); bool if_oper_not_stationed = facilityname.length() < 4; // 只有形如1F01或B101才是设施标签 diff --git a/src/MaaCore/Task/Interface/CopilotTask.cpp b/src/MaaCore/Task/Interface/CopilotTask.cpp index 99b7cbff1d..9f892037ee 100644 --- a/src/MaaCore/Task/Interface/CopilotTask.cpp +++ b/src/MaaCore/Task/Interface/CopilotTask.cpp @@ -1,6 +1,6 @@ #include "CopilotTask.h" -#include +#include #include "Config/Miscellaneous/CopilotConfig.h" #include "Config/TaskData.h" diff --git a/src/MaaCore/Task/Miscellaneous/AutoRecruitTask.cpp b/src/MaaCore/Task/Miscellaneous/AutoRecruitTask.cpp index 75aab4deb6..f83bdb78fe 100644 --- a/src/MaaCore/Task/Miscellaneous/AutoRecruitTask.cpp +++ b/src/MaaCore/Task/Miscellaneous/AutoRecruitTask.cpp @@ -12,8 +12,8 @@ #include "Vision/OCRer.h" #include +#include #include -#include namespace asst::recruit_calc { diff --git a/src/MaaCore/Vision/Miscellaneous/PipelineAnalyzer.cpp b/src/MaaCore/Vision/Miscellaneous/PipelineAnalyzer.cpp index 544b9fd282..4205600c3c 100644 --- a/src/MaaCore/Vision/Miscellaneous/PipelineAnalyzer.cpp +++ b/src/MaaCore/Vision/Miscellaneous/PipelineAnalyzer.cpp @@ -1,6 +1,6 @@ #include "PipelineAnalyzer.h" -#include +#include #include #include "Config/TaskData.h" diff --git a/src/MaaCore/Vision/Miscellaneous/StageDropsImageAnalyzer.cpp b/src/MaaCore/Vision/Miscellaneous/StageDropsImageAnalyzer.cpp index 4e413e4c90..a17ec4cd6c 100644 --- a/src/MaaCore/Vision/Miscellaneous/StageDropsImageAnalyzer.cpp +++ b/src/MaaCore/Vision/Miscellaneous/StageDropsImageAnalyzer.cpp @@ -1,7 +1,7 @@ #include "StageDropsImageAnalyzer.h" +#include #include -#include #include @@ -136,9 +136,9 @@ bool asst::StageDropsImageAnalyzer::analyze_times() std::string raw_str = rec_analyzer.get_result().text; Log.info(__FUNCTION__, "raw_str", raw_str); - std::regex re(R"(\d+)"); - std::smatch match; - if (!std::regex_search(raw_str, match, re)) { + boost::regex re(R"(\d+)"); + boost::smatch match; + if (!boost::regex_search(raw_str, match, re)) { m_times = -2; Log.error(__FUNCTION__, "regex_search failed"); return false; diff --git a/src/MaaCore/Vision/OCRer.cpp b/src/MaaCore/Vision/OCRer.cpp index 178c2fca8c..1691089132 100644 --- a/src/MaaCore/Vision/OCRer.cpp +++ b/src/MaaCore/Vision/OCRer.cpp @@ -1,8 +1,10 @@ #include "OCRer.h" -#include +#include #include +#include + #include "Config/Miscellaneous/OcrConfig.h" #include "Config/Miscellaneous/OcrPack.h" #include "Config/TaskData.h" @@ -67,6 +69,22 @@ void OCRer::postproc_trim_(Result& res) const utils::string_trim(res.text); } +static const boost::wregex& gen_regex(const std::wstring& pattern) +{ + static std::shared_mutex mtx; + static std::unordered_map s_cache; + + { + std::shared_lock slock(mtx); + if (auto it = s_cache.find(pattern); it != s_cache.end()) { + return it->second; + } + } + + std::unique_lock ulock(mtx); + return s_cache.emplace(pattern, boost::wregex(pattern)).first->second; +} + void OCRer::postproc_replace_(Result& res) const { if (m_params.replace.empty()) { @@ -78,12 +96,12 @@ void OCRer::postproc_replace_(Result& res) const std::wstring regex_u16 = MAA_NS::to_u16(regex); std::wstring new_str_u16 = MAA_NS::to_u16(new_str); if (m_params.replace_full) { - if (std::regex_search(text_u16, std::wregex(regex_u16))) { + if (boost::regex_search(text_u16, gen_regex(regex_u16))) { text_u16 = new_str_u16; } } else { - text_u16 = std::regex_replace(text_u16, std::wregex(regex_u16), new_str_u16); + text_u16 = boost::regex_replace(text_u16, gen_regex(regex_u16), new_str_u16); } } res.text = MAA_NS::from_u16(text_u16); diff --git a/src/MaaCore/Vision/Roguelike/RoguelikeRecruitSupportAnalyzer.cpp b/src/MaaCore/Vision/Roguelike/RoguelikeRecruitSupportAnalyzer.cpp index a94f78f253..c93cd24341 100644 --- a/src/MaaCore/Vision/Roguelike/RoguelikeRecruitSupportAnalyzer.cpp +++ b/src/MaaCore/Vision/Roguelike/RoguelikeRecruitSupportAnalyzer.cpp @@ -1,7 +1,7 @@ #include "RoguelikeRecruitSupportAnalyzer.h" #include -#include +#include #include "Config/Miscellaneous/BattleDataConfig.h" #include "Config/TaskData.h" @@ -128,8 +128,8 @@ bool asst::RoguelikeRecruitSupportAnalyzer::analyze() const auto& results = analyzer.get_result(); for (const auto& result : results) { Log.info(__FUNCTION__, "| RefreshSupportBtn parse `", result.text, "`", result.score); - std::smatch match_results; - if (std::regex_search(result.text, match_results, std::regex("[0-9]{2}:[0-9]{2}:[0-9]{2}"))) { + boost::smatch match_results; + if (boost::regex_search(result.text, match_results, boost::regex("[0-9]{2}:[0-9]{2}:[0-9]{2}"))) { const auto& match_str = match_results[0].str(); const auto& hour = std::atoi(match_str.substr(2).c_str()); const auto& min = std::atoi(match_str.substr(3, 2).c_str()); diff --git a/src/MaaUtils b/src/MaaUtils index f0c49deecb..9b4b5f2dcd 160000 --- a/src/MaaUtils +++ b/src/MaaUtils @@ -1 +1 @@ -Subproject commit f0c49deecbf9d0f212db68cf412ca3a24c2ec6d6 +Subproject commit 9b4b5f2dcde21344b237bfeb795e67a8eff2ce33