Skip to content

Commit

Permalink
Backport to 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD committed Jul 27, 2024
1 parent 3651bd7 commit e3781ef
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 232 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@

@Mixin(ClientboundLevelChunkPacketData.class)
public abstract class ClientboundLevelChunkPacketDataMixin {
@Shadow
@Final
private byte[] buffer;

@Inject(
method = "<init>(Lnet/minecraft/world/level/chunk/LevelChunk;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/protocol/game/ClientboundLevelChunkPacketData;extractChunkData(Lnet/minecraft/network/FriendlyByteBuf;Lnet/minecraft/world/level/chunk/LevelChunk;)V"
)
)
private void setChunkPacketInfoBuffer(LevelChunk levelChunk, CallbackInfo ci) {
// custom arguments
// 1.20.1 - Moved setBuffer to extractChunkData, because mixins can't inject early enough into constructors
@Inject(method = "extractChunkData", at = @At("HEAD"))
private static void initializeChunkSectionIndex(FriendlyByteBuf friendlyByteBuf, LevelChunk levelChunk, CallbackInfo ci, @Share("chunkSectionIndex") LocalIntRef chunkSectionIndexRef) {
ChunkPacketInfo<BlockState> chunkPacketInfo = Arguments.PACKET_INFO.get();

if (chunkPacketInfo != null) {
chunkPacketInfo.setBuffer(this.buffer);
chunkPacketInfo.setBuffer(friendlyByteBuf.array());
}
}

@Inject(method = "extractChunkData", at = @At("HEAD"))
private static void initializeChunkSectionIndex(FriendlyByteBuf friendlyByteBuf, LevelChunk levelChunk, CallbackInfo ci, @Share("chunkSectionIndex") LocalIntRef chunkSectionIndexRef) {
chunkSectionIndexRef.set(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public abstract class ClientboundLevelChunkWithLightPacketMixin implements IChun
@Unique
boolean antixray$ready = false;

@Unique
IClientboundChunkBatchStartPacket antixray$batchStartPacket;

@WrapOperation(
method = "<init>(Lnet/minecraft/world/level/chunk/LevelChunk;Lnet/minecraft/world/level/lighting/LevelLightEngine;Ljava/util/BitSet;Ljava/util/BitSet;)V",
at = @At(
Expand All @@ -44,9 +41,6 @@ public ClientboundLevelChunkPacketData setChunkPacketInfoArgument(
@Share("controller") LocalRef<ChunkPacketBlockController> controllerLocalRef,
@Share("chunkPacketInfo") LocalRef<ChunkPacketInfo<BlockState>> chunkPacketInfoLocalRef
) {
// custom argument
this.antixray$batchStartPacket = Arguments.BATCH_START_PACKET.get();

final ChunkPacketBlockController controller = Util.getBlockController(chunk.getLevel());
final ChunkPacketInfo<BlockState> packetInfo = controller.getChunkPacketInfo((ClientboundLevelChunkWithLightPacket) (Object) this, chunk);

Expand Down Expand Up @@ -82,10 +76,6 @@ public void modifyBlocks(
@Override
public void antixray$setReady(boolean antixray$ready) {
this.antixray$ready = antixray$ready;
if (antixray$batchStartPacket != null) {
// Chunk packets may not have a batch start packet, if they are manually sent by other mods
antixray$batchStartPacket.antixray$notifyChunkReady();
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,83 @@
package me.drex.antixray.common.mixin;

import com.google.common.collect.Queues;
import io.netty.channel.Channel;
import me.drex.antixray.common.util.Util;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.protocol.Packet;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Queue;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;


@Mixin(Connection.class)
public abstract class ConnectionMixin {

@Unique
private final Queue<BooleanSupplier> antiXray$isActionReady = Queues.newConcurrentLinkedQueue();
@Shadow
private Channel channel;

@Shadow
@Final
private Queue<Consumer<Connection>> pendingActions;
private Queue<Object> queue;

@Shadow
protected abstract void sendPacket(Packet<?> $$0, PacketSendListener $$1, boolean $$2);
@Shadow protected abstract void sendPacket(Packet<?> $$0, PacketSendListener $$1);

/**
* @author Drex
* @reason Wait for chunk packets to be ready (fully obfuscated)
*/
@Overwrite
private void flushQueue() {
assert pendingActions.size() == antiXray$isActionReady.size();
private boolean flushQueue() {
if (this.channel != null && this.channel.isOpen()) {
synchronized (this.pendingActions) {
while (!this.antiXray$isActionReady.isEmpty() && (this.antiXray$isActionReady.peek().getAsBoolean())) {
pendingActions.poll().accept((Connection) (Object) this);
antiXray$isActionReady.poll();
synchronized (this.queue) {
while (!this.queue.isEmpty()) {
if (this.queue.peek() instanceof ConnectionPacketHolderAccessor packetAccessor) { // poll -> peek
if (!Util.isReady(packetAccessor.getPacket())) {
return false; // Return false if the peeked packet is a chunk packet which is not ready
} else {
this.queue.poll(); // poll here
this.sendPacket(packetAccessor.getPacket(), packetAccessor.getListener()); // dispatch the packet
}
}

}
}
}
return true;
}

@Redirect(
method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;Z)V",
method = "tick",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/Connection;sendPacket(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;Z)V"
target = "Lnet/minecraft/network/Connection;flushQueue()V"
)
)
public void redirectSendPacket(Connection instance, Packet<?> packet, PacketSendListener listener, boolean flush) {
if (this.antiXray$isActionReady.isEmpty() && Util.isReady(packet)) {
this.sendPacket(packet, listener, flush);
} else {
pendingActions.add(connection -> this.sendPacket(packet, listener, flush));
antiXray$isActionReady.add(() -> Util.isReady(packet));
}
public void replaceFlushQueue(Connection connection) {
// Calling our own implementation
this.flushQueue();
}

@Inject(
method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;Z)V",
@Redirect(
method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V",
at = @At(
value = "INVOKE",
target = "Ljava/util/Queue;add(Ljava/lang/Object;)Z"
target = "Lnet/minecraft/network/Connection;flushQueue()V"
)
)
public void addToActionReadyQueue(Packet<?> packet, PacketSendListener listener, boolean flush, CallbackInfo ci) {
antiXray$isActionReady.add(() -> Util.isReady(packet));
public void noop(Connection connection) {
// no-op
// The call to our own flushQueue method has been moved inside the previous if statement (see below)
}

@Inject(
method = {"flushChannel", "runOnceConnected"},
@Redirect(
method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V",
at = @At(
value = "INVOKE",
target = "Ljava/util/Queue;add(Ljava/lang/Object;)Z"
target = "Lnet/minecraft/network/Connection;isConnected()Z"
)
)
public void addToActionReadyQueue(CallbackInfo ci) {
antiXray$isActionReady.add(() -> true);
public boolean redirectIfStatement(Connection connection, Packet<?> packet, PacketSendListener listener) {
return connection.isConnected() && this.flushQueue() && Util.isReady(packet);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.drex.antixray.common.mixin;

import net.minecraft.network.PacketSendListener;
import net.minecraft.network.protocol.Packet;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(targets = "net.minecraft.network.Connection$PacketHolder")
public interface ConnectionPacketHolderAccessor {
@Accessor("packet")
Packet<?> getPacket();

@Accessor("listener")
PacketSendListener getListener();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import me.drex.antixray.common.util.Arguments;
import me.drex.antixray.common.util.ChunkPacketInfo;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.VarInt;
import net.minecraft.util.BitStorage;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.Palette;
Expand Down Expand Up @@ -41,7 +40,7 @@ public long[] initializeChunkPacketInfo(BitStorage storage, Operation<long[]> or
chunkPacketInfo.setBits(chunkSectionIndex, storage.getBits());
//noinspection unchecked
chunkPacketInfo.setPalette(chunkSectionIndex, (Palette<BlockState>) this.palette);
chunkPacketInfo.setIndex(chunkSectionIndex, buf.writerIndex() + VarInt.getByteSize(storage.getRaw().length));
chunkPacketInfo.setIndex(chunkSectionIndex, buf.writerIndex() + FriendlyByteBuf.getVarIntSize(storage.getRaw().length));
}
return original.call(storage);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.drex.antixray.common.util;

import me.drex.antixray.common.interfaces.IClientboundChunkBatchStartPacket;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkAccess;

Expand All @@ -13,5 +12,4 @@ public class Arguments {
public static final ThreadLocal<ChunkAccess> CHUNK_ACCESS = new ThreadLocal<>();
public static final ThreadLocal<Integer> CHUNK_SECTION_INDEX = new ThreadLocal<>();
public static final ThreadLocal<Object[]> PRESET_VALUES = new ThreadLocal<>();
public static final ThreadLocal<IClientboundChunkBatchStartPacket> BATCH_START_PACKET = new ThreadLocal<>();
}
3 changes: 1 addition & 2 deletions common/src/main/resources/antixray.common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
"ChunkAccessMixin",
"ChunkSerializerMixin",
"ClientboundBundlePacketMixin",
"ClientboundChunkBatchStartPacketMixin",
"ClientboundLevelChunkPacketDataMixin",
"ClientboundLevelChunkWithLightPacketMixin",
"ConnectionMixin",
"ConnectionPacketHolderAccessor",
"LevelChunkSectionMixin",
"LevelMixin",
"PalettedContainer$DataMixin",
"PalettedContainerMixin",
"PlayerChunkSenderMixin",
"ServerLevelMixin",
"ServerPlayerGameModeMixin"
],
Expand Down
4 changes: 0 additions & 4 deletions common/src/main/resources/architectury.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
],
"net/minecraft/class_8042": [
"me/drex/antixray/common/interfaces/IPacket"
],
"net/minecraft/class_8739": [
"me/drex/antixray/common/interfaces/IClientboundChunkBatchStartPacket",
"me/drex/antixray/common/interfaces/IPacket"
]
}
}

This file was deleted.

Loading

0 comments on commit e3781ef

Please sign in to comment.