From ff00d01fd723d29f47651e05dff60d1de93699a6 Mon Sep 17 00:00:00 2001 From: BruceChen Date: Wed, 8 Apr 2026 01:33:38 +0800 Subject: [PATCH] refactor: simplify color handling in ClassicConsoleBackend - Removed hex color handling logic from ClassicConsoleBackend, streamlining the WriteLineFormatted method. - Updated ColorHelper to ensure consistent formatting for color codes. - Ensured console-specific settings are reapplied after backend initialization in Program.cs. --- MinecraftClient/ClassicConsoleBackend.cs | 20 -------------------- MinecraftClient/ColorHelper.cs | 6 +++--- MinecraftClient/Program.cs | 6 ++++++ 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/MinecraftClient/ClassicConsoleBackend.cs b/MinecraftClient/ClassicConsoleBackend.cs index 099447b2..ac4912c9 100644 --- a/MinecraftClient/ClassicConsoleBackend.cs +++ b/MinecraftClient/ClassicConsoleBackend.cs @@ -1,6 +1,4 @@ using System; -using System.Text.RegularExpressions; -using static MinecraftClient.Settings.ConsoleConfigHealper.ConsoleConfig; namespace MinecraftClient { @@ -28,29 +26,11 @@ namespace MinecraftClient ConsoleInteractive.ConsoleWriter.WriteLine(text); } - private static readonly Regex HexColorRegex = new(@"§#([0-9a-fA-F]{6})", RegexOptions.Compiled); - public void WriteLineFormatted(string text) { - bool hasHex = text.Contains("§#"); - if (hasHex) - text = ResolveHexColors(text); ConsoleInteractive.ConsoleWriter.WriteLineFormatted(text); } - private static string ResolveHexColors(string text) - { - var mode = Settings.Config.Console.General.ConsoleColorMode; - return HexColorRegex.Replace(text, match => - { - ReadOnlySpan hex = match.Groups[1].ValueSpan; - byte r = Convert.ToByte(hex[..2].ToString(), 16); - byte g = Convert.ToByte(hex[2..4].ToString(), 16); - byte b = Convert.ToByte(hex[4..6].ToString(), 16); - return ColorHelper.GetColorEscapeCode(r, g, b, foreground: true, mode); - }); - } - public void BeginReadThread() { ConsoleInteractive.ConsoleReader.MessageReceived += ForwardMessage; diff --git a/MinecraftClient/ColorHelper.cs b/MinecraftClient/ColorHelper.cs index 54559d58..4c7880aa 100644 --- a/MinecraftClient/ColorHelper.cs +++ b/MinecraftClient/ColorHelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using static MinecraftClient.Settings.ConsoleConfigHealper.ConsoleConfig; namespace MinecraftClient @@ -100,9 +100,9 @@ namespace MinecraftClient } } if (foreground) - return $"§{best_idx:X}"; + return $"§{best_idx:x}"; else - return $"§§{best_idx:X}"; + return $"§§{best_idx:x}"; } case ConsoleColorModeType.vt100_4bit: diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index ae706296..83297eda 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -202,6 +202,11 @@ namespace MinecraftClient { ConsoleIO.Backend = new ClassicConsoleBackend(); ConsoleIO.Backend.Init(); + + // Config deserialization triggers OnSettingUpdate before the backend + // exists, so console-specific settings (UseVT100ColorCode, colors, etc.) + // are never applied. Re-apply them now that the backend is ready. + Config.Console.OnSettingUpdate(); } if (!ProcessStartupState(startupState)) @@ -240,6 +245,7 @@ namespace MinecraftClient ConsoleIO.Backend = new ClassicConsoleBackend(); ConsoleIO.Backend.Init(); + Config.Console.OnSettingUpdate(); ConsoleIO.WriteLineFormatted("§c" + Translations.mcc_tui_startup_failed); ConsoleIO.WriteLine(exception.ToString());