diff --git a/source/MaaUtils/Logger/Logger.cpp b/source/MaaUtils/Logger/Logger.cpp index 34fd9a48e..e76550671 100644 --- a/source/MaaUtils/Logger/Logger.cpp +++ b/source/MaaUtils/Logger/Logger.cpp @@ -184,20 +184,7 @@ void Logger::open() if (ofs_.is_open()) { ofs_.close(); } - -#ifdef _WIN32 - - // https://stackoverflow.com/questions/55513974/controlling-inheritability-of-file-handles-created-by-c-stdfstream-in-window - std::string str_log_path = path_to_crt_string(log_path_); - FILE* file_ptr = fopen(str_log_path.c_str(), "a"); - SetHandleInformation((HANDLE)_get_osfhandle(_fileno(file_ptr)), HANDLE_FLAG_INHERIT, 0); - ofs_ = std::ofstream(file_ptr); - -#else - ofs_ = std::ofstream(log_path_, std::ios::out | std::ios::app); - -#endif } void Logger::close() diff --git a/source/MaaUtils/Platform/PlatformPosix.cpp b/source/MaaUtils/Platform/PlatformPosix.cpp index b62f7ed4a..a685b49ec 100644 --- a/source/MaaUtils/Platform/PlatformPosix.cpp +++ b/source/MaaUtils/Platform/PlatformPosix.cpp @@ -21,16 +21,6 @@ std::string path_to_utf8_string(const std::filesystem::path& path) return path.native(); } -std::string path_to_ansi_string(const std::filesystem::path& path) -{ - return path.native(); -} - -std::string path_to_crt_string(const std::filesystem::path& path) -{ - return path.native(); -} - MAA_NS_END -#endif \ No newline at end of file +#endif diff --git a/source/MaaUtils/Platform/PlatformWin32.cpp b/source/MaaUtils/Platform/PlatformWin32.cpp index bd28353c3..1d40021f2 100644 --- a/source/MaaUtils/Platform/PlatformWin32.cpp +++ b/source/MaaUtils/Platform/PlatformWin32.cpp @@ -18,79 +18,6 @@ std::string path_to_utf8_string(const std::filesystem::path& path) return from_osstring(osstr); } -std::string get_ansi_short_path(const std::filesystem::path& path) -{ - wchar_t short_path[MAX_PATH] = { 0 }; - auto osstr = path.native(); - string_replace_all_(osstr, L"\\", L"/"); - auto shortlen = GetShortPathNameW(osstr.c_str(), short_path, MAX_PATH); - if (shortlen == 0) { - return {}; - } - BOOL failed = FALSE; - auto ansilen = - WideCharToMultiByte(CP_ACP, 0, short_path, shortlen, nullptr, 0, nullptr, &failed); - if (failed) { - return {}; - } - std::string result(ansilen, 0); - WideCharToMultiByte(CP_ACP, 0, short_path, shortlen, result.data(), ansilen, nullptr, nullptr); - return result; -} - -std::string path_to_crt_string(const std::filesystem::path& path) -{ - // UCRT may use UTF-8 encoding while ANSI code page is still some other MBCS encoding - // so we use CRT wcstombs instead of WideCharToMultiByte - size_t mbsize = 0; - auto osstr = path.native(); - string_replace_all_(osstr, L"\\", L"/"); - auto err = wcstombs_s(&mbsize, nullptr, 0, osstr.c_str(), osstr.size()); - if (err != 0) { - // cannot convert (CRT is not using UTF-8), fallback to short path name in ACP - return get_ansi_short_path(path); - } - std::string result(mbsize, 0); - err = wcstombs_s(&mbsize, result.data(), mbsize, osstr.c_str(), osstr.size()); - if (err != 0) { - return {}; - } - return result.substr(0, mbsize - 1); -} - -std::string path_to_ansi_string(const std::filesystem::path& path) -{ - // UCRT may use UTF-8 encoding while ANSI code page is still some other MBCS encoding - // so we use CRT wcstombs instead of WideCharToMultiByte - BOOL failed = FALSE; - auto osstr = path.native(); - string_replace_all_(osstr, L"\\", L"/"); - auto ansilen = WideCharToMultiByte( - CP_ACP, - 0, - osstr.c_str(), - (int)osstr.size(), - nullptr, - 0, - nullptr, - &failed); - if (failed) { - // contains character that cannot be converted, fallback to short path name in ACP - return get_ansi_short_path(path); - } - std::string result(ansilen, 0); - WideCharToMultiByte( - CP_ACP, - 0, - osstr.c_str(), - (int)osstr.size(), - result.data(), - ansilen, - nullptr, - &failed); - return result; -} - os_string to_osstring(std::string_view utf8_str) { int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.data(), (int)utf8_str.size(), nullptr, 0); @@ -260,6 +187,8 @@ std::set list_processes() } HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + OnScopeLeave([&]() { CloseHandle(process); }); + if (process == nullptr) { auto error = GetLastError(); LogWarn << "Failed to OpenProcess" << VAR(error) << VAR(pid); @@ -271,14 +200,12 @@ std::set list_processes() if (!EnumProcessModules(process, &mod, sizeof(mod), &mod_read)) { auto error = GetLastError(); LogWarn << "Failed to EnumProcessModules" << VAR(error) << VAR(pid); - CloseHandle(process); continue; } memset(name_buff, 0, sizeof(name_buff)); GetModuleBaseNameW(process, mod, name_buff, sizeof(name_buff) / sizeof(WCHAR)); - CloseHandle(process); result.emplace(pid, from_osstring(name_buff)); } @@ -293,6 +220,8 @@ std::set list_processes() std::optional get_process_path(os_pid pid) { HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + OnScopeLeave([&]() { CloseHandle(process); }); + if (process == nullptr) { auto error = GetLastError(); LogError << "Failed to OpenProcess" << VAR(error) << VAR(pid); @@ -306,10 +235,9 @@ std::optional get_process_path(os_pid pid) return std::nullopt; } - CloseHandle(process); return filename; } MAA_NS_END -#endif \ No newline at end of file +#endif diff --git a/source/MaaUtils/Runtime/Runtime_Win.cpp b/source/MaaUtils/Runtime/Runtime_Win.cpp index 8bc9852b0..14a8f9206 100644 --- a/source/MaaUtils/Runtime/Runtime_Win.cpp +++ b/source/MaaUtils/Runtime/Runtime_Win.cpp @@ -15,9 +15,8 @@ const std::filesystem::path& library_dir() void init_library_dir(HINSTANCE hinstDLL) { - char buffer[MAX_PATH + 1] = { 0 }; - GetModuleFileName(hinstDLL, buffer, MAX_PATH); - // it's crt string + WCHAR buffer[MAX_PATH] = { 0 }; + GetModuleFileNameW(hinstDLL, buffer, MAX_PATH); s_library_dir_cache = std::filesystem::path(buffer).parent_path(); } diff --git a/source/binding/Python/maa/instance.py b/source/binding/Python/maa/instance.py index 9c33e5e42..67ba66add 100644 --- a/source/binding/Python/maa/instance.py +++ b/source/binding/Python/maa/instance.py @@ -121,7 +121,7 @@ def inited(self) -> bool: return bool(Library.framework.MaaInited(self._handle)) - async def run_task(self, task_type: str, param: Dict = {}) -> bool: + async def run_task(self, task_type: str, param: Dict = {}) -> Optional[TaskDetail]: """ Async run a task. @@ -134,7 +134,7 @@ async def run_task(self, task_type: str, param: Dict = {}) -> bool: await future.wait() return future.get() - async def run_recogintion(self, task_type: str, param: Dict = {}) -> bool: + async def run_recogintion(self, task_type: str, param: Dict = {}) -> Optional[TaskDetail]: """ Async run a recognition. @@ -147,7 +147,7 @@ async def run_recogintion(self, task_type: str, param: Dict = {}) -> bool: await future.wait() return future.get() - async def run_action(self, task_type: str, param: Dict = {}) -> bool: + async def run_action(self, task_type: str, param: Dict = {}) -> Optional[TaskDetail]: """ Async run a action. diff --git a/source/include/Utils/Platform.h b/source/include/Utils/Platform.h index d8ba48932..a81b30988 100644 --- a/source/include/Utils/Platform.h +++ b/source/include/Utils/Platform.h @@ -33,8 +33,6 @@ inline std::filesystem::path path(std::string_view utf8_str) } MAA_UTILS_API std::string path_to_utf8_string(const std::filesystem::path& path); -MAA_UTILS_API std::string path_to_ansi_string(const std::filesystem::path& path); -MAA_UTILS_API std::string path_to_crt_string(const std::filesystem::path& path); namespace path_literals {