From 9aedb90fc62533df5f42c1b6c5caa8e7bbd5523e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AF=E3=82=84=E3=81=97=E3=81=86=E3=82=81?= <122680126+HayashiUme@users.noreply.github.com> Date: Tue, 9 Jun 2026 23:07:44 +0800 Subject: [PATCH] Set the console codepage to ensure correct text display regardless of encoding (#1337) --- .../Console/Windows/ConsoleEncoding/ConsoleEncoding.cs | 8 ++++---- BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/BepInEx.Core/Console/Windows/ConsoleEncoding/ConsoleEncoding.cs b/BepInEx.Core/Console/Windows/ConsoleEncoding/ConsoleEncoding.cs index b70f1e1..b9d7c1c 100644 --- a/BepInEx.Core/Console/Windows/ConsoleEncoding/ConsoleEncoding.cs +++ b/BepInEx.Core/Console/Windows/ConsoleEncoding/ConsoleEncoding.cs @@ -41,7 +41,7 @@ internal partial class ConsoleEncoding : Encoding public override int GetByteCount(char[] chars, int index, int count) { WriteCharBuffer(chars, index, count); - var result = WideCharToMultiByte(_codePage, 0, chars, count, _zeroByte, 0, IntPtr.Zero, IntPtr.Zero); + var result = WideCharToMultiByte(_codePage, 0, _charBuffer, count, _zeroByte, 0, IntPtr.Zero, IntPtr.Zero); return result; } @@ -50,7 +50,7 @@ internal partial class ConsoleEncoding : Encoding var byteCount = GetByteCount(chars, charIndex, charCount); WriteCharBuffer(chars, charIndex, charCount); ExpandByteBuffer(byteCount); - _ = WideCharToMultiByte(_codePage, 0, chars, charCount, _byteBuffer, byteCount, IntPtr.Zero, + _ = WideCharToMultiByte(_codePage, 0, _charBuffer, charCount, _byteBuffer, byteCount, IntPtr.Zero, IntPtr.Zero); var readCount = Math.Min(bytes.Length, byteCount); ReadByteBuffer(bytes, byteIndex, readCount); @@ -60,7 +60,7 @@ internal partial class ConsoleEncoding : Encoding public override int GetCharCount(byte[] bytes, int index, int count) { WriteByteBuffer(bytes, index, count); - var result = MultiByteToWideChar(_codePage, 0, bytes, count, _zeroChar, 0); + var result = MultiByteToWideChar(_codePage, 0, _byteBuffer, count, _zeroChar, 0); return result; } @@ -69,7 +69,7 @@ internal partial class ConsoleEncoding : Encoding var charCount = GetCharCount(bytes, byteIndex, byteCount); WriteByteBuffer(bytes, byteIndex, byteCount); ExpandCharBuffer(charCount); - _ = MultiByteToWideChar(_codePage, 0, bytes, byteCount, _charBuffer, charCount); + _ = MultiByteToWideChar(_codePage, 0, _byteBuffer, byteCount, _charBuffer, charCount); var readCount = Math.Min(chars.Length, charCount); ReadCharBuffer(chars, charIndex, readCount); return readCount; diff --git a/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs b/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs index 1edb857..802cd52 100644 --- a/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs +++ b/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs @@ -92,8 +92,9 @@ internal class WindowsConsoleDriver : IConsoleDriver // Make sure of ConsoleEncoding helper class because on some Monos // Encoding.GetEncoding throws NotImplementedException on most codepages // NOTE: We don't set Console.OutputEncoding because it resets any existing Console.Out writers - if (!useManagedEncoder) - ConsoleEncoding.ConsoleCodePage = codepage; + // Always set the console output codepage so the Windows console interprets bytes correctly, + // regardless of whether we use a managed encoder or ConsoleEncoding to produce those bytes. + ConsoleEncoding.ConsoleCodePage = codepage; // If stdout exists, write to it, otherwise make it the same as console out // Not sure if this is needed? Does the original Console.Out still work?