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());