From 7f409d6e033680baebad2f293362362bfd8a75e5 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 27 Mar 2024 11:05:59 +0800 Subject: [PATCH] fix --- source/MaaProjectInterface/CLI/interactor.cpp | 28 +-- source/MaaProjectInterface/Impl/Parser.cpp | 3 + source/MaaUtils/Logger/Logger.cpp | 30 ++- source/include/Utils/Locale.hpp | 226 ------------------ source/include/Utils/LoggerUtils.h | 2 + 5 files changed, 48 insertions(+), 241 deletions(-) delete mode 100644 source/include/Utils/Locale.hpp diff --git a/source/MaaProjectInterface/CLI/interactor.cpp b/source/MaaProjectInterface/CLI/interactor.cpp index ddee7d754..a6433a9e0 100644 --- a/source/MaaProjectInterface/CLI/interactor.cpp +++ b/source/MaaProjectInterface/CLI/interactor.cpp @@ -163,7 +163,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), @@ -171,7 +171,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); @@ -183,11 +183,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 +244,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 +307,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 +350,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 +376,7 @@ 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 +398,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,14 +477,14 @@ 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) + std::cout << "\t\t- " << MAA_LOG_NS::utf8_to_crt(key) << ": " << MAA_LOG_NS::utf8_to_crt(value) << "\n"; } } @@ -507,5 +507,5 @@ 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; + std::cout << MAA_LOG_NS::utf8_to_crt(std::format("on_maafw_notify: {} {}", msg, entry)) << std::endl; } \ No newline at end of file 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/MaaUtils/Logger/Logger.cpp b/source/MaaUtils/Logger/Logger.cpp index 097986bd9..34fd9a48e 100644 --- a/source/MaaUtils/Logger/Logger.cpp +++ b/source/MaaUtils/Logger/Logger.cpp @@ -13,7 +13,6 @@ #include "Utils/Codec.h" #include "Utils/ImageIo.h" -#include "Utils/Locale.hpp" #include "Utils/Platform.h" #include "Utils/Uuid.h" @@ -21,6 +20,35 @@ 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"); 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 04b30f783..a62e37953 100644 --- a/source/include/Utils/LoggerUtils.h +++ b/source/include/Utils/LoggerUtils.h @@ -36,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,