Skip to content

Commit

Permalink
First actual working build following the massive changes
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Aug 19, 2020
1 parent 41753ef commit 3f8f06a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/main/java/cc/ghast/packet/PacketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public enum PacketManager {
private ChainManager manager;
private ChannelListener listener;

public void init() {
//this.inbound = new ArtemisDecoder(ProtocolDirection.IN);
//new ServerConnectionInjector().inject();
listener = new ChannelListener(this);
public void init(Plugin plugin) {
this.plugin = plugin;
this.manager = new ChainManager();
this.listener = new ChannelListener(this);
System.out.println("[Artemis Test] Enabled Test Decoder");
;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/cc/ghast/packet/PacketPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cc.ghast.packet;

import org.bukkit.plugin.java.JavaPlugin;

/**
* @author Ghast
* @since 19/08/2020
* Artemis © 2020
*/
public class PacketPlugin extends JavaPlugin {

@Override
public void onEnable() {
PacketManager.INSTANCE.init(this);
}
}
7 changes: 5 additions & 2 deletions src/main/java/cc/ghast/packet/codec/ArtemisDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import cc.ghast.packet.buffer.ProtocolByteBuf;
import cc.ghast.packet.buffer.types.Converters;
import cc.ghast.packet.profile.Profile;
import cc.ghast.packet.protocol.EnumProtocol;
import cc.ghast.packet.protocol.EnumProtocolCurrent;
import cc.ghast.packet.protocol.EnumProtocolLegacy;
import cc.ghast.packet.protocol.ProtocolDirection;
import cc.ghast.packet.wrapper.netty.MutableByteBuf;
Expand All @@ -32,12 +34,12 @@ public class ArtemisDecoder extends ChannelDuplexHandler {

private final Profile profile;
private final Inflater inflater;
private EnumProtocolLegacy protocol;
private EnumProtocol protocol;

public ArtemisDecoder(Profile profile) {
this.profile = profile;
this.inflater = new Inflater();
this.protocol = EnumProtocolLegacy.HANDSHAKE;
this.protocol = EnumProtocolCurrent.HANDSHAKE;
}


Expand Down Expand Up @@ -147,6 +149,7 @@ private MutableByteBuf decompress(MutableByteBuf byteBuf) throws DataFormatExcep

private void handleHandshake(PacketHandshakeClientSetProtocol handshake){
profile.setVersion(ProtocolVersion.getVersion(handshake.getProtocolVersion()));
System.out.println("Version=" + handshake.getVersion());
protocol = EnumProtocolLegacy.PLAY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class InjectorLegacy implements Injector {
@Override
public void inject(AsyncPlayerPreLoginEvent event) {
Channel channel = (Channel) ReflectUtil.getChannel(event.getUniqueId(), event.getAddress().toString());
Channel channel = (Channel) ReflectUtil.getChannel(event.getUniqueId(), event.getAddress().getHostAddress());
inject(channel, event.getUniqueId(), event.getAddress());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class InjectorModern implements Injector {
@Override
public void inject(AsyncPlayerPreLoginEvent event) {
Channel channel = (Channel) ReflectUtil.getChannel(event.getUniqueId(), event.getAddress().toString());
Channel channel = (Channel) ReflectUtil.getChannel(event.getUniqueId(), event.getAddress().getHostAddress());
inject(channel, event.getUniqueId(), event.getAddress());
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cc/ghast/packet/protocol/EnumProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface EnumProtocol {
Packet<?> getPacket(ProtocolDirection direction, int id, UUID playerId, ProtocolVersion version);
Class<? extends Packet<?>> getPacketClass(ProtocolDirection direction, String name);
int getOrdinal();
}
29 changes: 19 additions & 10 deletions src/main/java/cc/ghast/packet/protocol/EnumProtocolCurrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.Getter;
import lombok.SneakyThrows;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

Expand All @@ -28,17 +29,17 @@
public enum EnumProtocolCurrent implements EnumProtocol {
HANDSHAKE(-1, new Pair[][]{
// Client
{
new Pair[]{
new Pair<>(PacketHandshakeClientSetProtocol.class, "PacketHandshakeInSetProtocol")
},

// Server
{}
new Pair[]{}

}),
PLAY(0, new Pair[][]{
// Client
{
new Pair[]{
new Pair<>(PacketPlayClientKeepAlive.class, "PacketPlayInKeepAlive"),
new Pair<>(PacketPlayClientChat.class, "PacketPlayInChat"),
new Pair<>(PacketPlayClientUseEntity.class, "PacketPlayInUseEntity"),
Expand Down Expand Up @@ -158,25 +159,26 @@ public enum EnumProtocolCurrent implements EnumProtocol {
}),
STATUS(1, new Pair[][]{
// Client
{
new Pair[]{
new Pair<>(PacketStatusClientStart.class, "PacketStatusInStart"),
new Pair<>(PacketStatusClientPing.class, "PacketStatusInPing")
},

// Server
{
new Pair[]{
new Pair<>(PacketStatusServerInfoServer.class, "PacketStatusOutInfoServer"),
new Pair<>(PacketStatusServerPing.class, "PacketStatusOutPing")}
new Pair<>(PacketStatusServerPing.class, "PacketStatusOutPing")
}
}),
LOGIN(2, new Pair[][]{
// Client
{
new Pair[]{
new Pair<>(PacketLoginClientStart.class, "PacketLoginInStart"),
new Pair<>(PacketLoginClientEncryptionBegin.class, "PacketLoginInEncryptionBegin")
},

// Server
{
new Pair[]{
new Pair<>(PacketLoginServerDisconnect.class, "PacketLoginOutDisconnect"),
new Pair<>(PacketLoginServerEncryptionBegin.class, "PacketLoginOutEncryptionBegin"),
new Pair<>(PacketLoginServerSuccess.class, "PacketLoginOutSuccess"),
Expand All @@ -189,11 +191,12 @@ public enum EnumProtocolCurrent implements EnumProtocol {
// Direction = Ordinal so 0 = IN and 1 = OUT
// Second bit is just the id lol
private final Pair<Class<? extends Packet<?>>, String>[][] packets;
private final Map<ProtocolDirection, Map<Integer, Class<? extends Packet<?>>>> packetMap = ReflectUtil.getPacketMap(this);
private final Map<ProtocolDirection, Map<Integer, Class<? extends Packet<?>>>> packetMap;

EnumProtocolCurrent(int id, Pair<Class<? extends Packet<?>>, String>[][] packets) {
this.id = id;
this.packets = packets;
this.packetMap = ReflectUtil.getPacketMap(this);
}

@Override
Expand All @@ -202,7 +205,13 @@ public Packet<?> getPacket(ProtocolDirection direction, int id, UUID playerId, P
Class<? extends Packet<?>> clazz = packetMap.get(direction).get(id);
return clazz.getConstructor(UUID.class, ProtocolVersion.class).newInstance(playerId, version);
}


@Override
public int getOrdinal() {
return ordinal();
}

@Override
public Class<? extends Packet<?>> getPacketClass(ProtocolDirection direction, String name){
for (Pair<Class<? extends Packet<?>>, String> pair : packets[direction.ordinal()]) {
if (pair.getV().equalsIgnoreCase(name)) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cc/ghast/packet/protocol/EnumProtocolLegacy.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,14 @@ public Packet<?> getPacket(ProtocolDirection enumProtocolDirection, int i, UUID
Class<? extends Packet<?>> clazz = this.packetMap.get(enumProtocolDirection).get(i);
return clazz == null ? null : clazz.getConstructor(UUID.class, ProtocolVersion.class).newInstance(player, version);
}

@Override
public Class<? extends Packet<?>> getPacketClass(ProtocolDirection direction, String name) {
return this.packetMap.get(direction).get(i);
}

@Override
public int getOrdinal() {
return ordinal();
}
}
27 changes: 18 additions & 9 deletions src/main/java/cc/ghast/packet/reflections/ReflectUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cc.ghast.packet.reflections;

import cc.ghast.packet.protocol.EnumProtocol;
import cc.ghast.packet.protocol.EnumProtocolCurrent;
import cc.ghast.packet.protocol.ProtocolDirection;
import cc.ghast.packet.wrapper.packet.Packet;
import com.google.common.collect.Maps;
import org.bukkit.Bukkit;

import java.net.SocketAddress;
Expand All @@ -20,7 +22,7 @@ public class ReflectUtil {
*/
public static final Class<?> MINECRAFT_SERVER_CLAZZ = Reflection.getMinecraftClass("MinecraftServer");
public static final Class<?> CRAFT_SERVER_CLAZZ = Reflection.getCraftBukkitClass("CraftServer");
public static FieldAccessor<?> MINECRAFT_SERVER_FIELD = Reflection.getField(MINECRAFT_SERVER_CLAZZ, CRAFT_SERVER_CLAZZ, 0);
public static FieldAccessor<?> MINECRAFT_SERVER_FIELD = Reflection.getField(CRAFT_SERVER_CLAZZ, MINECRAFT_SERVER_CLAZZ, 0);
public static Object MINECRAFT_SERVER = MINECRAFT_SERVER_FIELD.get(Bukkit.getServer());

/*
Expand All @@ -36,7 +38,7 @@ public class ReflectUtil {
*/
public static final Class<?> NETWORK_MANAGER_CLAZZ = Reflection.getMinecraftClass("NetworkManager");
public static final FieldAccessor<List> NETWORK_MANAGERS_FIELD = Reflection.getField(SERVER_CONNECTION_CLAZZ, List.class, 1);

public static final FieldAccessor<?> CHANNEL_FIELD = Reflection.getField(NETWORK_MANAGER_CLAZZ, "channel", 0);
/*
Socket Field
*/
Expand All @@ -46,14 +48,14 @@ public class ReflectUtil {
/*
Enum Protocol Class
*/
public static final Class<?> ENUM_PROTOCOL_CLAZZ = Reflection.getMinecraftClass("EnumProtocolLegacy");
public static final Class<?> ENUM_PROTOCOL_CLAZZ = Reflection.getMinecraftClass("EnumProtocol");
public static final Object[] ENUM_PROTOCOLS = ENUM_PROTOCOL_CLAZZ.getEnumConstants();
public static final FieldAccessor<Map> PACKET_MAP_FIELD = Reflection.getField(ENUM_PROTOCOL_CLAZZ, Map.class, 1);

/*
Enum Direction Class
*/
public static final Class<?> ENUM_DIRECTION_CLAZZ = Reflection.getMinecraftClass("ProtocolDirection");
public static final Class<?> ENUM_DIRECTION_CLAZZ = Reflection.getMinecraftClass("EnumProtocolDirection");

// ServerBound = [0] -> To server
// ClientBound = [1] -> To client
Expand All @@ -66,22 +68,24 @@ public static Object getChannel(UUID uuid, String address){

Object future = futures.parallelStream().filter(ch -> {
SocketAddress address1 = (SocketAddress) ADDRESS_FIELD.get(ch);
return address.equalsIgnoreCase(parseAddress(address1));
String parsed = parseAddress(address1);
return address.equalsIgnoreCase(parsed);
}).findFirst().orElse(null);

return future;
if (future != null) return CHANNEL_FIELD.get(future);
return null;
}

private static String parseAddress(SocketAddress address) {
return address.toString().split("/")[1].split(":")[0];
}

public static Map<ProtocolDirection, Map<Integer, Class<? extends Packet<?>>>> getPacketMap(EnumProtocolCurrent id) {
public static Map<ProtocolDirection, Map<Integer, Class<? extends Packet<?>>>> getPacketMap(EnumProtocol id) {
// Create the map
Map<ProtocolDirection, Map<Integer, Class<? extends Packet<?>>>> map = new HashMap<>();

// Get the map from the id to match the Spigot enum protocol
Object enumProtocol = ENUM_PROTOCOLS[id.ordinal()];
Object enumProtocol = ENUM_PROTOCOLS[id.getOrdinal()];

// For every direction, we'll seek to getting all the values from it's map
for (int i = 0; i < ProtocolDirection.values().length; i++) {
Expand All @@ -94,8 +98,13 @@ public static Map<ProtocolDirection, Map<Integer, Class<? extends Packet<?>>>> g
// Get the map from the packet map
Map map1 = PACKET_MAP_FIELD.get(enumProtocol);

Map interest = (Map) map1.get(DIRECTIONS[i]);

// Map can be nullable. Just skip if it is
if (interest == null) continue;

// For every value iterated, get the integer and the clazz and match the name
map1.forEach((packetId, clazz) -> {
interest.forEach((packetId, clazz) -> {
// Grab the packet ID
int packet = (int) packetId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cc.ghast.packet.nms.ProtocolVersion;
import cc.ghast.packet.buffer.ProtocolByteBuf;
import cc.ghast.packet.wrapper.packet.Packet;
import cc.ghast.packet.wrapper.packet.ServerPacket;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
main: cc.ghast.packet.PacketManager
main: cc.ghast.packet.PacketPlugin
name: PacketTest
version: 1.0-SNAPSHOT

0 comments on commit 3f8f06a

Please sign in to comment.