3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 00:50:13 +01:00

Fix ProtocolSupport compat on 1.17

Fixes #2632

Thank you md_5, very cool
Dieser Commit ist enthalten in:
kennytv 2021-07-23 10:15:59 +02:00
Ursprung d14db9396b
Commit db87774426
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
2 geänderte Dateien mit 50 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -41,11 +41,12 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor; import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.lang.reflect.Method; import java.lang.reflect.Method;
//TODO maybe clean this up a bit 👀 //TODO maybe clean this up a bit 👀
public class ClassGenerator { public final class ClassGenerator {
private static final boolean useModules = hasModuleMethod(); private static final boolean useModules = hasModuleMethod();
private static HandlerConstructor constructor = new BasicHandlerConstructor(); private static HandlerConstructor constructor = new BasicHandlerConstructor();
private static String psPackage; private static String psPackage;
@ -75,7 +76,7 @@ public class ClassGenerator {
Class encodeSuper; Class encodeSuper;
Class decodeSuper; Class decodeSuper;
if (isMultiplatformPS()) { if (isMultiplatformPS()) {
psConnectListener = makePSConnectListener(pool, shouldUseNewHandshakeVersionMethod()); psConnectListener = makePSConnectListener(pool);
return; return;
} else { } else {
String psPackage = getOldPSPackage(); String psPackage = getOldPSPackage();
@ -106,7 +107,7 @@ public class ClassGenerator {
" return new BukkitDecodeHandler(info, minecraftDecoder);\n" + " return new BukkitDecodeHandler(info, minecraftDecoder);\n" +
" }", generated)); " }", generated));
constructor = (HandlerConstructor) toClass(generated).newInstance(); constructor = (HandlerConstructor) toClass(generated).getConstructor().newInstance();
} catch (ReflectiveOperationException | CannotCompileException | NotFoundException e) { } catch (ReflectiveOperationException | CannotCompileException | NotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -195,7 +196,8 @@ public class ClassGenerator {
} }
} }
private static Class makePSConnectListener(ClassPool pool, boolean newVersionMethod) { private static Class makePSConnectListener(ClassPool pool) {
HandshakeProtocolType type = handshakeVersionMethod();
try { try {
// Reference classes // Reference classes
CtClass toExtend = pool.get("protocolsupport.api.Connection$PacketListener"); CtClass toExtend = pool.get("protocolsupport.api.Connection$PacketListener");
@ -228,11 +230,7 @@ public class ClassGenerator {
+ " if (event.getPacket() instanceof PacketHandshakingInSetProtocol) {\n" + " if (event.getPacket() instanceof PacketHandshakingInSetProtocol) {\n"
// Get protocol version. // Get protocol version.
+ " PacketHandshakingInSetProtocol packet = (PacketHandshakingInSetProtocol) event.getPacket();\n" + " PacketHandshakingInSetProtocol packet = (PacketHandshakingInSetProtocol) event.getPacket();\n"
+ (newVersionMethod ? ( + " int protoVersion = packet." + type.methodName() + "();\n"
" int protoVersion = packet.getProtocolVersion();\n"
) : (
" int protoVersion = packet.b();\n"
))
// ViaVersion has at this point already spoofed the connectionversion. (Since it is higher up the pipeline) // ViaVersion has at this point already spoofed the connectionversion. (Since it is higher up the pipeline)
// If via has put the protoVersion to the server we can spoof ProtocolSupport's version. // If via has put the protoVersion to the server we can spoof ProtocolSupport's version.
+ " if (connection.getVersion() == ProtocolVersion.MINECRAFT_FUTURE && protoVersion == com.viaversion.viaversion.api.Via.getAPI().getServerVersion().lowestSupportedVersion()) {\n" + " if (connection.getVersion() == ProtocolVersion.MINECRAFT_FUTURE && protoVersion == com.viaversion.viaversion.api.Via.getAPI().getServerVersion().lowestSupportedVersion()) {\n"
@ -251,16 +249,16 @@ public class ClassGenerator {
} }
public static void registerPSConnectListener(ViaVersionPlugin plugin) { public static void registerPSConnectListener(ViaVersionPlugin plugin) {
if (getPSConnectListener() != null) { if (psConnectListener != null) {
try { try {
Class<? extends Event> connectionOpenEvent = (Class<? extends Event>) Class.forName("protocolsupport.api.events.ConnectionOpenEvent"); Class<? extends Event> connectionOpenEvent = (Class<? extends Event>) Class.forName("protocolsupport.api.events.ConnectionOpenEvent");
Bukkit.getPluginManager().registerEvent(connectionOpenEvent, new Listener() { Bukkit.getPluginManager().registerEvent(connectionOpenEvent, new Listener() {
}, EventPriority.HIGH, new EventExecutor() { }, EventPriority.HIGH, new EventExecutor() {
@Override @Override
public void execute(Listener listener, Event event) throws EventException { public void execute(@NonNull Listener listener, @NonNull Event event) throws EventException {
try { try {
Object connection = event.getClass().getMethod("getConnection").invoke(event); Object connection = event.getClass().getMethod("getConnection").invoke(event);
Object connectListener = getPSConnectListener().getConstructor(connection.getClass()).newInstance(connection); Object connectListener = psConnectListener.getConstructor(connection.getClass()).newInstance(connection);
Method addConnectListener = connection.getClass().getMethod("addPacketListener", Class.forName("protocolsupport.api.Connection$PacketListener")); Method addConnectListener = connection.getClass().getMethod("addPacketListener", Class.forName("protocolsupport.api.Connection$PacketListener"));
addConnectListener.invoke(connection, connectListener); addConnectListener.invoke(connection, connectListener);
} catch (Exception e) { } catch (Exception e) {
@ -304,15 +302,31 @@ public class ClassGenerator {
} }
} }
public static boolean shouldUseNewHandshakeVersionMethod() { public static HandshakeProtocolType handshakeVersionMethod() {
Class<?> clazz = null;
// Check for the mapped method
try { try {
NMSUtil.nms( clazz = NMSUtil.nms(
"PacketHandshakingInSetProtocol", "PacketHandshakingInSetProtocol",
"net.minecraft.network.protocol.handshake.PacketHandshakingInSetProtocol" "net.minecraft.network.protocol.handshake.PacketHandshakingInSetProtocol"
).getMethod("getProtocolVersion"); );
return true; clazz.getMethod("getProtocolVersion");
} catch (Exception e) { return HandshakeProtocolType.MAPPED;
return false; } catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException ignored) {
}
// Check for obfusacted b/c methods
try {
if (clazz.getMethod("b").getReturnType() == int.class) {
return HandshakeProtocolType.OBFUSCATED_OLD;
} else if (clazz.getMethod("c").getReturnType() == int.class) {
return HandshakeProtocolType.OBFUSCATED_NEW;
}
throw new UnsupportedOperationException("Protocol version method not found in " + clazz.getSimpleName());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
} }
} }
@ -329,4 +343,21 @@ public class ClassGenerator {
return false; return false;
} }
} }
private enum HandshakeProtocolType {
MAPPED("getProtocolVersion"),
OBFUSCATED_OLD("b"),
OBFUSCATED_NEW("c");
private final String methodName;
HandshakeProtocolType(String methodName) {
this.methodName = methodName;
}
public String methodName() {
return methodName;
}
}
} }

Datei anzeigen

@ -127,7 +127,7 @@ public final class InventoryPackets extends ItemRewriter<Protocol1_17To1_16_4> {
PacketWrapper packet = wrapper.create(ServerboundPackets1_16_2.WINDOW_CONFIRMATION); PacketWrapper packet = wrapper.create(ServerboundPackets1_16_2.WINDOW_CONFIRMATION);
packet.write(Type.UNSIGNED_BYTE, inventoryId); packet.write(Type.UNSIGNED_BYTE, inventoryId);
packet.write(Type.SHORT, confirmationId); packet.write(Type.SHORT, confirmationId);
packet.write(Type.BYTE, (byte) 1); // Accept packet.write(Type.BOOLEAN, true); // Accept
packet.sendToServer(Protocol1_17To1_16_4.class); packet.sendToServer(Protocol1_17To1_16_4.class);
} }