Skip to content

Commit

Permalink
simplify handler mixins, fixed memory leak
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
deirn committed Jan 8, 2025
1 parent 1b12a2e commit f7f20a0
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 140 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ group = lol.bai
majorVersion = 0

fabricLoader = 0.15.11
fabricApi = 0.99.3+1.21
fabricApi = 0.102.0+1.21
forge = 51.0.8
neo = 21.0.8-beta

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package lol.bai.badpackets.impl.handler;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.netty.buffer.Unpooled;
import lol.bai.badpackets.api.PacketReceiver;
import lol.bai.badpackets.api.PacketSender;
Expand All @@ -28,6 +18,16 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class AbstractPacketHandler<C, B extends FriendlyByteBuf> implements PacketSender {

protected final ChannelRegistry<B, C> registry;
Expand Down Expand Up @@ -169,10 +169,4 @@ public boolean canSend(ResourceLocation id) {
return sendableChannels.contains(id);
}

public interface Holder {

boolean badpackets_receive(CustomPacketPayload payload);

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package lol.bai.badpackets.impl.handler;

import java.util.Set;
import java.util.function.Consumer;

import lol.bai.badpackets.api.PacketReceiver;
import lol.bai.badpackets.api.play.ClientPlayContext;
import lol.bai.badpackets.impl.platform.PlatformProxy;
Expand All @@ -16,6 +13,9 @@
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;

import java.util.Set;
import java.util.function.Consumer;

public class ClientPlayPacketHandler extends AbstractPacketHandler<ClientPlayContext, RegistryFriendlyByteBuf> implements ClientPlayContext {

private final Minecraft client;
Expand All @@ -34,7 +34,7 @@ public static ClientPlayPacketHandler get() {
throw new IllegalStateException("Cannot get c2s sender when not in play stage!");
}

return ((ClientPlayPacketHandler.Holder) listener).badpackets_getHandler();
return ((PacketHandlerHolder<ClientPlayPacketHandler>) listener).badpackets_handler();
}

@Override
Expand Down Expand Up @@ -65,10 +65,4 @@ public ClientPacketListener handler() {
return listener;
}

public interface Holder extends AbstractPacketHandler.Holder {

ClientPlayPacketHandler badpackets_getHandler();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lol.bai.badpackets.impl.handler;

public interface PacketHandlerHolder<T extends AbstractPacketHandler<?, ?>> {

T badpackets_handler();

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package lol.bai.badpackets.impl.handler;

import java.util.Set;
import java.util.function.Consumer;

import lol.bai.badpackets.api.PacketReceiver;
import lol.bai.badpackets.api.play.ServerPlayContext;
import lol.bai.badpackets.impl.platform.PlatformProxy;
Expand All @@ -18,6 +15,9 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;

import java.util.Set;
import java.util.function.Consumer;

public class ServerPlayPacketHandler extends AbstractPacketHandler<ServerPlayContext, RegistryFriendlyByteBuf> implements ServerPlayContext {

private final MinecraftServer server;
Expand All @@ -30,7 +30,7 @@ public ServerPlayPacketHandler(MinecraftServer server, ServerGamePacketListenerI
}

public static ServerPlayPacketHandler get(ServerPlayer player) {
return ((ServerPlayPacketHandler.Holder) player.connection).badpackets_getHandler();
return ((PacketHandlerHolder<ServerPlayPacketHandler>) player.connection).badpackets_handler();
}

@Override
Expand Down Expand Up @@ -65,10 +65,4 @@ public ServerGamePacketListenerImpl handler() {
return handler;
}

public interface Holder extends AbstractPacketHandler.Holder {

ServerPlayPacketHandler badpackets_getHandler();

}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package lol.bai.badpackets.impl.mixin;

import java.util.function.Function;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import lol.bai.badpackets.impl.handler.AbstractPacketHandler;
import lol.bai.badpackets.impl.handler.PacketHandlerHolder;
import lol.bai.badpackets.impl.registry.ChannelCodecFinder;
import lol.bai.badpackets.impl.registry.ChannelRegistry;
import net.minecraft.network.codec.StreamCodec;
Expand All @@ -17,6 +15,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.Function;

@Mixin(ClientboundCustomPayloadPacket.class)
public abstract class MixinClientboundCustomPayloadPacket {

Expand All @@ -25,7 +25,7 @@ public abstract class MixinClientboundCustomPayloadPacket {

@Inject(method = "handle(Lnet/minecraft/network/protocol/common/ClientCommonPacketListener;)V", at = @At("HEAD"), cancellable = true)
private void badpackets_handle(ClientCommonPacketListener listener, CallbackInfo ci) {
if (listener instanceof AbstractPacketHandler.Holder holder && holder.badpackets_receive(payload())) {
if (listener instanceof PacketHandlerHolder<?> holder && holder.badpackets_handler().receive(payload())) {
ci.cancel();
}
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/lol/bai/badpackets/impl/mixin/MixinConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lol.bai.badpackets.impl.mixin;

import io.netty.channel.ChannelHandlerContext;
import lol.bai.badpackets.impl.handler.PacketHandlerHolder;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketListener;
import net.minecraft.network.ProtocolInfo;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Connection.class)
public class MixinConnection {

@Shadow
@Nullable
private volatile PacketListener packetListener;

@Inject(method = "setupInboundProtocol", at = @At("HEAD"))
private <T extends PacketListener> void badpackets_cleanOldListener(ProtocolInfo<T> info, T listener, CallbackInfo ci) {
badpackets_cleanListener();
}

@Inject(method = "channelInactive", at = @At("HEAD"))
private void badpackets_cleanListener(ChannelHandlerContext ctx, CallbackInfo ci) {
badpackets_cleanListener();
}

@Inject(method = "handleDisconnection", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/PacketListener;onDisconnect(Lnet/minecraft/network/DisconnectionDetails;)V"))
private void badpackets_cleanListener(CallbackInfo ci) {
badpackets_cleanListener();
}

@Unique
private void badpackets_cleanListener() {
if (this.packetListener instanceof PacketHandlerHolder<?> holder) {
holder.badpackets_handler().remove();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lol.bai.badpackets.impl.mixin;

import net.minecraft.network.DisconnectionDetails;
import net.minecraft.network.protocol.common.ServerboundPongPacket;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -17,15 +16,6 @@ private void badpackets_onPong(ServerboundPongPacket packet, CallbackInfo ci) {
badpackets_onPong(packet.getId());
}

@Inject(method = "onDisconnect", at = @At("HEAD"))
private void badpackets_removePacketHandler(DisconnectionDetails details, CallbackInfo ci) {
badpackets_removePacketHandler();
}

@Unique
protected void badpackets_removePacketHandler() {
}

@Unique
protected void badpackets_onPong(int id) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package lol.bai.badpackets.impl.mixin;

import java.util.Queue;

import lol.bai.badpackets.impl.Constants;
import lol.bai.badpackets.impl.handler.AbstractPacketHandler;
import lol.bai.badpackets.impl.handler.PacketHandlerHolder;
import lol.bai.badpackets.impl.handler.ServerConfigPacketHandler;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ConfigurationTask;
Expand All @@ -23,8 +19,10 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Queue;

@Mixin(ServerConfigurationPacketListenerImpl.class)
public abstract class MixinServerConfigurationPacketListenerImpl extends MixinServerCommonPacketListenerImpl implements ServerConfigPacketHandler.TaskFinisher, AbstractPacketHandler.Holder {
public abstract class MixinServerConfigurationPacketListenerImpl extends MixinServerCommonPacketListenerImpl implements ServerConfigPacketHandler.TaskFinisher, PacketHandlerHolder<ServerConfigPacketHandler> {

@Shadow
@Final
Expand All @@ -51,11 +49,6 @@ private void badpackets_initPacketHandler(CallbackInfo ci) {
configurationTasks.addAll(ServerConfigPacketHandler.CUSTOM_TASKS.values());
}

@Inject(method = "handleConfigurationFinished", at = @At("RETURN"))
private void badpackets_removePacketHandler(ServerboundFinishConfigurationPacket packet, CallbackInfo ci) {
badpackets_packetHandler.remove();
}

@Inject(method = "startNextTask", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "FIELD", opcode = Opcodes.PUTFIELD, target = "Lnet/minecraft/server/network/ServerConfigurationPacketListenerImpl;currentTask:Lnet/minecraft/server/network/ConfigurationTask;"))
private void badpackets_attachCustomTaskContext(CallbackInfo ci, ConfigurationTask task) {
if (task instanceof ServerConfigPacketHandler.CustomTask custom) {
Expand All @@ -71,13 +64,8 @@ private void badpackets_detachCustomTaskContext(ConfigurationTask.Type type, Cal
}

@Override
protected void badpackets_removePacketHandler() {
badpackets_packetHandler.remove();
}

@Override
public boolean badpackets_receive(CustomPacketPayload payload) {
return badpackets_packetHandler.receive(payload);
public ServerConfigPacketHandler badpackets_handler() {
return badpackets_packetHandler;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package lol.bai.badpackets.impl.mixin;

import lol.bai.badpackets.impl.handler.PacketHandlerHolder;
import lol.bai.badpackets.impl.handler.ServerPlayPacketHandler;
import net.minecraft.network.Connection;
import net.minecraft.network.DisconnectionDetails;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.network.protocol.game.ServerboundConfigurationAcknowledgedPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
Expand All @@ -16,7 +14,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerGamePacketListenerImpl.class)
public class MixinServerGamePacketListenerImpl extends MixinServerCommonPacketListenerImpl implements ServerPlayPacketHandler.Holder {
public class MixinServerGamePacketListenerImpl extends MixinServerCommonPacketListenerImpl implements PacketHandlerHolder<ServerPlayPacketHandler> {

@Unique
private ServerPlayPacketHandler badpacket_packetHandler;
Expand All @@ -26,23 +24,8 @@ private void badpackets_createServerPacketHandler(MinecraftServer minecraftServe
badpacket_packetHandler = new ServerPlayPacketHandler(minecraftServer, (ServerGamePacketListenerImpl) (Object) this, connection);
}

@Inject(method = "onDisconnect", at = @At("HEAD"))
private void badpackets_removeServerPacketHandler(DisconnectionDetails details, CallbackInfo ci) {
badpacket_packetHandler.remove();
}

@Inject(method = "handleConfigurationAcknowledged", at = @At("RETURN"))
private void badpacekts_removePacketHandler(ServerboundConfigurationAcknowledgedPacket packet, CallbackInfo ci) {
badpacket_packetHandler.remove();
}

@Override
public boolean badpackets_receive(CustomPacketPayload payload) {
return badpacket_packetHandler.receive(payload);
}

@Override
public ServerPlayPacketHandler badpackets_getHandler() {
public ServerPlayPacketHandler badpackets_handler() {
return badpacket_packetHandler;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package lol.bai.badpackets.impl.mixin;

import java.util.function.Function;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import lol.bai.badpackets.impl.handler.AbstractPacketHandler;
import lol.bai.badpackets.impl.handler.PacketHandlerHolder;
import lol.bai.badpackets.impl.registry.ChannelCodecFinder;
import lol.bai.badpackets.impl.registry.ChannelRegistry;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand All @@ -18,6 +16,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.Function;

@Mixin(ServerboundCustomPayloadPacket.class)
public abstract class MixinServerboundCustomPayloadPacket {

Expand All @@ -26,7 +26,7 @@ public abstract class MixinServerboundCustomPayloadPacket {

@Inject(method = "handle(Lnet/minecraft/network/protocol/common/ServerCommonPacketListener;)V", at = @At("HEAD"), cancellable = true)
private void badpackets_handle(ServerCommonPacketListener listener, CallbackInfo ci) {
if (listener instanceof AbstractPacketHandler.Holder holder && holder.badpackets_receive(payload())) {
if (listener instanceof PacketHandlerHolder<?> holder && holder.badpackets_handler().receive(payload())) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lol.bai.badpackets.impl.mixin.client;

import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl;
import net.minecraft.network.DisconnectionDetails;
import net.minecraft.network.protocol.common.ClientboundPingPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -12,20 +11,11 @@
@Mixin(ClientCommonPacketListenerImpl.class)
public abstract class MixinClientCommonPacketListenerImpl {

@Inject(method = "onDisconnect", at = @At("HEAD"))
private void badpackets_removeClientPacketHandler(DisconnectionDetails details, CallbackInfo ci) {
badpackets_removeClientPacketHandler(details);
}

@Inject(method = "handlePing", at = @At("HEAD"), cancellable = true)
private void badpackets_handlePing(ClientboundPingPacket packet, CallbackInfo ci) {
if (badpackets_handlePing(packet.getId())) ci.cancel();
}

@Unique
protected void badpackets_removeClientPacketHandler(DisconnectionDetails details) {
}

@Unique
protected boolean badpackets_handlePing(int id) {
return false;
Expand Down
Loading

0 comments on commit f7f20a0

Please sign in to comment.