From cf92cfa59f7cdba21e3c2035b342aaafec85b4e9 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 22 Feb 2026 14:55:29 +0100 Subject: [PATCH] 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> --- .../worldserver/CommandLine/CliRunnable.cpp | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/server/apps/worldserver/CommandLine/CliRunnable.cpp b/src/server/apps/worldserver/CommandLine/CliRunnable.cpp index 78b723a27..307bee1a5 100644 --- a/src/server/apps/worldserver/CommandLine/CliRunnable.cpp +++ b/src/server/apps/worldserver/CommandLine/CliRunnable.cpp @@ -25,7 +25,9 @@ #include "World.h" #include -#if AC_PLATFORM != AC_PLATFORM_WINDOWS +#if AC_PLATFORM == AC_PLATFORM_WINDOWS +#include +#else #include "Chat.h" #include "ChatCommand.h" #include @@ -108,6 +110,10 @@ int kb_hit_return() void CliThread() { #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 // later it will be printed after command queue updates PrintCliPrefix(); @@ -134,6 +140,14 @@ void CliThread() fInfo.dwTimeout = 0; 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 ///- 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 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(); - continue; + commandbuf[charsRead] = L'\0'; + if (!WStrToUtf8(commandbuf, charsRead, command)) + { + PrintCliPrefix(); + continue; + } } } #else