Move charset loading to fix issue with static fields

This commit is contained in:
P0nk
2021-09-09 08:20:45 +02:00
parent 52551b2451
commit 05cae9022f
2 changed files with 34 additions and 35 deletions

View File

@@ -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<WorldConfig> 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());
}
}
}