From c841e9952b1080366d1a33dc4c3a25c404c505c4 Mon Sep 17 00:00:00 2001 From: P0nk Date: Wed, 8 Sep 2021 18:46:16 +0200 Subject: [PATCH 1/3] Respect charset when loading scripts --- config.yaml | 2 +- src/main/java/config/ServerConfig.java | 2 +- src/main/java/constants/string/CharsetConstants.java | 4 ++-- src/main/java/net/packet/ByteBufInPacket.java | 2 +- src/main/java/net/packet/ByteBufOutPacket.java | 4 ++-- src/main/java/scripting/AbstractScriptManager.java | 3 ++- src/main/java/tools/HexTool.java | 2 +- src/test/java/net/packet/ByteBufInPacketTest.java | 2 +- 8 files changed, 11 insertions(+), 10 deletions(-) diff --git a/config.yaml b/config.yaml index 811dcd19de..664db06e50 100644 --- a/config.yaml +++ b/config.yaml @@ -307,7 +307,7 @@ server: #Miscellaneous Configuration TIMEZONE: GMT-3 - PACKET_CHARSET: US_ASCII # Defaults to US_ASCII if not set + CHARSET: US-ASCII # Defaults to US-ASCII if not set USE_DISPLAY_NUMBERS_WITH_COMMA: true #Enforce comma on displayed strings (use this when USE_UNITPRICE_WITH_COMMA is active and you still want to display comma-separated values). USE_UNITPRICE_WITH_COMMA: true #Set this accordingly with the layout of the unitPrices on Item.wz XML's, whether it's using commas or dots to represent fractions. MAX_MONITORED_BUFFSTATS: 5 #Limits accounting for "dormant" buff effects, that should take place when stronger stat buffs expires. diff --git a/src/main/java/config/ServerConfig.java b/src/main/java/config/ServerConfig.java index 3bf11c561f..4676162bb8 100644 --- a/src/main/java/config/ServerConfig.java +++ b/src/main/java/config/ServerConfig.java @@ -154,7 +154,7 @@ public class ServerConfig { //Miscellaneous Configuration public String TIMEZONE; - public String PACKET_CHARSET; + public String CHARSET; public boolean USE_DISPLAY_NUMBERS_WITH_COMMA; public boolean USE_UNITPRICE_WITH_COMMA; public byte MAX_MONITORED_BUFFSTATS; diff --git a/src/main/java/constants/string/CharsetConstants.java b/src/main/java/constants/string/CharsetConstants.java index d6870e2632..96d4f751cc 100644 --- a/src/main/java/constants/string/CharsetConstants.java +++ b/src/main/java/constants/string/CharsetConstants.java @@ -24,7 +24,7 @@ import java.util.Optional; public class CharsetConstants { private static final Logger log = LoggerFactory.getLogger(CharsetConstants.class); - public static final Charset PACKET_CHARSET = loadCharset(); + public static final Charset CHARSET = loadCharset(); private enum Language { LANGUAGE_US("US-ASCII"), @@ -56,7 +56,7 @@ public class CharsetConstants { } private static Charset loadCharset() { - String configCharset = YamlConfig.config.server.PACKET_CHARSET; + String configCharset = YamlConfig.config.server.CHARSET; if (configCharset != null) { Language language = Language.fromCharset(configCharset); return Charset.forName(language.getCharset()); diff --git a/src/main/java/net/packet/ByteBufInPacket.java b/src/main/java/net/packet/ByteBufInPacket.java index d682e9f357..19dfdfffc0 100644 --- a/src/main/java/net/packet/ByteBufInPacket.java +++ b/src/main/java/net/packet/ByteBufInPacket.java @@ -50,7 +50,7 @@ public class ByteBufInPacket implements InPacket { short length = readShort(); byte[] stringBytes = new byte[length]; byteBuf.readBytes(stringBytes); - return new String(stringBytes, CharsetConstants.PACKET_CHARSET); + return new String(stringBytes, CharsetConstants.CHARSET); } @Override diff --git a/src/main/java/net/packet/ByteBufOutPacket.java b/src/main/java/net/packet/ByteBufOutPacket.java index 866588c41d..ba3da1d663 100644 --- a/src/main/java/net/packet/ByteBufOutPacket.java +++ b/src/main/java/net/packet/ByteBufOutPacket.java @@ -78,12 +78,12 @@ public class ByteBufOutPacket implements OutPacket { @Override public void writeString(String value) { writeShort((short) value.length()); - writeBytes(value.getBytes(CharsetConstants.PACKET_CHARSET)); + writeBytes(value.getBytes(CharsetConstants.CHARSET)); } @Override public void writeFixedString(String value) { - writeBytes(value.getBytes(CharsetConstants.PACKET_CHARSET)); + writeBytes(value.getBytes(CharsetConstants.CHARSET)); } @Override diff --git a/src/main/java/scripting/AbstractScriptManager.java b/src/main/java/scripting/AbstractScriptManager.java index 187f46b3a0..1df195d82f 100644 --- a/src/main/java/scripting/AbstractScriptManager.java +++ b/src/main/java/scripting/AbstractScriptManager.java @@ -23,6 +23,7 @@ package scripting; import client.MapleClient; import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine; +import constants.string.CharsetConstants; import tools.FilePrinter; import javax.script.*; @@ -55,7 +56,7 @@ public abstract class AbstractScriptManager { enableScriptHostAccess(graalScriptEngine); - try (FileReader fr = new FileReader(scriptFile)) { + try (FileReader fr = new FileReader(scriptFile, CharsetConstants.CHARSET)) { engine.eval(fr); } catch (final ScriptException | IOException t) { FilePrinter.printError(FilePrinter.INVOCABLE + path.substring(12), t, path); diff --git a/src/main/java/tools/HexTool.java b/src/main/java/tools/HexTool.java index 0a2d4f0cf1..0352e1f7f2 100644 --- a/src/main/java/tools/HexTool.java +++ b/src/main/java/tools/HexTool.java @@ -99,7 +99,7 @@ public class HexTool { } } - return new String(ret, CharsetConstants.PACKET_CHARSET); + return new String(ret, CharsetConstants.CHARSET); } /** diff --git a/src/test/java/net/packet/ByteBufInPacketTest.java b/src/test/java/net/packet/ByteBufInPacketTest.java index 8c4b287cf3..3a27dd795a 100644 --- a/src/test/java/net/packet/ByteBufInPacketTest.java +++ b/src/test/java/net/packet/ByteBufInPacketTest.java @@ -83,7 +83,7 @@ class ByteBufInPacketTest { void readString() { final String writtenString = "You have gained experience (+3200)"; byteBuf.writeShortLE(writtenString.length()); - byte[] writtenStringBytes = writtenString.getBytes(CharsetConstants.PACKET_CHARSET); + byte[] writtenStringBytes = writtenString.getBytes(CharsetConstants.CHARSET); byteBuf.writeBytes(writtenStringBytes); String readString = inPacket.readString(); From 52551b24512ceb7955d058c3a552829818ddb51e Mon Sep 17 00:00:00 2001 From: P0nk Date: Wed, 8 Sep 2021 20:01:43 +0200 Subject: [PATCH 2/3] Load config.yaml using its own charset Strings in the config are therefore respected by the configured charset --- config.yaml | 2 +- src/main/java/config/YamlConfig.java | 44 +++++++++++++++---- .../constants/string/CharsetConstants.java | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/config.yaml b/config.yaml index 664db06e50..f09e0b9fdd 100644 --- a/config.yaml +++ b/config.yaml @@ -307,7 +307,7 @@ server: #Miscellaneous Configuration TIMEZONE: GMT-3 - CHARSET: US-ASCII # Defaults to US-ASCII if not set + CHARSET: US-ASCII # Is loaded first, so applies to the rest of this config. Defaults to US-ASCII if invalid or not set. USE_DISPLAY_NUMBERS_WITH_COMMA: true #Enforce comma on displayed strings (use this when USE_UNITPRICE_WITH_COMMA is active and you still want to display comma-separated values). USE_UNITPRICE_WITH_COMMA: true #Set this accordingly with the layout of the unitPrices on Item.wz XML's, whether it's using commas or dots to represent fractions. MAX_MONITORED_BUFFSTATS: 5 #Limits accounting for "dormant" buff effects, that should take place when stronger stat buffs expires. diff --git a/src/main/java/config/YamlConfig.java b/src/main/java/config/YamlConfig.java index c8adc338d1..ec9e1c7b56 100644 --- a/src/main/java/config/YamlConfig.java +++ b/src/main/java/config/YamlConfig.java @@ -1,32 +1,58 @@ package config; import com.esotericsoftware.yamlbeans.YamlReader; +import constants.string.CharsetConstants; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.List; public class YamlConfig { - - public static final YamlConfig config = fromFile("config.yaml"); + private static final String CONFIG_FILE_NAME = "config.yaml"; + public static final YamlConfig config = loadConfig(CONFIG_FILE_NAME); public List worlds; public ServerConfig server; - public static YamlConfig fromFile(String filename) { + private static YamlConfig loadConfig(String fileName) { try { - YamlReader reader = new YamlReader(new FileReader(filename)); + YamlReader reader = new YamlReader(new FileReader(fileName, CharsetConstants.CHARSET)); YamlConfig config = reader.read(YamlConfig.class); reader.close(); return config; - } catch (FileNotFoundException e) { - String message = "Could not read config file " + filename + ": " + e.getMessage(); - throw new RuntimeException(message); } catch (IOException e) { - String message = "Could not successfully parse config file " + filename + ": " + e.getMessage(); - throw new RuntimeException(message); + throw new RuntimeException(getLoadConfigErrorMessage(e)); + } + } + + private static String getLoadConfigErrorMessage(IOException e) { + if (e instanceof FileNotFoundException) { + return "Could not read config file " + CONFIG_FILE_NAME + ": " + e.getMessage(); + } + + return "Could not successfully parse config file " + CONFIG_FILE_NAME + ": " + e.getMessage(); + } + + public static String loadCharset() { + try { + YamlReader reader = new YamlReader(new FileReader(CONFIG_FILE_NAME, StandardCharsets.US_ASCII)); + reader.getConfig().readConfig.setIgnoreUnknownProperties(true); + StrippedYamlConfig charsetConfig = reader.read(StrippedYamlConfig.class); + reader.close(); + return charsetConfig.server.CHARSET; + } catch (IOException e) { + throw new RuntimeException(getLoadConfigErrorMessage(e)); + } + } + + private static class StrippedYamlConfig { + public StrippedServerConfig server; + + private static class StrippedServerConfig { + public String CHARSET; } } } diff --git a/src/main/java/constants/string/CharsetConstants.java b/src/main/java/constants/string/CharsetConstants.java index 96d4f751cc..631ddeb255 100644 --- a/src/main/java/constants/string/CharsetConstants.java +++ b/src/main/java/constants/string/CharsetConstants.java @@ -56,7 +56,7 @@ public class CharsetConstants { } private static Charset loadCharset() { - String configCharset = YamlConfig.config.server.CHARSET; + String configCharset = YamlConfig.loadCharset(); if (configCharset != null) { Language language = Language.fromCharset(configCharset); return Charset.forName(language.getCharset()); From 05cae9022f374cdf8537a6e6312e36d7b18785d5 Mon Sep 17 00:00:00 2001 From: P0nk Date: Thu, 9 Sep 2021 08:20:45 +0200 Subject: [PATCH 3/3] Move charset loading to fix issue with static fields --- src/main/java/config/YamlConfig.java | 41 ++++--------------- .../constants/string/CharsetConstants.java | 28 ++++++++++++- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/main/java/config/YamlConfig.java b/src/main/java/config/YamlConfig.java index ec9e1c7b56..77a0a9a780 100644 --- a/src/main/java/config/YamlConfig.java +++ b/src/main/java/config/YamlConfig.java @@ -6,53 +6,26 @@ import constants.string.CharsetConstants; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.List; public class YamlConfig { - private static final String CONFIG_FILE_NAME = "config.yaml"; - public static final YamlConfig config = loadConfig(CONFIG_FILE_NAME); + public static final String CONFIG_FILE_NAME = "config.yaml"; + public static final YamlConfig config = loadConfig(); public List worlds; public ServerConfig server; - private static YamlConfig loadConfig(String fileName) { + private static YamlConfig loadConfig() { try { - YamlReader reader = new YamlReader(new FileReader(fileName, CharsetConstants.CHARSET)); + YamlReader reader = new YamlReader(new FileReader(CONFIG_FILE_NAME, CharsetConstants.CHARSET)); YamlConfig config = reader.read(YamlConfig.class); reader.close(); return config; + } catch (FileNotFoundException e) { + throw new RuntimeException("Could not read config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); } catch (IOException e) { - throw new RuntimeException(getLoadConfigErrorMessage(e)); - } - } - - private static String getLoadConfigErrorMessage(IOException e) { - if (e instanceof FileNotFoundException) { - return "Could not read config file " + CONFIG_FILE_NAME + ": " + e.getMessage(); - } - - return "Could not successfully parse config file " + CONFIG_FILE_NAME + ": " + e.getMessage(); - } - - public static String loadCharset() { - try { - YamlReader reader = new YamlReader(new FileReader(CONFIG_FILE_NAME, StandardCharsets.US_ASCII)); - reader.getConfig().readConfig.setIgnoreUnknownProperties(true); - StrippedYamlConfig charsetConfig = reader.read(StrippedYamlConfig.class); - reader.close(); - return charsetConfig.server.CHARSET; - } catch (IOException e) { - throw new RuntimeException(getLoadConfigErrorMessage(e)); - } - } - - private static class StrippedYamlConfig { - public StrippedServerConfig server; - - private static class StrippedServerConfig { - public String CHARSET; + throw new RuntimeException("Could not successfully parse config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); } } } diff --git a/src/main/java/constants/string/CharsetConstants.java b/src/main/java/constants/string/CharsetConstants.java index 631ddeb255..da20cbfc01 100644 --- a/src/main/java/constants/string/CharsetConstants.java +++ b/src/main/java/constants/string/CharsetConstants.java @@ -13,10 +13,14 @@ package constants.string; * CharsetConstants */ +import com.esotericsoftware.yamlbeans.YamlReader; import config.YamlConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -55,8 +59,22 @@ public class CharsetConstants { } } + private static String loadCharsetFromConfig() { + try { + YamlReader reader = new YamlReader(new FileReader(YamlConfig.CONFIG_FILE_NAME, StandardCharsets.US_ASCII)); + reader.getConfig().readConfig.setIgnoreUnknownProperties(true); + StrippedYamlConfig charsetConfig = reader.read(StrippedYamlConfig.class); + reader.close(); + return charsetConfig.server.CHARSET; + } catch (FileNotFoundException e) { + throw new RuntimeException("Could not read config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); + } catch (IOException e) { + throw new RuntimeException("Could not successfully parse charset from config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage()); + } + } + private static Charset loadCharset() { - String configCharset = YamlConfig.loadCharset(); + String configCharset = loadCharsetFromConfig(); if (configCharset != null) { Language language = Language.fromCharset(configCharset); return Charset.forName(language.getCharset()); @@ -64,4 +82,12 @@ public class CharsetConstants { return StandardCharsets.US_ASCII; } + + private static class StrippedYamlConfig { + public StrippedServerConfig server; + + private static class StrippedServerConfig { + public String CHARSET; + } + } } \ No newline at end of file