1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-10 18:33:00 +00:00

Add a MessageType for network messages

Everything old is new again!

CC's network message implementation has gone through several iterations:

 - Originally network messages were implemented with a single class,
   which held an packet id/type and and opaque blobs of data (as
   string/int/byte/NBT arrays), and a big switch statement to decode and
   process this data.

 - In 42d3901ee3, we split the messages
   into different classes all inheriting from NetworkMessage - this bit
   we've stuck with ever since.

   Each packet had a `getId(): int` method, which returned the
   discriminator for this packet.

 - However, getId() was only used when registering the packet, not when
   sending, and so in ce0685c31f we
   removed it, just passing in a constant integer at registration
   instead.

 - In 53abe5e56e, we made some relatively
   minor changes to make the code more multi-loader/split-source
   friendly. However, this meant when we finally came to add Fabric
   support (8152f19b6e), we had to
   re-implement a lot of Forge's network code.

In 1.20.4, Forge moves to a system much closer to Fabric's (and indeed,
Minecraft's own CustomPacketPayload), and so it makes sense to adapt to
that now. As such, we:

 - Add a new MessageType interface. This is implemented by the
   loader-specific modules, and holds whatever information is needed to
   register the packet (e.g. discriminator, reader function).

 - Each NetworkMessage now has a type(): MessageType<?> function. This
   is used by the Fabric networking code (and for NeoForge's on 1.20.4)
   instead of a class lookup.

 - NetworkMessages now creates/stores these MessageType<T>s (much like
   we'd do for registries), and provides getters for the
   clientbound/serverbound messages. Mod initialisers then call these
   getters to register packets.

 - For Forge, this is relatively unchanged. For Fabric, we now
   `FabricPacket`s.
This commit is contained in:
Jonathan Coates
2024-01-03 09:15:38 +00:00
parent ed3a17f9b9
commit 234f69e8e5
33 changed files with 370 additions and 185 deletions

View File

@@ -13,6 +13,7 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.impl.AbstractComputerCraftAPI;
import dan200.computercraft.impl.ComputerCraftAPIService;
import dan200.computercraft.shared.config.ConfigFile;
import dan200.computercraft.shared.network.MessageType;
import dan200.computercraft.shared.network.NetworkMessage;
import dan200.computercraft.shared.network.client.ClientNetworkContext;
import dan200.computercraft.shared.network.container.ContainerData;
@@ -168,6 +169,13 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat
throw new UnsupportedOperationException("Cannot open menu inside tests");
}
@Override
public <T extends NetworkMessage<?>> MessageType<T> createMessageType(int id, ResourceLocation channel, Class<T> klass, FriendlyByteBuf.Reader<T> reader) {
record TypeImpl<T extends NetworkMessage<?>>(Function<FriendlyByteBuf, T> reader) implements MessageType<T> {
}
return new TypeImpl<>(reader);
}
@Override
public ComponentAccess<IPeripheral> createPeripheralAccess(Consumer<Direction> invalidate) {
throw new UnsupportedOperationException("Cannot interact with the world inside tests");

View File

@@ -29,7 +29,7 @@ class PlayRecordClientMessageTest {
@Property
public void testRoundTrip(@ForAll("message") PlayRecordClientMessage message) {
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
message.toBytes(buffer);
message.write(buffer);
var converted = new PlayRecordClientMessage(buffer);
assertEquals(buffer.readableBytes(), 0, "Whole packet was read");

View File

@@ -63,7 +63,7 @@ public class UploadFileMessageTest {
private static List<UploadFileMessage> roundtripPackets(List<UploadFileMessage> packets) {
return packets.stream().map(packet -> {
var buffer = new FriendlyByteBuf(Unpooled.directBuffer());
packet.toBytes(buffer);
packet.write(buffer);
// We include things like file size in the packet, but not in the count, so grant a slightly larger threshold.
assertThat("Packet is too large", buffer.writerIndex(), lessThanOrEqualTo(MAX_PACKET_SIZE + 128));
if ((packet.flag & FLAG_LAST) == 0) {