diff --git a/source/LibraryHolder/CMakeLists.txt b/source/LibraryHolder/CMakeLists.txt index 716b5e8a8..0390ccb98 100644 --- a/source/LibraryHolder/CMakeLists.txt +++ b/source/LibraryHolder/CMakeLists.txt @@ -7,7 +7,7 @@ set_target_properties(LibraryHolder PROPERTIES POSITION_INDEPENDENT_CODE ON) target_include_directories(LibraryHolder PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${MAA_PRIVATE_INC} ${MAA_PUBLIC_INC}) -target_link_libraries(LibraryHolder MaaUtils HeaderOnlyLibraries Boost::system) +target_link_libraries(LibraryHolder MaaUtils HeaderOnlyLibraries Boost::system ${OpenCV_LIBS}) add_dependencies(LibraryHolder MaaUtils) diff --git a/source/LibraryHolder/ControlUnit/ControlUnit.cpp b/source/LibraryHolder/ControlUnit/ControlUnit.cpp index 60da3d41f..e5422b597 100644 --- a/source/LibraryHolder/ControlUnit/ControlUnit.cpp +++ b/source/LibraryHolder/ControlUnit/ControlUnit.cpp @@ -23,8 +23,8 @@ bool check_version(const std::string& func_name) LogInfo << typeid(ControlUnitT).name() << "Library version:" << version; if (std::strcmp(version, MAA_VERSION) != 0) { - LogWarn << "ControlUnit and MaaFramework are not same version," - << "ControlUnit:" << version << "MaaFramework:" << MAA_VERSION; + LogWarn << "ControlUnit and MaaFramework are not same version," << "ControlUnit:" << version + << "MaaFramework:" << MAA_VERSION; } return true; } @@ -219,4 +219,4 @@ std::shared_ptr destroy_control_unit); } -MAA_NS_END \ No newline at end of file +MAA_NS_END diff --git a/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp b/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp index 45c898e9f..4f2105fc6 100644 --- a/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp +++ b/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp @@ -15,6 +15,7 @@ #include "Screencap/RawByNetcat.h" #include "Screencap/RawWithGzip.h" #include "Utils/Logger.h" +#include "Utils/Platform.h" using MAA_NS::path; diff --git a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp index f29ecb500..703280d46 100644 --- a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp @@ -5,8 +5,8 @@ #include #include -#include "Utils/Codec.h" #include "Utils/Logger.h" +#include "Utils/Platform.h" MAA_CTRL_UNIT_NS_BEGIN diff --git a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp index 4043d19aa..4d5236783 100644 --- a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp @@ -6,6 +6,7 @@ #include #include "Utils/Logger.h" +#include "Utils/Platform.h" MAA_CTRL_UNIT_NS_BEGIN diff --git a/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp index a3b794ef0..c96391b88 100644 --- a/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp +++ b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp @@ -4,6 +4,7 @@ #include "MaaFramework/MaaMsg.h" #include "Utils/Logger.h" +#include "Utils/Platform.h" MAA_CTRL_UNIT_NS_BEGIN diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp index 95094b4e1..ca5907020 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp @@ -5,7 +5,7 @@ #include #include "Utils/Logger.h" -#include "Utils/NoWarningCV.hpp" +#include "Utils/Platform.h" MAA_CTRL_UNIT_NS_BEGIN @@ -77,4 +77,4 @@ bool MinicapBase::init(int swidth, int sheight) return set_wh(swidth, sheight); } -MAA_CTRL_UNIT_NS_END \ No newline at end of file +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp b/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp index 9dc44d79f..4058fe281 100644 --- a/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp +++ b/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp @@ -5,6 +5,7 @@ #include "CarouselImage/CarouselImage.h" #include "ReplayRecording/ReplayRecordingMgr.h" #include "Utils/Logger.h" +#include "Utils/Platform.h" MaaStringView MaaDbgControlUnitGetVersion() { @@ -43,4 +44,4 @@ void MaaDbgControlUnitDestroy(MaaControlUnitHandle handle) if (handle) { delete handle; } -} \ No newline at end of file +} diff --git a/source/MaaFramework/Instance/InstanceStatus.cpp b/source/MaaFramework/Instance/InstanceStatus.cpp index cf372a268..b93ec8103 100644 --- a/source/MaaFramework/Instance/InstanceStatus.cpp +++ b/source/MaaFramework/Instance/InstanceStatus.cpp @@ -1,6 +1,7 @@ #include "InstanceStatus.h" #include "Utils/Logger.h" +#include "Utils/NoWarningCV.hpp" MAA_NS_BEGIN @@ -128,4 +129,4 @@ bool InstanceStatus::cv_mat_equal(const cv::Mat& lhs, const cv::Mat& rhs) return eq2; } -MAA_NS_END \ No newline at end of file +MAA_NS_END diff --git a/source/MaaFramework/Resource/PipelineResMgr.cpp b/source/MaaFramework/Resource/PipelineResMgr.cpp index b6bf645b7..78e64568c 100644 --- a/source/MaaFramework/Resource/PipelineResMgr.cpp +++ b/source/MaaFramework/Resource/PipelineResMgr.cpp @@ -2,6 +2,7 @@ #include "Utils/Codec.h" #include "Utils/Logger.h" +#include "Utils/Platform.h" #include "Utils/StringMisc.hpp" #include "Vision/VisionTypes.h" @@ -1417,4 +1418,4 @@ bool PipelineResMgr::parse_action_target( return true; } -MAA_RES_NS_END \ No newline at end of file +MAA_RES_NS_END diff --git a/source/MaaFramework/Task/PipelineTask.cpp b/source/MaaFramework/Task/PipelineTask.cpp index 62f432e30..4c41907ea 100644 --- a/source/MaaFramework/Task/PipelineTask.cpp +++ b/source/MaaFramework/Task/PipelineTask.cpp @@ -10,6 +10,7 @@ #include "Task/CustomAction.h" #include "Utils/ImageIo.h" #include "Utils/Logger.h" +#include "Utils/Uuid.h" MAA_TASK_NS_BEGIN @@ -292,4 +293,4 @@ std::filesystem::path PipelineTask::dump_image(const cv::Mat& image) const return filepath; } -MAA_TASK_NS_END \ No newline at end of file +MAA_TASK_NS_END diff --git a/source/MaaProjectInterface/CLI/interactor.cpp b/source/MaaProjectInterface/CLI/interactor.cpp index ab8a36a02..808319a03 100644 --- a/source/MaaProjectInterface/CLI/interactor.cpp +++ b/source/MaaProjectInterface/CLI/interactor.cpp @@ -1,6 +1,8 @@ #include "interactor.h" +#include #include +#include #include #include "MaaToolkit/Device/MaaToolkitDevice.h" @@ -162,7 +164,7 @@ void Interactor::print_config() const std::cout << "Controller:\n\n"; std::cout << "\t" - << MaaNS::utf8_to_crt(std::format( + << MAA_LOG_NS::utf8_to_crt(std::format( "{}\n\t\t{}\n\t\t{}", config_.configuration().controller.name, MaaNS::path_to_utf8_string(config_.configuration().controller.adb_path), @@ -170,7 +172,7 @@ void Interactor::print_config() const << "\n\n"; std::cout << "Resource:\n\n"; - std::cout << "\t" << MaaNS::utf8_to_crt(config_.configuration().resource) << "\n\n"; + std::cout << "\t" << MAA_LOG_NS::utf8_to_crt(config_.configuration().resource) << "\n\n"; std::cout << "Tasks:\n\n"; print_config_tasks(false); @@ -182,11 +184,11 @@ void Interactor::welcome() const std::cout << "Welcome to use Maa Project Interface CLI!\n"; } else { - std::cout << MaaNS::utf8_to_crt(config_.interface_data().message) << "\n"; + std::cout << MAA_LOG_NS::utf8_to_crt(config_.interface_data().message) << "\n"; } std::cout << "MaaFramework: " << MAA_VERSION << "\n\n"; - std::cout << "Version: " << MaaNS::utf8_to_crt(config_.interface_data().version) << "\n\n"; + std::cout << "Version: " << MAA_LOG_NS::utf8_to_crt(config_.interface_data().version) << "\n\n"; } bool Interactor::interact_once() @@ -244,7 +246,7 @@ void Interactor::select_controller() if (all_controllers.size() != 1) { std::cout << "### Select controller ###\n\n"; for (size_t i = 0; i < all_controllers.size(); ++i) { - std::cout << MaaNS::utf8_to_crt( + std::cout << MAA_LOG_NS::utf8_to_crt( std::format("\t{}. {}\n", i + 1, all_controllers[i].name)); } std::cout << "\n"; @@ -307,7 +309,7 @@ void Interactor::select_adb_auto_detect() std::string path = MaaToolkitGetDeviceAdbPath(i); std::string address = MaaToolkitGetDeviceAdbSerial(i); - std::cout << MaaNS::utf8_to_crt( + std::cout << MAA_LOG_NS::utf8_to_crt( std::format("\t{}. {}\n\t\t{}\n\t\t{}\n", i + 1, name, path, address)); } std::cout << "\n"; @@ -350,7 +352,7 @@ void Interactor::select_resource() if (all_resources.size() != 1) { std::cout << "### Select resource ###\n\n"; for (size_t i = 0; i < all_resources.size(); ++i) { - std::cout << MaaNS::utf8_to_crt( + std::cout << MAA_LOG_NS::utf8_to_crt( std::format("\t{}. {}\n", i + 1, all_resources[i].name)); } std::cout << "\n"; @@ -376,7 +378,8 @@ void Interactor::add_task() std::cout << "### Add task ###\n\n"; for (size_t i = 0; i < all_data_tasks.size(); ++i) { - std::cout << MaaNS::utf8_to_crt(std::format("\t{}. {}\n", i + 1, all_data_tasks[i].name)); + std::cout << MAA_LOG_NS::utf8_to_crt( + std::format("\t{}. {}\n", i + 1, all_data_tasks[i].name)); } std::cout << "\n"; auto input_indexes = input_multi(all_data_tasks.size()); @@ -398,12 +401,12 @@ void Interactor::add_task() Configuration::Option { option_name, opt.default_case }); continue; } - std::cout << MaaNS::utf8_to_crt(std::format( + std::cout << MAA_LOG_NS::utf8_to_crt(std::format( "\n\n## Input option of \"{}\" for \"{}\" ##\n\n", option_name, data_task.name)); for (size_t i = 0; i < opt.cases.size(); ++i) { - std::cout << MaaNS::utf8_to_crt( + std::cout << MAA_LOG_NS::utf8_to_crt( std::format("\t{}. {}\n", i + 1, opt.cases[i].name)); } std::cout << "\n"; @@ -477,15 +480,15 @@ void Interactor::print_config_tasks(bool with_index) const for (size_t i = 0; i < all_config_tasks.size(); ++i) { const auto& task = all_config_tasks[i]; if (with_index) { - std::cout << MaaNS::utf8_to_crt(std::format("\t{}. {}\n", i + 1, task.name)); + std::cout << MAA_LOG_NS::utf8_to_crt(std::format("\t{}. {}\n", i + 1, task.name)); } else { - std::cout << MaaNS::utf8_to_crt(std::format("\t- {}\n", task.name)); + std::cout << MAA_LOG_NS::utf8_to_crt(std::format("\t- {}\n", task.name)); } for (const auto& [key, value] : task.option) { - std::cout << "\t\t- " << MaaNS::utf8_to_crt(key) << ": " << MaaNS::utf8_to_crt(value) - << "\n"; + std::cout << "\t\t- " << MAA_LOG_NS::utf8_to_crt(key) << ": " + << MAA_LOG_NS::utf8_to_crt(value) << "\n"; } } std::cout << "\n"; @@ -507,5 +510,6 @@ void Interactor::on_maafw_notify( std::ignore = pthis; std::string entry = json::parse(details_json).value_or(json::value())["entry"].as_string(); - std::cout << MaaNS::utf8_to_crt(std::format("on_maafw_notify: {} {}", msg, entry)) << std::endl; -} \ No newline at end of file + std::cout << MAA_LOG_NS::utf8_to_crt(std::format("on_maafw_notify: {} {}", msg, entry)) + << std::endl; +} diff --git a/source/MaaProjectInterface/CLI/main.cpp b/source/MaaProjectInterface/CLI/main.cpp index bbbb4a3aa..08ed40511 100644 --- a/source/MaaProjectInterface/CLI/main.cpp +++ b/source/MaaProjectInterface/CLI/main.cpp @@ -3,6 +3,7 @@ #include "MaaToolkit/MaaToolkitAPI.h" #include "Utils/Runtime.h" +#include "Utils/Platform.h" #include "interactor.h" diff --git a/source/MaaProjectInterface/Impl/Parser.cpp b/source/MaaProjectInterface/Impl/Parser.cpp index 01a4eadb7..342594f01 100644 --- a/source/MaaProjectInterface/Impl/Parser.cpp +++ b/source/MaaProjectInterface/Impl/Parser.cpp @@ -1,5 +1,8 @@ #include "ProjectInterface/Parser.h" +#include +#include + #include "Utils/Logger.h" MAA_PROJECT_INTERFACE_NS_BEGIN diff --git a/source/MaaToolkit/API/MaaToolkitConfig.cpp b/source/MaaToolkit/API/MaaToolkitConfig.cpp index 0c9201c71..782fe9780 100644 --- a/source/MaaToolkit/API/MaaToolkitConfig.cpp +++ b/source/MaaToolkit/API/MaaToolkitConfig.cpp @@ -5,6 +5,7 @@ #include "Config/GlobalOptionConfig.h" #include "Utils/Logger.h" #include "Utils/Runtime.h" +#include "Utils/Platform.h" MaaBool MaaToolkitInitOptionConfig(MaaStringView user_path, MaaStringView default_json) { diff --git a/source/MaaToolkit/API/MaaToolkitExecAgent.cpp b/source/MaaToolkit/API/MaaToolkitExecAgent.cpp index da9ac12da..0cc083139 100644 --- a/source/MaaToolkit/API/MaaToolkitExecAgent.cpp +++ b/source/MaaToolkit/API/MaaToolkitExecAgent.cpp @@ -7,6 +7,7 @@ #include "ExecAgent/ActionExecAgent.h" #include "ExecAgent/RecognizerExecAgent.h" #include "Utils/Logger.h" +#include "Utils/Platform.h" using namespace MAA_TOOLKIT_NS; @@ -136,4 +137,4 @@ MaaBool MaaToolkitUnregisterCustomActionExecutor(MaaInstanceHandle handle, MaaStringView action_name) { return UnregisterExecutor(ExecutorType::Action, handle, action_name); -} \ No newline at end of file +} diff --git a/source/MaaToolkit/AdbDevice/DeviceMgr.cpp b/source/MaaToolkit/AdbDevice/DeviceMgr.cpp index 4503e0d23..72710041b 100644 --- a/source/MaaToolkit/AdbDevice/DeviceMgr.cpp +++ b/source/MaaToolkit/AdbDevice/DeviceMgr.cpp @@ -2,6 +2,7 @@ #include +#include "ControlUnit/ControlUnitAPI.h" #include "LibraryHolder/ControlUnit.h" #include "Utils/Logger.h" @@ -168,4 +169,4 @@ MaaAdbControllerType DeviceMgr::check_adb_controller_type( return kInputType | kScreencapType; } -MAA_TOOLKIT_NS_END \ No newline at end of file +MAA_TOOLKIT_NS_END diff --git a/source/MaaToolkit/ExecAgent/ExecAgentBase.h b/source/MaaToolkit/ExecAgent/ExecAgentBase.h index 823e54836..1305a68c5 100644 --- a/source/MaaToolkit/ExecAgent/ExecAgentBase.h +++ b/source/MaaToolkit/ExecAgent/ExecAgentBase.h @@ -6,6 +6,7 @@ #include #include #include +#include #include diff --git a/source/MaaUtils/CMakeLists.txt b/source/MaaUtils/CMakeLists.txt index 6b4feab7e..fbab43469 100644 --- a/source/MaaUtils/CMakeLists.txt +++ b/source/MaaUtils/CMakeLists.txt @@ -4,7 +4,7 @@ file(GLOB_RECURSE maa_utils_header ${MAA_PRIVATE_INC}/Utils/*) add_library(MaaUtils SHARED ${maa_utils_src} ${maa_utils_header}) target_include_directories(MaaUtils PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${MAA_PRIVATE_INC} ${MAA_PUBLIC_INC}) -target_link_libraries(MaaUtils PRIVATE HeaderOnlyLibraries Boost::system) +target_link_libraries(MaaUtils PRIVATE HeaderOnlyLibraries Boost::system ${OpenCV_LIBS}) target_compile_definitions(MaaUtils PRIVATE MAA_UTILS_EXPORTS) install( diff --git a/source/MaaUtils/Logger/Logger.cpp b/source/MaaUtils/Logger/Logger.cpp index 862744552..34fd9a48e 100644 --- a/source/MaaUtils/Logger/Logger.cpp +++ b/source/MaaUtils/Logger/Logger.cpp @@ -2,19 +2,53 @@ #include "Utils/Logger.h" -#include "Utils/Platform.h" - #ifdef _WIN32 +#include "Utils/SafeWindows.hpp" + #include #include #else #include #endif +#include "Utils/Codec.h" +#include "Utils/ImageIo.h" +#include "Utils/Platform.h" +#include "Utils/Uuid.h" + #pragma message("MaaUtils MAA_VERSION: " MAA_VERSION) MAA_LOG_NS_BEGIN +std::string utf8_to_crt(std::string_view utf8_str) +{ +#ifdef _WIN32 + const char* src_str = utf8_str.data(); + const int byte_len = static_cast(utf8_str.length() * sizeof(char)); + int len = MultiByteToWideChar(CP_UTF8, 0, src_str, byte_len, nullptr, 0); + const std::size_t wsz_ansi_length = static_cast(len) + 1U; + auto wsz_ansi = new wchar_t[wsz_ansi_length]; + memset(wsz_ansi, 0, sizeof(wsz_ansi[0]) * wsz_ansi_length); + MultiByteToWideChar(CP_UTF8, 0, src_str, byte_len, wsz_ansi, len); + + len = WideCharToMultiByte(CP_ACP, 0, wsz_ansi, -1, nullptr, 0, nullptr, nullptr); + const std::size_t sz_ansi_length = static_cast(len) + 1; + auto sz_ansi = new char[sz_ansi_length]; + memset(sz_ansi, 0, sizeof(sz_ansi[0]) * sz_ansi_length); + WideCharToMultiByte(CP_ACP, 0, wsz_ansi, -1, sz_ansi, len, nullptr, nullptr); + std::string strTemp(sz_ansi); + + delete[] wsz_ansi; + wsz_ansi = nullptr; + delete[] sz_ansi; + sz_ansi = nullptr; + + return strTemp; +#else + return std::string(utf8_str); +#endif +} + constexpr separator separator::none(""); constexpr separator separator::space(" "); constexpr separator separator::tab("\t"); @@ -222,4 +256,32 @@ LogStream Logger::internal_dbg() return debug("Logger"); } +std::string StringConverter::operator()(const std::filesystem::path& path) const +{ + return path_to_utf8_string(path); +} + +std::string StringConverter::operator()(const std::wstring& wstr) const +{ + return from_u16(wstr); +} + +std::string StringConverter::operator()(const cv::Mat& image) const +{ + if (dumps_dir_.empty()) { + return "Not logging"; + } + if (image.empty()) { + return "Empty image"; + } + + std::string filename = std::format("{}-{}.png", format_now_for_filename(), make_uuid()); + auto filepath = dumps_dir_ / path(filename); + bool ret = MAA_NS::imwrite(filepath, image); + if (!ret) { + return "Failed to write image"; + } + return this->operator()(filepath); +} + MAA_LOG_NS_END diff --git a/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp b/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp index 1d48ab148..3ebffea91 100644 --- a/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp +++ b/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp @@ -2,6 +2,7 @@ #include "Utils/Logger.h" #include "Utils/SafeWindows.hpp" +#include "Utils/Platform.h" MAA_CTRL_UNIT_NS_BEGIN diff --git a/source/MaaWin32ControlUnit/Manager/ControlUnitMgr.h b/source/MaaWin32ControlUnit/Manager/ControlUnitMgr.h index 3e3064793..8a7742d0e 100644 --- a/source/MaaWin32ControlUnit/Manager/ControlUnitMgr.h +++ b/source/MaaWin32ControlUnit/Manager/ControlUnitMgr.h @@ -5,6 +5,7 @@ #include "Base/UnitBase.h" #include "ControlUnit/ControlUnitAPI.h" #include "Utils/MessageNotifier.hpp" +#include "Utils/SafeWindows.hpp" MAA_CTRL_UNIT_NS_BEGIN diff --git a/source/MaaWin32ControlUnit/Screencap/FramePoolScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/FramePoolScreencap.cpp index a6058076a..4348966e3 100644 --- a/source/MaaWin32ControlUnit/Screencap/FramePoolScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/FramePoolScreencap.cpp @@ -7,6 +7,7 @@ #include "HwndUtils.hpp" #include "Utils/Logger.h" +#include "Utils/NoWarningCV.hpp" MAA_CTRL_UNIT_NS_BEGIN @@ -186,4 +187,4 @@ bool FramePoolScreencap::init_texture(winrt::com_ptr raw_textur MAA_CTRL_UNIT_NS_END -#endif \ No newline at end of file +#endif diff --git a/source/include/LibraryHolder/ControlUnit.h b/source/include/LibraryHolder/ControlUnit.h index 9f3eb4aee..ec167b9cf 100644 --- a/source/include/LibraryHolder/ControlUnit.h +++ b/source/include/LibraryHolder/ControlUnit.h @@ -1,12 +1,14 @@ #pragma once -#include "LibraryHolder.h" - #include -#include "ControlUnit/ControlUnitAPI.h" +#include "LibraryHolder.h" #include "Utils/Platform.h" +MAA_CTRL_UNIT_NS_BEGIN +class ControlUnitAPI; +MAA_CTRL_UNIT_NS_END + MAA_NS_BEGIN class AdbControlUnitLibraryHolder : public LibraryHolder diff --git a/source/include/ProjectInterface/Types.h b/source/include/ProjectInterface/Types.h index 75e932e23..ce282d09a 100644 --- a/source/include/ProjectInterface/Types.h +++ b/source/include/ProjectInterface/Types.h @@ -4,9 +4,10 @@ #include #include +#include + #include "Conf/Conf.h" #include "MaaFramework/MaaDef.h" -#include "Utils/JsonExt.hpp" MAA_PROJECT_INTERFACE_NS_BEGIN diff --git a/source/include/Utils/Locale.hpp b/source/include/Utils/Locale.hpp deleted file mode 100644 index 415bdb1c2..000000000 --- a/source/include/Utils/Locale.hpp +++ /dev/null @@ -1,226 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#include "SafeWindows.hpp" -#elif defined(__linux__) -#include -#endif - -#include "Conf/Conf.h" - -MAA_NS_BEGIN - -template -inline std::string ansi_to_utf8(std::string_view ansi_str) -{ -#ifdef _WIN32 - const char* src_str = ansi_str.data(); - const int byte_len = static_cast(ansi_str.length() * sizeof(char)); - int len = MultiByteToWideChar(CP_ACP, 0, src_str, byte_len, nullptr, 0); - const std::size_t wstr_length = static_cast(len) + 1U; - auto wstr = new wchar_t[wstr_length]; - memset(wstr, 0, sizeof(wstr[0]) * wstr_length); - MultiByteToWideChar(CP_ACP, 0, src_str, byte_len, wstr, len); - - len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr); - const std::size_t str_length = static_cast(len) + 1; - auto str = new char[str_length]; - memset(str, 0, sizeof(str[0]) * str_length); - WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, nullptr, nullptr); - std::string strTemp = str; - - delete[] wstr; - wstr = nullptr; - delete[] str; - str = nullptr; - - return strTemp; -#elif defined(__linux__) - iconv_t conv = ::iconv_open("utf-8", "gbk"); - if (conv == (iconv_t)-1) { - // error - return std::string(ansi_str); - } - - const char* src_str = ansi_str.data(); - size_t src_len = ::strlen(src_str) + 1; - size_t dst_len = src_len * 2; // ensure sufficient space - - std::unique_ptr> utf8 = std::make_unique(dst_len); - char* dst = utf8.get(); - - char* in_buf = const_cast(src_str); - char* out_buf = dst; - auto res = ::iconv(conv, &in_buf, &src_len, &out_buf, &dst_len); - if (res == (decltype(res))-1) { - // error - ::iconv_close(conv); - return std::string(ansi_str); - } - - ::iconv_close(conv); - - return dst; -#else - return std::string(ansi_str); -#endif -} - -template -inline std::string utf8_to_ansi(std::string_view utf8_str) -{ -#ifdef _WIN32 - const char* src_str = utf8_str.data(); - const int byte_len = static_cast(utf8_str.length() * sizeof(char)); - int len = MultiByteToWideChar(CP_UTF8, 0, src_str, byte_len, nullptr, 0); - const std::size_t wsz_ansi_length = static_cast(len) + 1U; - auto wsz_ansi = new wchar_t[wsz_ansi_length]; - memset(wsz_ansi, 0, sizeof(wsz_ansi[0]) * wsz_ansi_length); - MultiByteToWideChar(CP_UTF8, 0, src_str, byte_len, wsz_ansi, len); - - len = WideCharToMultiByte(CP_ACP, 0, wsz_ansi, -1, nullptr, 0, nullptr, nullptr); - const std::size_t sz_ansi_length = static_cast(len) + 1; - auto sz_ansi = new char[sz_ansi_length]; - memset(sz_ansi, 0, sizeof(sz_ansi[0]) * sz_ansi_length); - WideCharToMultiByte(CP_ACP, 0, wsz_ansi, -1, sz_ansi, len, nullptr, nullptr); - std::string strTemp(sz_ansi); - - delete[] wsz_ansi; - wsz_ansi = nullptr; - delete[] sz_ansi; - sz_ansi = nullptr; - - return strTemp; -#elif defined(__linux__) - iconv_t conv = ::iconv_open("gbk", "utf-8"); - if (conv == (iconv_t)-1) { - // error - return std::string(utf8_str); - } - - const char* src_str = utf8_str.data(); - size_t src_len = ::strlen(src_str) + 1; - size_t dst_len = src_len * 2; - - std::unique_ptr> ansi = std::make_unique(dst_len); - char* dst = ansi.get(); - - char* in_buf = const_cast(src_str); - char* out_buf = dst; - auto res = ::iconv(conv, &in_buf, &src_len, &out_buf, &dst_len); - if (res == (decltype(res))-1) { - // error - ::iconv_close(conv); - return std::string(utf8_str); - } - - ::iconv_close(conv); - - return dst; -#else - return std::string(utf8_str); -#endif -} - -template -inline std::string utf8_to_unicode_escape(std::string_view utf8_str) -{ -#ifdef _WIN32 - const char* src_str = utf8_str.data(); - int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0); - const std::size_t wstr_length = static_cast(len) + 1U; - auto wstr = new wchar_t[wstr_length]; - memset(wstr, 0, sizeof(wstr[0]) * wstr_length); - MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wstr, len); - - std::string unicode_escape_str = {}; - constinit static char hexcode[] = "0123456789abcdef"; - for (const wchar_t* pchr = wstr; *pchr; ++pchr) { - const wchar_t& chr = *pchr; - if (chr > 255) { - unicode_escape_str += "\\u"; - unicode_escape_str.push_back(hexcode[chr >> 12]); - unicode_escape_str.push_back(hexcode[(chr >> 8) & 15]); - unicode_escape_str.push_back(hexcode[(chr >> 4) & 15]); - unicode_escape_str.push_back(hexcode[chr & 15]); - } - else { - unicode_escape_str.push_back(chr & 255); - } - } - - delete[] wstr; - wstr = nullptr; - - return unicode_escape_str; -#elif defined(__linux__) - auto locale = setlocale(LC_ALL, ""); - - const char* from = utf8_str.data(); - size_t len = strlen(from) + 1; - - std::unique_ptr> to = - std::make_unique(len); - mbstowcs(to.get(), from, len); - - setlocale(LC_ALL, locale); - - std::string unicode_escape_str = {}; - constinit static char hexcode[] = "0123456789abcdef"; - for (const wchar_t* pchr = to.get(); *pchr; ++pchr) { - const wchar_t& chr = *pchr; - if (chr > 255) { - unicode_escape_str += "\\u"; - unicode_escape_str.push_back(hexcode[chr >> 12]); - unicode_escape_str.push_back(hexcode[(chr >> 8) & 15]); - unicode_escape_str.push_back(hexcode[(chr >> 4) & 15]); - unicode_escape_str.push_back(hexcode[chr & 15]); - } - else { - unicode_escape_str.push_back(chr & 255); - } - } - - return unicode_escape_str; -#else - return std::string(utf8_str); -#endif -} - -inline std::string utf8_to_crt(std::string_view utf8_str) -{ -#ifdef _WIN32 - return utf8_to_ansi(utf8_str); -#else - return std::string(utf8_str); -#endif -} - -inline std::string load_file_without_bom(const std::filesystem::path& path) -{ - std::ifstream ifs(path, std::ios::in); - if (!ifs.is_open()) { - return {}; - } - std::stringstream iss; - iss << ifs.rdbuf(); - ifs.close(); - std::string str = iss.str(); - - if (str.starts_with("\xEF\xBB\xBF")) { - str.assign(str.begin() + 3, str.end()); - } - return str; -} - -MAA_NS_END \ No newline at end of file diff --git a/source/include/Utils/LoggerUtils.h b/source/include/Utils/LoggerUtils.h index 5f042e9ef..a62e37953 100644 --- a/source/include/Utils/LoggerUtils.h +++ b/source/include/Utils/LoggerUtils.h @@ -1,5 +1,9 @@ #pragma once +#if defined(__APPLE__) || defined(__linux__) +#include +#endif + #include #include #include @@ -10,22 +14,13 @@ #include #include -#if defined(__APPLE__) || defined(__linux__) -#include -#endif - #include -#include "Conf/Conf.h" #include "MaaFramework/MaaDef.h" #include "MaaFramework/MaaPort.h" -#include "Codec.h" -#include "ImageIo.h" -#include "Locale.hpp" -#include "Platform.h" +#include "Conf/Conf.h" #include "Time.hpp" -#include "Uuid.h" namespace cv { @@ -41,6 +36,8 @@ inline std::ostream& operator<<(std::ostream& os, const std::chrono::millisecond } #endif +MAA_UTILS_API std::string utf8_to_crt(std::string_view utf8_str); + enum class level { fatal = MaaLoggingLevel_Fatal, @@ -70,7 +67,7 @@ struct MAA_UTILS_API separator template concept has_output_operator = requires { std::declval() << std::declval(); }; -class StringConverter +class MAA_UTILS_API StringConverter { public: StringConverter(std::filesystem::path dumps_dir) @@ -79,30 +76,9 @@ class StringConverter } public: - std::string operator()(const std::filesystem::path& path) const - { - return path_to_utf8_string(path); - } - - std::string operator()(const std::wstring& wstr) const { return from_u16(wstr); } - - std::string operator()(const cv::Mat& image) const - { - if (dumps_dir_.empty()) { - return "Not logging"; - } - if (image.empty()) { - return "Empty image"; - } - - std::string filename = std::format("{}-{}.png", format_now_for_filename(), make_uuid()); - auto filepath = dumps_dir_ / path(filename); - bool ret = MAA_NS::imwrite(filepath, image); - if (!ret) { - return "Failed to write image"; - } - return this->operator()(filepath); - } + std::string operator()(const std::filesystem::path& path) const; + std::string operator()(const std::wstring& wstr) const; + std::string operator()(const cv::Mat& image) const; template std::string operator()(const std::optional& value) const