From cf59dc97205dafa77f951060510f60fb0ed647c5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 14 Jun 2022 09:47:45 +0200 Subject: [PATCH] Add MetaInfos --- .../steamwar/network/packets/MetaInfos.java | 23 ++++++++++++++ .../network/packets/NetworkPacket.java | 4 +++ .../network/packets/PacketHandler.java | 31 ++++++++++++++----- 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/de/steamwar/network/packets/MetaInfos.java diff --git a/src/de/steamwar/network/packets/MetaInfos.java b/src/de/steamwar/network/packets/MetaInfos.java new file mode 100644 index 0000000..61632d7 --- /dev/null +++ b/src/de/steamwar/network/packets/MetaInfos.java @@ -0,0 +1,23 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.network.packets; + +public interface MetaInfos { +} diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index b1d6bf3..35c68c4 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -40,6 +40,10 @@ public abstract class NetworkPacket implements Serializable { PacketHandler.handlePacket(deserialize(data)); } + public static void handle(MetaInfos metaInfos, byte[] data) { + PacketHandler.handlePacket(metaInfos, deserialize(data)); + } + @SneakyThrows public static NetworkPacket deserialize(byte[] data) { ByteArrayInputStream bais = new ByteArrayInputStream(data); diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index f23afe5..11b9bcf 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -36,21 +36,34 @@ public abstract class PacketHandler { private final Map, Method> HANDLER_MAP = new HashMap<>(); public static void handlePacket(NetworkPacket packet) { + handlePacket(null, packet); + } + + public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) { for (PacketHandler handler : PACKET_HANDLERS) { - handler.handle(packet); + handler.handle(metaInfos, packet); } } protected PacketHandler() { Method[] methods = getClass().getMethods(); for (Method method : methods) { - if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { + Handler handler = method.getAnnotation(Handler.class); + if (handler == null) { continue; } - if (method.isAnnotationPresent(Handler.class)) { - Class packetClass = (Class) method.getParameterTypes()[0]; - HANDLER_MAP.put(packetClass, method); + 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; + } + + Class packetClass = (Class) method.getParameterTypes()[0]; + HANDLER_MAP.put(packetClass, method); } } @@ -63,12 +76,16 @@ public abstract class PacketHandler { } @SneakyThrows - public void handle(NetworkPacket packet) { + public void handle(MetaInfos metaInfos, NetworkPacket packet) { Method method = HANDLER_MAP.get(packet.getClass()); if (method == null) { return; } - method.invoke(this, packet); + if (method.getParameterCount() == 2) { + method.invoke(this, packet, metaInfos); + } else { + method.invoke(this, packet); + } } @Retention(RetentionPolicy.RUNTIME)