mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-14 19:25:23 -05:00
Network addon session refactor (#3394)
* refactor network addon session handling * Check payload size * Fix ClientLoginNetworkAddon does not handle unsuccessful query responses Closes #3384 * Adjust some logging. --------- Co-authored-by: deirn <deirn@bai.lol>
This commit is contained in:
parent
a44e16a6dc
commit
bff13c8545
20 changed files with 131 additions and 119 deletions
|
@ -18,7 +18,6 @@ package net.fabricmc.fabric.impl.networking.client;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
|
||||
|
@ -51,17 +50,10 @@ public final class ClientConfigurationNetworkAddon extends AbstractChanneledNetw
|
|||
|
||||
// Must register pending channels via lateinit
|
||||
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkState.CONFIGURATION);
|
||||
|
||||
// Register global receivers and attach to session
|
||||
this.receiver.startSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lateInit() {
|
||||
for (Map.Entry<Identifier, ClientConfigurationNetworking.ConfigurationChannelHandler> entry : this.receiver.getHandlers().entrySet()) {
|
||||
this.registerChannel(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
protected void invokeInitEvent() {
|
||||
ClientConfigurationConnectionEvents.INIT.invoker().onConfigurationInit(this.handler, this.client);
|
||||
}
|
||||
|
||||
|
@ -153,7 +145,6 @@ public final class ClientConfigurationNetworkAddon extends AbstractChanneledNetw
|
|||
@Override
|
||||
protected void invokeDisconnectEvent() {
|
||||
ClientConfigurationConnectionEvents.DISCONNECT.invoker().onConfigurationDisconnect(this.handler, this.client);
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -51,9 +51,11 @@ public final class ClientLoginNetworkAddon extends AbstractNetworkAddon<ClientLo
|
|||
super(ClientNetworkingImpl.LOGIN, "ClientLoginNetworkAddon for Client");
|
||||
this.handler = handler;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invokeInitEvent() {
|
||||
ClientLoginConnectionEvents.INIT.invoker().onLoginStart(this.handler, this.client);
|
||||
this.receiver.startSession(this);
|
||||
}
|
||||
|
||||
public boolean handlePacket(LoginQueryRequestS2CPacket packet) {
|
||||
|
@ -86,7 +88,7 @@ public final class ClientLoginNetworkAddon extends AbstractNetworkAddon<ClientLo
|
|||
try {
|
||||
CompletableFuture<@Nullable PacketByteBuf> future = handler.receive(this.client, this.handler, buf, futureListeners::add);
|
||||
future.thenAccept(result -> {
|
||||
LoginQueryResponseC2SPacket packet = new LoginQueryResponseC2SPacket(queryId, new PacketByteBufLoginQueryResponse(result));
|
||||
LoginQueryResponseC2SPacket packet = new LoginQueryResponseC2SPacket(queryId, result == null ? null : new PacketByteBufLoginQueryResponse(result));
|
||||
GenericFutureListener<? extends Future<? super Void>> listener = null;
|
||||
|
||||
for (GenericFutureListener<? extends Future<? super Void>> each : futureListeners) {
|
||||
|
@ -114,11 +116,6 @@ public final class ClientLoginNetworkAddon extends AbstractNetworkAddon<ClientLo
|
|||
@Override
|
||||
protected void invokeDisconnectEvent() {
|
||||
ClientLoginConnectionEvents.DISCONNECT.invoker().onLoginDisconnect(this.handler, this.client);
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
public void handleConfigurationTransition() {
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -18,7 +18,6 @@ package net.fabricmc.fabric.impl.networking.client;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.mojang.logging.LogUtils;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -53,17 +52,10 @@ public final class ClientPlayNetworkAddon extends AbstractChanneledNetworkAddon<
|
|||
|
||||
// Must register pending channels via lateinit
|
||||
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkState.PLAY);
|
||||
|
||||
// Register global receivers and attach to session
|
||||
this.receiver.startSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lateInit() {
|
||||
for (Map.Entry<Identifier, ClientPlayNetworking.PlayChannelHandler> entry : this.receiver.getHandlers().entrySet()) {
|
||||
this.registerChannel(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
protected void invokeInitEvent() {
|
||||
ClientPlayConnectionEvents.INIT.invoker().onPlayInit(this.handler, this.client);
|
||||
}
|
||||
|
||||
|
@ -148,7 +140,6 @@ public final class ClientPlayNetworkAddon extends AbstractChanneledNetworkAddon<
|
|||
@Override
|
||||
protected void invokeDisconnectEvent() {
|
||||
ClientPlayConnectionEvents.DISCONNECT.invoker().onPlayDisconnect(this.handler, this.client);
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -23,7 +23,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|||
|
||||
import net.minecraft.client.network.ClientCommonNetworkHandler;
|
||||
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions;
|
||||
import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon;
|
||||
|
@ -32,11 +31,6 @@ import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
|
|||
|
||||
@Mixin(ClientCommonNetworkHandler.class)
|
||||
public abstract class ClientCommonNetworkHandlerMixin implements NetworkHandlerExtensions {
|
||||
@Inject(method = "onDisconnected", at = @At("HEAD"))
|
||||
private void handleDisconnection(Text reason, CallbackInfo ci) {
|
||||
this.getAddon().handleDisconnect();
|
||||
}
|
||||
|
||||
@Inject(method = "onCustomPayload(Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;)V", at = @At("HEAD"), cancellable = true)
|
||||
public void onCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) {
|
||||
if (packet.payload() instanceof PacketByteBufPayload payload) {
|
||||
|
|
|
@ -28,7 +28,6 @@ import net.minecraft.client.MinecraftClient;
|
|||
import net.minecraft.client.network.ClientLoginNetworkHandler;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import net.fabricmc.fabric.impl.networking.NetworkHandlerExtensions;
|
||||
import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon;
|
||||
|
@ -64,16 +63,6 @@ abstract class ClientLoginNetworkHandlerMixin implements NetworkHandlerExtension
|
|||
}
|
||||
}
|
||||
|
||||
@Inject(method = "onDisconnected", at = @At("HEAD"))
|
||||
private void invokeLoginDisconnectEvent(Text reason, CallbackInfo ci) {
|
||||
this.addon.handleDisconnect();
|
||||
}
|
||||
|
||||
@Inject(method = "onSuccess", at = @At("HEAD"))
|
||||
private void handleConfigurationTransition(CallbackInfo ci) {
|
||||
addon.handleConfigurationTransition();
|
||||
}
|
||||
|
||||
@Inject(method = "onSuccess", at = @At("TAIL"))
|
||||
private void handleConfigurationReady(CallbackInfo ci) {
|
||||
NetworkHandlerExtensions networkHandlerExtensions = (NetworkHandlerExtensions) connection.getPacketListener();
|
||||
|
|
|
@ -60,8 +60,6 @@ public abstract class AbstractChanneledNetworkAddon<H> extends AbstractNetworkAd
|
|||
this.sendableChannels = Collections.synchronizedSet(new HashSet<>());
|
||||
}
|
||||
|
||||
public abstract void lateInit();
|
||||
|
||||
protected void registerPendingChannels(ChannelInfoHolder holder, NetworkState state) {
|
||||
final Collection<Identifier> pending = holder.getPendingChannelsNames(state);
|
||||
|
||||
|
@ -211,7 +209,7 @@ public abstract class AbstractChanneledNetworkAddon<H> extends AbstractNetworkAd
|
|||
assert negotiatedVersion == 1; // We only support version 1 for now
|
||||
|
||||
commonVersion = negotiatedVersion;
|
||||
this.logger.info("Negotiated common packet version {}", commonVersion);
|
||||
this.logger.debug("Negotiated common packet version {}", commonVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -52,6 +52,17 @@ public abstract class AbstractNetworkAddon<H> {
|
|||
this.logger = LoggerFactory.getLogger(description);
|
||||
}
|
||||
|
||||
public final void lateInit() {
|
||||
this.receiver.startSession(this);
|
||||
invokeInitEvent();
|
||||
}
|
||||
|
||||
protected abstract void invokeInitEvent();
|
||||
|
||||
public final void endSession() {
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public H getHandler(Identifier channel) {
|
||||
Lock lock = this.lock.readLock();
|
||||
|
@ -64,13 +75,32 @@ public abstract class AbstractNetworkAddon<H> {
|
|||
}
|
||||
}
|
||||
|
||||
private void assertNotReserved(Identifier channel) {
|
||||
if (this.isReservedChannel(channel)) {
|
||||
throw new IllegalArgumentException(String.format("Cannot (un)register handler for reserved channel with name \"%s\"", channel));
|
||||
}
|
||||
}
|
||||
|
||||
public void registerChannels(Map<Identifier, H> map) {
|
||||
Lock lock = this.lock.writeLock();
|
||||
lock.lock();
|
||||
|
||||
try {
|
||||
for (Map.Entry<Identifier, H> entry : map.entrySet()) {
|
||||
assertNotReserved(entry.getKey());
|
||||
|
||||
boolean unique = this.handlers.putIfAbsent(entry.getKey(), entry.getValue()) == null;
|
||||
if (unique) handleRegistration(entry.getKey());
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean registerChannel(Identifier channelName, H handler) {
|
||||
Objects.requireNonNull(channelName, "Channel name cannot be null");
|
||||
Objects.requireNonNull(handler, "Packet handler cannot be null");
|
||||
|
||||
if (this.isReservedChannel(channelName)) {
|
||||
throw new IllegalArgumentException(String.format("Cannot register handler for reserved channel with name \"%s\"", channelName));
|
||||
}
|
||||
assertNotReserved(channelName);
|
||||
|
||||
Lock lock = this.lock.writeLock();
|
||||
lock.lock();
|
||||
|
@ -90,10 +120,7 @@ public abstract class AbstractNetworkAddon<H> {
|
|||
|
||||
public H unregisterChannel(Identifier channelName) {
|
||||
Objects.requireNonNull(channelName, "Channel name cannot be null");
|
||||
|
||||
if (this.isReservedChannel(channelName)) {
|
||||
throw new IllegalArgumentException(String.format("Cannot register handler for reserved channel with name \"%s\"", channelName));
|
||||
}
|
||||
assertNotReserved(channelName);
|
||||
|
||||
Lock lock = this.lock.writeLock();
|
||||
lock.lock();
|
||||
|
@ -129,6 +156,7 @@ public abstract class AbstractNetworkAddon<H> {
|
|||
public final void handleDisconnect() {
|
||||
if (disconnected.compareAndSet(false, true)) {
|
||||
invokeDisconnectEvent();
|
||||
endSession();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,11 +26,15 @@ import java.util.concurrent.locks.ReadWriteLock;
|
|||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.minecraft.network.NetworkState;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
public final class GlobalReceiverRegistry<H> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalReceiverRegistry.class);
|
||||
|
||||
private final NetworkState state;
|
||||
|
||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
@ -134,7 +138,11 @@ public final class GlobalReceiverRegistry<H> {
|
|||
lock.lock();
|
||||
|
||||
try {
|
||||
this.trackedAddons.add(addon);
|
||||
if (this.trackedAddons.add(addon)) {
|
||||
addon.registerChannels(handlers);
|
||||
}
|
||||
|
||||
this.logTrackedAddonSize();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
@ -145,17 +153,29 @@ public final class GlobalReceiverRegistry<H> {
|
|||
lock.lock();
|
||||
|
||||
try {
|
||||
this.logTrackedAddonSize();
|
||||
this.trackedAddons.remove(addon);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In practice, trackedAddons should never contain more than the number of players.
|
||||
*/
|
||||
private void logTrackedAddonSize() {
|
||||
if (LOGGER.isTraceEnabled() && this.trackedAddons.size() > 1) {
|
||||
LOGGER.trace("{} receiver registry tracks {} addon instances", state.getId(), trackedAddons.size());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleRegistration(Identifier channelName, H handler) {
|
||||
Lock lock = this.lock.writeLock();
|
||||
lock.lock();
|
||||
|
||||
try {
|
||||
this.logTrackedAddonSize();
|
||||
|
||||
for (AbstractNetworkAddon<H> addon : this.trackedAddons) {
|
||||
addon.registerChannel(channelName, handler);
|
||||
}
|
||||
|
@ -169,6 +189,8 @@ public final class GlobalReceiverRegistry<H> {
|
|||
lock.lock();
|
||||
|
||||
try {
|
||||
this.logTrackedAddonSize();
|
||||
|
||||
for (AbstractNetworkAddon<H> addon : this.trackedAddons) {
|
||||
addon.unregisterChannel(channelName);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,13 @@ public class PayloadHelper {
|
|||
byteBuf.writeBytes(data.copy());
|
||||
}
|
||||
|
||||
public static PacketByteBuf read(PacketByteBuf byteBuf) {
|
||||
public static PacketByteBuf read(PacketByteBuf byteBuf, int maxSize) {
|
||||
int size = byteBuf.readableBytes();
|
||||
|
||||
if (size < 0 || size > maxSize) {
|
||||
throw new IllegalArgumentException("Payload may not be larger than %d bytes".formatted(maxSize));
|
||||
}
|
||||
|
||||
PacketByteBuf newBuf = PacketByteBufs.create();
|
||||
newBuf.writeBytes(byteBuf.copy());
|
||||
byteBuf.skipBytes(byteBuf.readableBytes());
|
||||
|
|
|
@ -18,7 +18,6 @@ package net.fabricmc.fabric.impl.networking.server;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.network.NetworkState;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
|
@ -52,16 +51,10 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw
|
|||
|
||||
// Must register pending channels via lateinit
|
||||
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkState.CONFIGURATION);
|
||||
|
||||
// Register global receivers and attach to session
|
||||
this.receiver.startSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lateInit() {
|
||||
for (Map.Entry<Identifier, ServerConfigurationNetworking.ConfigurationChannelHandler> entry : this.receiver.getHandlers().entrySet()) {
|
||||
this.registerChannel(entry.getKey(), entry.getValue());
|
||||
}
|
||||
protected void invokeInitEvent() {
|
||||
}
|
||||
|
||||
public void preConfiguration() {
|
||||
|
@ -177,7 +170,6 @@ public final class ServerConfigurationNetworkAddon extends AbstractChanneledNetw
|
|||
@Override
|
||||
protected void invokeDisconnectEvent() {
|
||||
ServerConfigurationConnectionEvents.DISCONNECT.invoker().onConfigureDisconnect(handler, server);
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -67,9 +67,11 @@ public final class ServerLoginNetworkAddon extends AbstractNetworkAddon<ServerLo
|
|||
this.handler = handler;
|
||||
this.server = ((ServerLoginNetworkHandlerAccessor) handler).getServer();
|
||||
this.queryIdFactory = QueryIdFactory.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invokeInitEvent() {
|
||||
ServerLoginConnectionEvents.INIT.invoker().onLoginInit(handler, this.server);
|
||||
this.receiver.startSession(this);
|
||||
}
|
||||
|
||||
// return true if no longer ticks query
|
||||
|
@ -202,11 +204,6 @@ public final class ServerLoginNetworkAddon extends AbstractNetworkAddon<ServerLo
|
|||
@Override
|
||||
protected void invokeDisconnectEvent() {
|
||||
ServerLoginConnectionEvents.DISCONNECT.invoker().onLoginDisconnect(this.handler, this.server);
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
public void handleConfigurationTransition() {
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -18,8 +18,8 @@ package net.fabricmc.fabric.impl.networking.server;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import net.minecraft.network.NetworkState;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
|
@ -35,31 +35,23 @@ import net.fabricmc.fabric.impl.networking.AbstractChanneledNetworkAddon;
|
|||
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
|
||||
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
|
||||
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
|
||||
import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonNetworkHandlerAccessor;
|
||||
|
||||
public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<ServerPlayNetworking.PlayChannelHandler> {
|
||||
private final ServerPlayNetworkHandler handler;
|
||||
private final MinecraftServer server;
|
||||
private boolean sentInitialRegisterPacket;
|
||||
|
||||
public ServerPlayNetworkAddon(ServerPlayNetworkHandler handler, MinecraftServer server) {
|
||||
super(ServerNetworkingImpl.PLAY, ((ServerCommonNetworkHandlerAccessor) handler).getConnection(), "ServerPlayNetworkAddon for " + handler.player.getEntityName());
|
||||
public ServerPlayNetworkAddon(ServerPlayNetworkHandler handler, ClientConnection connection, MinecraftServer server) {
|
||||
super(ServerNetworkingImpl.PLAY, connection, "ServerPlayNetworkAddon for " + handler.player.getEntityName());
|
||||
this.handler = handler;
|
||||
this.server = server;
|
||||
|
||||
// Must register pending channels via lateinit
|
||||
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkState.PLAY);
|
||||
|
||||
// Register global receivers and attach to session
|
||||
this.receiver.startSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lateInit() {
|
||||
for (Map.Entry<Identifier, ServerPlayNetworking.PlayChannelHandler> entry : this.receiver.getHandlers().entrySet()) {
|
||||
this.registerChannel(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
protected void invokeInitEvent() {
|
||||
ServerPlayConnectionEvents.INIT.invoker().onPlayInit(this.handler, this.server);
|
||||
}
|
||||
|
||||
|
@ -139,7 +131,6 @@ public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<
|
|||
@Override
|
||||
protected void invokeDisconnectEvent() {
|
||||
ServerPlayConnectionEvents.DISCONNECT.invoker().onPlayDisconnect(this.handler, this.server);
|
||||
this.receiver.endSession(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.spongepowered.asm.mixin.Shadow;
|
|||
import org.spongepowered.asm.mixin.Unique;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
@ -88,13 +89,29 @@ abstract class ClientConnectionMixin implements ChannelInfoHolder {
|
|||
}
|
||||
}
|
||||
|
||||
@Inject(method = "channelInactive", at = @At("HEAD"))
|
||||
private void handleDisconnect(ChannelHandlerContext channelHandlerContext, CallbackInfo ci) {
|
||||
if (packetListener instanceof NetworkHandlerExtensions) { // not the case for client/server query
|
||||
((NetworkHandlerExtensions) packetListener).getAddon().handleDisconnect();
|
||||
@Inject(method = "setPacketListener", at = @At("HEAD"))
|
||||
private void unwatchAddon(PacketListener packetListener, CallbackInfo ci) {
|
||||
if (this.packetListener instanceof NetworkHandlerExtensions oldListener) {
|
||||
oldListener.getAddon().endSession();
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "channelInactive", at = @At("HEAD"))
|
||||
private void disconnectAddon(ChannelHandlerContext channelHandlerContext, CallbackInfo ci) {
|
||||
if (packetListener instanceof NetworkHandlerExtensions extension) {
|
||||
extension.getAddon().handleDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@ModifyVariable(method = "handleDisconnection", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/listener/PacketListener;onDisconnected(Lnet/minecraft/text/Text;)V"))
|
||||
private PacketListener disconnectAddon(PacketListener packetListener) {
|
||||
if (packetListener instanceof NetworkHandlerExtensions extension) {
|
||||
extension.getAddon().handleDisconnect();
|
||||
}
|
||||
|
||||
return packetListener;
|
||||
}
|
||||
|
||||
@Inject(method = "sendInternal", at = @At(value = "INVOKE", target = "Lio/netty/channel/ChannelFuture;addListener(Lio/netty/util/concurrent/GenericFutureListener;)Lio/netty/channel/ChannelFuture;", remap = false), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true)
|
||||
private void sendInternal(Packet<?> packet, @Nullable PacketCallbacks callbacks, boolean flush, CallbackInfo ci, ChannelFuture channelFuture) {
|
||||
if (callbacks instanceof GenericFutureListenerHolder holder) {
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.networking;
|
||||
|
||||
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.callback.CallbackInfoReturnable;
|
||||
|
@ -31,12 +33,16 @@ import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
|
|||
|
||||
@Mixin(CustomPayloadC2SPacket.class)
|
||||
public class CustomPayloadC2SPacketMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private static int MAX_PAYLOAD_SIZE;
|
||||
|
||||
@Inject(
|
||||
method = "readPayload",
|
||||
at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/c2s/common/CustomPayloadC2SPacket;readUnknownPayload(Lnet/minecraft/util/Identifier;Lnet/minecraft/network/PacketByteBuf;)Lnet/minecraft/network/packet/UnknownCustomPayload;"),
|
||||
cancellable = true
|
||||
)
|
||||
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<CustomPayload> cir) {
|
||||
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf)));
|
||||
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.networking;
|
||||
|
||||
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.callback.CallbackInfoReturnable;
|
||||
|
@ -31,12 +33,16 @@ import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
|
|||
|
||||
@Mixin(CustomPayloadS2CPacket.class)
|
||||
public class CustomPayloadS2CPacketMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private static int MAX_PAYLOAD_SIZE;
|
||||
|
||||
@Inject(
|
||||
method = "readPayload",
|
||||
at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;readUnknownPayload(Lnet/minecraft/util/Identifier;Lnet/minecraft/network/PacketByteBuf;)Lnet/minecraft/network/packet/UnknownCustomPayload;"),
|
||||
cancellable = true
|
||||
)
|
||||
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<CustomPayload> cir) {
|
||||
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf)));
|
||||
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.networking;
|
||||
|
||||
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.callback.CallbackInfoReturnable;
|
||||
|
@ -31,8 +33,12 @@ import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
|
|||
|
||||
@Mixin(LoginQueryRequestS2CPacket.class)
|
||||
public class LoginQueryRequestS2CPacketMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private static int MAX_PAYLOAD_SIZE;
|
||||
|
||||
@Inject(method = "readPayload", at = @At("HEAD"), cancellable = true)
|
||||
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<LoginQueryRequestPayload> cir) {
|
||||
cir.setReturnValue(new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf)));
|
||||
cir.setReturnValue(new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.networking;
|
||||
|
||||
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.callback.CallbackInfoReturnable;
|
||||
|
@ -30,6 +32,10 @@ import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
|
|||
|
||||
@Mixin(LoginQueryResponseC2SPacket.class)
|
||||
public class LoginQueryResponseC2SPacketMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private static int MAX_PAYLOAD_SIZE;
|
||||
|
||||
@Inject(method = "readPayload", at = @At("HEAD"), cancellable = true)
|
||||
private static void readResponse(int queryId, PacketByteBuf buf, CallbackInfoReturnable<LoginQueryResponsePayload> cir) {
|
||||
boolean hasPayload = buf.readBoolean();
|
||||
|
@ -39,6 +45,6 @@ public class LoginQueryResponseC2SPacketMixin {
|
|||
return;
|
||||
}
|
||||
|
||||
cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf)));
|
||||
cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,11 +141,6 @@ public abstract class ServerConfigurationNetworkHandlerMixin extends ServerCommo
|
|||
return false;
|
||||
}
|
||||
|
||||
@Inject(method = "onDisconnected", at = @At("HEAD"))
|
||||
private void handleDisconnection(Text reason, CallbackInfo ci) {
|
||||
this.addon.handleDisconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerConfigurationNetworkAddon getAddon() {
|
||||
return addon;
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package net.fabricmc.fabric.mixin.networking;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
|
@ -42,10 +41,6 @@ import net.fabricmc.fabric.impl.networking.server.ServerLoginNetworkAddon;
|
|||
|
||||
@Mixin(ServerLoginNetworkHandler.class)
|
||||
abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerExtensions, DisconnectPacketSource, PacketCallbackListener {
|
||||
@Shadow
|
||||
@Final
|
||||
private MinecraftServer server;
|
||||
|
||||
@Shadow
|
||||
protected abstract void tickVerify(GameProfile profile);
|
||||
|
||||
|
@ -82,16 +77,6 @@ abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerExtension
|
|||
return -1;
|
||||
}
|
||||
|
||||
@Inject(method = "onDisconnected", at = @At("HEAD"))
|
||||
private void handleDisconnection(Text reason, CallbackInfo ci) {
|
||||
this.addon.handleDisconnect();
|
||||
}
|
||||
|
||||
@Inject(method = "sendSuccessPacket", at = @At("HEAD"))
|
||||
private void handlePlayTransitionNormal(GameProfile profile, CallbackInfo ci) {
|
||||
this.addon.handleConfigurationTransition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sent(Packet<?> packet) {
|
||||
if (packet instanceof LoginQueryRequestS2CPacket) {
|
||||
|
|
|
@ -47,16 +47,11 @@ abstract class ServerPlayNetworkHandlerMixin extends ServerCommonNetworkHandler
|
|||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
private void initAddon(CallbackInfo ci) {
|
||||
this.addon = new ServerPlayNetworkAddon((ServerPlayNetworkHandler) (Object) this, this.server);
|
||||
this.addon = new ServerPlayNetworkAddon((ServerPlayNetworkHandler) (Object) this, connection, server);
|
||||
// A bit of a hack but it allows the field above to be set in case someone registers handlers during INIT event which refers to said field
|
||||
this.addon.lateInit();
|
||||
}
|
||||
|
||||
@Inject(method = "onDisconnected", at = @At("HEAD"))
|
||||
private void handleDisconnection(Text reason, CallbackInfo ci) {
|
||||
this.addon.handleDisconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerPlayNetworkAddon getAddon() {
|
||||
return this.addon;
|
||||
|
|
Loading…
Reference in a new issue