Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyValue.loadFromFile #224

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ crashlytics-build.properties
fabric.properties

/netlogs/

# JavaSteam residual files
cellid.txt
loginkey.txt
sentry.bin
server_list.bin
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ 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 */

// https://mvnrepository.com/artifact/com.squareup.okhttp3/mockwebserver3-junit5
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
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/in/dragonbra/javasteam/types/KeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/in/dragonbra/javasteam/types/KeyValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
Binary file added src/test/resources/textkeyvalues/appinfo_utf8.txt
Binary file not shown.