Now understands forge packets

This commit is contained in:
Adrian Bergqvist 2023-11-27 20:11:36 +01:00
parent c3ae255be6
commit 161700c7e6
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
8 changed files with 101 additions and 27 deletions

View File

@ -1,5 +1,6 @@
package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@ -12,7 +13,11 @@ public class ACKPacket implements IForgeLoginWrapperPacket<Context.ClientContext
}
@Override
public ByteBuf encode() {
return Unpooled.EMPTY_BUFFER;
ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, 99);
return buf;
}
@Override

View File

@ -1,18 +1,39 @@
package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class ConfigDataPacket implements IForgeLoginWrapperPacket<Context> {
private final String fileName;
private final byte[] fileData;
private final Context context;
public ConfigDataPacket(Context context) {
public ConfigDataPacket(String fileName, byte[] fileData, Context context) {
this.fileName = fileName;
this.fileData = fileData;
this.context = context;
}
public static ConfigDataPacket read(ByteBuf input, Context context, boolean FML3) {
String registryName = ProtocolUtils.readString(input);
byte[] snapshot = ProtocolUtils.readByteArray(input);
return new ConfigDataPacket(registryName, snapshot, context);
}
@Override
public ByteBuf encode() {
return null;
ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, 4);
ProtocolUtils.writeString(buf, fileName);
ProtocolUtils.writeByteArray(buf, fileData);
return buf;
}
@Override

View File

@ -1,14 +1,12 @@
package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder;
import io.netty.buffer.ByteBuf;
public class GenericForgeLoginWrapperPacket<T extends Context> extends DeferredByteBufHolder implements IForgeLoginWrapperPacket<T> {
private final T context;
private GenericForgeLoginWrapperPacket(ByteBuf input, T context) {
GenericForgeLoginWrapperPacket(ByteBuf input, T context) {
super(input);
this.context = context;
}

View File

@ -0,0 +1,9 @@
package org.adde0109.ambassador.forge.packet;
import io.netty.buffer.ByteBuf;
public class ModDataPacket extends GenericForgeLoginWrapperPacket<Context> {
ModDataPacket(ByteBuf input, Context context) {
super(input, context);
}
}

View File

@ -79,12 +79,7 @@ public class ModListPacket implements IForgeLoginWrapperPacket<Context> {
dataPackRegistries.forEach(k -> ProtocolUtils.writeString(buf, k));
}
ByteBuf output = Unpooled.buffer();
ProtocolUtils.writeString(output, "fml:handshake");
ProtocolUtils.writeVarInt(output, buf.readableBytes());
output.writeBytes(buf);
return output;
return buf;
}
@Override

View File

@ -69,12 +69,7 @@ public class ModListReplyPacket implements IForgeLoginWrapperPacket<Context.Clie
ProtocolUtils.writeString(buf, v);
});
ByteBuf output = Unpooled.buffer();
ProtocolUtils.writeString(output, "fml:handshake");
ProtocolUtils.writeVarInt(output, buf.readableBytes());
output.writeBytes(buf);
return output;
return buf;
}
@Override

View File

@ -1,17 +1,47 @@
package org.adde0109.ambassador.forge.packet;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class RegistryPacket implements IForgeLoginWrapperPacket<Context> {
private final String registryName;
private final byte[] snapshot;
private final Context context;
public RegistryPacket(Context context) {
public RegistryPacket(String registryName, byte[] snapshot, Context context) {
this.registryName = registryName;
this.snapshot = snapshot;
this.context = context;
}
public static RegistryPacket read(ByteBuf input, Context context, boolean FML3) {
String registryName = ProtocolUtils.readString(input);
byte[] snapshot = null;
if (input.readBoolean()) {
snapshot = new byte[input.readableBytes()];
input.readBytes(snapshot);
}
return new RegistryPacket(registryName, snapshot, context);
}
@Override
public ByteBuf encode() {
return null;
ByteBuf buf = Unpooled.buffer();
ProtocolUtils.writeVarInt(buf, 3);
ProtocolUtils.writeString(buf, registryName);
if (snapshot != null) {
buf.writeBoolean(true);
buf.writeBytes(snapshot);
} else {
buf.writeBoolean(false);
}
return buf;
}
@Override

View File

@ -5,6 +5,7 @@ import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.MessageToMessageCodec;
@ -29,7 +30,7 @@ public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBu
Context context;
if (in instanceof LoginPluginMessage msg && msg.getChannel().equals("fml:loginwrapper")) {
context = Context.createContext(msg.getId());
} else if (in instanceof LoginPluginResponse msg && loginWrapperIDs.remove(msg.getId()) != null) {
} else if (in instanceof LoginPluginResponse msg && loginWrapperIDs.remove(Integer.valueOf(msg.getId()))) {
context = Context.createContext(msg.getId(), msg.isSuccess());
} else {
return;
@ -50,6 +51,7 @@ public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBu
break;
case 99:
out.add(new ACKPacket(clientContext));
break;
default:
throw new DecoderException();
}
@ -59,11 +61,17 @@ public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBu
out.add(ModListPacket.read(buf, context, FML3));
break;
case 3:
out.add(new RegistryPacket(context));
out.add(RegistryPacket.read(buf, context, FML3));
break;
case 4:
out.add(new ConfigDataPacket(context));
out.add(ConfigDataPacket.read(buf, context, FML3));
break;
case 5:
if (FML3) {
buf.readerIndex(originalReaderIndex);
out.add(ModDataPacket.create(buf.retain(), context));
break;
}
default:
throw new DecoderException();
}
@ -77,11 +85,24 @@ public class ForgeLoginWrapperCodec extends MessageToMessageCodec<DeferredByteBu
@Override
protected void encode(ChannelHandlerContext ctx, IForgeLoginWrapperPacket<?> msg, List<Object> out) throws Exception {
if (msg.getContext() instanceof Context.ClientContext clientContext) {
out.add(new LoginPluginResponse(clientContext.getResponseID(), clientContext.success(), msg.encode()));
ByteBuf wrapped;
if (msg instanceof GenericForgeLoginWrapperPacket<?>) {
wrapped = msg.encode();
} else {
out.add(new LoginPluginMessage(msg.getContext().getResponseID(), "fml:loginwrapper", msg.encode()));
this.loginWrapperIDs.add(msg.getContext().getResponseID());
wrapped = Unpooled.buffer();
ByteBuf encoded = msg.encode();
ProtocolUtils.writeString(wrapped, "fml:handshake");
ProtocolUtils.writeVarInt(wrapped, encoded.readableBytes());
wrapped.writeBytes(encoded);
}
if (msg.getContext() instanceof Context.ClientContext clientContext) {
out.add(new LoginPluginResponse(clientContext.getResponseID(), clientContext.success(), wrapped));
} else {
out.add(new LoginPluginMessage(msg.getContext().getResponseID(), "fml:loginwrapper", wrapped));
if (!(msg instanceof ModDataPacket)) {
this.loginWrapperIDs.add(msg.getContext().getResponseID());
}
}
}
}