Skip to content

Commit

Permalink
handshake update (i think)
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Sep 29, 2023
1 parent a9f931a commit 5b120e1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.wispforest.owo.mixin;

import net.minecraft.client.network.ClientCommonNetworkHandler;
import net.minecraft.network.ClientConnection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ClientCommonNetworkHandler.class)
public interface ClientCommonNetworkHandlerAccessor {
@Accessor
ClientConnection getConnection();
}

This file was deleted.

92 changes: 47 additions & 45 deletions src/main/java/io/wispforest/owo/network/OwoHandshake.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package io.wispforest.owo.network;

import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.wispforest.owo.Owo;
import io.wispforest.owo.mixin.ClientLoginNetworkHandlerAccessor;
import io.wispforest.owo.mixin.ServerLoginNetworkHandlerAccessor;
import io.wispforest.owo.mixin.ClientCommonNetworkHandlerAccessor;
import io.wispforest.owo.mixin.ServerCommonNetworkHandlerAccessor;
import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.ops.TextOps;
import io.wispforest.owo.particles.systems.ParticleSystemController;
import io.wispforest.owo.util.OwoFreezer;
import io.wispforest.owo.util.ServicesFrozenException;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.client.networking.v1.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientLoginNetworkHandler;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand All @@ -36,9 +29,9 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.ToIntFunction;

@SuppressWarnings("UnstableApiUsage")
@ApiStatus.Internal
public final class OwoHandshake {

Expand All @@ -60,7 +53,10 @@ private OwoHandshake() {}
// Registration
// ------------

public static void enable() {}
public static void enable() {
if (OwoFreezer.isFrozen())
throw new ServicesFrozenException("The oωo handshake may only be enabled during mod initialization");
}

public static void requireHandshake() {
if (OwoFreezer.isFrozen())
Expand All @@ -70,68 +66,71 @@ public static void requireHandshake() {
}

static {
ServerLoginConnectionEvents.QUERY_START.register(OwoHandshake::queryStart);
ServerLoginNetworking.registerGlobalReceiver(OwoHandshake.CHANNEL_ID, OwoHandshake::syncServer);

if (!ENABLED) {
ServerPlayNetworking.registerGlobalReceiver(OwoHandshake.OFF_CHANNEL_ID, (server, player, handler, buf, responseSender) -> {});
} else {
ServerLifecycleEvents.SERVER_STARTED.register(server -> QUERY_RECEIVED = true);
}
ServerConfigurationConnectionEvents.CONFIGURE.register(OwoHandshake::configureStart);
ServerConfigurationNetworking.registerGlobalReceiver(OwoHandshake.CHANNEL_ID, OwoHandshake::syncServer);

if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
ClientLoginNetworking.registerGlobalReceiver(OwoHandshake.CHANNEL_ID, OwoHandshake::syncClient);
ClientPlayConnectionEvents.JOIN.register(OwoHandshake::handleJoinClient);
if (!ENABLED) {
ClientConfigurationNetworking.registerGlobalReceiver(OwoHandshake.OFF_CHANNEL_ID, (client, handler, buf, responseSender) -> {});
}

ClientConfigurationNetworking.registerGlobalReceiver(OwoHandshake.CHANNEL_ID, OwoHandshake::syncClient);
ClientConfigurationConnectionEvents.READY.register(OwoHandshake::handleReadyClient);

ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> QUERY_RECEIVED = false);
ClientLoginConnectionEvents.DISCONNECT.register((handler, client) -> QUERY_RECEIVED = false);
ClientConfigurationConnectionEvents.DISCONNECT.register((handler, client) -> QUERY_RECEIVED = false);
}
}

