Added more injection methods

This commit is contained in:
Lenni0451 2023-01-19 18:57:38 +01:00
parent 9405e88cfb
commit 982e89df3d
3 changed files with 27 additions and 8 deletions

View file

@ -114,7 +114,8 @@ jar {
manifest { manifest {
attributes( attributes(
"Main-Class": "net.raphimc.viaproxy.ViaProxy", "Main-Class": "net.raphimc.viaproxy.ViaProxy",
"Multi-Release": "true" "Multi-Release": "true",
"Launcher-Agent-Class": "net.raphimc.viaproxy.ViaProxy"
) )
} }

View file

@ -6,4 +6,4 @@ org.gradle.configureondemand=true
# Project properties # Project properties
maven_name=ViaProxy maven_name=ViaProxy
maven_group=net.raphimc maven_group=net.raphimc
maven_version=3.0.8 maven_version=3.0.9

View file

@ -28,7 +28,9 @@ import net.lenni0451.classtransform.mixinstranslator.MixinsTranslator;
import net.lenni0451.classtransform.utils.loader.EnumLoaderPriority; import net.lenni0451.classtransform.utils.loader.EnumLoaderPriority;
import net.lenni0451.classtransform.utils.loader.InjectionClassLoader; import net.lenni0451.classtransform.utils.loader.InjectionClassLoader;
import net.lenni0451.classtransform.utils.tree.IClassProvider; import net.lenni0451.classtransform.utils.tree.IClassProvider;
import net.lenni0451.reflect.Agents;
import net.lenni0451.reflect.ClassLoaders; import net.lenni0451.reflect.ClassLoaders;
import net.lenni0451.reflect.Methods;
import net.raphimc.netminecraft.constants.MCPipeline; import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.netminecraft.netty.connection.NetServer; import net.raphimc.netminecraft.netty.connection.NetServer;
import net.raphimc.viaproxy.cli.ConsoleHandler; import net.raphimc.viaproxy.cli.ConsoleHandler;
@ -46,16 +48,22 @@ import net.raphimc.viaproxy.util.logging.Logger;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.lang.instrument.Instrumentation;
public class ViaProxy { public class ViaProxy {
public static final String VERSION = "${version}"; public static final String VERSION = "${version}";
private static Instrumentation instrumentation;
public static SaveManager saveManager; public static SaveManager saveManager;
public static NetServer currentProxyServer; public static NetServer currentProxyServer;
public static ChannelGroup c2pChannels; public static ChannelGroup c2pChannels;
public static ViaProxyUI ui; public static ViaProxyUI ui;
public static void agentmain(final String args, final Instrumentation instrumentation) {
ViaProxy.instrumentation = instrumentation;
}
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
final IClassProvider classProvider = new GuavaClassPathProvider(); final IClassProvider classProvider = new GuavaClassPathProvider();
final TransformerManager transformerManager = new TransformerManager(classProvider); final TransformerManager transformerManager = new TransformerManager(classProvider);
@ -63,17 +71,27 @@ public class ViaProxy {
transformerManager.addBytecodeTransformer(new Java17ToJava8(classProvider)); transformerManager.addBytecodeTransformer(new Java17ToJava8(classProvider));
transformerManager.addTransformer("net.raphimc.viaproxy.injection.transformer.**"); transformerManager.addTransformer("net.raphimc.viaproxy.injection.transformer.**");
transformerManager.addTransformer("net.raphimc.viaproxy.injection.mixins.**"); transformerManager.addTransformer("net.raphimc.viaproxy.injection.mixins.**");
final InjectionClassLoader injectionClassLoader = new InjectionClassLoader(transformerManager, ClassLoaders.getSystemClassPath()); if (instrumentation != null) {
injectionClassLoader.addProtectedPackage("com.sun."); transformerManager.hookInstrumentation(instrumentation);
injectionClassLoader.setPriority(EnumLoaderPriority.PARENT_FIRST); injectedMain("Launcher Agent", args);
injectionClassLoader.executeMain(ViaProxy.class.getName(), "injectedMain", args); return;
}
try {
transformerManager.hookInstrumentation(Agents.getInstrumentation());
injectedMain("Runtime Agent", args);
} catch (Throwable t) {
final InjectionClassLoader injectionClassLoader = new InjectionClassLoader(transformerManager, ClassLoaders.getSystemClassPath());
injectionClassLoader.setPriority(EnumLoaderPriority.PARENT_FIRST);
Thread.currentThread().setContextClassLoader(injectionClassLoader);
Methods.invoke(null, Methods.getDeclaredMethod(injectionClassLoader.loadClass(ViaProxy.class.getName()), "injectedMain", String.class, String[].class), "Injection ClassLoader", args);
}
} }
public static void injectedMain(String[] args) throws InterruptedException { public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException {
Logger.setup(); Logger.setup();
final boolean hasUI = args.length == 0 && !GraphicsEnvironment.isHeadless(); final boolean hasUI = args.length == 0 && !GraphicsEnvironment.isHeadless();
ConsoleHandler.hookConsole(); ConsoleHandler.hookConsole();
Logger.LOGGER.info("Initializing ViaProxy v" + VERSION + "..."); Logger.LOGGER.info("Initializing ViaProxy {} v{} (Injected using {})...", hasUI ? "GUI" : "CLI", VERSION, injectionMethod);
saveManager = new SaveManager(); saveManager = new SaveManager();
setNettyParameters(); setNettyParameters();
MCPipeline.useOptimizedPipeline(); MCPipeline.useOptimizedPipeline();