fix(Core/CLI): Replace fgetws with ReadConsoleW for Windows console UTF-8 input (#24725)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
Kitzunu
2026-02-22 14:55:29 +01:00
committed by GitHub
parent 0759d47000
commit cf92cfa59f

View File

@@ -25,7 +25,9 @@
#include "World.h" #include "World.h"
#include <fmt/core.h> #include <fmt/core.h>
#if AC_PLATFORM != AC_PLATFORM_WINDOWS #if AC_PLATFORM == AC_PLATFORM_WINDOWS
#include <windows.h>
#else
#include "Chat.h" #include "Chat.h"
#include "ChatCommand.h" #include "ChatCommand.h"
#include <cstring> #include <cstring>
@@ -108,6 +110,10 @@ int kb_hit_return()
void CliThread() void CliThread()
{ {
#if AC_PLATFORM == AC_PLATFORM_WINDOWS #if AC_PLATFORM == AC_PLATFORM_WINDOWS
// Set console code pages to UTF-8
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
// print this here the first time // print this here the first time
// later it will be printed after command queue updates // later it will be printed after command queue updates
PrintCliPrefix(); PrintCliPrefix();
@@ -134,6 +140,14 @@ void CliThread()
fInfo.dwTimeout = 0; fInfo.dwTimeout = 0;
FlashWindowEx(&fInfo); FlashWindowEx(&fInfo);
} }
// Get console input handle once for reading commands
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (hStdIn == INVALID_HANDLE_VALUE)
{
LOG_ERROR("server.worldserver", "Failed to get console input handle");
return;
}
#endif #endif
///- As long as the World is running (no World::m_stopEvent), get the command line and handle it ///- As long as the World is running (no World::m_stopEvent), get the command line and handle it
@@ -145,12 +159,18 @@ void CliThread()
#if AC_PLATFORM == AC_PLATFORM_WINDOWS #if AC_PLATFORM == AC_PLATFORM_WINDOWS
wchar_t commandbuf[256]; wchar_t commandbuf[256];
if (fgetws(commandbuf, sizeof(commandbuf), stdin)) DWORD charsRead = 0;
if (ReadConsoleW(hStdIn, commandbuf, sizeof(commandbuf) / sizeof(wchar_t) - 1, &charsRead, nullptr))
{ {
if (!WStrToUtf8(commandbuf, wcslen(commandbuf), command)) if (charsRead > 0)
{ {
PrintCliPrefix(); commandbuf[charsRead] = L'\0';
continue; if (!WStrToUtf8(commandbuf, charsRead, command))
{
PrintCliPrefix();
continue;
}
} }
} }
#else #else