Skip to content

Commit

Permalink
Load network codec only once (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konicai authored Sep 21, 2023
1 parent 5fffeaa commit 953194c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.github.steveice10.mc.protocol.codec.PacketCodec;
import com.github.steveice10.mc.protocol.codec.PacketStateCodec;
import com.github.steveice10.mc.protocol.data.ProtocolState;
import com.github.steveice10.opennbt.NBTIO;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.Server;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.codec.PacketCodecHelper;
Expand All @@ -19,17 +21,31 @@
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.jetbrains.annotations.Nullable;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Objects;
import java.util.UUID;
import java.util.zip.GZIPInputStream;

/**
* Implements the Minecraft protocol.
*/
public class MinecraftProtocol extends PacketProtocol {

/**
* The network codec sent from the server to the client during {@link ProtocolState#CONFIGURATION}.
* Lazily loaded once when {@link #newServerSession(Server, Session)} is invoked,
* if {@link #isUseDefaultListeners()} is true.
*/
@Nullable
private static CompoundTag DEFAULT_NETWORK_CODEC;

/**
* The codec used for the Minecraft protocol.
*/
Expand Down Expand Up @@ -156,7 +172,11 @@ public void newServerSession(Server server, Session session) {
this.setState(ProtocolState.HANDSHAKE);

if (this.useDefaultListeners) {
session.addListener(new ServerListener());
if (DEFAULT_NETWORK_CODEC == null) {
DEFAULT_NETWORK_CODEC = loadNetworkCodec();
}

session.addListener(new ServerListener(DEFAULT_NETWORK_CODEC));
}
}

Expand Down Expand Up @@ -231,4 +251,13 @@ public Class<? extends Packet> getServerboundClass(int id) {
public PacketDefinition<?, ?> getClientboundDefinition(int id) {
return this.stateCodec.getClientboundDefinition(id);
}

public static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = Objects.requireNonNull(MinecraftProtocol.class.getClassLoader().getResourceAsStream("networkCodec.nbt")) ;
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (Exception e) {
throw new AssertionError("Unable to load network codec.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundStatusRequestPacket;
import com.github.steveice10.opennbt.NBTIO;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
Expand All @@ -37,10 +36,6 @@
import net.kyori.adventure.text.Component;

import javax.crypto.SecretKey;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
Expand All @@ -49,7 +44,6 @@
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import java.util.zip.GZIPInputStream;

/**
* Handles initial login and status requests for servers.
Expand All @@ -71,13 +65,16 @@ public class ServerListener extends SessionAdapter {
}
}

private final CompoundTag networkCodec;

private final byte[] challenge = new byte[4];
private String username = "";

private long lastPingTime = 0;
private int lastPingId = 0;

public ServerListener() {
public ServerListener(CompoundTag networkCodec) {
this.networkCodec = networkCodec;
new Random().nextBytes(this.challenge);
}

Expand Down Expand Up @@ -132,7 +129,7 @@ public void packetReceived(Session session, Packet packet) {
new Thread(new UserAuthTask(session, key)).start();
} else if (packet instanceof ServerboundLoginAcknowledgedPacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.CONFIGURATION);
session.send(new ClientboundRegistryDataPacket(loadNetworkCodec()));
session.send(new ClientboundRegistryDataPacket(networkCodec));
session.send(new ClientboundFinishConfigurationPacket());
}
} else if (protocol.getState() == ProtocolState.STATUS) {
Expand Down Expand Up @@ -254,14 +251,4 @@ public void run() {
}
}
}

public static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = ServerListener.class.getClassLoader().getResourceAsStream("networkCodec.nbt");
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (IOException e) {
e.printStackTrace();
throw new AssertionError("Unable to load network codec.");
}
}
}

0 comments on commit 953194c

Please sign in to comment.