Skip to content

Commit

Permalink
Catch packets to make sure it doesn't break
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Sep 21, 2023
1 parent c279e4e commit 9e07c37
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.88.5+1.20.2

maven_group = eu.pb4

mod_version = 0.6.0-rc.1
mod_version = 0.6.0-rc.2

minecraft_version_supported = ">=1.20.2-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static void handle(GameProfile profile, SyncedClientOptions options, Serv
connection.disableAutoRead();
while (iterator.hasNext()) {
var handler = iterator.next().apply(c);
connection.enableAutoRead();
if (handler != null) {
connection.enableAutoRead();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ public static void handle(ServerPlayerEntity player, SyncedClientOptions options
connection.disableAutoRead();
while (iterator.hasNext()) {
var handler = iterator.next().apply(c);
connection.enableAutoRead();
if (handler != null) {
connection.enableAutoRead();
return;
}
}
finish.accept(c);
}, new MutableObject<>(options));

((TempPlayerLoginAttachments) player).polymerNet$setLatePackets(context.storedPackets);

context.continueRunning.accept(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -29,7 +28,7 @@ static ExtClientConnection of(ServerCommonNetworkHandler networkHandler) {
Object2IntMap<Identifier> polymerNet$getSupportMap();
Object2ObjectMap<Identifier, NbtElement> polymerNet$getMetadataMap();

void polymerNet$ignorePacketsUntilChange(Consumer<CustomPayloadC2SPacket> consumer);
void polymerNet$wrongPacketConsumer(Consumer<CustomPayloadC2SPacket> consumer);
Channel polymerNet$getChannel();

static ExtClientConnection of(ClientConnection connection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public class ClientConnectionMixin implements ExtClientConnection {
private final Object2ObjectMap<Identifier, NbtElement> polymerNet$metadata = new Object2ObjectOpenHashMap<>();
@Unique
private Consumer<CustomPayloadC2SPacket> polymerNet$packetConsumer;
@Unique
private boolean polymerNet$packetActive;

@Override
public boolean polymerNet$hasPolymer() {
return !this.polymerNet$version.isEmpty();
Expand Down Expand Up @@ -76,17 +73,9 @@ public class ClientConnectionMixin implements ExtClientConnection {
return this.polymerNet$metadata;
}

@Inject(method = "setPacketListener", at = @At("HEAD"))
private void polymerNet$removeConsumer(PacketListener listener, CallbackInfo ci) {
this.polymerNet$packetActive = listener instanceof ServerLoginNetworkHandler;
if (listener instanceof ServerPlayNetworkHandler) {
this.polymerNet$packetConsumer = null;
}
}

@Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
private void polymerNet$handlePacket(ChannelHandlerContext channelHandlerContext, Packet<?> packet, CallbackInfo ci) {
if (this.polymerNet$packetActive && this.polymerNet$packetConsumer != null) {
if (this.polymerNet$packetConsumer != null) {
if (packet instanceof CustomPayloadC2SPacket c) {
this.polymerNet$packetConsumer.accept(c);
}
Expand All @@ -96,7 +85,7 @@ public class ClientConnectionMixin implements ExtClientConnection {
}

@Override
public void polymerNet$ignorePacketsUntilChange(Consumer<CustomPayloadC2SPacket> consumer) {
public void polymerNet$wrongPacketConsumer(Consumer<CustomPayloadC2SPacket> consumer) {
this.polymerNet$packetConsumer = consumer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ public class PlayerManagerMixin {
}
PolymerServerNetworking.ON_PLAY_SYNC.invoke(x -> x.accept(player.networkHandler, handshake));



var packets = ((TempPlayerLoginAttachments) player).polymerNet$getLatePackets();
if (packets != null) {
((TempPlayerLoginAttachments) player).polymerNet$setLatePackets(null);
for (var packet : packets) {
try {
packet.apply(player.networkHandler);
} catch (Throwable e) {

}
}
}

if (((TempPlayerLoginAttachments) player).polymerNet$getForceRespawnPacket()) {
var world = player.getServerWorld();
connection.send(new PlayerRespawnS2CPacket(player.createCommonPlayerSpawnInfo(world), PlayerRespawnS2CPacket.KEEP_ALL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.NetworkState;
import net.minecraft.network.listener.ServerConfigurationPacketListener;
import net.minecraft.network.listener.ServerPlayPacketListener;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.*;
Expand All @@ -27,19 +29,29 @@ public ServerConfigurationNetworkHandlerMixin(MinecraftServer server, ClientConn
@WrapOperation(method = "onReady", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/server/network/ConnectedClientData;)V"))
private void polymerNet$prePlayHandshakeHackfest(PlayerManager manager, ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, Operation<Void> original) {
EarlyPlayConnectionMagic.handle(player, clientData.syncedOptions(), (ServerConfigurationNetworkHandler) (Object) this, player.server, connection, (context) -> {
((ExtClientConnection) connection).polymerNet$ignorePacketsUntilChange(context.storedPackets()::add);
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(context.storedPackets()::add);
connection.disableAutoRead();
var attr = ((ExtClientConnection) connection).polymerNet$getChannel().attr(ClientConnection.SERVERBOUND_PROTOCOL_KEY);
attr.set(NetworkState.CONFIGURATION.getHandler(NetworkSide.SERVERBOUND));
connection.setPacketListener(this);
attr.set(NetworkState.PLAY.getHandler(NetworkSide.SERVERBOUND));
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(null);

if (connection.isOpen()) {
var oldPlayer = player.server.getPlayerManager().getPlayer(this.getProfile().getId());
if (oldPlayer != null) {
this.disconnect(Text.translatable("multiplayer.disconnect.duplicate_login"));
} else {
original.call(manager, connection, player, new ConnectedClientData(clientData.gameProfile(), clientData.latency(), context.options().getValue()));
if (this.connection.getPacketListener() instanceof ServerPlayPacketListener listener) {
for (var packetx : context.storedPackets()) {
try {
packetx.apply(listener);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
connection.enableAutoRead();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package eu.pb4.polymer.networking.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.authlib.GameProfile;
import eu.pb4.polymer.networking.api.util.ServerDynamicPacket;
import eu.pb4.polymer.networking.impl.*;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.NetworkState;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.listener.ServerCommonPacketListener;
import net.minecraft.network.listener.ServerConfigurationPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.network.packet.c2s.common.SyncedClientOptions;
import net.minecraft.network.packet.c2s.login.EnterConfigurationC2SPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.*;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -30,7 +19,6 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerLoginNetworkHandler.class)
Expand Down Expand Up @@ -72,11 +60,13 @@ public abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerEx
}
ci.cancel();
EarlyConfigurationConnectionMagic.handle(this.profile, SyncedClientOptions.createDefault(), (ServerLoginNetworkHandler) (Object) this, this.server, connection, (context) -> {
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(context.storedPackets()::add);
connection.disableAutoRead();
var attr = ((ExtClientConnection) connection).polymerNet$getChannel().attr(ClientConnection.SERVERBOUND_PROTOCOL_KEY);
attr.set(NetworkState.LOGIN.getHandler(NetworkSide.SERVERBOUND));
connection.setPacketListener((ServerLoginNetworkHandler) (Object) this);
attr.set(NetworkState.CONFIGURATION.getHandler(NetworkSide.SERVERBOUND));
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(null);

if (connection.isOpen()) {
this.polymerNet$ignoreCall = true;
Expand Down

0 comments on commit 9e07c37

Please sign in to comment.