From 832c460a830c0f2e1542b59b0b4b78f8bc6c77e6 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 23 Apr 2022 02:08:31 -0400 Subject: [PATCH] Implement packet send/receive events --- .../emu/grasscutter/server/event/Event.java | 7 ++-- .../server/event/game/ReceivePacketEvent.java | 35 +++++++++++++++++++ .../server/event/game/SendPacketEvent.java | 30 ++++++++++++++++ .../server/game/GameServerPacketHandler.java | 11 +++--- .../grasscutter/server/game/GameSession.java | 12 +++---- 5 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java create mode 100644 src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java diff --git a/src/main/java/emu/grasscutter/server/event/Event.java b/src/main/java/emu/grasscutter/server/event/Event.java index 7dd67b84d..bea7dd66f 100644 --- a/src/main/java/emu/grasscutter/server/event/Event.java +++ b/src/main/java/emu/grasscutter/server/event/Event.java @@ -18,10 +18,9 @@ public abstract class Event { /** * Cancels the event if possible. */ - public void cancel() throws IllegalAccessException { - if(!(this instanceof Cancellable)) - throw new IllegalAccessException("Event is not cancellable."); - this.cancelled = true; + public void cancel() { + if(this instanceof Cancellable) + this.cancelled = true; } /** diff --git a/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java b/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java new file mode 100644 index 000000000..51109c720 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java @@ -0,0 +1,35 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.game.GameSession; + +public final class ReceivePacketEvent extends ServerEvent implements Cancellable { + private final GameSession gameSession; + private final int packetId; + private byte[] packetData; + + public ReceivePacketEvent(GameSession gameSession, int packetId, byte[] packetData) { + super(Type.GAME); + + this.gameSession = gameSession; + this.packetId = packetId; + this.packetData = packetData; + } + + public GameSession getGameSession() { + return this.gameSession; + } + + public int getPacketId() { + return this.packetId; + } + + public void setPacketData(byte[] packetData) { + this.packetData = packetData; + } + + public byte[] getPacketData() { + return this.packetData; + } +} diff --git a/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java b/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java new file mode 100644 index 000000000..7a25b4e10 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java @@ -0,0 +1,30 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.net.packet.GenshinPacket; +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.game.GameSession; + +public final class SendPacketEvent extends ServerEvent implements Cancellable { + private final GameSession gameSession; + private GenshinPacket packet; + + public SendPacketEvent(GameSession gameSession, GenshinPacket packet) { + super(Type.GAME); + + this.gameSession = gameSession; + this.packet = packet; + } + + public GameSession getGameSession() { + return this.gameSession; + } + + public void setPacket(GenshinPacket packet) { + this.packet = packet; + } + + public GenshinPacket getPacket() { + return this.packet; + } +} diff --git a/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java b/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java index 62a57df91..50d508bed 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java +++ b/src/main/java/emu/grasscutter/server/game/GameServerPacketHandler.java @@ -2,6 +2,7 @@ package emu.grasscutter.server.game; import java.util.Set; +import emu.grasscutter.server.event.game.ReceivePacketEvent; import org.reflections.Reflections; import emu.grasscutter.Grasscutter; @@ -48,9 +49,7 @@ public class GameServerPacketHandler { } public void handle(GameSession session, int opcode, byte[] header, byte[] payload) { - PacketHandler handler = null; - - handler = this.handlers.get(opcode); + PacketHandler handler = this.handlers.get(opcode); if (handler != null) { try { @@ -77,8 +76,10 @@ public class GameServerPacketHandler { } } - // Handle - handler.handle(session, header, payload); + // Invoke event. + ReceivePacketEvent event = new ReceivePacketEvent(session, opcode, payload); event.call(); + if(!event.isCanceled()) // If event is not canceled, continue. + handler.handle(session, header, event.getPacketData()); } catch (Exception ex) { // TODO Remove this when no more needed ex.printStackTrace(); diff --git a/src/main/java/emu/grasscutter/server/game/GameSession.java b/src/main/java/emu/grasscutter/server/game/GameSession.java index ebd66dc20..53b4f32cc 100644 --- a/src/main/java/emu/grasscutter/server/game/GameSession.java +++ b/src/main/java/emu/grasscutter/server/game/GameSession.java @@ -10,6 +10,7 @@ import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.net.packet.GenshinPacket; import emu.grasscutter.net.packet.PacketOpcodesUtil; import emu.grasscutter.netty.MihoyoKcpChannel; +import emu.grasscutter.server.event.game.SendPacketEvent; import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.FileUtils; import emu.grasscutter.utils.Utils; @@ -161,16 +162,15 @@ public class GameSession extends MihoyoKcpChannel { genshinPacket.buildHeader(this.getNextClientSequence()); } - // Build packet - byte[] data = genshinPacket.build(); - // Log if (Grasscutter.getConfig().getGameServerOptions().LOG_PACKETS) { logPacket(genshinPacket); } - - // Send - send(data); + + // Invoke event. + SendPacketEvent event = new SendPacketEvent(this, genshinPacket); event.call(); + if(!event.isCanceled()) // If event is not cancelled, continue. + this.send(event.getPacket().build()); } private void logPacket(int opcode) {