Skip to content

Commit

Permalink
epic packet dump rewrite (also makes bundle packets work)
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Mar 28, 2023
1 parent e0ce77e commit b351c8c
Show file tree
Hide file tree
Showing 28 changed files with 409 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public GadgetConfigScreen(@Nullable Screen parent) {
FieldPathStep::remapFieldId,
id -> MappingsManager.displayMappings().unmapFieldId(id)
);
return new OptionComponentFactory.Result(layout, layout);
return new OptionComponentFactory.Result<>(layout, layout);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.wispforest.owo.config.Option;
import io.wispforest.owo.config.annotation.Expanded;
import io.wispforest.owo.config.ui.component.ConfigTextBox;
import io.wispforest.owo.config.ui.component.OptionComponent;
import io.wispforest.owo.config.ui.component.OptionValueProvider;
import io.wispforest.owo.config.ui.component.SearchAnchorComponent;
import io.wispforest.owo.ops.TextOps;
import io.wispforest.owo.ui.component.ButtonComponent;
Expand All @@ -29,7 +29,7 @@
import net.minecraft.util.Formatting;

@SuppressWarnings("UnstableApiUsage")
public class RemappingListOptionContainer extends CollapsibleContainer implements OptionComponent {
public class RemappingListOptionContainer extends CollapsibleContainer implements Component, OptionValueProvider {

protected final Option<List<String>> backingOption;
protected final List<String> backingList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.component.Components;
import io.wispforest.owo.ui.container.Containers;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.ScrollContainer;
import io.wispforest.owo.ui.container.VerticalFlowLayout;
import io.wispforest.owo.ui.core.*;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.MutableText;
Expand All @@ -16,7 +16,7 @@

import java.util.*;

public class DumpStatsScreen extends BaseOwoScreen<VerticalFlowLayout> {
public class DumpStatsScreen extends BaseOwoScreen<FlowLayout> {
private final Map<String, PacketTypeData> packetTypes = new HashMap<>();
private final Screen parent;
private final List<ProcessedDumpedPacket> packets;
Expand All @@ -34,19 +34,19 @@ public DumpStatsScreen(Screen parent, List<ProcessedDumpedPacket> packets) {
}

@Override
protected @NotNull OwoUIAdapter<VerticalFlowLayout> createAdapter() {
protected @NotNull OwoUIAdapter<FlowLayout> createAdapter() {
return OwoUIAdapter.create(this, Containers::verticalFlow);
}

@Override
protected void build(VerticalFlowLayout rootComponent) {
protected void build(FlowLayout rootComponent) {
rootComponent
.horizontalAlignment(HorizontalAlignment.CENTER)
.verticalAlignment(VerticalAlignment.CENTER)
.surface(Surface.VANILLA_TRANSLUCENT);

VerticalFlowLayout main = new BasedVerticalFlowLayout(Sizing.fill(100), Sizing.content());
ScrollContainer<VerticalFlowLayout> scroll = Containers.verticalScroll(Sizing.fill(95), Sizing.fill(90), main)
FlowLayout main = new BasedVerticalFlowLayout(Sizing.fill(100), Sizing.content());
ScrollContainer<FlowLayout> scroll = Containers.verticalScroll(Sizing.fill(95), Sizing.fill(90), main)
.scrollbar(ScrollContainer.Scrollbar.flat(Color.ofArgb(0xA0FFFFFF)));

packetTypes
Expand Down
40 changes: 0 additions & 40 deletions src/main/java/io/wispforest/gadget/client/dump/ErrorPacket.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class OpenDumpScreen extends BaseOwoScreen<VerticalFlowLayout> {
public class OpenDumpScreen extends BaseOwoScreen<FlowLayout> {
private final Screen parent;
private ProgressToast toast;
private final List<ProcessedDumpedPacket> packets;
private VerticalFlowLayout main;
private FlowLayout main;
private FlowLayout infoButton;
private BasedSliderComponent timeSlider;
private final long startTime;
Expand Down Expand Up @@ -82,19 +82,19 @@ public static void openWithProgress(Screen parent, Path path) {
}

@Override
protected @NotNull OwoUIAdapter<VerticalFlowLayout> createAdapter() {
protected @NotNull OwoUIAdapter<FlowLayout> createAdapter() {
return OwoUIAdapter.create(this, Containers::verticalFlow);
}

@Override
protected void build(VerticalFlowLayout rootComponent) {
protected void build(FlowLayout rootComponent) {
rootComponent
.horizontalAlignment(HorizontalAlignment.CENTER)
.verticalAlignment(VerticalAlignment.CENTER)
.surface(Surface.VANILLA_TRANSLUCENT);

this.main = new BasedVerticalFlowLayout(Sizing.fill(100), Sizing.content());
ScrollContainer<VerticalFlowLayout> scroll = Containers.verticalScroll(Sizing.fill(95), Sizing.fill(90), this.main)
ScrollContainer<FlowLayout> scroll = Containers.verticalScroll(Sizing.fill(95), Sizing.fill(90), this.main)
.scrollbar(ScrollContainer.Scrollbar.flat(Color.ofArgb(0xA0FFFFFF)));

var searchBox = Components.textBox(Sizing.fill(95));
Expand Down Expand Up @@ -140,9 +140,9 @@ protected void build(VerticalFlowLayout rootComponent) {

rebuild("", startTime);

VerticalFlowLayout sidebar = Containers.verticalFlow(Sizing.content(), Sizing.content());
FlowLayout sidebar = Containers.verticalFlow(Sizing.content(), Sizing.content());

infoButton = new VerticalFlowLayout(Sizing.fixed(16), Sizing.fixed(16)) {
infoButton = new FlowLayout(Sizing.fixed(16), Sizing.fixed(16), FlowLayout.Algorithm.VERTICAL) {
private int totalComponents = -1;
private int frameNumber = 11;

Expand Down Expand Up @@ -191,7 +191,7 @@ public void drawTooltip(MatrixStack matrices, int mouseX, int mouseY, float part
infoButton.mouseLeave().subscribe(
() -> infoButton.surface(Surface.BLANK));

VerticalFlowLayout statsButton = Containers.verticalFlow(Sizing.fixed(16), Sizing.fixed(16));
FlowLayout statsButton = Containers.verticalFlow(Sizing.fixed(16), Sizing.fixed(16));

statsButton
.child(Components.label(Text.translatable("text.gadget.stats"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.wispforest.gadget.client.dump;

import io.wispforest.gadget.dump.PacketDumping;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import io.wispforest.gadget.util.NetworkUtil;
Expand Down Expand Up @@ -54,9 +55,8 @@ public static List<DumpedPacket> readV0(InputStream is) throws IOException {
case 0b0110 -> NetworkState.LOGIN;
default -> throw new IllegalStateException();
};
int packetId = buf.readVarInt();
int size = buf.readableBytes();
Packet<?> packet = readPacket(packetId, state, outbound, buf);
Packet<?> packet = PacketDumping.readPacket(buf, state, outbound ? NetworkSide.SERVERBOUND : NetworkSide.CLIENTBOUND);
Identifier channelId = NetworkUtil.getChannelOrNull(packet);

if (packet instanceof LoginQueryRequestS2CPacket req) {
Expand Down Expand Up @@ -118,9 +118,8 @@ private static List<DumpedPacket> readV1(DataInputStream is) throws IOException
default -> throw new IllegalStateException();
};
long sentAt = buf.readLong();
int packetId = buf.readVarInt();
int size = buf.readableBytes();
Packet<?> packet = readPacket(packetId, state, outbound, buf);
Packet<?> packet = PacketDumping.readPacket(buf, state, outbound ? NetworkSide.SERVERBOUND : NetworkSide.CLIENTBOUND);
Identifier channelId = NetworkUtil.getChannelOrNull(packet);

if (packet instanceof LoginQueryRequestS2CPacket req) {
Expand All @@ -137,14 +136,4 @@ private static List<DumpedPacket> readV1(DataInputStream is) throws IOException
}
}

private static Packet<?> readPacket(int packetId, NetworkState state, boolean outbound, PacketByteBuf buf) {
int startOfData = buf.readerIndex();

try {
return state.getPacketHandler(outbound ? NetworkSide.SERVERBOUND : NetworkSide.CLIENTBOUND, packetId, buf);
} catch (Exception e) {
buf.readerIndex(startOfData);
return new ErrorPacket(buf, packetId, e);
}
}
}
26 changes: 7 additions & 19 deletions src/main/java/io/wispforest/gadget/client/dump/PacketDumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.wispforest.gadget.Gadget;
import io.wispforest.gadget.client.gui.NotificationToast;
import io.wispforest.gadget.dump.PacketDumping;
import io.wispforest.gadget.util.NetworkUtil;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -100,27 +102,13 @@ public static void dump(boolean outbound, Packet<?> packet) {
case LOGIN -> flags |= 0b0110;
}

buf.writeInt(0);
buf.writeShort(flags);
try (var lengthWriter = NetworkUtil.writeByteLength(buf)) {
buf.writeShort(flags);

buf.writeLong(System.currentTimeMillis());
buf.writeLong(System.currentTimeMillis());

int packetId = state.getPacketId(outbound ? NetworkSide.SERVERBOUND : NetworkSide.CLIENTBOUND, packet);

// TODO: fix bundle packets.
if (packetId == -1)
throw new UnsupportedOperationException("Invalid packet: " + packet);

buf.writeVarInt(packetId);

packet.write(buf);

int totalLength = buf.readableBytes();
int prevWriterIdx = buf.writerIndex();

buf.writerIndex(0);
buf.writeInt(totalLength - 4);
buf.writerIndex(prevWriterIdx);
PacketDumping.writePacket(buf, packet, state, outbound ? NetworkSide.SERVERBOUND : NetworkSide.CLIENTBOUND);
}

ByteBuffer nioBuf = buf.nioBuffer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.wispforest.gadget.client.gui.BasedLabelComponent;
import io.wispforest.gadget.client.gui.GuiUtil;
import io.wispforest.gadget.client.gui.LayoutCacheWrapper;
import io.wispforest.gadget.dump.GadgetReadErrorPacket;
import io.wispforest.gadget.dump.GadgetWriteErrorPacket;
import io.wispforest.gadget.util.ReflectionUtil;
import io.wispforest.owo.ui.container.*;
import io.wispforest.owo.ui.core.*;
Expand All @@ -29,7 +31,7 @@ public DumpedPacket packet() {

public Component component() {
if (component == null || component.get() == null) {
VerticalFlowLayout view = Containers.verticalFlow(Sizing.content(), Sizing.content());
FlowLayout view = Containers.verticalFlow(Sizing.content(), Sizing.content());

view
.padding(Insets.of(5))
Expand All @@ -40,8 +42,10 @@ public Component component() {

MutableText typeText = Text.literal("");

if (packet.packet() instanceof ErrorPacket errorPacket) {
typeText.append(Text.translatable("text.gadget.packet_read_error", errorPacket.getPacketId()));
if (packet.packet() instanceof GadgetReadErrorPacket errorPacket) {
typeText.append(Text.translatable("text.gadget.packet_read_error", errorPacket.packetId()));
} else if (packet.packet() instanceof GadgetWriteErrorPacket errorPacket) {
typeText.append(Text.translatable("text.gadget.packet_write_error", errorPacket.packetId()));
} else {
typeText.append(ReflectionUtil.nameWithoutPackage(packet.packet().getClass()));

Expand All @@ -55,7 +59,10 @@ public Component component() {

DrawPacketHandler.EVENT.invoker().onDrawPacket(packet, view);

if (!packet.drawErrors().isEmpty() || !packet.searchTextErrors().isEmpty() || packet.packet() instanceof ErrorPacket) {
if (!packet.drawErrors().isEmpty()
|| !packet.searchTextErrors().isEmpty()
|| packet.packet() instanceof GadgetReadErrorPacket
|| packet.packet() instanceof GadgetWriteErrorPacket) {
CollapsibleContainer errors = Containers.collapsible(
Sizing.content(),
Sizing.content(),
Expand All @@ -70,8 +77,13 @@ public Component component() {
((FlowLayout) errors.children().get(0))
.padding(Insets.of(2, 2, 2, 0));

if (packet.packet() instanceof ErrorPacket error) {
errors.child(GuiUtil.showException(error.getException())
if (packet.packet() instanceof GadgetReadErrorPacket error) {
errors.child(GuiUtil.showException(error.exception())
.margins(Insets.bottom(2)));
}

if (packet.packet() instanceof GadgetWriteErrorPacket error) {
errors.child(GuiUtil.showExceptionText(error.exceptionText())
.margins(Insets.bottom(2)));
}

Expand All @@ -88,7 +100,7 @@ public Component component() {
view.child(1, errors);
}

HorizontalFlowLayout fullRow = Containers.horizontalFlow(Sizing.fill(100), Sizing.content());
FlowLayout fullRow = Containers.horizontalFlow(Sizing.fill(100), Sizing.content());

fullRow
.child(view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.wispforest.gadget.client.dump.DumpedPacket;
import io.wispforest.gadget.util.NetworkUtil;
import io.wispforest.owo.ui.container.VerticalFlowLayout;
import io.wispforest.owo.ui.container.FlowLayout;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

Expand All @@ -11,7 +11,7 @@ public interface DrawPacketHandler {
packet.drawErrors().clear();

for (var callback : callbacks) {
try (var reset = NetworkUtil.resetIndexes(packet.packet())) {
try (var ignored = NetworkUtil.resetIndexes(packet.packet())) {
boolean result = callback.onDrawPacket(packet, view);
if (result)
return true;
Expand All @@ -23,5 +23,5 @@ public interface DrawPacketHandler {
return false;
});

boolean onDrawPacket(DumpedPacket packet, VerticalFlowLayout view);
boolean onDrawPacket(DumpedPacket packet, FlowLayout view);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.wispforest.owo.particles.systems.ParticleSystem;
import io.wispforest.owo.particles.systems.ParticleSystemController;
import io.wispforest.owo.ui.component.Components;
import io.wispforest.owo.ui.container.VerticalFlowLayout;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.util.VectorSerializer;
import io.wispforest.gadget.util.NetworkUtil;
Expand Down Expand Up @@ -156,7 +156,7 @@ public static void init() {
// TODO: OwO handshake and config sync.
}

private static void drawHandshakeMap(Map<Identifier, Integer> data, Text prefix, VerticalFlowLayout view) {
private static void drawHandshakeMap(Map<Identifier, Integer> data, Text prefix, FlowLayout view) {
for (var entry : data.entrySet()) {
view.child(Components.label(
Text.literal("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.wispforest.gadget.client.dump.handler;

import io.wispforest.gadget.Gadget;
import io.wispforest.gadget.client.dump.ErrorPacket;
import io.wispforest.gadget.dump.GadgetReadErrorPacket;
import io.wispforest.gadget.client.gui.GuiUtil;
import io.wispforest.gadget.dump.GadgetWriteErrorPacket;
import io.wispforest.gadget.util.NetworkUtil;
import io.wispforest.gadget.client.field.FieldDataIsland;
import io.wispforest.gadget.util.ReflectionUtil;
Expand Down Expand Up @@ -33,11 +34,13 @@ public static void init() {

DrawPacketHandler.EVENT.addPhaseOrdering(Event.DEFAULT_PHASE, LAST_PHASE);
DrawPacketHandler.EVENT.register(LAST_PHASE, (packet, view) -> {
if (packet.packet() instanceof ErrorPacket errorPacket) {
view.child(GuiUtil.hexDump(errorPacket.getData(), true));
if (packet.packet() instanceof GadgetReadErrorPacket errorPacket) {
view.child(GuiUtil.hexDump(errorPacket.data(), true));
return true;
}

if (packet.packet() instanceof GadgetWriteErrorPacket) return true;

if (packet.channelId() != null) {
var buf = NetworkUtil.unwrapCustom(packet.packet());
byte[] bytes = new byte[buf.readableBytes()];
Expand Down
Loading

0 comments on commit b351c8c

Please sign in to comment.