Hopefully more stable version of the original packet fix

This commit is contained in:
embeddedt 2023-05-14 19:59:39 -04:00
parent 0f2764b79d
commit b21ee9a7e7
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 21 additions and 18 deletions

View File

@ -11,23 +11,13 @@ import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ClientPacketListener.class) @Mixin(ClientPacketListener.class)
@ClientOnlyMixin @ClientOnlyMixin
public class ClientPlayNetHandlerMixin implements IClientNetHandler { public class ClientPlayNetHandlerMixin {
private FriendlyByteBuf savedCopy = null;
/** /**
* @author embeddedt * @author embeddedt
* @reason Release the packet buffer at the end. Needed in f * @reason allow the other function to track use of the buffer
*/ */
@Redirect(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;getData()Lnet/minecraft/network/FriendlyByteBuf;")) @Redirect(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;getData()Lnet/minecraft/network/FriendlyByteBuf;"))
private FriendlyByteBuf saveCopyForRelease(ClientboundCustomPayloadPacket instance) { private FriendlyByteBuf saveCopyForRelease(ClientboundCustomPayloadPacket instance) {
FriendlyByteBuf copy = instance.getData(); return ((IClientNetHandler)instance).getCopiedCustomBuffer();
savedCopy = copy;
return copy;
}
@Override
public FriendlyByteBuf getCopiedCustomBuffer() {
FriendlyByteBuf copy = savedCopy;
savedCopy = null;
return copy;
} }
} }

View File

@ -15,9 +15,13 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientboundCustomPayloadPacket.class) @Mixin(ClientboundCustomPayloadPacket.class)
@ClientOnlyMixin @ClientOnlyMixin
public class SCustomPayloadPlayPacketMixin { public abstract class SCustomPayloadPlayPacketMixin implements IClientNetHandler {
@Shadow private FriendlyByteBuf data; @Shadow private FriendlyByteBuf data;
@Shadow public abstract FriendlyByteBuf getData();
private FriendlyByteBuf usedByteBuf = null;
private boolean needsRelease; private boolean needsRelease;
@Inject(method = "<init>(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/network/FriendlyByteBuf;)V", at = @At("RETURN")) @Inject(method = "<init>(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/network/FriendlyByteBuf;)V", at = @At("RETURN"))
@ -30,16 +34,25 @@ public class SCustomPayloadPlayPacketMixin {
this.needsRelease = true; this.needsRelease = true;
} }
@Override
public FriendlyByteBuf getCopiedCustomBuffer() {
FriendlyByteBuf buf = this.getData();
usedByteBuf = buf;
return buf;
}
@Redirect(method = "handle(Lnet/minecraft/network/protocol/game/ClientGamePacketListener;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientGamePacketListener;handleCustomPayload(Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;)V")) @Redirect(method = "handle(Lnet/minecraft/network/protocol/game/ClientGamePacketListener;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientGamePacketListener;handleCustomPayload(Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;)V"))
private void handleAndFree(ClientGamePacketListener instance, ClientboundCustomPayloadPacket sCustomPayloadPlayPacket) { private void handleAndFree(ClientGamePacketListener instance, ClientboundCustomPayloadPacket sCustomPayloadPlayPacket) {
usedByteBuf = null;
try { try {
instance.handleCustomPayload(sCustomPayloadPlayPacket); instance.handleCustomPayload(sCustomPayloadPlayPacket);
} finally { } finally {
FriendlyByteBuf copied = ((IClientNetHandler)instance).getCopiedCustomBuffer(); FriendlyByteBuf buf = usedByteBuf;
if(copied != null) if(buf != null && buf.refCnt() > 0) {
copied.release(); buf.release();
}
} }
if(this.needsRelease) if(this.needsRelease && this.data.refCnt() > 0)
this.data.release(); this.data.release();
} }
} }