Optimize meta infos even more
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-06-14 14:57:39 +02:00
Ursprung 2ddfa9e80f
Commit ea9a4c2a81
2 geänderte Dateien mit 13 neuen und 29 gelöschten Zeilen

Datei anzeigen

@ -27,6 +27,8 @@ import java.io.*;
@EqualsAndHashCode @EqualsAndHashCode
public abstract class NetworkPacket implements Serializable { public abstract class NetworkPacket implements Serializable {
public transient MetaInfos metaInfos;
@SneakyThrows @SneakyThrows
public byte[] serialize() { public byte[] serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -41,7 +43,9 @@ public abstract class NetworkPacket implements Serializable {
} }
public static void handle(MetaInfos metaInfos, byte[] data) { public static void handle(MetaInfos metaInfos, byte[] data) {
PacketHandler.handlePacket(metaInfos, deserialize(data)); NetworkPacket networkPacket = deserialize(data);
networkPacket.metaInfos = metaInfos;
PacketHandler.handlePacket(networkPacket);
} }
@SneakyThrows @SneakyThrows

Datei anzeigen

@ -36,36 +36,24 @@ public abstract class PacketHandler {
private final Map<Class<? extends NetworkPacket>, Method> HANDLER_MAP = new HashMap<>(); private final Map<Class<? extends NetworkPacket>, Method> HANDLER_MAP = new HashMap<>();
public static void handlePacket(NetworkPacket packet) { public static void handlePacket(NetworkPacket packet) {
handlePacket(null, packet);
}
public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) {
for (PacketHandler handler : PACKET_HANDLERS) { for (PacketHandler handler : PACKET_HANDLERS) {
handler.handle(metaInfos, packet); handler.handle(packet);
} }
} }
protected PacketHandler() { protected PacketHandler() {
Method[] methods = getClass().getMethods(); Method[] methods = getClass().getMethods();
for (Method method : methods) { for (Method method : methods) {
Handler handler = method.getAnnotation(Handler.class); if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
if (handler == null) {
continue;
}
if (!(method.getParameterCount() > 0 && method.getParameterCount() < 3)) {
continue;
}
if (!NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) {
continue;
}
if (method.getParameterCount() == 2 && !MetaInfos.class.isAssignableFrom(method.getParameterTypes()[1])) {
continue; continue;
} }
if (method.isAnnotationPresent(Handler.class)) {
Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0]; Class<? extends NetworkPacket> packetClass = (Class<? extends NetworkPacket>) method.getParameterTypes()[0];
HANDLER_MAP.put(packetClass, method); HANDLER_MAP.put(packetClass, method);
} }
} }
}
public void register() { public void register() {
PACKET_HANDLERS.add(this); PACKET_HANDLERS.add(this);
@ -76,21 +64,13 @@ public abstract class PacketHandler {
} }
@SneakyThrows @SneakyThrows
public void handle(MetaInfos metaInfos, NetworkPacket packet) { public void handle(NetworkPacket packet) {
Method method = HANDLER_MAP.get(packet.getClass()); Method method = HANDLER_MAP.get(packet.getClass());
if (method == null) { if (method == null) {
return; return;
} }
if (method.getParameterCount() == 2) {
if (metaInfos == null || method.getParameterTypes()[1].isAssignableFrom(metaInfos.getClass())) {
method.invoke(this, packet, metaInfos);
} else {
throw new IllegalArgumentException("MetaInfos is not assignable to " + method.getParameterTypes()[1]);
}
} else {
method.invoke(this, packet); method.invoke(this, packet);
} }
}
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
protected @interface Handler {} protected @interface Handler {}