diff --git a/.gitignore b/.gitignore index 8e867fdb..65bc1a2e 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,9 @@ crashlytics-build.properties fabric.properties /netlogs/ + +# JavaSteam residual files +cellid.txt +loginkey.txt +sentry.bin +server_list.bin \ No newline at end of file diff --git a/build.gradle b/build.gradle index 8d9275dd..a3026dc5 100644 --- a/build.gradle +++ b/build.gradle @@ -83,6 +83,8 @@ dependencies { implementation 'org.apache.commons:commons-lang3:3.12.0' // https://mvnrepository.com/artifact/org.java-websocket/Java-WebSocket implementation 'org.java-websocket:Java-WebSocket:1.5.3' + // https://mvnrepository.com/artifact/commons-io/commons-io + implementation 'commons-io:commons-io:2.11.0' /* Unit Testing */ @@ -90,8 +92,6 @@ dependencies { testImplementation 'com.squareup.okhttp3:mockwebserver3-junit5:5.0.0-alpha.10' // https://mvnrepository.com/artifact/commons-codec/commons-codec testImplementation 'commons-codec:commons-codec:1.15' - // https://mvnrepository.com/artifact/commons-io/commons-io - testImplementation 'commons-io:commons-io:2.11.0' // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1' // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine diff --git a/src/main/java/in/dragonbra/javasteam/types/KeyValue.java b/src/main/java/in/dragonbra/javasteam/types/KeyValue.java index 2f98db1c..0256a4cd 100644 --- a/src/main/java/in/dragonbra/javasteam/types/KeyValue.java +++ b/src/main/java/in/dragonbra/javasteam/types/KeyValue.java @@ -5,12 +5,15 @@ import in.dragonbra.javasteam.util.log.LogManager; import in.dragonbra.javasteam.util.log.Logger; import in.dragonbra.javasteam.util.stream.BinaryReader; +import in.dragonbra.javasteam.util.stream.MemoryStream; +import org.apache.commons.io.IOUtils; import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.EnumSet; @@ -20,6 +23,7 @@ /** * Represents a recursive string key to arbitrary value container. */ +@SuppressWarnings("unchecked") public class KeyValue { private static final Logger logger = LogManager.getLogger(KeyValue.class); @@ -439,20 +443,26 @@ private static KeyValue loadFromFile(String path, boolean asBinary) { } try (FileInputStream fis = new FileInputStream(file)) { + // Massage the incoming file to be encoded as UTF-8. + String fisString = IOUtils.toString(fis, Charset.defaultCharset()); + byte[] fisStringToBytes = fisString.getBytes(StandardCharsets.UTF_8); + MemoryStream ms = new MemoryStream(fisStringToBytes, 0, fisStringToBytes.length - 1); + KeyValue kv = new KeyValue(); if (asBinary) { - if (!kv.tryReadAsBinary(fis)) { + if (!kv.tryReadAsBinary(ms)) { return null; } } else { - if (!kv.readAsText(fis)) { + if (!kv.readAsText(ms)) { return null; } } return kv; - } catch (IOException e) { + } catch (Exception e) { + e.printStackTrace(); return null; } } diff --git a/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java b/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java index 26642efc..d9a17383 100644 --- a/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java +++ b/src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java @@ -7,11 +7,13 @@ import in.dragonbra.javasteam.util.stream.SeekOrigin; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import java.io.EOFException; import java.io.IOException; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -512,4 +514,16 @@ public void keyValuesHandlesEnum() { assertEquals(EChatPermission.OwnerDefault, kv.get("name").asEnum(EChatPermission.class, EChatPermission.EveryoneDefault)); } + + @Test + public void keyValues_loadAsText_should_read_successfully() { + URL file = this.getClass().getClassLoader().getResource("textkeyvalues/appinfo_utf8.txt"); + + Assertions.assertNotNull(file, "Resource file was null"); + + KeyValue kv = KeyValue.loadAsText(file.getPath()); + + Assertions.assertEquals("1234567", kv.get("appid").getValue(), "appid should be 1234567"); + Assertions.assertEquals(2, kv.getChildren().size(), "Children should be 2"); + } } diff --git a/src/test/resources/textkeyvalues/appinfo_utf8.txt b/src/test/resources/textkeyvalues/appinfo_utf8.txt new file mode 100644 index 00000000..1fd546be Binary files /dev/null and b/src/test/resources/textkeyvalues/appinfo_utf8.txt differ