ViaVersion/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityViaInjector.java

122 lines
5.6 KiB
Java
Raw Normal View History

/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
2024-01-01 12:39:45 +01:00
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.velocity.platform;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.VelocityPlugin;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.platform.ViaInjector;
2021-04-26 21:16:10 +02:00
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.util.ReflectionUtil;
import com.viaversion.viaversion.velocity.handlers.VelocityChannelInitializer;
import io.netty.channel.ChannelInitializer;
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSortedSet;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
2023-06-06 12:30:56 +02:00
import org.jetbrains.annotations.Nullable;
public class VelocityViaInjector implements ViaInjector {
2023-06-06 12:30:56 +02:00
public static final Method GET_PLAYER_INFO_FORWARDING_MODE = getPlayerInfoForwardingModeMethod();
2023-06-06 12:30:56 +02:00
private static @Nullable Method getPlayerInfoForwardingModeMethod() {
try {
2023-06-06 12:30:56 +02:00
return Class.forName("com.velocitypowered.proxy.config.VelocityConfiguration").getMethod("getPlayerInfoForwardingMode");
} catch (NoSuchMethodException | ClassNotFoundException e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Failed to get getPlayerInfoForwardingMode method from Velocity, please report this issue on our GitHub.", e);
2023-06-06 12:30:56 +02:00
return null;
}
}
private ChannelInitializer getInitializer() throws Exception {
Object connectionManager = ReflectionUtil.get(VelocityPlugin.PROXY, "cm", Object.class);
Object channelInitializerHolder = ReflectionUtil.invoke(connectionManager, "getServerChannelInitializer");
2021-03-26 12:51:38 +01:00
return (ChannelInitializer) ReflectionUtil.invoke(channelInitializerHolder, "get");
}
private ChannelInitializer getBackendInitializer() throws Exception {
Object connectionManager = ReflectionUtil.get(VelocityPlugin.PROXY, "cm", Object.class);
Object channelInitializerHolder = ReflectionUtil.invoke(connectionManager, "getBackendChannelInitializer");
return (ChannelInitializer) ReflectionUtil.invoke(channelInitializerHolder, "get");
}
@Override
public void inject() throws Exception {
2021-07-05 18:21:26 +02:00
Via.getPlatform().getLogger().info("Replacing channel initializers; you can safely ignore the following two warnings.");
Object connectionManager = ReflectionUtil.get(VelocityPlugin.PROXY, "cm", Object.class);
Object channelInitializerHolder = ReflectionUtil.invoke(connectionManager, "getServerChannelInitializer");
ChannelInitializer originalInitializer = getInitializer();
channelInitializerHolder.getClass().getMethod("set", ChannelInitializer.class)
.invoke(channelInitializerHolder, new VelocityChannelInitializer(originalInitializer, false));
Object backendInitializerHolder = ReflectionUtil.invoke(connectionManager, "getBackendChannelInitializer");
ChannelInitializer backendInitializer = getBackendInitializer();
backendInitializerHolder.getClass().getMethod("set", ChannelInitializer.class)
2021-03-26 12:51:38 +01:00
.invoke(backendInitializerHolder, new VelocityChannelInitializer(backendInitializer, true));
}
@Override
public void uninject() {
Via.getPlatform().getLogger().severe("ViaVersion cannot remove itself from Velocity without a reboot!");
}
@Override
public int getServerProtocolVersion() throws Exception {
2018-11-17 14:08:51 +01:00
return getLowestSupportedProtocolVersion();
}
2021-03-26 12:51:38 +01:00
@Override
public IntSortedSet getServerProtocolVersions() throws Exception {
int lowestSupportedProtocolVersion = getLowestSupportedProtocolVersion();
IntSortedSet set = new IntLinkedOpenHashSet();
for (com.velocitypowered.api.network.ProtocolVersion version : com.velocitypowered.api.network.ProtocolVersion.SUPPORTED_VERSIONS) {
if (version.getProtocol() >= lowestSupportedProtocolVersion) {
set.add(version.getProtocol());
}
}
return set;
}
2018-11-26 18:58:17 +01:00
public static int getLowestSupportedProtocolVersion() {
try {
2023-06-06 12:30:56 +02:00
if (GET_PLAYER_INFO_FORWARDING_MODE != null
&& ((Enum<?>) GET_PLAYER_INFO_FORWARDING_MODE.invoke(VelocityPlugin.PROXY.getConfiguration()))
2021-03-26 12:51:38 +01:00
.name().equals("MODERN")) {
return ProtocolVersion.v1_13.getVersion();
}
} catch (IllegalAccessException | InvocationTargetException ignored) {
}
2018-11-26 18:58:17 +01:00
return com.velocitypowered.api.network.ProtocolVersion.MINIMUM_VERSION.getProtocol();
}
@Override
public JsonObject getDump() {
JsonObject data = new JsonObject();
try {
data.addProperty("currentInitializer", getInitializer().getClass().getName());
} catch (Exception e) {
// Ignored
}
return data;
}
}