Merge pull request #53 from P0nk/charset-fixes

Charset fixes
This commit is contained in:
Ponk
2021-09-09 08:33:25 +02:00
committed by GitHub
9 changed files with 44 additions and 18 deletions

View File

@@ -307,7 +307,7 @@ server:
#Miscellaneous Configuration
TIMEZONE: GMT-3
PACKET_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.

View File

@@ -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;

View File

@@ -1,6 +1,7 @@
package config;
import com.esotericsoftware.yamlbeans.YamlReader;
import constants.string.CharsetConstants;
import java.io.FileNotFoundException;
import java.io.FileReader;
@@ -9,24 +10,22 @@ import java.util.List;
public class YamlConfig {
public static final YamlConfig config = fromFile("config.yaml");
public static final String CONFIG_FILE_NAME = "config.yaml";
public static final YamlConfig config = loadConfig();
public List<WorldConfig> worlds;
public ServerConfig server;
public static YamlConfig fromFile(String filename) {
private static YamlConfig loadConfig() {
try {
YamlReader reader = new YamlReader(new FileReader(filename));
YamlReader reader = new YamlReader(new FileReader(CONFIG_FILE_NAME, 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);
throw new RuntimeException("Could not read config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage());
} catch (IOException e) {
String message = "Could not successfully parse config file " + filename + ": " + e.getMessage();
throw new RuntimeException(message);
throw new RuntimeException("Could not successfully parse config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage());
}
}
}

View File

@@ -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;
@@ -24,7 +28,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"),
@@ -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.config.server.PACKET_CHARSET;
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;
}
}
}

View File

@@ -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

View File

@@ -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

View File

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

View File

@@ -99,7 +99,7 @@ public class HexTool {
}
}
return new String(ret, CharsetConstants.PACKET_CHARSET);
return new String(ret, CharsetConstants.CHARSET);
}
/**

View File

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