Fixed some things

This commit is contained in:
Adrian Bergqvist 2023-11-27 17:56:56 +01:00
parent 57282b69ab
commit c3ae255be6
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
9 changed files with 47 additions and 35 deletions

View File

@ -9,6 +9,8 @@ import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.AvailableCommands; import com.velocitypowered.proxy.protocol.packet.AvailableCommands;
import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage; import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.PluginMessage; import com.velocitypowered.proxy.protocol.packet.PluginMessage;
import org.adde0109.ambassador.forge.packet.Context;
import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.pipeline.CommandDecoderErrorCatcher; import org.adde0109.ambassador.forge.pipeline.CommandDecoderErrorCatcher;
import org.adde0109.ambassador.forge.pipeline.ForgeLoginWrapperCodec; import org.adde0109.ambassador.forge.pipeline.ForgeLoginWrapperCodec;
@ -57,8 +59,7 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
VelocityForgeBackendConnectionPhase() { VelocityForgeBackendConnectionPhase() {
} }
public void handle(VelocityServerConnection server, ConnectedPlayer player, LoginPluginMessage message) { public void handle(VelocityServerConnection server, ConnectedPlayer player, IForgeLoginWrapperPacket message) {
VelocityForgeBackendConnectionPhase newPhase = getNewPhase(server,message); VelocityForgeBackendConnectionPhase newPhase = getNewPhase(server,message);
server.setConnectionPhase(newPhase); server.setConnectionPhase(newPhase);
@ -75,7 +76,6 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
return; return;
} }
} }
message.retain();
player.getConnection().write(message); player.getConnection().write(message);
//Forge server //Forge server
//To avoid unnecessary resets, we wait until we get the handshake even if we know that we should //To avoid unnecessary resets, we wait until we get the handshake even if we know that we should
@ -93,7 +93,7 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
} }
private VelocityForgeBackendConnectionPhase getNewPhase(VelocityServerConnection serverConnection, private VelocityForgeBackendConnectionPhase getNewPhase(VelocityServerConnection serverConnection,
LoginPluginMessage packet) { IForgeLoginWrapperPacket<Context> packet) {
VelocityForgeBackendConnectionPhase phaseToTransitionTo = nextPhase(); VelocityForgeBackendConnectionPhase phaseToTransitionTo = nextPhase();
if (phaseToTransitionTo != this) { if (phaseToTransitionTo != this) {
phaseToTransitionTo.onTransitionToNewPhase(serverConnection); phaseToTransitionTo.onTransitionToNewPhase(serverConnection);

View File

@ -148,7 +148,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
public ModListReplyPacket clientModList; public ModListReplyPacket clientModList;
public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket msg, VelocityServerConnection server) { public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket<Context.ClientContext> msg, VelocityServerConnection server) {
player.setPhase(nextPhase()); player.setPhase(nextPhase());
if (msg instanceof ModListReplyPacket replyPacket) { if (msg instanceof ModListReplyPacket replyPacket) {
@ -164,7 +164,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
replyPacket.getChannels().put(MinecraftChannelIdentifier.from("ambassador:commands"),"1"); replyPacket.getChannels().put(MinecraftChannelIdentifier.from("ambassador:commands"),"1");
} }
player.getConnectionInFlight().getConnection().write(msg.encode()); player.getConnectionInFlight().getConnection().write(msg);
return true; return true;
} }

View File

@ -15,7 +15,7 @@ public class GenericForgeLoginWrapperPacket<T extends Context> extends DeferredB
static public GenericForgeLoginWrapperPacket<Context> create(ByteBuf input, Context context) { static public GenericForgeLoginWrapperPacket<Context> create(ByteBuf input, Context context) {
return new GenericForgeLoginWrapperPacket<>(input, context); return new GenericForgeLoginWrapperPacket<>(input.retain(), context);
} }
@Override @Override

View File

@ -27,7 +27,7 @@ public class ModListPacket implements IForgeLoginWrapperPacket<Context> {
this.dataPackRegistries = dataPackRegistries; this.dataPackRegistries = dataPackRegistries;
} }
public static ModListPacket read(ByteBuf input, Context context) { public static ModListPacket read(ByteBuf input, Context context, boolean FML3) {
List<String> mods = new ArrayList<>(); List<String> mods = new ArrayList<>();
int len = ProtocolUtils.readVarInt(input); int len = ProtocolUtils.readVarInt(input);
@ -45,8 +45,9 @@ public class ModListPacket implements IForgeLoginWrapperPacket<Context> {
for (int x = 0; x < len; x++) for (int x = 0; x < len; x++)
registries.add(ProtocolUtils.readString(input, 32767)); registries.add(ProtocolUtils.readString(input, 32767));
List<String> dataPackRegistries = new ArrayList<>(); List<String> dataPackRegistries = null;
if (input.isReadable()) { if (FML3) {
dataPackRegistries = new ArrayList<>();
len = ProtocolUtils.readVarInt(input); len = ProtocolUtils.readVarInt(input);
for (int x = 0; x < len; x++) for (int x = 0; x < len; x++)
dataPackRegistries.add(ProtocolUtils.readString(input, 0x100)); dataPackRegistries.add(ProtocolUtils.readString(input, 0x100));
@ -74,8 +75,8 @@ public class ModListPacket implements IForgeLoginWrapperPacket<Context> {
registries.forEach(k -> ProtocolUtils.writeString(buf, k)); registries.forEach(k -> ProtocolUtils.writeString(buf, k));
if (dataPackRegistries != null) { if (dataPackRegistries != null) {
ProtocolUtils.writeVarInt(buf, registries.size()); ProtocolUtils.writeVarInt(buf, dataPackRegistries.size());
registries.forEach(k -> ProtocolUtils.writeString(buf, k)); dataPackRegistries.forEach(k -> ProtocolUtils.writeString(buf, k));
} }
ByteBuf output = Unpooled.buffer(); ByteBuf output = Unpooled.buffer();

View File

@ -15,8 +15,13 @@ import java.util.List;
public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBufHolder, IForgeLoginWrapperPacket<?>> { public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBufHolder, IForgeLoginWrapperPacket<?>> {
private final boolean FML3;
private final List<Integer> loginWrapperIDs = new ArrayList<>(); private final List<Integer> loginWrapperIDs = new ArrayList<>();
public ForgeLoginWrapperCodec(boolean fml3) {
FML3 = fml3;
}
@Override @Override
protected void decode(ChannelHandlerContext ctx, DeferredByteBufHolder in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, DeferredByteBufHolder in, List<Object> out) throws Exception {
ByteBuf buf = in.content(); ByteBuf buf = in.content();
@ -51,7 +56,7 @@ public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBu
} else { } else {
switch (packetID) { switch (packetID) {
case 1: case 1:
out.add(ModListPacket.read(buf, context)); out.add(ModListPacket.read(buf, context, FML3));
break; break;
case 3: case 3:
out.add(new RegistryPacket(context)); out.add(new RegistryPacket(context));

View File

@ -1,25 +1,42 @@
package org.adde0109.ambassador.forge.pipeline; package org.adde0109.ambassador.forge.pipeline;
import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation;
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.adde0109.ambassador.forge.ForgeConstants;
import org.adde0109.ambassador.forge.ForgeFMLConnectionType;
import org.adde0109.ambassador.forge.VelocityForgeBackendConnectionPhase;
import org.adde0109.ambassador.forge.packet.Context;
import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket; import org.adde0109.ambassador.forge.packet.IForgeLoginWrapperPacket;
import org.adde0109.ambassador.forge.VelocityForgeClientConnectionPhase; import org.adde0109.ambassador.forge.VelocityForgeClientConnectionPhase;
public class ForgeLoginWrapperHandler extends SimpleChannelInboundHandler<IForgeLoginWrapperPacket> { public class ForgeLoginWrapperHandler extends SimpleChannelInboundHandler<IForgeLoginWrapperPacket<?>> {
private final ConnectedPlayer player; private final MinecraftConnectionAssociation connection;
public ForgeLoginWrapperHandler(ConnectedPlayer player) { public ForgeLoginWrapperHandler(MinecraftConnectionAssociation connection) {
super(false); super(false);
this.player = player; this.connection = connection;
} }
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, IForgeLoginWrapperPacket msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, IForgeLoginWrapperPacket msg) throws Exception {
if (connection instanceof ConnectedPlayer player) {
VelocityForgeClientConnectionPhase phase = (VelocityForgeClientConnectionPhase) player.getPhase(); VelocityForgeClientConnectionPhase phase = (VelocityForgeClientConnectionPhase) player.getPhase();
phase.handle(player,msg,player.getConnectionInFlight()); phase.handle(player,msg,player.getConnectionInFlight());
} else if (connection instanceof VelocityServerConnection serverConnection){
if (!(serverConnection.getConnection().getType() instanceof ForgeFMLConnectionType)) {
serverConnection.getConnection().setType(serverConnection.getPlayer().getConnection().getType());
serverConnection.setConnectionPhase(serverConnection.getConnection().getType().getInitialBackendPhase());
}
VelocityForgeBackendConnectionPhase phase =
(VelocityForgeBackendConnectionPhase) serverConnection.getPhase();
phase.handle(serverConnection, serverConnection.getPlayer(), msg);
}
} }
} }

View File

@ -37,7 +37,8 @@ public class VelocityEventHandler {
player.getConnection().getChannel().pipeline().addBefore( player.getConnection().getChannel().pipeline().addBefore(
Connections.HANDLER, Connections.HANDLER,
ForgeConstants.FORGE_HANDSHAKE_DECODER, new ForgeLoginWrapperCodec()); ForgeConstants.FORGE_HANDSHAKE_DECODER, new ForgeLoginWrapperCodec(
player.getConnection().getType() == ForgeConstants.ForgeFML3));
player.getConnection().getChannel().pipeline().addAfter( player.getConnection().getChannel().pipeline().addAfter(
ForgeConstants.FORGE_HANDSHAKE_DECODER, ForgeConstants.FORGE_HANDSHAKE_DECODER,
ForgeConstants.FORGE_HANDSHAKE_HANDLER, new ForgeLoginWrapperHandler(player)); ForgeConstants.FORGE_HANDSHAKE_HANDLER, new ForgeLoginWrapperHandler(player));

View File

@ -25,19 +25,6 @@ public class ForgeLoginSessionHandler implements MinecraftSessionHandler {
this.server = server; this.server = server;
} }
@Override
public boolean handle(LoginPluginMessage packet) {
if (packet.getChannel().equals("fml:loginwrapper")) {
if (serverConnection.getPhase() == BackendConnectionPhases.UNKNOWN) {
VelocityForgeBackendConnectionPhase.NOT_STARTED.handle(serverConnection,serverConnection.getPlayer(),packet);
} else if (serverConnection.getPhase() instanceof VelocityForgeBackendConnectionPhase phase1) {
phase1.handle(serverConnection,serverConnection.getPlayer(),packet);
}
return true;
}
return original.handle(packet);
}
@Override @Override
public boolean handle(ServerLoginSuccess packet) { public boolean handle(ServerLoginSuccess packet) {
if ((serverConnection.getPhase() instanceof VelocityForgeBackendConnectionPhase phase)) { if ((serverConnection.getPhase() instanceof VelocityForgeBackendConnectionPhase phase)) {

View File

@ -34,12 +34,13 @@ public class VelocityForgeBackendHandshakeHandler extends ChannelInboundHandlerA
ForgeLoginSessionHandler forgeLoginSessionHandler = new ForgeLoginSessionHandler((LoginSessionHandler) connection.getActiveSessionHandler(), serverConnection,server); ForgeLoginSessionHandler forgeLoginSessionHandler = new ForgeLoginSessionHandler((LoginSessionHandler) connection.getActiveSessionHandler(), serverConnection,server);
connection.setActiveSessionHandler(StateRegistry.LOGIN, forgeLoginSessionHandler); connection.setActiveSessionHandler(StateRegistry.LOGIN, forgeLoginSessionHandler);
player.getConnection().getChannel().pipeline().addBefore( serverConnection.getConnection().getChannel().pipeline().addBefore(
Connections.HANDLER, Connections.HANDLER,
ForgeConstants.FORGE_HANDSHAKE_DECODER, new ForgeLoginWrapperCodec()); ForgeConstants.FORGE_HANDSHAKE_DECODER, new ForgeLoginWrapperCodec(
player.getConnection().getChannel().pipeline().addAfter( player.getConnection().getType() == ForgeConstants.ForgeFML3));
serverConnection.getConnection().getChannel().pipeline().addAfter(
ForgeConstants.FORGE_HANDSHAKE_DECODER, ForgeConstants.FORGE_HANDSHAKE_DECODER,
ForgeConstants.FORGE_HANDSHAKE_HANDLER, new ForgeLoginWrapperHandler(player)); ForgeConstants.FORGE_HANDSHAKE_HANDLER, new ForgeLoginWrapperHandler(serverConnection));
} }
ctx.pipeline().fireChannelActive(); ctx.pipeline().fireChannelActive();