mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-16 08:56:04 +01:00
Fix UTF-8 encoding in announcements
This commit is contained in:
@@ -5,34 +5,39 @@ import express.http.HttpContextHandler;
|
||||
import express.http.Request;
|
||||
import express.http.Response;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Objects;
|
||||
|
||||
import static emu.grasscutter.Configuration.*;
|
||||
|
||||
public final class AnnouncementHandler implements HttpContextHandler {
|
||||
@Override
|
||||
public void handle(Request request, Response response) throws IOException {//event
|
||||
public void handle(Request request, Response response) throws IOException {
|
||||
if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnContent")) {
|
||||
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\":" + readToString(new File(DATA("GameAnnouncement.json"))) +"}");
|
||||
String data = readToString(Paths.get(DATA("GameAnnouncement.json")));
|
||||
|
||||
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\":" + data + "}");
|
||||
} else if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnList")) {
|
||||
String data = readToString(new File(DATA("GameAnnouncementList.json"))).replace("System.currentTimeMillis()",String.valueOf(System.currentTimeMillis()));
|
||||
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": "+data +"}");
|
||||
String data = readToString(Paths.get(DATA("GameAnnouncementList.json")))
|
||||
.replace("System.currentTimeMillis()", String.valueOf(System.currentTimeMillis()));
|
||||
|
||||
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": " + data + "}");
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private static String readToString(File file) {
|
||||
long length = file.length();
|
||||
byte[] content = new byte[(int) length];
|
||||
|
||||
private static String readToString(Path path) {
|
||||
String content = "";
|
||||
|
||||
try {
|
||||
FileInputStream in = new FileInputStream(file);
|
||||
in.read(content); in.close();
|
||||
content = Files.readString(path, StandardCharsets.UTF_8);
|
||||
} catch (IOException ignored) {
|
||||
Grasscutter.getLogger().warn("File not found: " + file.getAbsolutePath());
|
||||
Grasscutter.getLogger().warn("Unable to open file " + path.toAbsolutePath());
|
||||
}
|
||||
|
||||
return new String(content);
|
||||
return content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user