Skip to content

Commit

Permalink
LibGDX:
Browse files Browse the repository at this point in the history
* Handle this cursed BOM
  • Loading branch information
Mikhael-Danilov committed Dec 24, 2024
1 parent 27e63f8 commit 09a2f34
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
5 changes: 3 additions & 2 deletions RemixedDungeon/src/main/java/com/nyrds/util/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.watabou.noosa.TextureFilm;
import com.watabou.pixeldungeon.utils.Utils;

import org.apache.commons.io.input.BOMInputStream;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -63,7 +64,7 @@ public static JSONObject readJsonFromString(final String in) throws JSONExceptio
public static JSONObject readJsonFromStream(InputStream stream, String tag) {
StringBuilder jsonDef = new StringBuilder();

try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(stream)))) {

String line = reader.readLine();

Expand All @@ -73,7 +74,7 @@ public static JSONObject readJsonFromStream(InputStream stream, String tag) {
}
reader.close();

return Util.sanitizeJson(jsonDef.toString());
return Util.sanitizeJson(jsonDef.toString().strip());
} catch (Exception e) {
EventCollector.logException(Utils.format("bad json in %s", tag));
return new JSONObject();
Expand Down
6 changes: 5 additions & 1 deletion RemixedDungeon/src/main/java/com/nyrds/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.nyrds.pixeldungeon.ml.BuildConfig;
import com.nyrds.platform.game.RemixedDungeon;
import com.nyrds.platform.util.PUtil;
import com.watabou.utils.Callback;

import org.hjson.HjsonOptions;
import org.hjson.JsonValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -68,7 +70,9 @@ static public int indexOf(Class<?>[] classList, @NotNull String name) {

public static JSONObject sanitizeJson(String jsonInput) {
// Parse the JSON input
JsonValue jsonValue = JsonValue.readHjson(jsonInput);
//PUtil.slog("json", jsonInput);
JsonValue jsonValue = JsonValue.readHjson(jsonInput).asObject();
//PUtil.slog("json", jsonValue.toString());

// Convert HJSON to JSON (sanitized)
String sanitizedJson = jsonValue.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public CaseInsensitiveFileCache(String... rootPaths) {
for (int i = rootPaths.length - 1; i >= 0; --i) {
cacheFiles(rootPaths[i]);
}

/*
for (String key : fileCache.keySet()) {
PUtil.slog("file", "Cached file: " + key);
}
*/

}

private void cacheFiles(String... rootPaths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import lombok.SneakyThrows;

public class FileSystem {

static CaseInsensitiveFileCache fileCache = new CaseInsensitiveFileCache(getAllResPaths());

static public FileHandle getInternalStorageFileHandle(String fileName) {
Expand Down Expand Up @@ -88,7 +87,16 @@ static public String[] listResources(String resName) {


static public @NotNull File getInternalStorageFile(String fileName) {
return getInternalStorageFileHandle(fileName).file();
FileHandle fileHandle = getInternalStorageFileHandle(fileName);
if(fileHandle == null) {
return new File("file_not_found") {
@Override
public boolean exists() {
return false;
}
};
}
return fileHandle.file();
}

static public @NotNull File getInternalStorageFileBase(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import com.nyrds.pixeldungeon.ml.R;
import com.nyrds.util.ModdingMode;
import com.nyrds.util.Util;
import com.watabou.pixeldungeon.utils.Utils;
import com.watabou.pixeldungeon.windows.WndSettings;

import org.apache.commons.io.input.BOMInputStream;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;

Expand Down Expand Up @@ -84,13 +86,13 @@ private static void clearModStrings() {
private static void parseStrings(String resource) {
PUtil.slog("Strings", "Parsing Strings from " + resource);
InputStream fis = ModdingMode.getInputMergedInputStream(resource);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
InputStreamReader isr = new InputStreamReader(new BOMInputStream(fis), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);

String line = Utils.EMPTY_STRING;

while ((line = br.readLine()) != null) {
//PUtil.slog("strings",line);
//PUtil.slog("strings","line"+line);
JSONArray entry = new JSONArray(line);

String keyString = entry.getString(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.watabou.pixeldungeon.utils.GLog;
import com.watabou.pixeldungeon.utils.Utils;

import org.apache.commons.io.input.BOMInputStream;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand Down Expand Up @@ -284,13 +285,13 @@ public static String getResource(String resName) {
public static @NotNull InputStream getInputMergedInputStream(String resName) {
InputStream modStream = null;
try {
modStream = new FileInputStream(FileSystem.getInternalStorageFile(resName));
modStream = new BOMInputStream(new FileInputStream(FileSystem.getInternalStorageFile(resName)));
} catch (Exception e) {
// ignore
}

try {
InputStream builtInStream = new FileInputStream(FileSystem.getInternalStorageFileBase(resName));
InputStream builtInStream = new BOMInputStream(new FileInputStream(FileSystem.getInternalStorageFileBase(resName)));
if(modStream == null) {
return builtInStream;
}
Expand Down

0 comments on commit 09a2f34

Please sign in to comment.