public static boolean isValid() {
public static boolean isValidClient() {
return ENABLED && QUERY_RECEIVED;
}

// -------
// Packets
// -------

private static void queryStart(ServerLoginNetworkHandler serverLoginNetworkHandler, MinecraftServer server, PacketSender sender, ServerLoginNetworking.LoginSynchronizer loginSynchronizer) {
private static void configureStart(ServerConfigurationNetworkHandler handler, MinecraftServer server) {
if (!ENABLED) return;

if (ServerConfigurationNetworking.canSend(handler, OFF_CHANNEL_ID)) {
Owo.LOGGER.info("[Handshake] Handshake disabled by client, skipping");
return;
}

if (!ServerConfigurationNetworking.canSend(handler, CHANNEL_ID)) {
if (!HANDSHAKE_REQUIRED) return;

handler.disconnect(TextOps.concat(PREFIX, Text.of("incompatible client")));
Owo.LOGGER.info("[Handshake] Handshake failed, client doesn't understand channel packet");
return;
}

var request = PacketByteBufs.create();
writeHashes(request, OwoNetChannel.OPTIONAL_CHANNELS, OwoHandshake::hashChannel);
sender.sendPacket(OwoHandshake.CHANNEL_ID, request);
Owo.LOGGER.info("[Handshake] Sending channel query");
ServerConfigurationNetworking.send(handler, OwoHandshake.CHANNEL_ID, request);
Owo.LOGGER.info("[Handshake] Sending channel packet");
}

@Environment(EnvType.CLIENT)
private static CompletableFuture<PacketByteBuf> syncClient(MinecraftClient client, ClientLoginNetworkHandler clientLoginNetworkHandler, PacketByteBuf buf, Consumer<GenericFutureListener<? extends Future<? super Void>>> genericFutureListenerConsumer) {
private static void syncClient(MinecraftClient client, ClientConfigurationNetworkHandler handler, PacketByteBuf buf, PacketSender sender) {
Owo.LOGGER.info("[Handshake] Sending client channels");
QUERY_RECEIVED = true;

if (buf.readableBytes() > 0) {
final var serverOptionalChannels = RESPONSE_SERIALIZER.deserializer().apply(buf);
((OwoClientConnectionExtension) ((ClientLoginNetworkHandlerAccessor) clientLoginNetworkHandler).owo$getConnection()).owo$setChannelSet(filterOptionalServices(serverOptionalChannels, OwoNetChannel.REGISTERED_CHANNELS, OwoHandshake::hashChannel));
((OwoClientConnectionExtension) ((ClientCommonNetworkHandlerAccessor) handler).getConnection()).owo$setChannelSet(filterOptionalServices(serverOptionalChannels, OwoNetChannel.REGISTERED_CHANNELS, OwoHandshake::hashChannel));
}

var response = PacketByteBufs.create();
writeHashes(response, OwoNetChannel.REQUIRED_CHANNELS, OwoHandshake::hashChannel);
writeHashes(response, ParticleSystemController.REGISTERED_CONTROLLERS, OwoHandshake::hashController);
writeHashes(response, OwoNetChannel.OPTIONAL_CHANNELS, OwoHandshake::hashChannel);

return CompletableFuture.completedFuture(response);
sender.sendPacket(CHANNEL_ID, response);
}

private static void syncServer(MinecraftServer server, ServerLoginNetworkHandler handler, boolean responded, PacketByteBuf buf, ServerLoginNetworking.LoginSynchronizer loginSynchronizer, PacketSender packetSender) {
if (!responded) {
if (!HANDSHAKE_REQUIRED) return;

handler.disconnect(TextOps.concat(PREFIX, Text.of("incompatible client")));
Owo.LOGGER.info("[Handshake] Handshake failed, client did not respond to channel query");
return;
}

private static void syncServer(MinecraftServer server, ServerConfigurationNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
Owo.LOGGER.info("[Handshake] Receiving client channels");

final var clientChannels = RESPONSE_SERIALIZER.deserializer().apply(buf);
Expand All @@ -148,17 +147,20 @@ private static void syncServer(MinecraftServer server, ServerLoginNetworkHandler

if (buf.readableBytes() > 0) {
final var clientOptionalChannels = RESPONSE_SERIALIZER.deserializer().apply(buf);
((OwoClientConnectionExtension) ((ServerLoginNetworkHandlerAccessor) handler).owo$getConnection()).owo$setChannelSet(filterOptionalServices(clientOptionalChannels, OwoNetChannel.OPTIONAL_CHANNELS, OwoHandshake::hashChannel));
((OwoClientConnectionExtension) ((ServerCommonNetworkHandlerAccessor) handler).owo$getConnection()).owo$setChannelSet(filterOptionalServices(clientOptionalChannels, OwoNetChannel.OPTIONAL_CHANNELS, OwoHandshake::hashChannel));
}

Owo.LOGGER.info("[Handshake] Handshake completed successfully");
}

@Environment(EnvType.CLIENT)
private static void handleJoinClient(ClientPlayNetworkHandler handler, PacketSender packetSender, MinecraftClient client) {
if (QUERY_RECEIVED || ClientPlayNetworking.canSend(OFF_CHANNEL_ID) || !HANDSHAKE_REQUIRED || !ENABLED) return;
private static void handleReadyClient(ClientConfigurationNetworkHandler handler, MinecraftClient client) {
if (ClientConfigurationNetworking.canSend(CHANNEL_ID) || !HANDSHAKE_REQUIRED || !ENABLED) return;

client.execute(() -> {
handler.getConnection().disconnect(TextOps.concat(PREFIX, Text.of("incompatible server")));
((ClientCommonNetworkHandlerAccessor) handler)
.getConnection()
.disconnect(TextOps.concat(PREFIX, Text.of("incompatible server")));
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/wispforest/owo/network/OwoNetChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public boolean canSendToPlayer(ServerPlayerEntity player) {
public boolean canSendToPlayer(ServerPlayNetworkHandler networkHandler) {
if (required) return true;

return OwoHandshake.isValid() ?
return OwoHandshake.isValidClient() ?
getChannelSet(((ServerCommonNetworkHandlerAccessor) networkHandler).owo$getConnection()).contains(this.packetId)
: ServerPlayNetworking.canSend(networkHandler, this.packetId);
}
Expand All @@ -229,7 +229,7 @@ public boolean canSendToPlayer(ServerPlayNetworkHandler networkHandler) {
public boolean canSendToServer() {
if (required) return true;

return OwoHandshake.isValid() ?
return OwoHandshake.isValidClient() ?
getChannelSet(MinecraftClient.getInstance().getNetworkHandler().getConnection()).contains(packetId)
: ClientPlayNetworking.canSend(this.packetId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/owo.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"package": "io.wispforest.owo.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
"ClientCommonNetworkHandlerAccessor",
"ClientConnectionMixin",
"Copenhagen",
"ItemStackMixin",
"NbtCompoundMixin",
"ScreenHandlerInvoker",
"ScreenHandlerMixin",
"ServerLoginNetworkHandlerAccessor",
"ServerPlayerEntityMixin",
"ServerPlayerInteractionManagerMixin",
"ServerCommonNetworkHandlerAccessor",
Expand Down

0 comments on commit 5b120e1

Please sign in to comment.