diff --git a/resource/config.json b/resource/config.json index 911ceaf2d8..956e1b411b 100644 --- a/resource/config.json +++ b/resource/config.json @@ -1,4 +1,4 @@ -{ +{ "version": "0.7", "options": { "connectType": 0, diff --git a/resource/recruit.json b/resource/recruit.json index cf80cfc5eb..97e5b9cc50 100644 --- a/resource/recruit.json +++ b/resource/recruit.json @@ -1,4 +1,4 @@ -[ +[ { "name": "Lancet-2", "type": "医疗", diff --git a/resource/tasks.json b/resource/tasks.json index f2c53f4bec..6445572a31 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -1,4 +1,4 @@ -{ +{ "SanityBegin": { "algorithm": "justreturn", "action": "doNothing", diff --git a/src/MeoAssistance/AbstractConfiger.cpp b/src/MeoAssistance/AbstractConfiger.cpp index e288ecd568..02843f4a57 100644 --- a/src/MeoAssistance/AbstractConfiger.cpp +++ b/src/MeoAssistance/AbstractConfiger.cpp @@ -1,21 +1,12 @@ #include "AbstractConfiger.h" -#include -#include - #include +#include "AsstUtils.hpp" + bool asst::AbstractConfiger::load(const std::string& filename) { - std::ifstream ifs(filename, std::ios::in); - if (!ifs.is_open()) { - m_last_error = filename + " can't be opened"; - return false; - } - std::stringstream iss; - iss << ifs.rdbuf(); - ifs.close(); - std::string content(iss.str()); + std::string content = utils::load_file_without_bom(filename); auto&& ret = json::parser::parse(content); if (!ret) { diff --git a/src/MeoAssistance/AbstractTask.cpp b/src/MeoAssistance/AbstractTask.cpp index 7feeb1f23c..aec2d17667 100644 --- a/src/MeoAssistance/AbstractTask.cpp +++ b/src/MeoAssistance/AbstractTask.cpp @@ -7,7 +7,7 @@ #include #include "Controller.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Logger.hpp" using namespace asst; @@ -52,7 +52,7 @@ bool AbstractTask::sleep(unsigned millisecond) bool AbstractTask::save_image(const cv::Mat& image, const std::string& dir) { std::filesystem::create_directory(dir); - const std::string time_str = StringReplaceAll(StringReplaceAll(GetFormatTimeString(), " ", "_"), ":", "-"); + const std::string time_str = utils::string_replace_all(utils::string_replace_all(utils::get_format_time(), " ", "_"), ":", "-"); const std::string filename = dir + time_str + ".png"; bool ret = cv::imwrite(filename, image); diff --git a/src/MeoAssistance/Assistance.cpp b/src/MeoAssistance/Assistance.cpp index 9de9b3f74c..0d108e70bc 100644 --- a/src/MeoAssistance/Assistance.cpp +++ b/src/MeoAssistance/Assistance.cpp @@ -8,7 +8,7 @@ #include "Controller.h" #include "Logger.hpp" -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Resource.h" using namespace asst; @@ -18,7 +18,7 @@ Assistance::Assistance(AsstCallback callback, void* callback_arg) { LogTraceFunction; - bool resource_ret = resource.load(GetResourceDir()); + bool resource_ret = resource.load(utils::get_resource_dir()); if (!resource_ret) { const std::string& error = resource.get_last_error(); log.error("resource broken", error); @@ -131,11 +131,11 @@ bool asst::Assistance::catch_remote(const std::string& address) std::unique_lock lock(m_mutex); EmulatorInfo remote_info = cfg.get_emulators_info().at("Remote"); - remote_info.adb.connect = StringReplaceAll(remote_info.adb.connect, "[Address]", address); - remote_info.adb.click = StringReplaceAll(remote_info.adb.click, "[Address]", address); - remote_info.adb.swipe = StringReplaceAll(remote_info.adb.swipe, "[Address]", address); - remote_info.adb.display = StringReplaceAll(remote_info.adb.display, "[Address]", address); - remote_info.adb.screencap = StringReplaceAll(remote_info.adb.screencap, "[Address]", address); + remote_info.adb.connect = utils::string_replace_all(remote_info.adb.connect, "[Address]", address); + remote_info.adb.click = utils::string_replace_all(remote_info.adb.click, "[Address]", address); + remote_info.adb.swipe = utils::string_replace_all(remote_info.adb.swipe, "[Address]", address); + remote_info.adb.display = utils::string_replace_all(remote_info.adb.display, "[Address]", address); + remote_info.adb.screencap = utils::string_replace_all(remote_info.adb.screencap, "[Address]", address); ret = ctrler.try_capture(remote_info, true); diff --git a/src/MeoAssistance/AsstAux.h b/src/MeoAssistance/AsstAux.h deleted file mode 100644 index c7100c9bb8..0000000000 --- a/src/MeoAssistance/AsstAux.h +++ /dev/null @@ -1,128 +0,0 @@ -#pragma once - -#include -#include - -namespace asst { - static std::string GetCurrentDir() - { - static std::string cur_dir; - if (cur_dir.empty()) { - char exepath_buff[_MAX_PATH] = { 0 }; - ::GetModuleFileNameA(NULL, exepath_buff, _MAX_PATH); - std::string exepath(exepath_buff); - cur_dir = exepath.substr(0, exepath.find_last_of('\\') + 1); - } - return cur_dir; - } - - static std::string GetResourceDir() - { - static std::string res_dir = GetCurrentDir() + "resource\\"; - return res_dir; - } - - static std::string StringReplaceAll(const std::string& src, const std::string& old_value, const std::string& new_value) - { - std::string str = src; - for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()) { - if ((pos = str.find(old_value, pos)) != std::string::npos) - str.replace(pos, old_value.length(), new_value); - else - break; - } - return str; - } - - static std::string GetFormatTimeString() - { - SYSTEMTIME curtime; - GetLocalTime(&curtime); - char buff[64] = { 0 }; - sprintf_s(buff, "%04d-%02d-%02d %02d:%02d:%02d.%03d", - curtime.wYear, curtime.wMonth, curtime.wDay, - curtime.wHour, curtime.wMinute, curtime.wSecond, curtime.wMilliseconds); - return buff; - } - - static std::string GbkToUtf8(const std::string& gbk_str) - { - const char* src_str = gbk_str.c_str(); - int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0); - wchar_t* wstr = new wchar_t[len + 1]; - memset(wstr, 0, len + 1); - MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len); - len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); - char* str = new char[len + 1]; - memset(str, 0, len + 1); - WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); - std::string strTemp = str; - if (wstr) delete[] wstr; - if (str) delete[] str; - return strTemp; - } - - static std::string Utf8ToGbk(const std::string& utf8_str) - { - const char* src_str = utf8_str.c_str(); - int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0); - wchar_t* wszGBK = new wchar_t[len + 1]; - memset(wszGBK, 0, len * 2 + 2); - MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len); - len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); - char* szGBK = new char[len + 1]; - memset(szGBK, 0, len + 1); - WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL); - std::string strTemp(szGBK); - if (wszGBK) delete[] wszGBK; - if (szGBK) delete[] szGBK; - return strTemp; - } - - template - constexpr inline RetTy make_rect(const ArgType& rect) - { - return RetTy{ rect.x, rect.y, rect.width, rect.height }; - } - - //template::value>::type> - // std::string VectorToString(const std::vector& vector, bool to_gbk = false) { - // if (vector.empty()) { - // return std::string(); - // } - // static const std::string inter = ", "; - // std::string str; - // for (const T& ele : vector) { - // if (to_gbk) { - // str += Utf8ToGbk(ele) + inter; - // } - // else { - // str += ele + inter; - // } - // } - - // if (!str.empty()) { - // str.erase(str.size() - inter.size(), inter.size()); - // } - // return str; - //} - - //template::value>::type> - // std::string VectorToString(const std::vector& vector) { - // if (vector.empty()) { - // return std::string(); - // } - // static const std::string inter = ", "; - // std::string str; - // for (const T& ele : vector) { - // str += std::to_string(ele) + inter; - // } - - // if (!str.empty()) { - // str.erase(str.size() - inter.size(), inter.size()); - // } - // return str; - //} -} \ No newline at end of file diff --git a/src/MeoAssistance/AsstCaller.cpp b/src/MeoAssistance/AsstCaller.cpp index 51c7b90c56..55814ee233 100644 --- a/src/MeoAssistance/AsstCaller.cpp +++ b/src/MeoAssistance/AsstCaller.cpp @@ -5,7 +5,7 @@ #include #include "Version.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Assistance.h" #if 0 @@ -35,7 +35,7 @@ AsstCallback _callback = nullptr; void CallbackTrans(asst::AsstMsg msg, const json::value& json, void* custom_arg) { - _callback(static_cast(msg), asst::Utf8ToGbk(json.to_string()).c_str(), custom_arg); + _callback(static_cast(msg), asst::utils::utf8_to_gbk(json.to_string()).c_str(), custom_arg); } void* AsstCreate() diff --git a/src/MeoAssistance/AsstUtils.hpp b/src/MeoAssistance/AsstUtils.hpp new file mode 100644 index 0000000000..dff3e8bcc1 --- /dev/null +++ b/src/MeoAssistance/AsstUtils.hpp @@ -0,0 +1,158 @@ +#pragma once + +#include +#include +#include +#include + +namespace asst { + namespace utils { + static std::string get_cur_dir() + { + static std::string cur_dir; + if (cur_dir.empty()) { + char exepath_buff[_MAX_PATH] = { 0 }; + ::GetModuleFileNameA(NULL, exepath_buff, _MAX_PATH); + std::string exepath(exepath_buff); + cur_dir = exepath.substr(0, exepath.find_last_of('\\') + 1); + } + return cur_dir; + } + + static std::string get_resource_dir() + { + static std::string res_dir = get_cur_dir() + "resource\\"; + return res_dir; + } + + static std::string string_replace_all(const std::string& src, const std::string& old_value, const std::string& new_value) + { + std::string str = src; + for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()) { + if ((pos = str.find(old_value, pos)) != std::string::npos) + str.replace(pos, old_value.length(), new_value); + else + break; + } + return str; + } + + static std::string get_format_time() + { + SYSTEMTIME curtime; + GetLocalTime(&curtime); + char buff[64] = { 0 }; + sprintf_s(buff, "%04d-%02d-%02d %02d:%02d:%02d.%03d", + curtime.wYear, curtime.wMonth, curtime.wDay, + curtime.wHour, curtime.wMinute, curtime.wSecond, curtime.wMilliseconds); + return buff; + } + + static std::string gbk_2_utf8(const std::string& gbk_str) + { + const char* src_str = gbk_str.c_str(); + int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0); + wchar_t* wstr = new wchar_t[len + 1]; + memset(wstr, 0, len + 1); + MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len); + len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); + char* str = new char[len + 1]; + memset(str, 0, len + 1); + WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); + std::string strTemp = str; + if (wstr) delete[] wstr; + if (str) delete[] str; + return strTemp; + } + + static std::string utf8_to_gbk(const std::string& utf8_str) + { + const char* src_str = utf8_str.c_str(); + int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0); + wchar_t* wszGBK = new wchar_t[len + 1]; + memset(wszGBK, 0, len * 2 + 2); + MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len); + len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); + char* szGBK = new char[len + 1]; + memset(szGBK, 0, len + 1); + WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL); + std::string strTemp(szGBK); + if (wszGBK) delete[] wszGBK; + if (szGBK) delete[] szGBK; + return strTemp; + } + + template + constexpr inline RetTy make_rect(const ArgType& rect) + { + return RetTy{ rect.x, rect.y, rect.width, rect.height }; + } + + static std::string load_file_without_bom(const std::string& filename) + { + std::ifstream ifs(filename, std::ios::in); + if (!ifs.is_open()) { + return std::string(); + } + std::stringstream iss; + iss << ifs.rdbuf(); + ifs.close(); + std::string str = iss.str(); + + using uchar = unsigned char; + constexpr static uchar Bom_0 = 0xEF; + constexpr static uchar Bom_1 = 0xBB; + constexpr static uchar Bom_2 = 0xBF; + + if (str.size() >= 3 + && static_cast(str.at(0)) == Bom_0 + && static_cast(str.at(1)) == Bom_1 + && static_cast(str.at(2)) == Bom_2) { + str.assign(str.begin() + 3, str.end()); + return str; + } + return str; + } + + //template::value>::type> + // std::string VectorToString(const std::vector& vector, bool to_gbk = false) { + // if (vector.empty()) { + // return std::string(); + // } + // static const std::string inter = ", "; + // std::string str; + // for (const T& ele : vector) { + // if (to_gbk) { + // str += utils::utf8_to_gbk(ele) + inter; + // } + // else { + // str += ele + inter; + // } + // } + + // if (!str.empty()) { + // str.erase(str.size() - inter.size(), inter.size()); + // } + // return str; + //} + + //template::value>::type> + // std::string VectorToString(const std::vector& vector) { + // if (vector.empty()) { + // return std::string(); + // } + // static const std::string inter = ", "; + // std::string str; + // for (const T& ele : vector) { + // str += std::to_string(ele) + inter; + // } + + // if (!str.empty()) { + // str.erase(str.size() - inter.size(), inter.size()); + // } + // return str; + //} + } +} \ No newline at end of file diff --git a/src/MeoAssistance/Controller.cpp b/src/MeoAssistance/Controller.cpp index 006186960c..8ee9628661 100644 --- a/src/MeoAssistance/Controller.cpp +++ b/src/MeoAssistance/Controller.cpp @@ -189,19 +189,19 @@ bool Controller::try_capture(const EmulatorInfo& info, bool without_handle) log.trace("Handle:", m_handle, "Name:", m_emulator_info.name); adb_dir = emulator_path.substr(0, emulator_path.find_last_of('\\') + 1); - adb_dir = '"' + StringReplaceAll(m_emulator_info.adb.path, "[EmulatorPath]", adb_dir) + '"'; - adb_dir = StringReplaceAll(adb_dir, "[ExecDir]", GetCurrentDir()); + adb_dir = '"' + utils::string_replace_all(m_emulator_info.adb.path, "[EmulatorPath]", adb_dir) + '"'; + adb_dir = utils::string_replace_all(adb_dir, "[ExecDir]", utils::get_cur_dir()); } else { // 使用辅助自带的标准adb m_emulator_info = info; - adb_dir = '"' + StringReplaceAll(m_emulator_info.adb.path, "[ExecDir]", GetCurrentDir()) + '"'; + adb_dir = '"' + utils::string_replace_all(m_emulator_info.adb.path, "[ExecDir]", utils::get_cur_dir()) + '"'; } // TODO,检查连接是否成功 - std::string connect_cmd = StringReplaceAll(m_emulator_info.adb.connect, "[Adb]", adb_dir); + std::string connect_cmd = utils::string_replace_all(m_emulator_info.adb.connect, "[Adb]", adb_dir); call_command(connect_cmd); - std::string display_cmd = StringReplaceAll(m_emulator_info.adb.display, "[Adb]", adb_dir); + std::string display_cmd = utils::string_replace_all(m_emulator_info.adb.display, "[Adb]", adb_dir); auto display_result = call_command(display_cmd); std::string display_pipe_str( std::make_move_iterator(display_result.begin()), @@ -232,9 +232,9 @@ bool Controller::try_capture(const EmulatorInfo& info, bool without_handle) m_control_scale = static_cast(m_emulator_info.adb.display_width) / static_cast(GeneralConfiger::WindowWidthDefault); } - m_emulator_info.adb.click = StringReplaceAll(m_emulator_info.adb.click, "[Adb]", adb_dir); - m_emulator_info.adb.swipe = StringReplaceAll(m_emulator_info.adb.swipe, "[Adb]", adb_dir); - m_emulator_info.adb.screencap = StringReplaceAll(m_emulator_info.adb.screencap, "[Adb]", adb_dir); + m_emulator_info.adb.click = utils::string_replace_all(m_emulator_info.adb.click, "[Adb]", adb_dir); + m_emulator_info.adb.swipe = utils::string_replace_all(m_emulator_info.adb.swipe, "[Adb]", adb_dir); + m_emulator_info.adb.screencap = utils::string_replace_all(m_emulator_info.adb.screencap, "[Adb]", adb_dir); return true; } @@ -420,8 +420,8 @@ int asst::Controller::click_without_scale(const Point& p, bool block) || p.y < 0 || p.y >= m_emulator_info.adb.display_height) { log.error("click point out of range"); } - std::string cur_cmd = StringReplaceAll(m_emulator_info.adb.click, "[x]", std::to_string(p.x)); - cur_cmd = StringReplaceAll(cur_cmd, "[y]", std::to_string(p.y)); + std::string cur_cmd = utils::string_replace_all(m_emulator_info.adb.click, "[x]", std::to_string(p.x)); + cur_cmd = utils::string_replace_all(cur_cmd, "[y]", std::to_string(p.y)); int id = push_cmd(cur_cmd); if (block) { wait(id); @@ -458,15 +458,15 @@ int asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int || p2.y < 0 || p2.y >= m_emulator_info.adb.display_height) { log.error("swipe point out of range"); } - std::string cur_cmd = StringReplaceAll(m_emulator_info.adb.swipe, "[x1]", std::to_string(p1.x)); - cur_cmd = StringReplaceAll(cur_cmd, "[y1]", std::to_string(p1.y)); - cur_cmd = StringReplaceAll(cur_cmd, "[x2]", std::to_string(p2.x)); - cur_cmd = StringReplaceAll(cur_cmd, "[y2]", std::to_string(p2.y)); + std::string cur_cmd = utils::string_replace_all(m_emulator_info.adb.swipe, "[x1]", std::to_string(p1.x)); + cur_cmd = utils::string_replace_all(cur_cmd, "[y1]", std::to_string(p1.y)); + cur_cmd = utils::string_replace_all(cur_cmd, "[x2]", std::to_string(p2.x)); + cur_cmd = utils::string_replace_all(cur_cmd, "[y2]", std::to_string(p2.y)); if (duration <= 0) { - cur_cmd = StringReplaceAll(cur_cmd, "[duration]", ""); + cur_cmd = utils::string_replace_all(cur_cmd, "[duration]", ""); } else { - cur_cmd = StringReplaceAll(cur_cmd, "[duration]", std::to_string(duration)); + cur_cmd = utils::string_replace_all(cur_cmd, "[duration]", std::to_string(duration)); } int id = push_cmd(cur_cmd); diff --git a/src/MeoAssistance/CreditShopImageAnalyzer.cpp b/src/MeoAssistance/CreditShopImageAnalyzer.cpp index 24bba53063..8c7ab304a4 100644 --- a/src/MeoAssistance/CreditShopImageAnalyzer.cpp +++ b/src/MeoAssistance/CreditShopImageAnalyzer.cpp @@ -5,7 +5,7 @@ #include "MatchImageAnalyzer.h" #include "Resource.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" bool asst::CreditShopImageAnalyzer::analyze() { @@ -86,7 +86,7 @@ bool asst::CreditShopImageAnalyzer::commoditys_analyze() } Rect commodity(credit_point.x - 60, credit_point.y - 180, 220, 220); #ifdef LOG_TRACE - cv::rectangle(m_image, make_rect(commodity), cv::Scalar(0, 0, 255), 2); + cv::rectangle(m_image, utils::make_rect(commodity), cv::Scalar(0, 0, 255), 2); #endif m_need_to_buy.emplace_back(commodity); } @@ -109,7 +109,7 @@ bool asst::CreditShopImageAnalyzer::sold_out_analyze() sold_out_analyzer.set_roi(commodity); if (sold_out_analyzer.analyze()) { #ifdef LOG_TRACE - cv::rectangle(m_image, make_rect(commodity), cv::Scalar(0, 0, 255)); + cv::rectangle(m_image, utils::make_rect(commodity), cv::Scalar(0, 0, 255)); cv::putText(m_image, "Sold Out", cv::Point(commodity.x, commodity.y), 1, 2, cv::Scalar(255, 0, 0)); #endif // LOG_TRACE diff --git a/src/MeoAssistance/Logger.hpp b/src/MeoAssistance/Logger.hpp index 7113085b0f..68e46271de 100644 --- a/src/MeoAssistance/Logger.hpp +++ b/src/MeoAssistance/Logger.hpp @@ -7,7 +7,7 @@ #include #include -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Version.h" namespace asst { @@ -43,8 +43,8 @@ namespace asst { log(level, std::forward(args)...); } - const std::string m_log_filename = asst::GetCurrentDir() + "asst.log"; - const std::string m_log_bak_filename = asst::GetCurrentDir() + "asst.bak.log"; + const std::string m_log_filename = asst::utils::get_cur_dir() + "asst.log"; + const std::string m_log_bak_filename = asst::utils::get_cur_dir() + "asst.bak.log"; private: Logger() { @@ -74,8 +74,8 @@ namespace asst { trace("MeoAssistance Process Start"); trace("Version", asst::Version); trace("Build DataTime", __DATE__, __TIME__); - trace("Working Path", GetCurrentDir()); - trace("Resource Path", GetResourceDir()); + trace("Working Path", utils::get_cur_dir()); + trace("Resource Path", utils::get_resource_dir()); trace("-----------------------------"); } @@ -86,7 +86,7 @@ namespace asst { char buff[128] = { 0 }; sprintf_s(buff, "[%s][%s][Px%x][Tx%x]", - asst::GetFormatTimeString().c_str(), + asst::utils::get_format_time().c_str(), level.data(), _getpid(), ::GetCurrentThreadId()); stream_args(std::cout, buff, std::forward(args)...); @@ -120,7 +120,7 @@ namespace asst { { inline void operator()(std::ostream& os, T&& first) { - os << Utf8ToGbk(first) << " "; + os << utils::utf8_to_gbk(first) << " "; } }; diff --git a/src/MeoAssistance/MatchImageAnalyzer.cpp b/src/MeoAssistance/MatchImageAnalyzer.cpp index 7e66acc9a5..fc1f337959 100644 --- a/src/MeoAssistance/MatchImageAnalyzer.cpp +++ b/src/MeoAssistance/MatchImageAnalyzer.cpp @@ -1,7 +1,7 @@ -#include "MatchImageAnalyzer.h" +#include "MatchImageAnalyzer.h" #include "Resource.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Logger.hpp" asst::MatchImageAnalyzer::MatchImageAnalyzer(const cv::Mat& image, const Rect& roi, std::string templ_name, double templ_thres, double hist_thres) @@ -30,7 +30,7 @@ bool asst::MatchImageAnalyzer::analyze() } if (match_templ(templ)) { if (m_use_cache) { - resource.templ().emplace_hist(m_templ_name, to_hist(templ), make_rect(m_result.rect)); + resource.templ().emplace_hist(m_templ_name, to_hist(templ), utils::make_rect(m_result.rect)); } return true; } @@ -56,7 +56,7 @@ cv::Mat asst::MatchImageAnalyzer::to_hist(const cv::Mat& src) bool asst::MatchImageAnalyzer::match_templ(const cv::Mat& templ) { cv::Mat matched; - cv::matchTemplate(m_image(make_rect(m_roi)), templ, matched, cv::TM_CCOEFF_NORMED); + cv::matchTemplate(m_image(utils::make_rect(m_roi)), templ, matched, cv::TM_CCOEFF_NORMED); double min_val = 0.0, max_val = 0.0; cv::Point min_loc, max_loc; @@ -76,7 +76,7 @@ bool asst::MatchImageAnalyzer::match_templ(const cv::Mat& templ) bool asst::MatchImageAnalyzer::comp_hist(const cv::Mat& hist, const cv::Rect roi) { - cv::Mat roi_image = m_image(make_rect(m_roi))(roi); + cv::Mat roi_image = m_image(utils::make_rect(m_roi))(roi); double score = 1.0 - cv::compareHist(to_hist(roi_image), hist, cv::HISTCMP_BHATTACHARYYA); log.trace("comp_hist | score:", score); diff --git a/src/MeoAssistance/MeoAssistance.vcxproj b/src/MeoAssistance/MeoAssistance.vcxproj index 3fb0089c7b..77d9d7c29a 100644 --- a/src/MeoAssistance/MeoAssistance.vcxproj +++ b/src/MeoAssistance/MeoAssistance.vcxproj @@ -18,7 +18,7 @@ - + diff --git a/src/MeoAssistance/MeoAssistance.vcxproj.filters b/src/MeoAssistance/MeoAssistance.vcxproj.filters index d4403586bf..559bbba852 100644 --- a/src/MeoAssistance/MeoAssistance.vcxproj.filters +++ b/src/MeoAssistance/MeoAssistance.vcxproj.filters @@ -60,7 +60,7 @@ 头文件\Utils - + 头文件\Utils diff --git a/src/MeoAssistance/OcrImageAnalyzer.cpp b/src/MeoAssistance/OcrImageAnalyzer.cpp index f12cccf87b..b6665f4c25 100644 --- a/src/MeoAssistance/OcrImageAnalyzer.cpp +++ b/src/MeoAssistance/OcrImageAnalyzer.cpp @@ -1,7 +1,7 @@ #include "OcrImageAnalyzer.h" #include "Resource.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Logger.hpp" bool asst::OcrImageAnalyzer::analyze() @@ -11,7 +11,7 @@ bool asst::OcrImageAnalyzer::analyze() if (!m_replace.empty()) { TextRectProc text_replace = [&](TextRect& tr) -> bool { for (const auto& [old_str, new_str] : m_replace) { - tr.text = StringReplaceAll(tr.text, old_str, new_str); + tr.text = utils::string_replace_all(tr.text, old_str, new_str); } return true; }; diff --git a/src/MeoAssistance/OcrPack.cpp b/src/MeoAssistance/OcrPack.cpp index 8fd1f43fbe..31c9b8cadb 100644 --- a/src/MeoAssistance/OcrPack.cpp +++ b/src/MeoAssistance/OcrPack.cpp @@ -3,7 +3,7 @@ #include #include -#include "AsstAux.h" +#include "AsstUtils.hpp" asst::OcrPack::OcrPack() : m_ocr_ptr(std::make_unique()) @@ -76,5 +76,5 @@ std::vector asst::OcrPack::recognize(const cv::Mat & image, cons ta.rect.y += roi.y; return pred(ta); }; - return recognize(image(make_rect(roi)), rect_cor); + return recognize(image(utils::make_rect(roi)), rect_cor); } \ No newline at end of file diff --git a/src/MeoAssistance/PenguinPack.cpp b/src/MeoAssistance/PenguinPack.cpp index fdc08094ef..e868359cbf 100644 --- a/src/MeoAssistance/PenguinPack.cpp +++ b/src/MeoAssistance/PenguinPack.cpp @@ -1,7 +1,5 @@ #include "PenguinPack.h" -#include -#include #include #include @@ -10,6 +8,8 @@ namespace penguin { #include } +#include "AsstUtils.hpp" + bool asst::PenguinPack::load(const std::string& dir) { bool ret = load_json(dir + "\\json\\stages.json", dir + "\\json\\hash_index.json"); @@ -35,18 +35,7 @@ std::string asst::PenguinPack::recognize(const cv::Mat& image) bool asst::PenguinPack::load_json(const std::string& stage_path, const std::string& hash_path) { - auto load_file = [](const std::string& path) -> std::string { - std::ifstream ifs(path, std::ios::in); - if (!ifs.is_open()) { - return std::string(); - } - std::stringstream iss; - iss << ifs.rdbuf(); - ifs.close(); - return iss.str(); - }; - - auto stage_parse_ret = json::parse(load_file(stage_path)); + auto stage_parse_ret = json::parse(utils::load_file_without_bom(stage_path)); if (!stage_parse_ret) { m_last_error = stage_path + " parsing failed"; return false; @@ -84,7 +73,7 @@ bool asst::PenguinPack::load_json(const std::string& stage_path, const std::stri return false; } std::string cvt_stage_string = cvt_stage_json.to_string(); - penguin::load_json(cvt_stage_string.c_str(), load_file(hash_path).c_str()); + penguin::load_json(cvt_stage_string.c_str(), utils::load_file_without_bom(hash_path).c_str()); return true; } diff --git a/src/MeoAssistance/PenguinUploader.cpp b/src/MeoAssistance/PenguinUploader.cpp index 2c45549972..67f75d4293 100644 --- a/src/MeoAssistance/PenguinUploader.cpp +++ b/src/MeoAssistance/PenguinUploader.cpp @@ -7,7 +7,7 @@ #include "Resource.h" #include "Version.h" #include "Logger.hpp" -#include "AsstAux.h" +#include "AsstUtils.hpp" bool asst::PenguinUploader::upload(const std::string& rec_res) { @@ -37,7 +37,7 @@ std::string asst::PenguinUploader::cvt_json(const std::string& rec_res) bool asst::PenguinUploader::request_penguin(const std::string& body) { std::string curl_cmd = R"(curl -H "Content-Type: application/json" -v -i )"; - curl_cmd += "-d \"" + StringReplaceAll(body, "\"", "\\\"") + "\" \"" + resource.cfg().get_options().penguin_api + '"'; + curl_cmd += "-d \"" + utils::string_replace_all(body, "\"", "\\\"") + "\" \"" + resource.cfg().get_options().penguin_api + '"'; log.trace("request_penguin |", curl_cmd); diff --git a/src/MeoAssistance/ProcessTask.cpp b/src/MeoAssistance/ProcessTask.cpp index 5245a46dea..1a013880dc 100644 --- a/src/MeoAssistance/ProcessTask.cpp +++ b/src/MeoAssistance/ProcessTask.cpp @@ -5,7 +5,7 @@ #include -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Controller.h" #include "PenguinUploader.h" #include "ProcessTaskImageAnalyzer.h" @@ -101,7 +101,7 @@ bool ProcessTask::run() auto& opt = resource.cfg().get_options(); if (opt.print_window) { - static const std::string dirname = GetCurrentDir() + "screenshot\\"; + static const std::string dirname = utils::get_cur_dir() + "screenshot\\"; save_image(image, dirname); } if (opt.upload_to_penguin) { diff --git a/src/MeoAssistance/ProcessTaskImageAnalyzer.cpp b/src/MeoAssistance/ProcessTaskImageAnalyzer.cpp index ee1235251a..162837a2eb 100644 --- a/src/MeoAssistance/ProcessTaskImageAnalyzer.cpp +++ b/src/MeoAssistance/ProcessTaskImageAnalyzer.cpp @@ -3,7 +3,7 @@ #include "Resource.h" #include "MatchImageAnalyzer.h" #include "OcrImageAnalyzer.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" asst::ProcessTaskImageAnalyzer::ProcessTaskImageAnalyzer(const cv::Mat& image, std::vector tasks_name) : AbstractImageAnalyzer(image), @@ -42,7 +42,7 @@ bool asst::ProcessTaskImageAnalyzer::ocr_analyze(std::shared_ptr task_ for (const TextRect& tr : m_ocr_cache) { TextRect temp = tr; for (const auto& [old_str, new_str] : ocr_task_ptr->replace_map) { - temp.text = StringReplaceAll(temp.text, old_str, new_str); + temp.text = utils::string_replace_all(temp.text, old_str, new_str); } for (const auto& text : ocr_task_ptr->text) { bool flag = false; diff --git a/src/MeoAssistance/ScreenCaptureTask.cpp b/src/MeoAssistance/ScreenCaptureTask.cpp index da0dd68cf5..7845325dd7 100644 --- a/src/MeoAssistance/ScreenCaptureTask.cpp +++ b/src/MeoAssistance/ScreenCaptureTask.cpp @@ -1,6 +1,6 @@ #include "ScreenCaptureTask.h" -#include "AsstAux.h" +#include "AsstUtils.hpp" #include "Controller.h" @@ -8,6 +8,6 @@ bool asst::ScreenCaptureTask::run() { const auto& image = ctrler.get_image(); - static const std::string dirname = GetCurrentDir() + "template\\"; + static const std::string dirname = utils::get_cur_dir() + "template\\"; return save_image(image, dirname); } \ No newline at end of file