2021-03-22 23:06:40 +01:00
/ *
* 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
2021-03-22 23:06:40 +01:00
*
* 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/>.
* /
2021-04-26 20:52:34 +02:00
package com.viaversion.viaversion.velocity.platform ;
2018-09-30 15:30:34 +02:00
2019-03-18 12:30:02 +01:00
import com.google.gson.JsonObject ;
2021-04-26 20:52:34 +02:00
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 ;
2021-04-26 20:52:34 +02:00
import com.viaversion.viaversion.util.ReflectionUtil ;
import com.viaversion.viaversion.velocity.handlers.VelocityChannelInitializer ;
2021-04-27 13:41:39 +02:00
import io.netty.channel.ChannelInitializer ;
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet ;
import it.unimi.dsi.fastutil.ints.IntSortedSet ;
2019-02-15 16:51:54 +01:00
import java.lang.reflect.InvocationTargetException ;
import java.lang.reflect.Method ;
2024-03-09 13:57:41 +01:00
import java.util.logging.Level ;
2023-06-06 12:30:56 +02:00
import org.jetbrains.annotations.Nullable ;
2019-02-15 16:51:54 +01:00
2018-09-30 15:30:34 +02:00
public class VelocityViaInjector implements ViaInjector {
2023-06-06 12:30:56 +02:00
public static final Method GET_PLAYER_INFO_FORWARDING_MODE = getPlayerInfoForwardingModeMethod ( ) ;
2019-02-15 16:51:54 +01:00
2023-06-06 12:30:56 +02:00
private static @Nullable Method getPlayerInfoForwardingModeMethod ( ) {
2019-02-15 16:51:54 +01:00
try {
2023-06-06 12:30:56 +02:00
return Class . forName ( " com.velocitypowered.proxy.config.VelocityConfiguration " ) . getMethod ( " getPlayerInfoForwardingMode " ) ;
2019-02-15 16:51:54 +01:00
} catch ( NoSuchMethodException | ClassNotFoundException e ) {
2024-03-09 13:57:41 +01:00
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 ;
2019-02-15 16:51:54 +01:00
}
}
2019-03-18 12:30:02 +01:00
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 " ) ;
2019-03-18 12:30:02 +01:00
}
2020-04-13 17:59:09 +02:00
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 " ) ;
}
2018-09-30 15:30:34 +02:00
@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. " ) ;
2018-09-30 15:30:34 +02:00
Object connectionManager = ReflectionUtil . get ( VelocityPlugin . PROXY , " cm " , Object . class ) ;
Object channelInitializerHolder = ReflectionUtil . invoke ( connectionManager , " getServerChannelInitializer " ) ;
2019-03-18 12:30:02 +01:00
ChannelInitializer originalInitializer = getInitializer ( ) ;
2018-09-30 15:30:34 +02:00
channelInitializerHolder . getClass ( ) . getMethod ( " set " , ChannelInitializer . class )
2020-04-13 17:59:09 +02:00
. 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 ) ) ;
2018-09-30 15:30:34 +02:00
}
@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 ( ) {
2019-02-15 16:51:54 +01:00
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 ( ) ;
}
2019-02-15 16:51:54 +01:00
} catch ( IllegalAccessException | InvocationTargetException ignored ) {
}
2018-11-26 18:58:17 +01:00
return com . velocitypowered . api . network . ProtocolVersion . MINIMUM_VERSION . getProtocol ( ) ;
2018-09-30 15:30:34 +02:00
}
2019-03-18 12:30:02 +01:00
@Override
public JsonObject getDump ( ) {
JsonObject data = new JsonObject ( ) ;
try {
data . addProperty ( " currentInitializer " , getInitializer ( ) . getClass ( ) . getName ( ) ) ;
} catch ( Exception e ) {
// Ignored
}
return data ;
}
2018-09-30 15:30:34 +02:00
}