From ddc7353ad1e6a68674ee61b8601f5630484a152a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 1 May 2022 21:50:26 +0200 Subject: [PATCH] :art: Refactor --- .../network/packets/NetworkHandler.java | 28 ------- .../network/packets/NetworkPacket.java | 37 ++++++--- .../network/packets/PacketHandler.java | 77 +++++++++++++++++ .../packets/client/ClientNetworkHandler.java | 31 ------- .../network/packets/client/ClientPacket.java | 37 --------- .../packets/client/ExecuteCommandPacket.java | 20 ++--- .../packets/client/ImALobbyPacket.java | 13 +-- .../client/InventoryCallbackPacket.java | 21 ++--- .../packets/client/PrepareSchemPacket.java | 21 ++--- .../network/packets/common/CommonPacket.java | 28 ------- .../packets/common/FightEndsPacket.java | 23 ++--- .../packets/common/FightInfoPacket.java | 31 ++----- .../packets/server/BaumemberUpdatePacket.java | 10 +-- .../packets/server/CloseInventoryPacket.java | 19 ++--- .../packets/server/InventoryPacket.java | 22 ++--- .../network/packets/server/PingPacket.java | 19 ++--- .../packets/server/ServerNetworkHandler.java | 30 ------- .../packets/server/StartingServerPacket.java | 19 ++--- testsrc/de/steamwar/RandomGenerator.java | 83 +++++++++++++++++++ testsrc/de/steamwar/network/AllPackets.java | 80 ++++++++++++++++++ .../de/steamwar/network/PacketTest.java | 27 +++--- 21 files changed, 351 insertions(+), 325 deletions(-) delete mode 100644 src/de/steamwar/network/packets/NetworkHandler.java create mode 100644 src/de/steamwar/network/packets/PacketHandler.java delete mode 100644 src/de/steamwar/network/packets/client/ClientNetworkHandler.java delete mode 100644 src/de/steamwar/network/packets/client/ClientPacket.java delete mode 100644 src/de/steamwar/network/packets/common/CommonPacket.java delete mode 100644 src/de/steamwar/network/packets/server/ServerNetworkHandler.java create mode 100644 testsrc/de/steamwar/RandomGenerator.java create mode 100644 testsrc/de/steamwar/network/AllPackets.java rename src/de/steamwar/network/packets/server/ServerPacket.java => testsrc/de/steamwar/network/PacketTest.java (57%) diff --git a/src/de/steamwar/network/packets/NetworkHandler.java b/src/de/steamwar/network/packets/NetworkHandler.java deleted file mode 100644 index ca19fe0..0000000 --- a/src/de/steamwar/network/packets/NetworkHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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; - -import de.steamwar.network.packets.common.FightInfoPacket; -import de.steamwar.network.packets.common.FightEndsPacket; - -public abstract class NetworkHandler { - public void handle(FightEndsPacket packet) {} - public void handle(FightInfoPacket packet) {} -} diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index c146913..32857c6 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -19,25 +19,36 @@ package de.steamwar.network.packets; -import java.io.Serializable; +import lombok.EqualsAndHashCode; +import java.io.*; + +@EqualsAndHashCode public abstract class NetworkPacket implements Serializable { - private final Sender sender; - - public NetworkPacket(Sender sender) { - this.sender = sender; + public byte[] serialize() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(this); + oos.flush(); + } catch (Exception e) { + throw new RuntimeException(e); + } + return baos.toByteArray(); } - public Sender getSender() { - return sender; + public static void handle(byte[] data) { + PacketHandler.handlePacket(deserialize(data)); } - public abstract void handle(NetworkHandler handler); - - public enum Sender { - SERVER, - CLIENT, - BOTH + public static NetworkPacket deserialize(byte[] data) { + ByteArrayInputStream bais = new ByteArrayInputStream(data); + try { + ObjectInputStream ois = new ObjectInputStream(bais); + return (NetworkPacket) ois.readObject(); + } catch (Exception e) { + throw new RuntimeException(e); + } } } diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java new file mode 100644 index 0000000..2f01ab0 --- /dev/null +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -0,0 +1,77 @@ +/* + * 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; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public abstract class PacketHandler { + + private static final List PACKET_HANDLERS = new ArrayList<>(); + + private final Map, Method> HANDLER_MAP = new HashMap<>(); + + public static void handlePacket(NetworkPacket packet) { + for (PacketHandler handler : PACKET_HANDLERS) { + handler.handle(packet); + } + } + + public PacketHandler() { + Method[] methods = getClass().getMethods(); + for (Method method : methods) { + if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { + continue; + } + if (method.isAnnotationPresent(Handler.class)) { + Class packetClass = (Class) method.getParameterTypes()[0]; + HANDLER_MAP.put(packetClass, method); + } + } + } + + public void register() { + PACKET_HANDLERS.add(this); + } + + public void unregister() { + PACKET_HANDLERS.remove(this); + } + + public void handle(NetworkPacket packet) { + Method method = HANDLER_MAP.get(packet.getClass()); + if (method == null) { + return; + } + try { + method.invoke(this, packet); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Retention(RetentionPolicy.RUNTIME) + protected @interface Handler {} +} diff --git a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java b/src/de/steamwar/network/packets/client/ClientNetworkHandler.java deleted file mode 100644 index becd500..0000000 --- a/src/de/steamwar/network/packets/client/ClientNetworkHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.client; - -import de.steamwar.network.packets.NetworkHandler; -import de.steamwar.network.packets.server.*; - -public abstract class ClientNetworkHandler extends NetworkHandler { - public void handle(BaumemberUpdatePacket packet) {} - public void handle(CloseInventoryPacket packet) {} - public void handle(InventoryPacket packet) {} - public void handle(PingPacket packet) {} - public void handle(StartingServerPacket packet) {} -} diff --git a/src/de/steamwar/network/packets/client/ClientPacket.java b/src/de/steamwar/network/packets/client/ClientPacket.java deleted file mode 100644 index 9829bf8..0000000 --- a/src/de/steamwar/network/packets/client/ClientPacket.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.client; - -import de.steamwar.network.packets.NetworkHandler; -import de.steamwar.network.packets.NetworkPacket; -import de.steamwar.network.packets.server.ServerNetworkHandler; - -public abstract class ClientPacket extends NetworkPacket { - public ClientPacket() { - super(Sender.CLIENT); - } - - @Override - public void handle(NetworkHandler handler) { - handle((ServerNetworkHandler) handler); - } - - public abstract void handle(ServerNetworkHandler handler); -} diff --git a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java index 62c3acb..0c56fc9 100644 --- a/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java +++ b/src/de/steamwar/network/packets/client/ExecuteCommandPacket.java @@ -19,21 +19,17 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class ExecuteCommandPacket extends ClientPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class ExecuteCommandPacket extends NetworkPacket { private static final long serialVersionUID = 6283457297487602016L; - @NonNull private int playerId; - @NonNull private String command; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/client/ImALobbyPacket.java b/src/de/steamwar/network/packets/client/ImALobbyPacket.java index 5656f63..dcd07dd 100644 --- a/src/de/steamwar/network/packets/client/ImALobbyPacket.java +++ b/src/de/steamwar/network/packets/client/ImALobbyPacket.java @@ -19,15 +19,10 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; +import de.steamwar.network.packets.NetworkPacket; +import lombok.EqualsAndHashCode; -@Data -public class ImALobbyPacket extends ClientPacket { +@EqualsAndHashCode(callSuper = true) +public class ImALobbyPacket extends NetworkPacket { private static final long serialVersionUID = 8110246509205246654L; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java index 1c11c66..2e1188b 100644 --- a/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java +++ b/src/de/steamwar/network/packets/client/InventoryCallbackPacket.java @@ -19,33 +19,22 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.*; @Builder -@Data +@NoArgsConstructor @AllArgsConstructor -public class InventoryCallbackPacket extends ClientPacket { +@EqualsAndHashCode(callSuper = true) +public class InventoryCallbackPacket extends NetworkPacket { private static final long serialVersionUID = -261823209186008718L; - @NonNull @Builder.Default private int position = -1; - @NonNull private int owner; - @NonNull private CallbackType type; - @NonNull @Builder.Default private ClickType clickType = ClickType.UNKNOWN; - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } - public enum CallbackType { CLICK, CLOSE, diff --git a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java index 55d2b39..5f9ae81 100644 --- a/src/de/steamwar/network/packets/client/PrepareSchemPacket.java +++ b/src/de/steamwar/network/packets/client/PrepareSchemPacket.java @@ -19,23 +19,18 @@ package de.steamwar.network.packets.client; -import de.steamwar.network.packets.server.ServerNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class PrepareSchemPacket extends ClientPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class PrepareSchemPacket extends NetworkPacket { private static final long serialVersionUID = -4798561188105813349L; - @NonNull private int player; - @NonNull private int schem; - @NonNull private String schemType; - - @Override - public void handle(ServerNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/common/CommonPacket.java b/src/de/steamwar/network/packets/common/CommonPacket.java deleted file mode 100644 index ad58090..0000000 --- a/src/de/steamwar/network/packets/common/CommonPacket.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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.common; - -import de.steamwar.network.packets.NetworkPacket; - -public abstract class CommonPacket extends NetworkPacket { - public CommonPacket() { - super(Sender.BOTH); - } -} diff --git a/src/de/steamwar/network/packets/common/FightEndsPacket.java b/src/de/steamwar/network/packets/common/FightEndsPacket.java index b89476e..93c992e 100644 --- a/src/de/steamwar/network/packets/common/FightEndsPacket.java +++ b/src/de/steamwar/network/packets/common/FightEndsPacket.java @@ -19,33 +19,22 @@ package de.steamwar.network.packets.common; -import de.steamwar.network.packets.NetworkHandler; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.*; import java.util.List; @Builder -@Data -public class FightEndsPacket extends CommonPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class FightEndsPacket extends NetworkPacket { private static final long serialVersionUID = 1279352415549011332L; - @NonNull private byte win; - @NonNull private int blueSchem; - @NonNull private int redSchem; - @NonNull private List bluePlayers; - @NonNull private List redPlayers; - @NonNull private String gameMode; - - @Override - public void handle(NetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/common/FightInfoPacket.java b/src/de/steamwar/network/packets/common/FightInfoPacket.java index ff2a8f7..b0dfb3f 100644 --- a/src/de/steamwar/network/packets/common/FightInfoPacket.java +++ b/src/de/steamwar/network/packets/common/FightInfoPacket.java @@ -19,49 +19,30 @@ package de.steamwar.network.packets.common; -import de.steamwar.network.packets.NetworkHandler; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.*; import java.util.List; @Builder -@Data -public class FightInfoPacket extends CommonPacket{ +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class FightInfoPacket extends NetworkPacket { private static final long serialVersionUID = 7448644597856605853L; - @NonNull private String serverName; - @NonNull private String gameMode; - @NonNull private String arena; - @NonNull private String blueName; - @NonNull private String redName; - @NonNull private String fightState; - @NonNull private int countdown; - @NonNull private int blueLeader; - @NonNull private int redLeader; - @NonNull private int blueSchem; - @NonNull private int redSchem; - @NonNull private List bluePlayers; - @NonNull private List redPlayers; - @NonNull private List spectators; - - @Override - public void handle(NetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java index ac610d5..b72dfe9 100644 --- a/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java +++ b/src/de/steamwar/network/packets/server/BaumemberUpdatePacket.java @@ -19,15 +19,13 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; +import de.steamwar.network.packets.NetworkPacket; import lombok.Data; +import lombok.EqualsAndHashCode; @Data -public class BaumemberUpdatePacket extends ServerPacket{ +@EqualsAndHashCode(callSuper = true) +public class BaumemberUpdatePacket extends NetworkPacket { private static final long serialVersionUID = 6863118892424244051L; - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java index 7ea7e6a..1212970 100644 --- a/src/de/steamwar/network/packets/server/CloseInventoryPacket.java +++ b/src/de/steamwar/network/packets/server/CloseInventoryPacket.java @@ -19,19 +19,16 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class CloseInventoryPacket extends ServerPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class CloseInventoryPacket extends NetworkPacket { private static final long serialVersionUID = -2191021190060504521L; - @NonNull private int playerId; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/InventoryPacket.java b/src/de/steamwar/network/packets/server/InventoryPacket.java index 1a507d1..298d518 100644 --- a/src/de/steamwar/network/packets/server/InventoryPacket.java +++ b/src/de/steamwar/network/packets/server/InventoryPacket.java @@ -19,27 +19,21 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import java.util.Map; -@Data -public class InventoryPacket extends ServerPacket { +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class InventoryPacket extends NetworkPacket { private static final long serialVersionUID = 8071052544654047316L; - @NonNull private String title; - @NonNull private int player; - @NonNull private int size; - @NonNull private Map items; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/PingPacket.java b/src/de/steamwar/network/packets/server/PingPacket.java index f2ca61e..0c7f0bf 100644 --- a/src/de/steamwar/network/packets/server/PingPacket.java +++ b/src/de/steamwar/network/packets/server/PingPacket.java @@ -19,18 +19,15 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class PingPacket extends ServerPacket { +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class PingPacket extends NetworkPacket { private static final long serialVersionUID = 714647343959550378L; - @NonNull private int id; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java b/src/de/steamwar/network/packets/server/ServerNetworkHandler.java deleted file mode 100644 index 7947b84..0000000 --- a/src/de/steamwar/network/packets/server/ServerNetworkHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.server; - -import de.steamwar.network.packets.NetworkHandler; -import de.steamwar.network.packets.client.*; - -public abstract class ServerNetworkHandler extends NetworkHandler { - public void handle(ExecuteCommandPacket packet) {} - public void handle(ImALobbyPacket packet) {} - public void handle(InventoryCallbackPacket packet) {} - public void handle(PrepareSchemPacket packet) {} -} diff --git a/src/de/steamwar/network/packets/server/StartingServerPacket.java b/src/de/steamwar/network/packets/server/StartingServerPacket.java index 3dadceb..2476e22 100644 --- a/src/de/steamwar/network/packets/server/StartingServerPacket.java +++ b/src/de/steamwar/network/packets/server/StartingServerPacket.java @@ -19,18 +19,15 @@ package de.steamwar.network.packets.server; -import de.steamwar.network.packets.client.ClientNetworkHandler; -import lombok.Data; -import lombok.NonNull; +import de.steamwar.network.packets.NetworkPacket; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; -@Data -public class StartingServerPacket extends ServerPacket { +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class StartingServerPacket extends NetworkPacket { private static final long serialVersionUID = 2808607245898121801L; - private int user; - - @Override - public void handle(ClientNetworkHandler handler) { - handler.handle(this); - } } diff --git a/testsrc/de/steamwar/RandomGenerator.java b/testsrc/de/steamwar/RandomGenerator.java new file mode 100644 index 0000000..b9ea1a2 --- /dev/null +++ b/testsrc/de/steamwar/RandomGenerator.java @@ -0,0 +1,83 @@ +/* + * 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; + +import java.util.*; +import java.util.function.Function; + +public class RandomGenerator { + + private static final Random random = new Random(); + private static final Map, Function, ?>> generators = new HashMap<>(); + + static { + generators.put(Integer.class, (clazz) -> random.nextInt()); + generators.put(int.class, (clazz) -> random.nextInt()); + generators.put(Double.class, (clazz) -> random.nextDouble()); + generators.put(double.class, (clazz) -> random.nextInt()); + generators.put(Boolean.class, (clazz) -> random.nextBoolean()); + generators.put(boolean.class, (clazz) -> random.nextBoolean()); + generators.put(Byte.class, (clazz) -> (byte) random.nextInt(127)); + generators.put(byte.class, (clazz) -> (byte) random.nextInt(127)); + + generators.put(List.class, aClass -> { + int length = random.nextInt(10); + List list = new ArrayList<>(); + for (int i = 0; i < length; i++) { + list.add(generators.get(Integer.class).apply(Integer.class)); + } + + return list; + }); + + generators.put(String.class, aClass -> { + int length = random.nextInt(10); + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < length; i++) { + builder.append(random.nextInt()); + } + + return builder.toString(); + }); + + generators.put(Map.class, aClass -> { + int length = random.nextInt(10); + Map map = new HashMap<>(); + for (int i = 0; i < length; i++) { + map.put(generators.get(Integer.class).apply(Integer.class), generators.get(String.class).apply(String.class)); + } + + return map; + }); + } + + public static T generateRandom(Class clazz) { + if(clazz.isEnum()) { + return generateRandomEnum(clazz); + } else { + return (T) generators.get(clazz).apply(clazz); + } + } + + public static T generateRandomEnum(Class clazz) { + return clazz.getEnumConstants()[random.nextInt(clazz.getEnumConstants().length)]; + } +} diff --git a/testsrc/de/steamwar/network/AllPackets.java b/testsrc/de/steamwar/network/AllPackets.java new file mode 100644 index 0000000..4d9fd86 --- /dev/null +++ b/testsrc/de/steamwar/network/AllPackets.java @@ -0,0 +1,80 @@ +/* + * 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; + +import de.steamwar.network.packets.NetworkPacket; +import de.steamwar.network.packets.client.ExecuteCommandPacket; +import de.steamwar.network.packets.client.ImALobbyPacket; +import de.steamwar.network.packets.client.InventoryCallbackPacket; +import de.steamwar.network.packets.client.PrepareSchemPacket; +import de.steamwar.network.packets.common.FightEndsPacket; +import de.steamwar.network.packets.common.FightInfoPacket; +import de.steamwar.network.packets.server.*; +import lombok.SneakyThrows; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.List; + +import static de.steamwar.RandomGenerator.*; + +public class AllPackets { + + public static final Class[] packets = new Class[]{ + ExecuteCommandPacket.class, + ImALobbyPacket.class, + InventoryCallbackPacket.class, + PrepareSchemPacket.class, + FightEndsPacket.class, + FightInfoPacket.class, + BaumemberUpdatePacket.class, + CloseInventoryPacket.class, + InventoryPacket.class, + PingPacket.class, + StartingServerPacket.class + }; + + @SneakyThrows + public static List getAllPackets() { + List packets = new ArrayList<>(); + + for (Class packet : AllPackets.packets) { + Constructor longConstructor = (Constructor) packet.getConstructors()[0]; + for (Constructor constructor : packet.getConstructors()) { + if (constructor.getParameterCount() > longConstructor.getParameterCount()) { + longConstructor = (Constructor) constructor; + } + } + + Object[] args = new Object[longConstructor.getParameterCount()]; + Parameter[] parameters = longConstructor.getParameters(); + for (int i = 0; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + args[i] = generateRandom(parameter.getType()); + } + + NetworkPacket packetInstance = longConstructor.newInstance(args); + packets.add(packetInstance); + } + return packets; + } +} diff --git a/src/de/steamwar/network/packets/server/ServerPacket.java b/testsrc/de/steamwar/network/PacketTest.java similarity index 57% rename from src/de/steamwar/network/packets/server/ServerPacket.java rename to testsrc/de/steamwar/network/PacketTest.java index bfab1e7..5a65da6 100644 --- a/src/de/steamwar/network/packets/server/ServerPacket.java +++ b/testsrc/de/steamwar/network/PacketTest.java @@ -17,21 +17,22 @@ * along with this program. If not, see . */ -package de.steamwar.network.packets.server; +package de.steamwar.network; -import de.steamwar.network.packets.NetworkHandler; import de.steamwar.network.packets.NetworkPacket; -import de.steamwar.network.packets.client.ClientNetworkHandler; +import org.junit.Test; -public abstract class ServerPacket extends NetworkPacket { - public ServerPacket() { - super(Sender.SERVER); +import static org.junit.Assert.*; + +public class PacketTest { + + @Test + public void testPacketSerialization() { + for (NetworkPacket allPacket : AllPackets.getAllPackets()) { + byte[] data = allPacket.serialize(); + NetworkPacket deserializedPacket = NetworkPacket.deserialize(data); + assertEquals("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, deserializedPacket); + System.out.println("Packet: " + allPacket.getClass().getName() + " is correctly deserialized"); + } } - - @Override - public void handle(NetworkHandler handler) { - handle((ClientNetworkHandler) handler); - } - - public abstract void handle(ClientNetworkHandler handler); }