Skip to content

Commit 80aab17

Browse files
committed
feat: more packets
1 parent 9c5073b commit 80aab17

10 files changed

Lines changed: 242 additions & 26 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ loom {
3232
}
3333
}
3434

35+
accessWidenerPath = file("src/main/resources/packetcapture.accesswidener")
3536
}
3637

3738
dependencies {

src/client/java/com/hamusuke/packetcap/PacketCapture.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.common.collect.Sets;
88
import com.google.gson.Gson;
99
import com.google.gson.stream.JsonWriter;
10+
import com.hamusuke.packetcap.clazz.visitor.ClassVisitor;
1011
import com.hamusuke.packetcap.filter.FilterType;
1112
import com.hamusuke.packetcap.filter.PacketFilter;
1213
import com.hamusuke.packetcap.gui.hud.PacketCaptureHud;
@@ -18,7 +19,6 @@
1819
import fuzs.forgeconfigapiport.fabric.api.forge.v4.ForgeConfigRegistry;
1920
import io.netty.buffer.ByteBuf;
2021
import io.netty.buffer.Unpooled;
21-
import io.netty.util.ReferenceCountUtil;
2222
import it.unimi.dsi.fastutil.Pair;
2323
import net.fabricmc.api.ClientModInitializer;
2424
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
@@ -248,6 +248,12 @@ public <T extends PacketListener> void onEncodingPacketInMultiplayer(NetworkStat
248248
}
249249

250250
var copied = byteBuf.copy();
251+
if (this.isPacketTrash(packet)) {
252+
this.sentPacketNum.incrementAndGet();
253+
this.sentBytes.addAndGet(copied.readableBytes());
254+
copied.release();
255+
return;
256+
}
251257

252258
CompletableFuture.supplyAsync(() -> {
253259
int packetId = -1;
@@ -259,6 +265,8 @@ public <T extends PacketListener> void onEncodingPacketInMultiplayer(NetworkStat
259265
return new PacketDetails(packet, copied, packetId, end, this.createHighlights(null, packet, end));
260266
}, SENT_PACKET_DETAIL_RETRIEVER)
261267
.whenComplete((dedicatedServerPacketDetails, throwable) -> {
268+
copied.release();
269+
262270
if (throwable != null) {
263271
LOGGER.warn("Error occurred while creating packet details", throwable);
264272
}
@@ -273,7 +281,7 @@ public <T extends PacketListener> void onDecodingPacketInMultiplayer(NetworkStat
273281
}
274282

275283
var byteBuf = buf.copy();
276-
var delivered = byteBuf.copy();
284+
var delivered = buf.copy();
277285

278286
CompletableFuture.supplyAsync(() -> {
279287
int i = byteBuf.readableBytes();
@@ -286,6 +294,12 @@ public <T extends PacketListener> void onDecodingPacketInMultiplayer(NetworkStat
286294
return null;
287295
}
288296

297+
if (this.isPacketTrash(packet)) {
298+
this.receivedPacketNum.incrementAndGet();
299+
this.receivedBytes.addAndGet(delivered.readableBytes());
300+
return null;
301+
}
302+
289303
int packetId = -1;
290304
if (protocolInfo.codec() instanceof PacketCodecDispatcherAccessor codec) {
291305
packetId = codec.getTypeToIndex().getOrDefault(codec.getPacketIdGetter().apply(packet), -1);
@@ -296,7 +310,8 @@ public <T extends PacketListener> void onDecodingPacketInMultiplayer(NetworkStat
296310
return new PacketDetails(packet, delivered, packetId, end, this.createHighlights(byteBuf, packet, end));
297311
}, RECEIVED_PACKET_DETAIL_RETRIEVER)
298312
.whenComplete((details, throwable) -> {
299-
ReferenceCountUtil.release(byteBuf);
313+
byteBuf.release();
314+
delivered.release();
300315

301316
if (throwable != null) {
302317
LOGGER.warn("Error occurred while creating packet details", throwable);
@@ -308,6 +323,11 @@ public <T extends PacketListener> void onDecodingPacketInMultiplayer(NetworkStat
308323
});
309324
}
310325

326+
private boolean isPacketTrash(Packet<?> packet) {
327+
var clazz = ClassVisitor.getClassName(packet.getClass());
328+
return this.trash(this.classNameDeobfuscater.deobfuscate(clazz));
329+
}
330+
311331
public synchronized void loadFilters() {
312332
var file = this.filterConfig.toFile();
313333
if (!file.getParentFile().exists()) {

src/client/java/com/hamusuke/packetcap/PacketDetails.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public PacketDetails(Packet<?> packet, ByteBuf data, int packetId, int packetIdE
3434
this.hex = ImmutableList.copyOf(ByteBufUtil.prettyHexDump(data).lines().toList());
3535
this.size = data.readableBytes();
3636
this.fSize = ByteConversion.convertBytes(this.size);
37-
data.release();
3837

3938
this.packetId = packetId;
4039
this.packetIdEndIndex = packetIdEndIndex;

src/client/java/com/hamusuke/packetcap/clazz/visitor/ClassVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static List<Field> getFields(Class<?> clazz) {
3838
return fields;
3939
}
4040

41-
private static String getClassName(Class<?> clazz) {
41+
public static String getClassName(Class<?> clazz) {
4242
var stringBuilder = new StringBuilder();
4343
var enclosingClass = clazz.getEnclosingClass();
4444
if (enclosingClass != null) {

src/client/java/com/hamusuke/packetcap/gui/screen/PacketDetailsScreen.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.hamusuke.packetcap.gui.components.ClassFieldList.HasClassField;
1010
import com.hamusuke.packetcap.highlight.Highlight;
1111
import net.minecraft.client.gui.DrawContext;
12+
import net.minecraft.client.gui.Element;
1213
import net.minecraft.client.gui.screen.Screen;
1314
import net.minecraft.client.gui.tooltip.HoveredTooltipPositioner;
1415
import net.minecraft.client.gui.widget.ButtonWidget;
@@ -20,6 +21,7 @@
2021
import org.joml.Vector2i;
2122

2223
import java.util.Map;
24+
import java.util.Optional;
2325

2426
public class PacketDetailsScreen extends Screen {
2527
private static final Text ADD_TO_FILTER = Text.translatable(PacketCapture.MOD_ID + ".add_to_filter");
@@ -115,7 +117,15 @@ private void renderHexDump(DrawContext gui, int mouseX, int mouseY, float v) {
115117

116118
private void renderHighlightWhenHoveredField(DrawContext gui, int mouseX, int mouseY) {
117119
if (this.packetFields.isMouseOver(mouseX, mouseY)) {
118-
var e = this.packetFields.hoveredElement(mouseX, mouseY);
120+
Optional<Element> e = Optional.empty();
121+
122+
for (var c : this.packetFields.children()) {
123+
if (c.isMouseOver(mouseX, mouseY)) {
124+
e = Optional.of(c);
125+
break;
126+
}
127+
}
128+
119129
e.filter(guiEventListener -> guiEventListener instanceof HasClassField).ifPresent(guiEventListener -> {
120130
var hasClassField = (HasClassField) guiEventListener;
121131
var name = hasClassField.getField().getName();

src/client/java/com/hamusuke/packetcap/highlight/DataHighlightInstruction.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.Function;
2020
import java.util.function.Predicate;
2121

22+
import static com.hamusuke.packetcap.highlight.Highlight.NO_HIGHLIGHT;
2223
import static com.hamusuke.packetcap.highlight.Highlight.getWrittenByteLen;
2324
import static com.hamusuke.packetcap.highlight.instruction.BasicInstructions.VAR_INT;
2425

@@ -67,19 +68,18 @@ protected List<Highlight<?>> sortAllHighlights(List<Highlight<?>> unordered) {
6768
return unordered;
6869
}
6970

70-
List<Highlight<?>> highlights = new ArrayList<>(unordered);
71+
Map<Integer, Highlight<?>> highlights = Maps.newTreeMap();
7172
for (int i = 0; i < unordered.size(); i++) {
7273
if (!this.highlightOrders.containsKey(i)) {
74+
highlights.put(i, unordered.get(i));
7375
continue;
7476
}
7577

7678
int fieldIndex = this.highlightOrders.get(i);
77-
var highlight = unordered.get(fieldIndex);
78-
highlights.set(fieldIndex, unordered.get(i));
79-
highlights.set(i, highlight);
79+
highlights.put(fieldIndex, unordered.get(i));
8080
}
8181

82-
return highlights;
82+
return List.copyOf(highlights.values());
8383
}
8484

8585
public static class DataHighlightInstructionBuilder<B extends ByteBuf, T> {
@@ -101,12 +101,12 @@ public static <B extends ByteBuf, T> DataHighlightInstructionBuilder<B, T> build
101101
}
102102

103103
public DataHighlightInstructionBuilder<B, T> indexed(int fieldIndex) {
104-
this.highlightOrders.put(this.instructionIndex.get(), fieldIndex);
104+
this.highlightOrders.putIfAbsent(this.instructionIndex.get(), fieldIndex);
105105
return this;
106106
}
107107

108108
public DataHighlightInstructionBuilder<B, T> notBeWritten() {
109-
this.instructions.add((curWriterIndex, receivedByteBuf, buf, value) -> List.of());
109+
this.instructions.add((curWriterIndex, receivedByteBuf, buf, value) -> Collections.singletonList(NO_HIGHLIGHT));
110110
return this;
111111
}
112112

0 commit comments

Comments
 (0)