Skip to content

Commit

Permalink
fix: 一些小问题优化改动
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed May 17, 2024
1 parent 5ad31a6 commit 98c757d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 109 deletions.
13 changes: 0 additions & 13 deletions source/MaaUtils/Logger/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 1 addition & 11 deletions source/MaaUtils/Platform/PlatformPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
#endif
82 changes: 5 additions & 77 deletions source/MaaUtils/Platform/PlatformWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -260,6 +187,8 @@ std::set<ProcessInfo> 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);
Expand All @@ -271,14 +200,12 @@ std::set<ProcessInfo> 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));
}
Expand All @@ -293,6 +220,8 @@ std::set<ProcessInfo> list_processes()
std::optional<std::filesystem::path> 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);
Expand All @@ -306,10 +235,9 @@ std::optional<std::filesystem::path> get_process_path(os_pid pid)
return std::nullopt;
}

CloseHandle(process);
return filename;
}

MAA_NS_END

#endif
#endif
5 changes: 2 additions & 3 deletions source/MaaUtils/Runtime/Runtime_Win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions source/binding/Python/maa/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions source/include/Utils/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 98c757d

Please sign in to comment.