aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/modernjava
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/moe/nea/modernjava')
-rw-r--r--src/main/java/moe/nea/modernjava/launch/FCPEntryPoint.java65
-rw-r--r--src/main/java/moe/nea/modernjava/launch/FCPMixinAwareTweaker.java28
-rw-r--r--src/main/java/moe/nea/modernjava/launch/FCPRelauncher.java102
-rw-r--r--src/main/java/moe/nea/modernjava/launch/WellKnownBlackboard.java17
-rw-r--r--src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java31
-rw-r--r--src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java33
6 files changed, 276 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/modernjava/launch/FCPEntryPoint.java b/src/main/java/moe/nea/modernjava/launch/FCPEntryPoint.java
new file mode 100644
index 0000000..3c32931
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/FCPEntryPoint.java
@@ -0,0 +1,65 @@
+package moe.nea.modernjava.launch;
+
+import moe.nea.modernjava.launch.util.ClassLoaderManipulations;
+import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+
+
+@IFMLLoadingPlugin.Name("ModernJavaRelauncher")
+public class FCPEntryPoint implements IFMLLoadingPlugin {
+ static URI fileUri;
+
+ static {
+ try {
+ fileUri = new URI(FCPEntryPoint.class.getProtectionDomain().getCodeSource().getLocation().toString().split("!")[0]);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ if (System.getProperty("modernjava.hasrelaunched") == null) {
+ try {
+ Class.forName("moe.nea.modernjava.launch.FCPRelauncher").getMethod("relaunch").invoke(null);
+ } catch (Throwable t) {
+ System.out.println("Failed to relaunch");
+ t.printStackTrace();
+ }
+ } else {
+ try {
+ ClassLoaderManipulations.addToParentClassLoader(fileUri.toURL());
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ List<String> tweakClasses = WellKnownBlackboard.tweakerNames();
+ tweakClasses.add(FCPMixinAwareTweaker.class.getName());
+ }
+ }
+
+
+ @Override
+ public String[] getASMTransformerClass() {
+ return new String[]{
+ };
+ }
+
+ @Override
+ public String getModContainerClass() {
+ return null;
+ }
+
+ @Override
+ public String getSetupClass() {
+ return null;
+ }
+
+ @Override
+ public void injectData(Map<String, Object> map) {
+
+ }
+
+ @Override
+ public String getAccessTransformerClass() {
+ return null;
+ }
+}
diff --git a/src/main/java/moe/nea/modernjava/launch/FCPMixinAwareTweaker.java b/src/main/java/moe/nea/modernjava/launch/FCPMixinAwareTweaker.java
new file mode 100644
index 0000000..585e943
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/FCPMixinAwareTweaker.java
@@ -0,0 +1,28 @@
+package moe.nea.modernjava.launch;
+
+import net.minecraft.launchwrapper.ITweaker;
+import net.minecraft.launchwrapper.LaunchClassLoader;
+
+import java.io.File;
+import java.util.List;
+
+public class FCPMixinAwareTweaker implements ITweaker {
+ @Override
+ public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) {
+ }
+
+ @Override
+ public void injectIntoClassLoader(LaunchClassLoader classLoader) {
+ classLoader.registerTransformer("moe.nea.modernjava.launch.transform.TransObjectHolderRef");
+ }
+
+ @Override
+ public String getLaunchTarget() {
+ return null;
+ }
+
+ @Override
+ public String[] getLaunchArguments() {
+ return new String[0];
+ }
+}
diff --git a/src/main/java/moe/nea/modernjava/launch/FCPRelauncher.java b/src/main/java/moe/nea/modernjava/launch/FCPRelauncher.java
new file mode 100644
index 0000000..ec27694
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/FCPRelauncher.java
@@ -0,0 +1,102 @@
+package moe.nea.modernjava.launch;
+
+import net.minecraft.launchwrapper.Launch;
+import net.minecraftforge.fml.common.launcher.FMLTweaker;
+import net.minecraftforge.fml.nea.moe.modernjava.IAMFML;
+
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class FCPRelauncher {
+ public static void relaunch() {
+
+ List<String> originalArgs = new ArrayList<>();
+
+ // Provided by FML
+ // This is highly processed so there might be some arguments that become lost, but almost everything should be in here.
+ // Namely non -- arguments get lost. I don't know any of these arguments that the vanilla launcher uses, so it should be fine?
+ // Also some tweakers are missing. But we can fix this.
+ Map<String, String> launchArgs = WellKnownBlackboard.launchArgs();
+ if ("UnknownFMLProfile".equals(launchArgs.get("--version"))) {
+ launchArgs.remove("--version");
+ }
+ for (Map.Entry<String, String> argument : launchArgs.entrySet()) {
+ originalArgs.add(argument.getKey());
+ originalArgs.add(argument.getValue());
+ }
+
+
+ originalArgs.add("--tweakClass");
+ originalArgs.add(FMLTweaker.class.getName());
+
+ System.out.println("Reconstructed original minecraft arguments: " + originalArgs);
+
+ String modernJavaPath = "/home/nea/.sdkman/candidates/java/16.0.2-tem/bin/java";
+
+
+ String thisJarFile = FCPEntryPoint.class.getProtectionDomain().getCodeSource().getLocation().getFile();
+ thisJarFile = "/home/nea/src/ModernJavaLauncher/target/build/libs/target.jar";
+ System.out.println("Found modern java jar at: " + thisJarFile);
+
+ System.out.println("Located modern minecraft at: " + modernJavaPath);
+
+ ProcessBuilder processBuilder = new ProcessBuilder();
+ processBuilder.inheritIO();
+ processBuilder.directory(null);
+
+
+ List<String> moduleOpens = new ArrayList<>();
+ moduleOpens.add("java.base/java.util=ALL-UNNAMED");
+ moduleOpens.add("java.base/jdk.internal.loader=ALL-UNNAMED");
+ moduleOpens.add("java.base/java.lang.reflect=ALL-UNNAMED");
+
+
+ List<String> fullCommandLine = new ArrayList<>();
+ fullCommandLine.add(modernJavaPath);
+ fullCommandLine.addAll(ManagementFactory.getRuntimeMXBean().getInputArguments());
+ fullCommandLine.add("-Dmodernjava.hasrelaunched=true");
+ fullCommandLine.add("-Dmodernjava.relaunchclasspath=" + thisJarFile + File.pathSeparator + ManagementFactory.getRuntimeMXBean().getClassPath());
+ fullCommandLine.add("--illegal-access=permit");
+ for (String open : moduleOpens) {
+ fullCommandLine.add("--add-opens");
+ fullCommandLine.add(open);
+ }
+ fullCommandLine.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005");
+ fullCommandLine.add("-javaagent:" + thisJarFile);
+ fullCommandLine.add("--add-modules=ALL-MODULE-PATH,ALL-SYSTEM,ALL-DEFAULT,java.sql");
+ fullCommandLine.add("-Xbootclasspath/a:" + thisJarFile);
+ fullCommandLine.add("moe.nea.modernjava.target.RelaunchEntryPoint");
+ fullCommandLine.addAll(originalArgs);
+
+ System.out.println("Full relaunch commandline: " + fullCommandLine);
+
+
+ processBuilder.command(fullCommandLine);
+ int exitCode;
+ try {
+ try {
+ Process process = processBuilder.start();
+ exitCode = process.waitFor();
+ } finally {
+ try {
+ new FileOutputStream(FileDescriptor.out).flush();
+ new FileOutputStream(FileDescriptor.err).flush();
+ } catch (IOException ignored) {
+ }
+ }
+ } catch (IOException | InterruptedException e) {
+ throw new RuntimeException("Failed to relaunch with old java version", e);
+ }
+
+
+ System.out.println("Exiting outer relaunch layer");
+ IAMFML.shutdown(exitCode);
+ }
+
+}
diff --git a/src/main/java/moe/nea/modernjava/launch/WellKnownBlackboard.java b/src/main/java/moe/nea/modernjava/launch/WellKnownBlackboard.java
new file mode 100644
index 0000000..ee60b2a
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/WellKnownBlackboard.java
@@ -0,0 +1,17 @@
+package moe.nea.modernjava.launch;
+
+import net.minecraft.launchwrapper.Launch;
+
+import java.util.List;
+import java.util.Map;
+
+@SuppressWarnings("unchecked")
+public class WellKnownBlackboard {
+ public static List<String> tweakerNames() {
+ return (List<String>) Launch.blackboard.get("TweakClasses");
+ }
+
+ public static Map<String, String> launchArgs() {
+ return (Map<String, String>) Launch.blackboard.get("launchArgs");
+ }
+}
diff --git a/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java b/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java
new file mode 100644
index 0000000..09f2368
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java
@@ -0,0 +1,31 @@
+package moe.nea.modernjava.launch.util;
+
+import net.minecraft.launchwrapper.Launch;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ClassLoaderManipulations {
+
+ public static void addToParentClassLoader(File file) {
+ try {
+ addToParentClassLoader(file.toURI().toURL());
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void addToParentClassLoader(URL file) {
+ try {
+ Launch.classLoader.addURL(file);
+ ClassLoader parentClassLoader = Launch.classLoader.getClass().getClassLoader();
+ Method addUrl = parentClassLoader.getClass().getDeclaredMethod("addURL", URL.class);
+ addUrl.setAccessible(true);
+ addUrl.invoke(parentClassLoader, file);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java b/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java
new file mode 100644
index 0000000..abc2364
--- /dev/null
+++ b/src/main/java/moe/nea/modernjava/launch/util/ReflectionUtils.java
@@ -0,0 +1,33 @@
+package moe.nea.modernjava.launch.util;
+
+import sun.misc.Unsafe;
+
+import java.lang.reflect.Field;
+
+public class ReflectionUtils {
+ private static Unsafe unsafe;
+
+ static {
+ try {
+ final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
+ unsafeField.setAccessible(true);
+ unsafe = (Unsafe) unsafeField.get(null);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ public static void makeFieldWritable(Field f) {
+ String s = "Doing nothing. We will use unsafe to set the value instead, if possible";
+ }
+
+ public static void doFieldWrite(Field field, Object object) throws IllegalAccessException {
+ if (unsafe == null) {
+ field.set(null, object);
+ } else {
+ Object o = unsafe.staticFieldBase(field);
+ long l = unsafe.staticFieldOffset(field);
+ unsafe.putObject(o, l, object);
+ }
+ }
+}