mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-19 02:15:45 +01:00
Added more server debug options (#1444)
Original commits: * Added more server debug options * made server debug code prettier * fixed initialization bug * Enables logging of packets contained in UnionCmdNotify, when debug level is WHITELIST or BLACKLIST
This commit is contained in:
@@ -26,10 +26,10 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
|
||||
private Account account;
|
||||
private Player player;
|
||||
|
||||
|
||||
private boolean useSecretKey;
|
||||
private SessionState state;
|
||||
|
||||
|
||||
private int clientTime;
|
||||
private long lastPingTime;
|
||||
private int lastClientSeq = 10;
|
||||
@@ -39,11 +39,11 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
this.state = SessionState.WAITING_FOR_TOKEN;
|
||||
this.lastPingTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
public GameServer getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
public InetSocketAddress getAddress() {
|
||||
try{
|
||||
return tunnel.getAddress();
|
||||
@@ -55,7 +55,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
public boolean useSecretKey() {
|
||||
return useSecretKey;
|
||||
}
|
||||
|
||||
|
||||
public Account getAccount() {
|
||||
return account;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
public void setAccount(Account account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
|
||||
public String getAccountId() {
|
||||
return this.getAccount().getId();
|
||||
}
|
||||
@@ -93,7 +93,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
public void setUseSecretKey(boolean useSecretKey) {
|
||||
this.useSecretKey = useSecretKey;
|
||||
}
|
||||
|
||||
|
||||
public int getClientTime() {
|
||||
return this.clientTime;
|
||||
}
|
||||
@@ -101,12 +101,12 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
public long getLastPingTime() {
|
||||
return lastPingTime;
|
||||
}
|
||||
|
||||
|
||||
public void updateLastPingTime(int clientTime) {
|
||||
this.clientTime = clientTime;
|
||||
this.lastPingTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
public int getNextClientSequence() {
|
||||
return ++lastClientSeq;
|
||||
}
|
||||
@@ -114,17 +114,21 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
public void replayPacket(int opcode, String name) {
|
||||
String filePath = PACKET(name);
|
||||
File p = new File(filePath);
|
||||
|
||||
|
||||
if (!p.exists()) return;
|
||||
|
||||
byte[] packet = FileUtils.read(p);
|
||||
|
||||
|
||||
BasePacket basePacket = new BasePacket(opcode);
|
||||
basePacket.setData(packet);
|
||||
|
||||
|
||||
send(basePacket);
|
||||
}
|
||||
|
||||
|
||||
public void logPacket( String sendOrRecv, int opcode, byte[] payload) {
|
||||
Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")");
|
||||
System.out.println(Utils.bytesToHex(payload));
|
||||
}
|
||||
public void send(BasePacket packet) {
|
||||
// Test
|
||||
if (packet.getOpcode() <= 0) {
|
||||
@@ -137,26 +141,34 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
if(PacketOpcodes.BANNED_PACKETS.contains(packet.getOpcode())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Header
|
||||
if (packet.shouldBuildHeader()) {
|
||||
packet.buildHeader(this.getNextClientSequence());
|
||||
}
|
||||
|
||||
|
||||
// Log
|
||||
if (SERVER.debugLevel == ServerDebugMode.ALL) {
|
||||
if (!loopPacket.contains(packet.getOpcode())) {
|
||||
Grasscutter.getLogger().info("SEND: " + PacketOpcodesUtil.getOpcodeName(packet.getOpcode()) + " (" + packet.getOpcode() + ")");
|
||||
System.out.println(Utils.bytesToHex(packet.getData()));
|
||||
logPacket("SEND",packet.getOpcode(), packet.getData());
|
||||
}
|
||||
}
|
||||
|
||||
if (SERVER.debugLevel == ServerDebugMode.WHITELIST && SERVER.DebugWhitelist.contains(packet.getOpcode())) {
|
||||
logPacket("SEND",packet.getOpcode(), packet.getData());
|
||||
}
|
||||
|
||||
if (SERVER.debugLevel == ServerDebugMode.BLACKLIST && !(SERVER.DebugBlacklist.contains(packet.getOpcode()))) {
|
||||
logPacket("SEND",packet.getOpcode(), packet.getData());
|
||||
}
|
||||
|
||||
// Invoke event.
|
||||
SendPacketEvent event = new SendPacketEvent(this, packet); event.call();
|
||||
if(!event.isCanceled()) { // If event is not cancelled, continue.
|
||||
tunnel.writeData(event.getPacket().build());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static final Set<Integer> loopPacket = Set.of(
|
||||
PacketOpcodes.PingReq,
|
||||
PacketOpcodes.PingRsp,
|
||||
@@ -216,10 +228,18 @@ public class GameSession implements GameSessionManager.KcpChannel {
|
||||
// Log packet
|
||||
if (allDebug) {
|
||||
if (!loopPacket.contains(opcode)) {
|
||||
Grasscutter.getLogger().info("RECV: " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")");
|
||||
System.out.println(Utils.bytesToHex(payload));
|
||||
logPacket("RECV",opcode, payload);
|
||||
}
|
||||
}
|
||||
|
||||
if (SERVER.debugLevel == ServerDebugMode.WHITELIST && SERVER.DebugWhitelist.contains(opcode)) {
|
||||
logPacket("RECV",opcode, payload);
|
||||
}
|
||||
|
||||
if (SERVER.debugLevel == ServerDebugMode.BLACKLIST && !(SERVER.DebugBlacklist.contains(opcode))) {
|
||||
logPacket("RECV",opcode, payload);
|
||||
}
|
||||
|
||||
// Handle
|
||||
getServer().getPacketHandler().handle(this, opcode, header, payload);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package emu.grasscutter.server.packet.recv;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.net.packet.Opcodes;
|
||||
import emu.grasscutter.net.packet.PacketOpcodes;
|
||||
import emu.grasscutter.net.proto.UnionCmdNotifyOuterClass.UnionCmdNotify;
|
||||
import emu.grasscutter.net.proto.UnionCmdOuterClass.UnionCmd;
|
||||
import emu.grasscutter.net.packet.PacketHandler;
|
||||
import emu.grasscutter.server.game.GameSession;
|
||||
import emu.grasscutter.Grasscutter.ServerDebugMode;
|
||||
|
||||
import static emu.grasscutter.Configuration.SERVER;
|
||||
|
||||
@Opcodes(PacketOpcodes.UnionCmdNotify)
|
||||
public class HandlerUnionCmdNotify extends PacketHandler {
|
||||
@@ -13,13 +17,21 @@ public class HandlerUnionCmdNotify extends PacketHandler {
|
||||
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
|
||||
UnionCmdNotify req = UnionCmdNotify.parseFrom(payload);
|
||||
for (UnionCmd cmd : req.getCmdListList()) {
|
||||
session.getServer().getPacketHandler().handle(session, cmd.getMessageId(), EMPTY_BYTE_ARRAY, cmd.getBody().toByteArray());
|
||||
int cmdOpcode = cmd.getMessageId();
|
||||
byte[] cmdPayload = cmd.getBody().toByteArray();
|
||||
if(Grasscutter.config.server.debugLevel == ServerDebugMode.WHITELIST && SERVER.DebugWhitelist.contains(cmd.getMessageId())) {
|
||||
session.logPacket("RECV in Union", cmdOpcode, cmdPayload);
|
||||
} else if (Grasscutter.config.server.debugLevel == ServerDebugMode.BLACKLIST && !SERVER.DebugBlacklist.contains(cmd.getMessageId())) {
|
||||
session.logPacket("RECV in Union", cmdOpcode, cmdPayload);
|
||||
}
|
||||
//debugLevel ALL ignores UnionCmdNotify, so we will also ignore the contained opcodes
|
||||
session.getServer().getPacketHandler().handle(session, cmd.getMessageId(), EMPTY_BYTE_ARRAY, cmd.getBody().toByteArray());
|
||||
}
|
||||
|
||||
|
||||
// Update
|
||||
session.getPlayer().getCombatInvokeHandler().update(session.getPlayer());
|
||||
session.getPlayer().getAbilityInvokeHandler().update(session.getPlayer());
|
||||
|
||||
|
||||
// Handle attack results last
|
||||
while (!session.getPlayer().getAttackResults().isEmpty()) {
|
||||
session.getPlayer().getScene().handleAttack(session.getPlayer().getAttackResults().poll());
|
||||
|
||||
Reference in New Issue
Block a user