Fix invalid JSON exception when querying gacha records

This commit is contained in:
KingRainbow44 2023-06-02 14:01:54 -04:00
parent 82189e03ed
commit d9e8810a5c
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 20 additions and 14 deletions

View File

@ -1,25 +1,21 @@
package emu.grasscutter.server.http.handlers; package emu.grasscutter.server.http.handlers;
import static emu.grasscutter.utils.lang.Language.translate;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.http.Router; import emu.grasscutter.server.http.Router;
import emu.grasscutter.utils.DispatchUtils; import emu.grasscutter.utils.*;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.Utils;
import io.javalin.Javalin; import io.javalin.Javalin;
import io.javalin.http.ContentType; import io.javalin.http.*;
import io.javalin.http.Context; import lombok.Getter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path; import java.util.*;
import java.util.Arrays;
import java.util.LinkedHashSet; import static emu.grasscutter.utils.lang.Language.translate;
import lombok.Getter;
/** Handles all gacha-related HTTP requests. */ /** Handles all gacha-related HTTP requests. */
public final class GachaHandler implements Router { public final class GachaHandler implements Router {
@ -48,14 +44,14 @@ public final class GachaHandler implements Router {
// Make request to dispatch server. // Make request to dispatch server.
var data = DispatchUtils.fetchGachaRecords(account.getId(), page, gachaType); var data = DispatchUtils.fetchGachaRecords(account.getId(), page, gachaType);
var records = data.get("records").getAsJsonArray(); var records = data.get("records").getAsString();
var maxPage = data.get("maxPage").getAsLong(); var maxPage = data.get("maxPage").getAsLong();
var locale = account.getLocale(); var locale = account.getLocale();
var template = var template =
new String( new String(
FileUtils.read(FileUtils.getDataPath("gacha/records.html")), StandardCharsets.UTF_8) FileUtils.read(FileUtils.getDataPath("gacha/records.html")), StandardCharsets.UTF_8)
.replace("'{{REPLACE_RECORDS}}'", records.toString()) .replace("'{{REPLACE_RECORDS}}'", Utils.unescapeJson(records))
.replace("'{{REPLACE_MAXPAGE}}'", String.valueOf(maxPage)) .replace("'{{REPLACE_MAXPAGE}}'", String.valueOf(maxPage))
.replace("{{TITLE}}", translate(locale, "gacha.records.title")) .replace("{{TITLE}}", translate(locale, "gacha.records.title"))
.replace("{{DATE}}", translate(locale, "gacha.records.date")) .replace("{{DATE}}", translate(locale, "gacha.records.date"))

View File

@ -525,4 +525,14 @@ public final class Utils {
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
} }
/**
* Unescapes a JSON string.
*
* @param json The JSON string to unescape.
* @return The unescaped JSON string.
*/
public static String unescapeJson(String json) {
return json.replaceAll("\"", "\"");
}
} }