1
0
mirror of https://github.com/GeyserMC/Floodgate.git synced 2025-12-21 15:59:21 +00:00

Small changes to platforms and injectors

This commit is contained in:
Tim203
2022-07-09 19:23:02 +02:00
parent 1815613be6
commit 904c584a2a
9 changed files with 97 additions and 158 deletions

View File

@@ -37,10 +37,9 @@ public interface PlatformInjector {
* Injects the server connection. This will allow various addons (like getting the Floodgate * Injects the server connection. This will allow various addons (like getting the Floodgate
* data and debug mode) to work. * data and debug mode) to work.
* *
* @return true if the connection has successfully been injected * @throws Exception if the platform couldn't be injected
* @throws Exception if something went wrong while injecting the server connection
*/ */
boolean inject() throws Exception; void inject() throws Exception;
/** /**
* Some platforms may not be able to remove their injection process. If so, this method will * Some platforms may not be able to remove their injection process. If so, this method will
@@ -56,10 +55,9 @@ public interface PlatformInjector {
* Removes the injection from the server. Please note that this function should only be used * Removes the injection from the server. Please note that this function should only be used
* internally (on plugin shutdown). This method will also remove every added addon. * internally (on plugin shutdown). This method will also remove every added addon.
* *
* @return true if the injection has successfully been removed * @throws Exception if the platform injection could not be removed
* @throws Exception if something went wrong while removing the injection
*/ */
boolean removeInjection() throws Exception; void removeInjection() throws Exception;
/** /**
* If the server connection is currently injected. * If the server connection is currently injected.

View File

@@ -49,8 +49,7 @@ public final class BungeeInjector extends CommonPlatformInjector {
@Getter private boolean injected; @Getter private boolean injected;
@Override @Override
public boolean inject() { public void inject() {
try {
// Can everyone just switch to Velocity please :) // Can everyone just switch to Velocity please :)
Field framePrepender = ReflectionUtils.getField(PipelineUtils.class, "framePrepender"); Field framePrepender = ReflectionUtils.getField(PipelineUtils.class, "framePrepender");
@@ -65,11 +64,6 @@ public final class BungeeInjector extends CommonPlatformInjector {
BungeeReflectionUtils.setFieldValue(null, framePrepender, customPrepender); BungeeReflectionUtils.setFieldValue(null, framePrepender, customPrepender);
injected = true; injected = true;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} }
@Override @Override
@@ -78,9 +72,9 @@ public final class BungeeInjector extends CommonPlatformInjector {
} }
@Override @Override
public boolean removeInjection() { public void removeInjection() {
logger.error("Floodgate cannot remove itself from Bungee without a reboot"); throw new IllegalStateException(
return false; "Floodgate cannot remove itself from Bungee without a reboot");
} }
void injectClient(Channel channel, boolean clientToProxy) { void injectClient(Channel channel, boolean clientToProxy) {

View File

@@ -64,20 +64,15 @@ public class FloodgatePlatform {
guice.getInstance(NewsChecker.class).start(); guice.getInstance(NewsChecker.class).start();
} }
public boolean enable(Module... postInitializeModules) { public void enable(Module... postInitializeModules) throws RuntimeException {
if (injector == null) { if (injector == null) {
logger.error("Failed to find the platform injector!"); throw new RuntimeException("Failed to find the platform injector!");
return false;
} }
try { try {
if (!injector.inject()) { injector.inject();
logger.error("Failed to inject the packet listener!");
return false;
}
} catch (Exception exception) { } catch (Exception exception) {
logger.error("Failed to inject the packet listener!", exception); throw new RuntimeException("Failed to inject the packet listener!", exception);
return false;
} }
this.guice = guice.createChildInjector(new PostInitializeModule(postInitializeModules)); this.guice = guice.createChildInjector(new PostInitializeModule(postInitializeModules));
@@ -85,8 +80,6 @@ public class FloodgatePlatform {
PrefixCheckTask.checkAndExecuteDelayed(config, logger); PrefixCheckTask.checkAndExecuteDelayed(config, logger);
guice.getInstance(Metrics.class); guice.getInstance(Metrics.class);
return true;
} }
public void disable() { public void disable() {
@@ -94,11 +87,9 @@ public class FloodgatePlatform {
if (injector != null && injector.canRemoveInjection()) { if (injector != null && injector.canRemoveInjection()) {
try { try {
if (!injector.removeInjection()) { injector.removeInjection();
logger.error("Failed to remove the injection!");
}
} catch (Exception exception) { } catch (Exception exception) {
logger.error("Failed to remove the injection!", exception); throw new RuntimeException("Failed to remove the injection!", exception);
} }
} }
} }

View File

@@ -424,7 +424,7 @@ public final class ReflectionUtils {
} }
@Nullable @Nullable
public static Method getMethod( public static Method getMethodThatReturns(
Class<?> clazz, Class<?> clazz,
Class<?> returnType, Class<?> returnType,
boolean declared, boolean declared,

View File

@@ -1,45 +0,0 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Floodgate
*/
package org.geysermc.floodgate;
import com.google.inject.Inject;
import com.google.inject.Module;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public final class SpigotPlatform extends FloodgatePlatform {
@Inject private JavaPlugin plugin;
@Override
public boolean enable(Module... postInitializeModules) {
boolean success = super.enable(postInitializeModules);
if (!success) {
Bukkit.getPluginManager().disablePlugin(plugin);
return false;
}
return true;
}
}

View File

@@ -27,6 +27,7 @@ package org.geysermc.floodgate;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.geysermc.floodgate.api.handshake.HandshakeHandlers; import org.geysermc.floodgate.api.handshake.HandshakeHandlers;
import org.geysermc.floodgate.api.logger.FloodgateLogger; import org.geysermc.floodgate.api.logger.FloodgateLogger;
@@ -43,7 +44,7 @@ import org.geysermc.floodgate.util.SpigotProtocolSupportHandler;
import org.geysermc.floodgate.util.SpigotProtocolSupportListener; import org.geysermc.floodgate.util.SpigotProtocolSupportListener;
public final class SpigotPlugin extends JavaPlugin { public final class SpigotPlugin extends JavaPlugin {
private SpigotPlatform platform; private FloodgatePlatform platform;
private Injector injector; private Injector injector;
@Override @Override
@@ -54,7 +55,7 @@ public final class SpigotPlugin extends JavaPlugin {
new SpigotPlatformModule(this) new SpigotPlatformModule(this)
); );
platform = injector.getInstance(SpigotPlatform.class); platform = injector.getInstance(FloodgatePlatform.class);
long endCtm = System.currentTimeMillis(); long endCtm = System.currentTimeMillis();
injector.getInstance(FloodgateLogger.class) injector.getInstance(FloodgateLogger.class)
@@ -66,14 +67,18 @@ public final class SpigotPlugin extends JavaPlugin {
boolean usePaperListener = ReflectionUtils.getClassSilently( boolean usePaperListener = ReflectionUtils.getClassSilently(
"com.destroystokyo.paper.event.profile.PreFillProfileEvent") != null; "com.destroystokyo.paper.event.profile.PreFillProfileEvent") != null;
try {
platform.enable( platform.enable(
new SpigotCommandModule(this), new SpigotCommandModule(this),
new SpigotAddonModule(), new SpigotAddonModule(),
new PluginMessageModule(), new PluginMessageModule(),
(usePaperListener ? new PaperListenerModule() : new SpigotListenerModule()) (usePaperListener ? new PaperListenerModule() : new SpigotListenerModule())
); );
} catch (Exception exception) {
Bukkit.getPluginManager().disablePlugin(this);
throw exception;
}
//todo add proper support for disabling things on shutdown and enabling this on enable
injector.getInstance(HandshakeHandlers.class) injector.getInstance(HandshakeHandlers.class)
.addHandshakeHandler(injector.getInstance(SpigotHandshakeHandler.class)); .addHandshakeHandler(injector.getInstance(SpigotHandshakeHandler.class));

View File

@@ -31,7 +31,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@@ -51,12 +50,16 @@ public final class SpigotInjector extends CommonPlatformInjector {
@Override @Override
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter") @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public boolean inject() throws Exception { public void inject() throws Exception {
if (isInjected()) { if (isInjected()) {
return true; return;
}
Object serverConnection = getServerConnection();
if (serverConnection == null) {
throw new RuntimeException("Unable to find server connection");
} }
if (getServerConnection() != null) {
for (Field field : serverConnection.getClass().getDeclaredFields()) { for (Field field : serverConnection.getClass().getDeclaredFields()) {
if (field.getType() == List.class) { if (field.getType() == List.class) {
field.setAccessible(true); field.setAccessible(true);
@@ -94,12 +97,10 @@ public final class SpigotInjector extends CommonPlatformInjector {
field.set(serverConnection, newList); field.set(serverConnection, newList);
injected = true; injected = true;
return true; return;
} }
} }
} }
return false;
}
public void injectClient(ChannelFuture future) { public void injectClient(ChannelFuture future) {
future.channel().pipeline().addFirst("floodgate-init", new ChannelInboundHandlerAdapter() { future.channel().pipeline().addFirst("floodgate-init", new ChannelInboundHandlerAdapter() {
@@ -120,9 +121,9 @@ public final class SpigotInjector extends CommonPlatformInjector {
} }
@Override @Override
public boolean removeInjection() throws Exception { public void removeInjection() {
if (!isInjected()) { if (!isInjected()) {
return true; return;
} }
// remove injection from clients // remove injection from clients
@@ -146,10 +147,9 @@ public final class SpigotInjector extends CommonPlatformInjector {
} }
injected = false; injected = false;
return true;
} }
public Object getServerConnection() throws IllegalAccessException, InvocationTargetException { public Object getServerConnection() {
if (serverConnection != null) { if (serverConnection != null) {
return serverConnection; return serverConnection;
} }
@@ -158,14 +158,11 @@ public final class SpigotInjector extends CommonPlatformInjector {
// method by CraftBukkit to get the instance of the MinecraftServer // method by CraftBukkit to get the instance of the MinecraftServer
Object minecraftServerInstance = ReflectionUtils.invokeStatic(minecraftServer, "getServer"); Object minecraftServerInstance = ReflectionUtils.invokeStatic(minecraftServer, "getServer");
for (Method method : minecraftServer.getDeclaredMethods()) { Method method = ReflectionUtils.getMethodThatReturns(
if (ClassNames.SERVER_CONNECTION.equals(method.getReturnType())) { minecraftServer, ClassNames.SERVER_CONNECTION, true
// making sure that it's a getter );
if (method.getParameterTypes().length == 0) {
serverConnection = method.invoke(minecraftServerInstance); serverConnection = ReflectionUtils.invoke(minecraftServerInstance, method);
}
}
}
return serverConnection; return serverConnection;
} }

View File

@@ -36,21 +36,19 @@ import io.netty.channel.ChannelInitializer;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.geysermc.floodgate.api.logger.FloodgateLogger;
import org.geysermc.floodgate.inject.CommonPlatformInjector; import org.geysermc.floodgate.inject.CommonPlatformInjector;
@RequiredArgsConstructor @RequiredArgsConstructor
public final class VelocityInjector extends CommonPlatformInjector { public final class VelocityInjector extends CommonPlatformInjector {
private final ProxyServer server; private final ProxyServer server;
private final FloodgateLogger logger;
@Getter private boolean injected; @Getter private boolean injected;
@Override @Override
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public boolean inject() { public void inject() {
if (isInjected()) { if (isInjected()) {
return true; return;
} }
Object connectionManager = getValue(server, "cm"); Object connectionManager = getValue(server, "cm");
@@ -72,7 +70,8 @@ public final class VelocityInjector extends CommonPlatformInjector {
Method backendSetter = getMethod(backendInitializerHolder, "set", ChannelInitializer.class); Method backendSetter = getMethod(backendInitializerHolder, "set", ChannelInitializer.class);
invoke(backendInitializerHolder, backendSetter, invoke(backendInitializerHolder, backendSetter,
new VelocityChannelInitializer(this, backendInitializer, true)); new VelocityChannelInitializer(this, backendInitializer, true));
return injected = true;
injected = true;
} }
@Override @Override
@@ -81,9 +80,9 @@ public final class VelocityInjector extends CommonPlatformInjector {
} }
@Override @Override
public boolean removeInjection() { public void removeInjection() {
logger.error("Floodgate cannot remove itself from Velocity without a reboot"); throw new IllegalStateException(
return false; "Floodgate cannot remove itself from Velocity without a reboot");
} }
@RequiredArgsConstructor @RequiredArgsConstructor

View File

@@ -124,8 +124,8 @@ public final class VelocityPlatformModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
public CommonPlatformInjector platformInjector(ProxyServer server, FloodgateLogger logger) { public CommonPlatformInjector platformInjector(ProxyServer server) {
return new VelocityInjector(server, logger); return new VelocityInjector(server);
} }
@Provides @Provides