mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-26 16:40:55 +00:00
Implement packet send/receive events
This commit is contained in:
parent
600decbc09
commit
832c460a83
@ -18,10 +18,9 @@ public abstract class Event {
|
|||||||
/**
|
/**
|
||||||
* Cancels the event if possible.
|
* Cancels the event if possible.
|
||||||
*/
|
*/
|
||||||
public void cancel() throws IllegalAccessException {
|
public void cancel() {
|
||||||
if(!(this instanceof Cancellable))
|
if(this instanceof Cancellable)
|
||||||
throw new IllegalAccessException("Event is not cancellable.");
|
this.cancelled = true;
|
||||||
this.cancelled = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package emu.grasscutter.server.game;
|
|||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import emu.grasscutter.server.event.game.ReceivePacketEvent;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
@ -48,9 +49,7 @@ public class GameServerPacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void handle(GameSession session, int opcode, byte[] header, byte[] payload) {
|
public void handle(GameSession session, int opcode, byte[] header, byte[] payload) {
|
||||||
PacketHandler handler = null;
|
PacketHandler handler = this.handlers.get(opcode);
|
||||||
|
|
||||||
handler = this.handlers.get(opcode);
|
|
||||||
|
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
try {
|
try {
|
||||||
@ -77,8 +76,10 @@ public class GameServerPacketHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle
|
// Invoke event.
|
||||||
handler.handle(session, header, payload);
|
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) {
|
} catch (Exception ex) {
|
||||||
// TODO Remove this when no more needed
|
// TODO Remove this when no more needed
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
@ -10,6 +10,7 @@ import emu.grasscutter.game.GenshinPlayer;
|
|||||||
import emu.grasscutter.net.packet.GenshinPacket;
|
import emu.grasscutter.net.packet.GenshinPacket;
|
||||||
import emu.grasscutter.net.packet.PacketOpcodesUtil;
|
import emu.grasscutter.net.packet.PacketOpcodesUtil;
|
||||||
import emu.grasscutter.netty.MihoyoKcpChannel;
|
import emu.grasscutter.netty.MihoyoKcpChannel;
|
||||||
|
import emu.grasscutter.server.event.game.SendPacketEvent;
|
||||||
import emu.grasscutter.utils.Crypto;
|
import emu.grasscutter.utils.Crypto;
|
||||||
import emu.grasscutter.utils.FileUtils;
|
import emu.grasscutter.utils.FileUtils;
|
||||||
import emu.grasscutter.utils.Utils;
|
import emu.grasscutter.utils.Utils;
|
||||||
@ -161,16 +162,15 @@ public class GameSession extends MihoyoKcpChannel {
|
|||||||
genshinPacket.buildHeader(this.getNextClientSequence());
|
genshinPacket.buildHeader(this.getNextClientSequence());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build packet
|
|
||||||
byte[] data = genshinPacket.build();
|
|
||||||
|
|
||||||
// Log
|
// Log
|
||||||
if (Grasscutter.getConfig().getGameServerOptions().LOG_PACKETS) {
|
if (Grasscutter.getConfig().getGameServerOptions().LOG_PACKETS) {
|
||||||
logPacket(genshinPacket);
|
logPacket(genshinPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send
|
// Invoke event.
|
||||||
send(data);
|
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) {
|
private void logPacket(int opcode) {
|
||||||
|
Loading…
Reference in New Issue
Block a user