Format code [skip actions]

This commit is contained in:
github-actions 2023-05-15 04:45:40 +00:00
parent bcc9ae10cd
commit e079eebc0f
21 changed files with 253 additions and 299 deletions

View File

@ -1,5 +1,8 @@
package emu.grasscutter;
import static emu.grasscutter.config.Configuration.SERVER;
import static emu.grasscutter.utils.Language.translate;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import emu.grasscutter.auth.AuthenticationSystem;
@ -26,6 +29,12 @@ import emu.grasscutter.server.http.handlers.GenericHandler;
import emu.grasscutter.server.http.handlers.LogHandler;
import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOError;
import java.io.IOException;
import java.util.Calendar;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
import org.jline.reader.EndOfFileException;
@ -37,16 +46,6 @@ import org.jline.terminal.TerminalBuilder;
import org.reflections.Reflections;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOError;
import java.io.IOException;
import java.util.Calendar;
import static emu.grasscutter.config.Configuration.SERVER;
import static emu.grasscutter.utils.Language.translate;
public final class Grasscutter {
public static final File configFile = new File("./config.json");
public static final Reflections reflector = new Reflections("emu.grasscutter");

View File

@ -4,12 +4,11 @@ import emu.grasscutter.game.Account;
import emu.grasscutter.server.http.objects.*;
import emu.grasscutter.utils.DispatchUtils;
import io.javalin.http.Context;
import javax.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import javax.annotation.Nullable;
/** Defines an authenticator for the server. Can be changed by plugins. */
public interface AuthenticationSystem {
@ -107,11 +106,11 @@ public interface AuthenticationSystem {
Authenticator<ComboTokenResJson> getSessionKeyAuthenticator();
/**
* This is the authenticator used for validating session tokens.
* This is a part of the logic in {@link DispatchUtils#authenticate(String, String)}.
* <p>
* Plugins can override this authenticator to add
* support for alternate session authentication methods.
* This is the authenticator used for validating session tokens. This is a part of the logic in
* {@link DispatchUtils#authenticate(String, String)}.
*
* <p>Plugins can override this authenticator to add support for alternate session authentication
* methods.
*
* @return {@code true} if the session token is valid, {@code false} otherwise.
*/

View File

@ -1,14 +1,14 @@
package emu.grasscutter.auth;
import static emu.grasscutter.config.Configuration.ACCOUNT;
import static emu.grasscutter.utils.Language.translate;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.DefaultAuthenticators.*;
import emu.grasscutter.game.Account;
import emu.grasscutter.server.http.objects.ComboTokenResJson;
import emu.grasscutter.server.http.objects.LoginResultJson;
import static emu.grasscutter.config.Configuration.ACCOUNT;
import static emu.grasscutter.utils.Language.translate;
/**
* The default Grasscutter authentication implementation. Allows all users to access any account.
*/

View File

@ -1,5 +1,8 @@
package emu.grasscutter.auth;
import static emu.grasscutter.config.Configuration.ACCOUNT;
import static emu.grasscutter.utils.Language.translate;
import at.favre.lib.crypto.bcrypt.BCrypt;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
@ -11,17 +14,13 @@ import emu.grasscutter.server.http.objects.ComboTokenResJson;
import emu.grasscutter.server.http.objects.LoginResultJson;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.Utils;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static emu.grasscutter.config.Configuration.ACCOUNT;
import static emu.grasscutter.utils.Language.translate;
import javax.crypto.Cipher;
/** A class containing default authenticators. */
public final class DefaultAuthenticators {
@ -349,7 +348,9 @@ public final class DefaultAuthenticators {
var client = Grasscutter.getGameServer().getDispatchClient();
var future = new CompletableFuture<Account>();
client.registerCallback(PacketIds.TokenValidateRsp, packet -> {
client.registerCallback(
PacketIds.TokenValidateRsp,
packet -> {
var data = IDispatcher.decode(packet);
// Check if the token is valid.
@ -360,8 +361,7 @@ public final class DefaultAuthenticators {
}
// Return the account data.
future.complete(IDispatcher.decode(
data.get("account"), Account.class));
future.complete(IDispatcher.decode(data.get("account"), Account.class));
});
client.sendMessage(PacketIds.TokenValidateReq, tokenRequest);

View File

@ -1,5 +1,7 @@
package emu.grasscutter.command.commands;
import static emu.grasscutter.utils.Language.translate;
import at.favre.lib.crypto.bcrypt.BCrypt;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
@ -9,12 +11,9 @@ import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;
import java.util.List;
import java.util.stream.Collectors;
import static emu.grasscutter.utils.Language.translate;
@Command(
label = "account",
usage = {

View File

@ -1,5 +1,7 @@
package emu.grasscutter.database;
import static emu.grasscutter.config.Configuration.DATABASE;
import com.mongodb.MongoCommandException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
@ -16,8 +18,6 @@ import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.game.Account;
import org.reflections.Reflections;
import static emu.grasscutter.config.Configuration.DATABASE;
public final class DatabaseManager {
private static Datastore gameDatastore;
private static Datastore dispatchDatastore;
@ -27,8 +27,7 @@ public final class DatabaseManager {
}
public static Datastore getAccountDatastore() {
if (Grasscutter.getRunMode() == ServerRunMode.HYBRID)
return gameDatastore;
if (Grasscutter.getRunMode() == ServerRunMode.HYBRID) return gameDatastore;
else return dispatchDatastore;
}

View File

@ -1,5 +1,7 @@
package emu.grasscutter.game.gacha;
import static emu.grasscutter.config.Configuration.GAME_OPTIONS;
import com.sun.nio.file.SensitivityWatchEventModifier;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader;
@ -29,14 +31,11 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.greenrobot.eventbus.Subscribe;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import static emu.grasscutter.config.Configuration.GAME_OPTIONS;
import org.greenrobot.eventbus.Subscribe;
public class GachaSystem extends BaseGameSystem {
private static final int starglitterId = 221;

View File

@ -1,17 +1,13 @@
package emu.grasscutter.server.dispatch;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.http.handlers.GachaHandler;
import emu.grasscutter.utils.Crypto;
import lombok.Getter;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import java.net.ConnectException;
import java.net.URI;
import java.nio.ByteBuffer;
@ -21,17 +17,17 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import lombok.Getter;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
public final class DispatchClient extends WebSocketClient implements IDispatcher {
@Getter private final Logger logger
= Grasscutter.getLogger();
@Getter private final Map<Integer, BiConsumer<WebSocket, JsonElement>> handlers
= new HashMap<>();
@Getter private final Logger logger = Grasscutter.getLogger();
@Getter private final Map<Integer, BiConsumer<WebSocket, JsonElement>> handlers = new HashMap<>();
@Getter private final Map<Integer, List<Consumer<JsonElement>>> callbacks
= new HashMap<>();
@Getter private final Map<Integer, List<Consumer<JsonElement>>> callbacks = new HashMap<>();
public DispatchClient(URI serverUri) {
super(serverUri);
@ -58,8 +54,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
var response = new JsonObject();
// Find a player with the specified account ID.
var player = Grasscutter.getGameServer()
.getPlayerByAccountId(accountId);
var player = Grasscutter.getGameServer().getPlayerByAccountId(accountId);
if (player == null) {
response.addProperty("retcode", 1);
this.sendMessage(PacketIds.GachaHistoryRsp, response);
@ -67,8 +62,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
}
// Fetch the gacha records.
GachaHandler.fetchGachaRecords(
player, response, page, type);
GachaHandler.fetchGachaRecords(player, response, page, type);
// Send the response.
this.sendMessage(PacketIds.GachaHistoryRsp, response);
@ -82,8 +76,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
public void sendMessage(int packetId, Object message) {
var serverMessage = this.encodeMessage(packetId, message);
// Serialize the message into JSON.
var serialized = JSON.toJson(serverMessage)
.getBytes(StandardCharsets.UTF_8);
var serialized = JSON.toJson(serverMessage).getBytes(StandardCharsets.UTF_8);
// Encrypt the message.
Crypto.xor(serialized, DISPATCH_INFO.encryptionKey);
// Send the message.
@ -100,8 +93,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
@Override
public void onMessage(String message) {
this.getLogger().debug("Received dispatch message from server:\n{}",
message);
this.getLogger().debug("Received dispatch message from server:\n{}", message);
}
@Override
@ -114,17 +106,20 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
this.getLogger().info("Dispatch connection closed.");
// Attempt to reconnect.
new Thread(() -> {
new Thread(
() -> {
try {
// Wait 5 seconds before reconnecting.
Thread.sleep(5000L);
} catch (Exception ignored) { }
} catch (Exception ignored) {
}
// Attempt to reconnect.
Grasscutter.getGameServer().setDispatchClient(
new DispatchClient(GameServer.getDispatchUrl()));
Grasscutter.getGameServer()
.setDispatchClient(new DispatchClient(GameServer.getDispatchUrl()));
Grasscutter.getGameServer().getDispatchClient().connect();
}).start();
})
.start();
}
@Override

View File

@ -1,16 +1,12 @@
package emu.grasscutter.server.dispatch;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.utils.Crypto;
import lombok.Getter;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
@ -19,18 +15,18 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import lombok.Getter;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
/* Internal communications server. */
public final class DispatchServer extends WebSocketServer implements IDispatcher {
@Getter private final Logger logger
= Grasscutter.getLogger();
@Getter private final Map<Integer, BiConsumer<WebSocket, JsonElement>> handlers
= new HashMap<>();
@Getter private final Logger logger = Grasscutter.getLogger();
@Getter private final Map<Integer, BiConsumer<WebSocket, JsonElement>> handlers = new HashMap<>();
@Getter private final Map<Integer, List<Consumer<JsonElement>>> callbacks
= new HashMap<>();
@Getter private final Map<Integer, List<Consumer<JsonElement>>> callbacks = new HashMap<>();
/**
* Constructs a new {@code DispatchServer} instance.
@ -52,15 +48,13 @@ public final class DispatchServer extends WebSocketServer implements IDispatcher
* @param object The packet data.
*/
private void handleLogin(WebSocket socket, JsonElement object) {
var dispatchKey = object.getAsString()
.replaceAll("\"", "");
var dispatchKey = object.getAsString().replaceAll("\"", "");
// Check if the dispatch key is valid.
if (!dispatchKey.equals(DISPATCH_INFO.dispatchKey)) {
this.getLogger().warn("Invalid dispatch key received from {}.",
socket.getRemoteSocketAddress());
this.getLogger().debug("Expected: {}, Received: {}",
DISPATCH_INFO.dispatchKey, dispatchKey);
this.getLogger()
.warn("Invalid dispatch key received from {}.", socket.getRemoteSocketAddress());
this.getLogger().debug("Expected: {}, Received: {}", DISPATCH_INFO.dispatchKey, dispatchKey);
socket.close();
} else {
socket.setAttachment(true);
@ -84,8 +78,7 @@ public final class DispatchServer extends WebSocketServer implements IDispatcher
// Create the response message.
var response = new JsonObject();
response.addProperty("valid", valid);
if (valid) response.add("account",
JSON.toJsonTree(account));
if (valid) response.add("account", JSON.toJsonTree(account));
// Send the response.
this.sendMessage(socket, PacketIds.TokenValidateRsp, response);
@ -98,8 +91,7 @@ public final class DispatchServer extends WebSocketServer implements IDispatcher
*/
public void sendMessage(int packetId, Object message) {
var serverMessage = this.encodeMessage(packetId, message);
this.getConnections().forEach(
socket -> this.sendMessage(socket, serverMessage));
this.getConnections().forEach(socket -> this.sendMessage(socket, serverMessage));
}
/**
@ -110,8 +102,7 @@ public final class DispatchServer extends WebSocketServer implements IDispatcher
*/
public void sendMessage(WebSocket socket, Object message) {
// Serialize the message into JSON.
var serialized = JSON.toJson(message)
.getBytes(StandardCharsets.UTF_8);
var serialized = JSON.toJson(message).getBytes(StandardCharsets.UTF_8);
// Encrypt the message.
Crypto.xor(serialized, DISPATCH_INFO.encryptionKey);
// Send the message.
@ -131,20 +122,18 @@ public final class DispatchServer extends WebSocketServer implements IDispatcher
@Override
public void onStart() {
this.getLogger().info("Dispatch server started on port {}.",
this.getPort());
this.getLogger().info("Dispatch server started on port {}.", this.getPort());
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
this.getLogger().debug("Dispatch client connected from {}.",
conn.getRemoteSocketAddress());
this.getLogger().debug("Dispatch client connected from {}.", conn.getRemoteSocketAddress());
}
@Override
public void onMessage(WebSocket conn, String message) {
this.getLogger().debug("Received dispatch message from {}:\n{}",
conn.getRemoteSocketAddress(), message);
this.getLogger()
.debug("Received dispatch message from {}:\n{}", conn.getRemoteSocketAddress(), message);
}
@Override
@ -154,8 +143,7 @@ public final class DispatchServer extends WebSocketServer implements IDispatcher
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
this.getLogger().debug("Dispatch client disconnected from {}.",
conn.getRemoteSocketAddress());
this.getLogger().debug("Dispatch client disconnected from {}.", conn.getRemoteSocketAddress());
}
@Override

View File

@ -1,25 +1,25 @@
package emu.grasscutter.server.dispatch;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.JsonAdapters.ByteArrayAdapter;
import org.java_websocket.WebSocket;
import org.slf4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import org.java_websocket.WebSocket;
import org.slf4j.Logger;
public interface IDispatcher {
Gson JSON = new GsonBuilder()
Gson JSON =
new GsonBuilder()
.disableHtmlEscaping()
.registerTypeAdapter(byte[].class, new ByteArrayAdapter())
.create();
@ -47,8 +47,7 @@ public interface IDispatcher {
var data = element.getAsString();
// Check if the element starts and ends with quotes.
if (data.startsWith("\"")
&& data.endsWith("\"")) {
if (data.startsWith("\"") && data.endsWith("\"")) {
// Remove the quotes.
data = data.substring(1, data.length() - 1);
}
@ -72,8 +71,7 @@ public interface IDispatcher {
// Decrypt the message.
Crypto.xor(message, DISPATCH_INFO.encryptionKey);
// Deserialize the message.
return JSON.fromJson(new String(
message, StandardCharsets.UTF_8), JsonObject.class);
return JSON.fromJson(new String(message, StandardCharsets.UTF_8), JsonObject.class);
}
/**
@ -116,8 +114,7 @@ public interface IDispatcher {
if (packetId != PacketIds.LoginNotify) {
if (socket.getAttachment() instanceof Boolean authenticated) {
if (!authenticated) {
this.getLogger().warn("Received packet ID {} from unauthenticated client.",
packetId);
this.getLogger().warn("Received packet ID {} from unauthenticated client.", packetId);
socket.close();
return;
}
@ -138,13 +135,11 @@ public interface IDispatcher {
// Get the callbacks.
var callbacks = this.getCallbacks().get(packetId);
// Call the callbacks.
callbacks.forEach(callback ->
callback.accept(packetData));
callbacks.forEach(callback -> callback.accept(packetData));
callbacks.clear();
}
} catch (Exception exception) {
this.getLogger().warn("Exception occurred while handling packet {}.",
packetId);
this.getLogger().warn("Exception occurred while handling packet {}.", packetId);
exception.printStackTrace();
}
}

View File

@ -1,5 +1,9 @@
package emu.grasscutter.server.game;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import static emu.grasscutter.utils.Language.translate;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerRunMode;
@ -37,22 +41,17 @@ import emu.grasscutter.server.event.internal.ServerStopEvent;
import emu.grasscutter.server.event.types.ServerEvent;
import emu.grasscutter.server.scheduler.ServerTaskScheduler;
import emu.grasscutter.task.TaskMap;
import kcp.highway.ChannelConfig;
import kcp.highway.KcpServer;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import java.net.InetSocketAddress;
import java.net.URI;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import static emu.grasscutter.utils.Language.translate;
import kcp.highway.ChannelConfig;
import kcp.highway.KcpServer;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
@Getter
public final class GameServer extends KcpServer {

View File

@ -1,5 +1,7 @@
package emu.grasscutter.server.http.dispatch;
import static emu.grasscutter.utils.Language.translate;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.OAuthAuthenticator.ClientType;
@ -12,8 +14,6 @@ import emu.grasscutter.utils.JsonUtils;
import io.javalin.Javalin;
import io.javalin.http.Context;
import static emu.grasscutter.utils.Language.translate;
/** Handles requests related to authentication. */
public final class AuthenticationHandler implements Router {
/**
@ -96,7 +96,8 @@ public final class AuthenticationHandler implements Router {
// Cached token login (from registry).
javalin.post("/hk4e_global/mdk/shield/api/verify", AuthenticationHandler::tokenLogin);
// Combo token login (from session key).
javalin.post("/hk4e_global/combo/granter/login/v2/login", AuthenticationHandler::sessionKeyLogin);
javalin.post(
"/hk4e_global/combo/granter/login/v2/login", AuthenticationHandler::sessionKeyLogin);
// CN
// Username & Password login (from client).

View File

@ -1,5 +1,7 @@
package emu.grasscutter.server.http.dispatch;
import static emu.grasscutter.config.Configuration.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.protobuf.ByteString;
@ -21,15 +23,12 @@ import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.Utils;
import io.javalin.Javalin;
import io.javalin.http.Context;
import org.slf4j.Logger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import static emu.grasscutter.config.Configuration.*;
import org.slf4j.Logger;
/** Handles requests related to region queries. */
public final class RegionHandler implements Router {
@ -342,7 +341,8 @@ public final class RegionHandler implements Router {
* @return A {@link QueryCurrRegionHttpRsp} object.
*/
public static QueryCurrRegionHttpRsp getCurrentRegion() {
return Grasscutter.getRunMode() == ServerRunMode.HYBRID ?
regions.get("os_usa").getRegionQuery() : null;
return Grasscutter.getRunMode() == ServerRunMode.HYBRID
? regions.get("os_usa").getRegionQuery()
: null;
}
}

View File

@ -1,5 +1,7 @@
package emu.grasscutter.server.http.handlers;
import static emu.grasscutter.utils.Language.translate;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
@ -12,16 +14,13 @@ import io.javalin.Javalin;
import io.javalin.http.ContentType;
import io.javalin.http.Context;
import io.javalin.http.staticfiles.Location;
import lombok.Getter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedHashSet;
import static emu.grasscutter.utils.Language.translate;
import lombok.Getter;
/** Handles all gacha-related HTTP requests. */
public final class GachaHandler implements Router {
@ -49,8 +48,7 @@ public final class GachaHandler implements Router {
if (gachaTypeStr != null) gachaType = Integer.parseInt(gachaTypeStr);
// 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").getAsString();
var maxPage = data.get("maxPage").getAsLong();
@ -77,7 +75,8 @@ public final class GachaHandler implements Router {
return;
}
String template;try {
String template;
try {
template = Files.readString(detailsTemplate);
} catch (IOException e) {
Grasscutter.getLogger().warn("Failed to read data/gacha/details.html");
@ -113,8 +112,7 @@ public final class GachaHandler implements Router {
// Add 5-star items.
var fiveStarItems = new LinkedHashSet<String>();
Arrays.stream(banner.getRateUpItems5())
.forEach(i -> fiveStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getRateUpItems5()).forEach(i -> fiveStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getFallbackItems5Pool1())
.forEach(i -> fiveStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getFallbackItems5Pool2())
@ -125,8 +123,7 @@ public final class GachaHandler implements Router {
// Add 4-star items.
var fourStarItems = new LinkedHashSet<String>();
Arrays.stream(banner.getRateUpItems4())
.forEach(i -> fourStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getRateUpItems4()).forEach(i -> fourStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getFallbackItems4Pool1())
.forEach(i -> fourStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getFallbackItems4Pool2())
@ -136,8 +133,7 @@ public final class GachaHandler implements Router {
// Add 3-star items.
var threeStarItems = new LinkedHashSet<String>();
Arrays.stream(banner.getFallbackItems3())
.forEach(i -> threeStarItems.add(Integer.toString(i)));
Arrays.stream(banner.getFallbackItems3()).forEach(i -> threeStarItems.add(Integer.toString(i)));
template = template.replace("{{THREE_STARS}}", "[" + String.join(",", threeStarItems) + "]");
// Done.
@ -153,15 +149,10 @@ public final class GachaHandler implements Router {
* @param page The page to fetch.
* @param type The gacha type to fetch.
*/
public static void fetchGachaRecords(
Player player, JsonObject response,
int page, int type
) {
public static void fetchGachaRecords(Player player, JsonObject response, int page, int type) {
var playerId = player.getUid();
var records = DatabaseHelper.getGachaRecords(
playerId, page, type).toString();
var maxPage = DatabaseHelper.getGachaRecordsMaxPage(
playerId, page, type);
var records = DatabaseHelper.getGachaRecords(playerId, page, type).toString();
var maxPage = DatabaseHelper.getGachaRecordsMaxPage(playerId, page, type);
// Finish the response.
response.addProperty("retcode", 0);

View File

@ -2,9 +2,7 @@ package emu.grasscutter.server.http.objects;
import lombok.Builder;
/**
* This request object is used in both token-related authenticators.
*/
/** This request object is used in both token-related authenticators. */
@Builder
public class LoginTokenRequestJson {
public String uid;

View File

@ -1,5 +1,7 @@
package emu.grasscutter.server.packet.recv;
import static emu.grasscutter.config.Configuration.ACCOUNT;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.player.Player;
@ -15,12 +17,9 @@ import emu.grasscutter.utils.ByteHelper;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.DispatchUtils;
import emu.grasscutter.utils.Utils;
import javax.crypto.Cipher;
import java.nio.ByteBuffer;
import java.security.Signature;
import static emu.grasscutter.config.Configuration.ACCOUNT;
import javax.crypto.Cipher;
@Opcodes(PacketOpcodes.GetPlayerTokenReq)
public class HandlerGetPlayerTokenReq extends PacketHandler {
@ -30,8 +29,7 @@ public class HandlerGetPlayerTokenReq extends PacketHandler {
// Fetch the account from the ID and token.
var accountId = req.getAccountUid();
var account = DispatchUtils.authenticate(
accountId, req.getAccountToken());
var account = DispatchUtils.authenticate(accountId, req.getAccountToken());
// Check the account.
if (account == null) {
@ -47,8 +45,7 @@ public class HandlerGetPlayerTokenReq extends PacketHandler {
// secondly !!!
// TODO - optimize
boolean kicked = false;
var exists = Grasscutter.getGameServer()
.getPlayerByAccountId(accountId);
var exists = Grasscutter.getGameServer().getPlayerByAccountId(accountId);
if (exists != null) {
var existsSession = exists.getSession();
if (existsSession != session) { // No self-kicking

View File

@ -1,5 +1,8 @@
package emu.grasscutter.server.packet.send;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import static emu.grasscutter.config.Configuration.lr;
import com.google.protobuf.ByteString;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerRunMode;
@ -11,12 +14,8 @@ import emu.grasscutter.net.proto.RegionInfoOuterClass.RegionInfo;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.http.dispatch.RegionHandler;
import emu.grasscutter.utils.Crypto;
import java.util.Objects;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import static emu.grasscutter.config.Configuration.lr;
public class PacketPlayerLoginRsp extends BasePacket {
private static QueryCurrRegionHttpRspOuterClass.QueryCurrRegionHttpRsp regionCache;

View File

@ -1,5 +1,7 @@
package emu.grasscutter.utils;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
@ -9,17 +11,15 @@ import emu.grasscutter.server.dispatch.IDispatcher;
import emu.grasscutter.server.dispatch.PacketIds;
import emu.grasscutter.server.http.handlers.GachaHandler;
import emu.grasscutter.server.http.objects.LoginTokenRequestJson;
import javax.annotation.Nullable;
import java.net.http.HttpClient;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
import javax.annotation.Nullable;
public interface DispatchUtils {
/** HTTP client used for dispatch queries. */
HttpClient HTTP_CLIENT = HttpClient.newBuilder()
HttpClient HTTP_CLIENT =
HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
@ -38,8 +38,7 @@ public interface DispatchUtils {
* @param token The token.
* @return {@code true} if the authentication request is valid, otherwise {@code false}.
*/
@Nullable
static Account authenticate(String accountId, String token) {
@Nullable static Account authenticate(String accountId, String token) {
return switch (Grasscutter.getRunMode()) {
case GAME_ONLY ->
// Use the authentication system to validate the token.
@ -47,13 +46,8 @@ public interface DispatchUtils {
.getSessionTokenValidator()
.authenticate(
AuthenticationRequest.builder()
.tokenRequest(
LoginTokenRequestJson.builder()
.uid(accountId)
.token(token)
.build()
).build()
);
.tokenRequest(LoginTokenRequestJson.builder().uid(accountId).token(token).build())
.build());
case HYBRID, DISPATCH_ONLY -> {
// Fetch the account from the database.
var account = DatabaseHelper.getAccountById(accountId);
@ -86,8 +80,9 @@ public interface DispatchUtils {
var future = new CompletableFuture<JsonObject>();
// Listen for the response.
var server = Grasscutter.getDispatchServer();
server.registerCallback(PacketIds.GachaHistoryRsp, packet ->
future.complete(IDispatcher.decode(packet, JsonObject.class)));
server.registerCallback(
PacketIds.GachaHistoryRsp,
packet -> future.complete(IDispatcher.decode(packet, JsonObject.class)));
// Broadcast the request.
server.sendMessage(PacketIds.GachaHistoryReq, request);
@ -104,16 +99,14 @@ public interface DispatchUtils {
var response = new JsonObject();
// Get the player's ID from the account.
var player = Grasscutter.getGameServer()
.getPlayerByAccountId(accountId);
var player = Grasscutter.getGameServer().getPlayerByAccountId(accountId);
if (player == null) {
response.addProperty("retcode", 1);
yield response;
}
// Fetch the gacha records.
GachaHandler.fetchGachaRecords(
player, response, page, gachaType);
GachaHandler.fetchGachaRecords(player, response, page, gachaType);
yield response;
}

View File

@ -11,13 +11,12 @@ import emu.grasscutter.data.common.DynamicFloat;
import it.unimi.dsi.fastutil.floats.FloatArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import lombok.val;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import lombok.val;
public class JsonAdapters {
static class DynamicFloatAdapter extends TypeAdapter<DynamicFloat> {

View File

@ -1,5 +1,9 @@
package emu.grasscutter.utils;
import static emu.grasscutter.config.Configuration.FALLBACK_LANGUAGE;
import static emu.grasscutter.utils.FileUtils.getCachePath;
import static emu.grasscutter.utils.FileUtils.getResourcePath;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
@ -13,8 +17,6 @@ import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.EqualsAndHashCode;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -26,10 +28,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static emu.grasscutter.config.Configuration.FALLBACK_LANGUAGE;
import static emu.grasscutter.utils.FileUtils.getCachePath;
import static emu.grasscutter.utils.FileUtils.getResourcePath;
import lombok.EqualsAndHashCode;
public final class Language {
private static final Map<String, Language> cachedLanguages = new ConcurrentHashMap<>();

View File

@ -1,31 +1,35 @@
package emu.grasscutter.utils;
import static emu.grasscutter.config.Configuration.*;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import emu.grasscutter.BuildConfig;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.net.packet.PacketOpcodesUtils;
import emu.grasscutter.tools.Dumpers;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import static emu.grasscutter.config.Configuration.*;
import org.slf4j.LoggerFactory;
/** A parser for start-up arguments. */
public final class StartupArguments {
/* A map of parameter -> argument handler. */
private static final Map<String, Function<String, Boolean>> argumentHandlers = new HashMap<>() {
private static final Map<String, Function<String, Boolean>> argumentHandlers =
new HashMap<>() {
{
putAll(Map.of("-dumppacketids",
putAll(
Map.of(
"-dumppacketids",
parameter -> {
PacketOpcodesUtils.dumpPacketIds();
return true;
},
"-version", StartupArguments::printVersion,
"-debug", StartupArguments::enableDebug,
"-version",
StartupArguments::printVersion,
"-debug",
StartupArguments::enableDebug,
"-lang",
parameter -> {
Grasscutter.setPreferredLanguage(parameter);
@ -54,18 +58,19 @@ public final class StartupArguments {
SERVER.http.encryption.useEncryption = false;
return false;
},
"-dump", StartupArguments::dump,
"-dump",
StartupArguments::dump,
// Aliases.
"-v", StartupArguments::printVersion
));
putAll(Map.of(
"-v",
StartupArguments::printVersion));
putAll(
Map.of(
"-debugall",
parameter -> {
StartupArguments.enableDebug("all");
return false;
}
));
}));
}
};