Packet Context Class

This commit is contained in:
Adrian Bergqvist 2023-11-23 18:07:00 +01:00
parent 0e5f3feb49
commit 669a856ec4
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
9 changed files with 77 additions and 50 deletions

View File

@ -68,6 +68,8 @@ public enum VelocityForgeBackendConnectionPhase implements BackendConnectionPhas
//Reset client if not ready to receive new handshake //Reset client if not ready to receive new handshake
VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase(); VelocityForgeClientConnectionPhase clientPhase = (VelocityForgeClientConnectionPhase) player.getPhase();
if (clientPhase != VelocityForgeClientConnectionPhase.NOT_STARTED) { if (clientPhase != VelocityForgeClientConnectionPhase.NOT_STARTED) {
//Initial Forge
//Forge -> Forge
clientPhase.resetConnectionPhase(player); clientPhase.resetConnectionPhase(player);
if (clientPhase == VelocityForgeClientConnectionPhase.COMPLETE) if (clientPhase == VelocityForgeClientConnectionPhase.COMPLETE)
{ {

View File

@ -16,6 +16,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.Ambassador; import org.adde0109.ambassador.Ambassador;
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.packet.ModListReplyPacket; import org.adde0109.ambassador.forge.packet.ModListReplyPacket;
import org.adde0109.ambassador.forge.pipeline.ForgeLoginWrapperDecoder; import org.adde0109.ambassador.forge.pipeline.ForgeLoginWrapperDecoder;
@ -96,7 +97,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
@Override @Override
public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket msg, VelocityServerConnection server) { public boolean handle(ConnectedPlayer player, IForgeLoginWrapperPacket msg, VelocityServerConnection server) {
if (msg.getId() == 98) { if (msg.getContext().getResponseID() == 98) {
player.getConnection().getChannel().pipeline().remove(ForgeConstants.RESET_LISTENER); player.getConnection().getChannel().pipeline().remove(ForgeConstants.RESET_LISTENER);
player.setPhase(NOT_STARTED); player.setPhase(NOT_STARTED);
@ -104,7 +105,7 @@ public enum VelocityForgeClientConnectionPhase implements ClientConnectionPhase
if (!(server.getConnection().getType() instanceof ForgeFMLConnectionType)) { if (!(server.getConnection().getType() instanceof ForgeFMLConnectionType)) {
// -> vanilla // -> vanilla
complete(player, msg.getSuccess()); complete(player, ((Context.ClientContext) msg.getContext()).success());
player.getConnectionInFlight().getConnection().getChannel().config().setAutoRead(true); player.getConnectionInFlight().getConnection().getChannel().config().setAutoRead(true);
} }

View File

@ -0,0 +1,35 @@
package org.adde0109.ambassador.forge.packet;
public class Context {
private final int responseID;
private Context(int responseID) {
this.responseID = responseID;
}
static Context createContext(int responseID) {
return new Context(responseID);
}
static ClientContext createContext(int responseID, boolean clientSuccess) {
return new ClientContext(responseID,clientSuccess);
}
public int getResponseID() {
return responseID;
}
public static class ClientContext extends Context {
private final boolean clientSuccess;
ClientContext(int responseID, boolean clientSuccess) {
super(responseID);
this.clientSuccess = clientSuccess;
}
public boolean success() {
return clientSuccess;
}
}
}

View File

@ -4,28 +4,31 @@ import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder; import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
public class GenericForgeLoginWrapperPacket extends DeferredByteBufHolder implements IForgeLoginWrapperPacket { public class GenericForgeLoginWrapperPacket<T extends Context> extends DeferredByteBufHolder implements IForgeLoginWrapperPacket<T> {
private final int id;
private final boolean success;
public GenericForgeLoginWrapperPacket(ByteBuf input, int id, boolean success) { private final T context;
private GenericForgeLoginWrapperPacket(ByteBuf input, T context) {
super(input); super(input);
this.id = id; this.context = context;
this.success = success; }
static public GenericForgeLoginWrapperPacket<Context> create(ByteBuf input, int id) {
return new GenericForgeLoginWrapperPacket<>(input, Context.createContext(id));
}
static public GenericForgeLoginWrapperPacket<Context> create(ByteBuf input, int id, boolean success) {
return new GenericForgeLoginWrapperPacket<>(input, Context.createContext(id, success));
} }
@Override @Override
public LoginPluginResponse encode() { public ByteBuf encode() {
return new LoginPluginResponse(id, true, content()); return content();
} }
@Override @Override
public int getId() { public T getContext() {
return id; return context;
} }
@Override
public boolean getSuccess() {
return success;
}
} }

View File

@ -1,10 +1,8 @@
package org.adde0109.ambassador.forge.packet; package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse; import io.netty.buffer.ByteBuf;
public interface IForgeLoginWrapperPacket { public interface IForgeLoginWrapperPacket<T extends Context> {
public LoginPluginResponse encode(); ByteBuf encode();
public int getId(); T getContext();
public boolean getSuccess();
} }

View File

@ -12,27 +12,22 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ModListReplyPacket implements IForgeLoginWrapperPacket { public class ModListReplyPacket implements IForgeLoginWrapperPacket<Context.ClientContext> {
private List<String> mods; private List<String> mods;
private Map<ChannelIdentifier, String> channels; private Map<ChannelIdentifier, String> channels;
private Map<String, String> registries; private Map<String, String> registries;
private final int id; private final Context.ClientContext context;
private final boolean success;
private ModListReplyPacket(List<String> mods, Map<ChannelIdentifier, private ModListReplyPacket(List<String> mods, Map<ChannelIdentifier,
String> channels, Map<String, String> registries, int id, boolean success) { String> channels, Map<String, String> registries, int id, boolean success) {
this.mods = mods; this.mods = mods;
this.channels = channels; this.channels = channels;
this.registries = registries; this.registries = registries;
this.id = id; this.context = Context.createContext(id, success);
this.success = success;
} }
public static ModListReplyPacket read(LoginPluginResponse msg) { public static ModListReplyPacket read(ByteBuf input, int msgID) {
ByteBuf input = msg.content();
List<String> mods = new ArrayList<>(); List<String> mods = new ArrayList<>();
int len = ProtocolUtils.readVarInt(input); int len = ProtocolUtils.readVarInt(input);
@ -50,11 +45,11 @@ public class ModListReplyPacket implements IForgeLoginWrapperPacket {
for (int x = 0; x < len; x++) for (int x = 0; x < len; x++)
registries.put(ProtocolUtils.readString(input, 32767), ProtocolUtils.readString(input, 0x100)); registries.put(ProtocolUtils.readString(input, 32767), ProtocolUtils.readString(input, 0x100));
return new ModListReplyPacket(mods, channels, registries, msg.getId(), true); return new ModListReplyPacket(mods, channels, registries, msgID, true);
} }
@Override @Override
public LoginPluginResponse encode() { public ByteBuf encode() {
ByteBuf buf = Unpooled.buffer(); ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, 2); ProtocolUtils.writeVarInt(buf, 2);
@ -79,17 +74,12 @@ public class ModListReplyPacket implements IForgeLoginWrapperPacket {
ProtocolUtils.writeVarInt(output, buf.readableBytes()); ProtocolUtils.writeVarInt(output, buf.readableBytes());
output.writeBytes(buf); output.writeBytes(buf);
return new LoginPluginResponse(id, true, output); return output;
} }
@Override @Override
public int getId() { public Context.ClientContext getContext() {
return id; return context;
}
@Override
public boolean getSuccess() {
return success;
} }
public List<String> getMods() { public List<String> getMods() {

View File

@ -26,16 +26,16 @@ public class ForgeLoginWrapperDecoder extends MessageToMessageDecoder<LoginPlugi
String channel = ProtocolUtils.readString(buf); String channel = ProtocolUtils.readString(buf);
if (!channel.equals("fml:handshake")) { if (!channel.equals("fml:handshake")) {
buf.readerIndex(originalReaderIndex); buf.readerIndex(originalReaderIndex);
out.add(new GenericForgeLoginWrapperPacket(buf.retain(), msg.getId(), true)); out.add(GenericForgeLoginWrapperPacket.create(buf.retain(), msg.getId(), true));
return; return;
} }
int length = ProtocolUtils.readVarInt(buf); int length = ProtocolUtils.readVarInt(buf);
int packetID = ProtocolUtils.readVarInt(buf); int packetID = ProtocolUtils.readVarInt(buf);
if (packetID == 2) { if (packetID == 2) {
out.add(ModListReplyPacket.read(msg)); out.add(ModListReplyPacket.read(msg.content(), msg.getId()));
} else { } else {
buf.readerIndex(originalReaderIndex); buf.readerIndex(originalReaderIndex);
out.add(new GenericForgeLoginWrapperPacket(buf.retain(), msg.getId(), true)); out.add(GenericForgeLoginWrapperPacket.create(buf.retain(), msg.getId(), true));
} }
} }

View File

@ -26,7 +26,7 @@ public class FML2CRPMResetCompleteDecoder extends ChannelInboundHandlerAdapter {
boolean success = buf.readBoolean(); boolean success = buf.readBoolean();
if (id == 98) { if (id == 98) {
try { try {
IForgeLoginWrapperPacket packet = new GenericForgeLoginWrapperPacket(Unpooled.EMPTY_BUFFER, id, success); IForgeLoginWrapperPacket packet = GenericForgeLoginWrapperPacket.create(Unpooled.EMPTY_BUFFER, id, success);
ctx.fireChannelRead(packet); ctx.fireChannelRead(packet);
} finally { } finally {
buf.release(); buf.release();

View File

@ -26,19 +26,17 @@ public class VelocityHandshakeSessionHandler extends HandshakeSessionHandler {
handshake.handle(original); handshake.handle(original);
if (connection.getType() == ConnectionTypes.VANILLA) { if (connection.getType() == ConnectionTypes.VANILLA) {
final String[] markerSplit = handshake.getServerAddress().split("\0"); final String[] markerSplit = handshake.getServerAddress().split("\0");
if (connection.getState() == StateRegistry.LOGIN && markerSplit.length > 1) { if (connection.getState() == StateRegistry.LOGIN && markerSplit.length > 1 && markerSplit[1].startsWith("FML")) {
switch (markerSplit[1]) { switch (markerSplit[1]) {
case "FML2": case "FML2":
connection.setType(ForgeConstants.ForgeFML2); connection.setType(ForgeConstants.ForgeFML2);
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new ClientPacketQueue(StateRegistry.LOGIN));
break; break;
case "FML3": case "FML3":
connection.setType(ForgeConstants.ForgeFML3); connection.setType(ForgeConstants.ForgeFML3);
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new ClientPacketQueue(StateRegistry.LOGIN));
break; break;
} }
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.SERVER_SUCCESS_LISTENER, new OutboundSuccessHolder());
connection.getChannel().pipeline().addAfter(Connections.MINECRAFT_ENCODER,ForgeConstants.PLUGIN_PACKET_QUEUE, new ClientPacketQueue(StateRegistry.LOGIN));
} }
} }
return true; return true;