From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Tue, 29 Nov 2022 21:03:47 +0100 Subject: [PATCH] Fix legacy colors in console License: MIT (https://opensource.org/licenses/MIT) Gale - https://galemc.org This patch is based on the following patch: "Fix legacy colors in console" By: BillyGalbreath As part of: Purpur (https://github.com/PurpurMC/Purpur) Licensed under: MIT (https://opensource.org/licenses/MIT) * Purpur copyright * MIT License Copyright (c) 2019-2022 PurpurMC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/main/java/io/papermc/paper/console/HexFormattingConverter.java b/src/main/java/io/papermc/paper/console/HexFormattingConverter.java index b9922b07cb105618390187d98acdf89e728e1f5a..a66d202749f243f6752df5027cb7c82f6132992a 100644 --- a/src/main/java/io/papermc/paper/console/HexFormattingConverter.java +++ b/src/main/java/io/papermc/paper/console/HexFormattingConverter.java @@ -17,6 +17,7 @@ import org.apache.logging.log4j.core.pattern.PatternFormatter; import org.apache.logging.log4j.core.pattern.PatternParser; import org.apache.logging.log4j.util.PerformanceSensitive; import org.apache.logging.log4j.util.PropertiesUtil; +import org.galemc.gale.configuration.GaleGlobalConfiguration; import java.util.List; import java.util.regex.Matcher; @@ -38,6 +39,7 @@ public final class HexFormattingConverter extends LogEventPatternConverter { private static final String ANSI_RESET = "\u001B[m"; private static final char COLOR_CHAR = 0x7f; + private static final char LEGACY_CHAR = 0xa7; // Gale - Purpur - fix legacy colors in console public static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder() .hexColors() .flattener(PaperAdventure.FLATTENER) @@ -49,6 +51,10 @@ public final class HexFormattingConverter extends LogEventPatternConverter { private static final String RESET_RGB_ANSI = ANSI_RESET + RGB_ANSI; private static final Pattern NAMED_PATTERN = Pattern.compile(COLOR_CHAR + "[0-9a-fk-orA-FK-OR]"); private static final Pattern RGB_PATTERN = Pattern.compile(COLOR_CHAR + "#([0-9a-fA-F]){6}"); + // Gale start - Purpur - fix legacy colors in console + private static final Pattern LEGACY_RGB_PATTERN = Pattern.compile(LEGACY_CHAR + "x((" + LEGACY_CHAR + "[0-9a-fA-F]){6})"); + private static final Pattern LEGACY_PATTERN = Pattern.compile(LEGACY_CHAR + "([0-9a-fk-orxA-FK-ORX])"); + // Gale end - Purpur - fix legacy colors in console private static final String[] RGB_ANSI_CODES = new String[]{ formatHexAnsi(NamedTextColor.BLACK), // Black ยง0 @@ -134,7 +140,27 @@ public final class HexFormattingConverter extends LogEventPatternConverter { } private static String convertRGBColors(final String input) { - return RGB_PATTERN.matcher(input).replaceAll(result -> { + // Gale start - Purpur - fix legacy colors in console - lets just shove this back in place + String toConvert; + if (fixLegacyColors()) { + Matcher matcher = LEGACY_RGB_PATTERN.matcher(input); + StringBuilder buffer = new StringBuilder(); + while (matcher.find()) { + String s = matcher.group().replace(String.valueOf(LEGACY_CHAR), "").replace('x', '#'); + int hex = Integer.decode(s); + int red = (hex >> 16) & 0xFF; + int green = (hex >> 8) & 0xFF; + int blue = hex & 0xFF; + String replacement = String.format(RGB_ANSI, red, green, blue); + matcher.appendReplacement(buffer, replacement); + } + matcher.appendTail(buffer); + toConvert = buffer.toString(); + } else { + toConvert = input; + } + return RGB_PATTERN.matcher(toConvert).replaceAll(result -> { + // Gale end - Purpur - fix legacy colors in console - lets just shove this back in place final int hex = Integer.decode(result.group().substring(1)); return formatHexAnsi(hex); }); @@ -152,10 +178,18 @@ public final class HexFormattingConverter extends LogEventPatternConverter { } private static String stripRGBColors(final String input) { - return RGB_PATTERN.matcher(input).replaceAll(""); + // Gale start - Purpur - fix legacy colors in console + String removedRGB = RGB_PATTERN.matcher(input).replaceAll(""); + return fixLegacyColors() ? LEGACY_RGB_PATTERN.matcher(removedRGB).replaceAll("") : removedRGB; + // Gale end - Purpur - fix legacy colors in console } static void format(String content, StringBuilder result, int start, boolean ansi) { + // Gale start - Purpur - fix legacy colors in console + if (fixLegacyColors()) { + content = LEGACY_PATTERN.matcher(content).replaceAll(COLOR_CHAR + "$1"); + } + // Gale end - Purpur - fix legacy colors in console int next = content.indexOf(COLOR_CHAR); int last = content.length() - 1; if (next == -1 || next == last) { @@ -210,4 +244,13 @@ public final class HexFormattingConverter extends LogEventPatternConverter { return new HexFormattingConverter(formatters, strip); } + // Gale start - Purpur - fix legacy colors in console + private static boolean fixLegacyColors() { + try { + return GaleGlobalConfiguration.get().logToConsole.legacyColors; + } catch (NullPointerException ignored) {} + return false; + } + // Gale end - Purpur - fix legacy colors in console + } diff --git a/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java b/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java index 882830dd0bddb81e1a592cdd089d4340beb946a7..f4f6f59af9ae02748763b82f2c2ae5da90737749 100644 --- a/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java +++ b/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java @@ -114,6 +114,7 @@ public class GaleGlobalConfiguration extends ConfigurationPart { public LogToConsole logToConsole; public class LogToConsole extends ConfigurationPart.Post { // Gale - EMC - softly log invalid pool element errors + public boolean legacyColors = true; // Gale - Purpur - fix legacy colors in console public boolean invalidStatistics = true; // Gale - EMC - do not log invalid statistics public boolean ignoredAdvancements = true; // Gale - Purpur - do not log ignored advancements public boolean setBlockInFarChunk = true; // Gale - Purpur - do not log setBlock in far chunks