mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-18 01:46:44 +01:00
"Autogenerate" data files with data fallbacks and moved keys folder into jar resources (#927)
* Autogenerate keys and data files * Update gacha html files Accidentally pushed with old html files * Keys no longer copied. No more manually retrieving listing files. Recursive directory creation Removed unused code from old GC as well. * Moved somethings and better errors * Fixed resources from loading twice * Data files fallback
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package emu.grasscutter.server.http.handlers;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.data.DataLoader;
|
||||
import emu.grasscutter.server.http.objects.HttpJsonResponse;
|
||||
import emu.grasscutter.server.http.Router;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
@@ -14,6 +15,7 @@ import io.javalin.Javalin;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -41,9 +43,21 @@ public final class AnnouncementsHandler implements Router {
|
||||
private static void getAnnouncement(Request request, Response response) {
|
||||
String data = "";
|
||||
if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnContent")) {
|
||||
data = readToString(new File(Utils.toFilePath(DATA("GameAnnouncement.json"))));
|
||||
try {
|
||||
data = FileUtils.readToString(DataLoader.load("GameAnnouncement.json"));
|
||||
} catch (Exception e) {
|
||||
if(e.getClass() == IOException.class) {
|
||||
Grasscutter.getLogger().info("Unable to read file 'GameAnnouncementList.json'. \n" + e);
|
||||
}
|
||||
}
|
||||
} else if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnList")) {
|
||||
data = readToString(new File(Utils.toFilePath(DATA("GameAnnouncementList.json"))));
|
||||
try {
|
||||
data = FileUtils.readToString(DataLoader.load("GameAnnouncementList.json"));
|
||||
} catch (Exception e) {
|
||||
if(e.getClass() == IOException.class) {
|
||||
Grasscutter.getLogger().info("Unable to read file 'GameAnnouncementList.json'. \n" + e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.send("{\"retcode\":404,\"message\":\"Unknown request path\"}");
|
||||
}
|
||||
@@ -64,29 +78,15 @@ public final class AnnouncementsHandler implements Router {
|
||||
}
|
||||
|
||||
private static void getPageResources(Request request, Response response) {
|
||||
String filename = Utils.toFilePath(DATA(request.path()));
|
||||
File file = new File(filename);
|
||||
if (file.exists() && file.isFile()) {
|
||||
MediaType fromExtension = MediaType.getByExtension(filename.substring(filename.lastIndexOf(".") + 1));
|
||||
try(InputStream filestream = DataLoader.load(request.path())) {
|
||||
String possibleFilename = Utils.toFilePath(DATA(request.path()));
|
||||
|
||||
MediaType fromExtension = MediaType.getByExtension(possibleFilename.substring(possibleFilename.lastIndexOf(".") + 1));
|
||||
response.type((fromExtension != null) ? fromExtension.getMIME() : "application/octet-stream");
|
||||
response.send(FileUtils.read(file));
|
||||
} else {
|
||||
Grasscutter.getLogger().warn("File does not exist: " + file);
|
||||
response.send(filestream.readAllBytes());
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().warn("File does not exist: " + request.path());
|
||||
response.status(404);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private static String readToString(File file) {
|
||||
byte[] content = new byte[(int) file.length()];
|
||||
|
||||
try {
|
||||
FileInputStream in = new FileInputStream(file);
|
||||
in.read(content); in.close();
|
||||
} catch (IOException ignored) {
|
||||
Grasscutter.getLogger().warn("File does not exist: " + file);
|
||||
}
|
||||
|
||||
return new String(content, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,18 +29,7 @@ import static emu.grasscutter.utils.Language.translate;
|
||||
* Handles all gacha-related HTTP requests.
|
||||
*/
|
||||
public final class GachaHandler implements Router {
|
||||
private final String gachaMappings;
|
||||
|
||||
public GachaHandler() {
|
||||
this.gachaMappings = Utils.toFilePath(DATA("gacha/mappings.js"));
|
||||
if(!(new File(this.gachaMappings).exists())) {
|
||||
try {
|
||||
Tools.createGachaMapping(this.gachaMappings);
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().warn("Failed to create gacha mappings.", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static final String gachaMappings = DATA(Utils.toFilePath("gacha/mappings.js"));
|
||||
|
||||
@Override public void applyRoutes(Express express, Javalin handle) {
|
||||
express.get("/gacha", GachaHandler::gachaRecords);
|
||||
|
||||
Reference in New Issue
Block a user