Add CommonCore module

This commit is contained in:
2024-08-04 20:37:28 +02:00
parent 11b4fd3b9b
commit 3e6f8c9b91
88 changed files with 6728 additions and 0 deletions
@@ -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 <https://www.gnu.org/licenses/>.
*/
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.Parameter;
import java.util.ArrayList;
import java.util.List;
import static de.steamwar.RandomGenerator.*;
public class AllPackets {
public static final Class<? extends NetworkPacket>[] 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<NetworkPacket> getAllPackets() {
List<NetworkPacket> packets = new ArrayList<>();
for (Class<? extends NetworkPacket> packet : AllPackets.packets) {
Constructor<? extends NetworkPacket> longConstructor = (Constructor<? extends NetworkPacket>) packet.getConstructors()[0];
for (Constructor<?> constructor : packet.getConstructors()) {
if (constructor.getParameterCount() > longConstructor.getParameterCount()) {
longConstructor = (Constructor<? extends NetworkPacket>) constructor;
}
}
Object[] args = new Object[longConstructor.getParameterCount()];
Parameter[] parameters = longConstructor.getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
parameter.getName();
args[i] = generateRandom(parameter.getType());
}
NetworkPacket packetInstance = longConstructor.newInstance(args);
packets.add(packetInstance);
}
return packets;
}
}
@@ -0,0 +1,52 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.network;
import de.steamwar.network.packets.PacketHandler;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
public class PacketHandlerTest {
TestPacketHandler packetHandler;
@Before
public void setUp() {
packetHandler = new TestPacketHandler();
packetHandler.register();
}
@Test
public void testPacketHandler() {
TestPacket packet = new TestPacket();
try {
PacketHandler.handlePacket(packet);
} catch (Exception e) {
assertThat(e, instanceOf(InvocationTargetException.class));
assertThat(e.getCause(), instanceOf(SecurityException.class));
assertThat(e.getCause().getMessage(), equalTo(packet.getClass().getName()));
}
}
}
@@ -0,0 +1,38 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.network;
import de.steamwar.network.packets.NetworkPacket;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
public class PacketTest {
@Test
public void testPacketSerialization() {
for (NetworkPacket allPacket : AllPackets.getAllPackets()) {
byte[] data = allPacket.serialize();
NetworkPacket deserializedPacket = NetworkPacket.deserialize(data);
assertThat("Packet: " + allPacket.getClass().getName() + " is not correctly deserialized", allPacket, equalTo(deserializedPacket));
}
}
}
@@ -0,0 +1,30 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.network;
import de.steamwar.network.packets.NetworkPacket;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class TestPacket extends NetworkPacket {
private static final long serialVersionUID = -9079104421295590626L;
}
@@ -0,0 +1,30 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.network;
import de.steamwar.network.packets.PacketHandler;
public class TestPacketHandler extends PacketHandler {
@Handler
public void handle(TestPacket packet) {
throw new SecurityException(packet.getClass().getName());
}
}