diff options
author | Linnea Gräf <nea@nea.moe> | 2024-06-20 02:48:38 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-06-20 02:48:38 +0200 |
commit | 5593488744dc5aa994b5ab2806ed56d7bc627368 (patch) | |
tree | 9d8701474adbd0ce3d51c3f2b92e7ed5724b02a7 /src/main/java/moe/nea/modernjava/launch/relaunch | |
parent | cb08a83e7a0f82f2459da7cb93b8c327b29d9838 (diff) | |
download | ModernJavaLauncher-5593488744dc5aa994b5ab2806ed56d7bc627368.tar.gz ModernJavaLauncher-5593488744dc5aa994b5ab2806ed56d7bc627368.tar.bz2 ModernJavaLauncher-5593488744dc5aa994b5ab2806ed56d7bc627368.zip |
Diffstat (limited to 'src/main/java/moe/nea/modernjava/launch/relaunch')
-rw-r--r-- | src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java | 273 | ||||
-rw-r--r-- | src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java | 248 |
2 files changed, 282 insertions, 239 deletions
diff --git a/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java b/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java index 3e13806..70002b5 100644 --- a/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java +++ b/src/main/java/moe/nea/modernjava/launch/relaunch/FCPRelauncher.java @@ -1,5 +1,6 @@ package moe.nea.modernjava.launch.relaunch; +import moe.nea.modernjava.launch.FCPEntryPoint; import moe.nea.modernjava.launch.util.PropertyNames; import moe.nea.modernjava.launch.util.TextIoUtils; import moe.nea.modernjava.launch.util.WellKnownBlackboard; @@ -14,126 +15,166 @@ import java.io.OutputStream; import java.lang.management.ManagementFactory; import java.nio.file.Files; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; public class FCPRelauncher { - /** - * @return the original arguments, as passed to the main method. - */ - public static List<String> getOriginalArguments() { - 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); - return originalArgs; - } - - public static File findJavaLauncher() { - JavaScanner javaScanner = new JavaScanner(); - javaScanner.scanDefaultPaths(); - javaScanner.prettyPrint(); - JavaScanner.LocalJavaVersion candidate = javaScanner.findCandidate(); - if (candidate == null) { - System.err.println("Looks like we couldn't find a java candidate. Either install one, or if you have one" + - " and we just cannot find it, specify -D" + PropertyNames.JAVA_SCAN_PATH + "=<java home here>." + - " We need a Java 16 JDK. Exiting now."); - IAMFML.shutdown(1); - throw new RuntimeException(); - } - System.out.println("Choosing Java Candidate:\n" + candidate.prettyPrint()); - return candidate.getJavaBinary(); - } - - public static File findAgentJar() { - try { - File file = File.createTempFile("mjr-agent", ".jar"); - try (InputStream is = FCPRelauncher.class.getResourceAsStream("/agent/agent.jar"); - OutputStream os = Files.newOutputStream(file.toPath())) { - assert is != null; - IOUtils.copy(is, os); - } - return file; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static void relaunch() { - - List<String> originalArgs = getOriginalArguments(); - - File modernJavaPath = findJavaLauncher(); - System.out.println("Located modern minecraft at: " + modernJavaPath); - - File agentFile = findAgentJar(); - System.out.println("Located agent jar at: " + agentFile); - - - 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.getAbsolutePath()); - fullCommandLine.addAll(ManagementFactory.getRuntimeMXBean().getInputArguments()); - fullCommandLine.add("-D" + PropertyNames.HAS_RELAUNCHED + "=true"); - fullCommandLine.add("-D" + PropertyNames.RELAUNCH_CLASSPATH + "=" + agentFile.getAbsolutePath() + File.pathSeparator + ManagementFactory.getRuntimeMXBean().getClassPath()); - fullCommandLine.add("--illegal-access=permit"); - for (String open : moduleOpens) { - fullCommandLine.add("--add-opens"); - fullCommandLine.add(open); - } - if (System.getProperty(PropertyNames.DEBUG_PORT) != null) - fullCommandLine.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:" + System.getProperty(PropertyNames.DEBUG_PORT)); - fullCommandLine.add("-javaagent:" + agentFile.getAbsolutePath()); - fullCommandLine.add("--add-modules=ALL-MODULE-PATH,ALL-SYSTEM,ALL-DEFAULT,java.sql"); - fullCommandLine.add("-Xbootclasspath/a:" + agentFile.getAbsolutePath()); - fullCommandLine.add("moe.nea.modernjava.agent.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 { - TextIoUtils.flushStdIO(); - } - } 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); - } + /** + * @return the original arguments, as passed to the main method. + */ + public static List<String> getOriginalArguments() { + 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); + return originalArgs; + } + + public static File findJavaLauncher() { + JavaScanner javaScanner = new JavaScanner(); + javaScanner.scanDefaultPaths(); + javaScanner.prettyPrint(); + JavaScanner.LocalJavaVersion candidate = javaScanner.findCandidate(); + if (candidate == null) { + System.err.println("Looks like we couldn't find a java candidate. Either install one, or if you have one" + + " and we just cannot find it, specify -D" + PropertyNames.JAVA_SCAN_PATH + "=<java home here>." + + " We need a Java 16 JDK. Exiting now."); + IAMFML.shutdown(1); + throw new RuntimeException(); + } + System.out.println("Choosing Java Candidate:\n" + candidate.prettyPrint()); + return candidate.getJavaBinary(); + } + + public static File copyResource(String name, String resourcePath) { + try { + File file = File.createTempFile(name, ".jar"); + try (InputStream is = FCPRelauncher.class.getResourceAsStream(resourcePath); + OutputStream os = Files.newOutputStream(file.toPath())) { + assert is != null; + IOUtils.copy(is, os); + } + return file; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static File findAgentJar() { + return copyResource("mjr-agent", "/agent/agent.jar"); + } + + public static void relaunch() { + + List<String> originalArgs = getOriginalArguments(); + + File modernJavaPath = findJavaLauncher(); + System.out.println("Located modern minecraft at: " + modernJavaPath); + + File agentFile = findAgentJar(); + System.out.println("Located agent jar at: " + agentFile); + + + 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.getAbsolutePath()); + fullCommandLine.addAll(ManagementFactory.getRuntimeMXBean().getInputArguments()); + fullCommandLine.add("-D" + PropertyNames.HAS_RELAUNCHED + "=true"); + fullCommandLine.add("-D" + PropertyNames.RELAUNCH_CLASSPATH + "=" + createClassPath(agentFile)); + fullCommandLine.add("--illegal-access=permit"); + for (String open : moduleOpens) { + fullCommandLine.add("--add-opens"); + fullCommandLine.add(open); + } + if (System.getProperty(PropertyNames.DEBUG_PORT) != null) + fullCommandLine.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:" + System.getProperty(PropertyNames.DEBUG_PORT)); + fullCommandLine.add("-Xbootclasspath/a:" + agentFile.getAbsolutePath()); + fullCommandLine.add("-Xbootclasspath/a:" + extractExtraDependencies().stream().map(File::getAbsolutePath).collect(Collectors.joining(File.pathSeparator))); + fullCommandLine.add("-javaagent:" + agentFile.getAbsolutePath()); + fullCommandLine.add("--add-modules=ALL-MODULE-PATH,ALL-SYSTEM,ALL-DEFAULT,java.sql"); + fullCommandLine.add("moe.nea.modernjava.agent.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 { + TextIoUtils.flushStdIO(); + } + } 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); + } + + private static String createClassPath(File agentFile) { + List<String> classPath = new ArrayList<>(); + classPath.addAll(Arrays.asList(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))); + classPath.removeIf(it -> it.matches("[/\\\\]asm-[^/\\\\]+-5\\.[^/\\\\]+\\.jar")); + classPath.add(0, agentFile.getAbsolutePath()); + return String.join(File.pathSeparator, classPath); + } + + public static List<File> extractExtraDependencies() { + try (ZipFile zipFile = new ZipFile(getCurrentJarFile())) { + List<File> extractedResources = new ArrayList<>(); + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + if (!entry.getName().startsWith("deps/")) continue; + if (!entry.getName().endsWith(".jar")) continue; + String[] split = entry.getName().split("/"); + extractedResources.add(copyResource(split[split.length - 1].split("\\.")[0], "/" + entry.getName())); + } + return extractedResources; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static File getCurrentJarFile() { + return new File(FCPEntryPoint.fileUri); + } } diff --git a/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java b/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java index 0003097..e50e5d4 100644 --- a/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java +++ b/src/main/java/moe/nea/modernjava/launch/relaunch/JavaScanner.java @@ -11,127 +11,129 @@ import java.util.List; public class JavaScanner { - public static final String JAVA_BINARY_PATH = "bin/java" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : ""); - public static final String JAVA_COMPILER_PATH = "bin/javac" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : ""); - - public boolean isJavaHome(File potential) { - if (new File(potential, JAVA_BINARY_PATH).exists()) { - return true; - } - return false; - } - - private List<LocalJavaVersion> versionList = new ArrayList<>(); - - public void scanDirectory(File file) { - scanDirectory(file, 1); - } - - public void scanDirectory(File file, int depthLimit) { - if (depthLimit < 0) return; - if (isJavaHome(file)) { - versionList.add(new LocalJavaVersion(file)); - } else { - for (File listFile : file.listFiles()) { - if (listFile.isDirectory()) { - scanDirectory(listFile, depthLimit - 1); - } - } - } - } - - public void prettyPrint() { - String s = "Fun fact these are the found Java Runtime Environments:\n"; - for (LocalJavaVersion localJavaVersion : versionList) { - s += localJavaVersion.prettyPrint(); - } - System.out.println(s); - } - - public void scanDefaultPaths() { - File home = new File(System.getProperty("user.home")); - scanDirectory(new File(home, ".sdkman/candidates/java")); - scanDirectory(new File(home, ".jdks")); - scanDirectory(new File("/usr"), 0); - String[] paths = System.getProperty(PropertyNames.JAVA_SCAN_PATH, "").split(File.pathSeparator); - for (String path : paths) { - if (!path.isEmpty()) { - scanDirectory(new File(path).getParentFile(), 3); - } - } - } - - public LocalJavaVersion findCandidate() { - LocalJavaVersion bestFit = null; - for (LocalJavaVersion localJavaVersion : versionList) { - if (localJavaVersion.isJdk() && localJavaVersion.getMajorVersion() == 16) { - bestFit = localJavaVersion; - } - } - return bestFit; - } - - public static class LocalJavaVersion { - private final File javaHome; - private String versionString; - - public LocalJavaVersion(File javaHome) { - this.javaHome = javaHome; - } - - public File getJavaHome() { - return javaHome; - } - - public File getJavaBinary() { - return new File(javaHome, JAVA_BINARY_PATH); - } - - public boolean isJdk() { - return new File(javaHome, JAVA_COMPILER_PATH).exists(); - } - - public String getVersionString() { - if (versionString == null) { - ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.command(getJavaBinary().getAbsolutePath(), "-version"); - processBuilder.redirectInput(ProcessBuilder.Redirect.PIPE); - processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE); - processBuilder.redirectError(ProcessBuilder.Redirect.PIPE); - try { - Process process = processBuilder.start(); - process.waitFor(); - versionString = IOUtils.toString(process.getInputStream()) + IOUtils.toString(process.getErrorStream()); - } catch (Exception e) { - e.printStackTrace(); - versionString = "<invalid>"; - } - } - return versionString; - } - - public String prettyPrint() { - return javaHome.getAbsolutePath() + ":\n" - + "\tJava Binary: " + getJavaBinary().getAbsolutePath() + "\n" - + "\tMajor Version: " + getMajorVersion() + "\n" - + "\tFull Version: " + getVersion() + "\n" - + "\tIs Jdk: " + isJdk() + "\n" - ; - } - - public String getVersion() { - String v = getVersionString(); - String[] s = v.split("\""); - if (s.length < 2) return null; - return s[1]; - } - - public int getMajorVersion() { - try { - return Integer.parseInt(getVersion().split("\\.")[0]); - } catch (Exception e) { - return -1; - } - } - } + public static final String JAVA_BINARY_PATH = "bin/java" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : ""); + public static final String JAVA_COMPILER_PATH = "bin/javac" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : ""); + + public boolean isJavaHome(File potential) { + if (new File(potential, JAVA_BINARY_PATH).exists()) { + return true; + } + return false; + } + + private List<LocalJavaVersion> versionList = new ArrayList<>(); + + public void scanDirectory(File file) { + scanDirectory(file, 1); + } + + public void scanDirectory(File file, int depthLimit) { + if (depthLimit < 0) return; + if (isJavaHome(file)) { + versionList.add(new LocalJavaVersion(file)); + } else { + File[] files = file.listFiles(); + if (files == null) return; + for (File listFile : files) { + if (listFile.isDirectory()) { + scanDirectory(listFile, depthLimit - 1); + } + } + } + } + + public void prettyPrint() { + String s = "Fun fact these are the found Java Runtime Environments:\n"; + for (LocalJavaVersion localJavaVersion : versionList) { + s += localJavaVersion.prettyPrint(); + } + System.out.println(s); + } + + public void scanDefaultPaths() { + File home = new File(System.getProperty("user.home")); + scanDirectory(new File(home, ".sdkman/candidates/java")); + scanDirectory(new File(home, ".jdks")); + scanDirectory(new File("/usr"), 0); + String[] paths = System.getProperty(PropertyNames.JAVA_SCAN_PATH, "").split(File.pathSeparator); + for (String path : paths) { + if (!path.isEmpty()) { + scanDirectory(new File(path).getParentFile(), 3); + } + } + } + + public LocalJavaVersion findCandidate() { + LocalJavaVersion bestFit = null; + for (LocalJavaVersion localJavaVersion : versionList) { + if (localJavaVersion.isJdk() && localJavaVersion.getMajorVersion() == 16) { + bestFit = localJavaVersion; + } + } + return bestFit; + } + + public static class LocalJavaVersion { + private final File javaHome; + private String versionString; + + public LocalJavaVersion(File javaHome) { + this.javaHome = javaHome; + } + + public File getJavaHome() { + return javaHome; + } + + public File getJavaBinary() { + return new File(javaHome, JAVA_BINARY_PATH); + } + + public boolean isJdk() { + return new File(javaHome, JAVA_COMPILER_PATH).exists(); + } + + public String getVersionString() { + if (versionString == null) { + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(getJavaBinary().getAbsolutePath(), "-version"); + processBuilder.redirectInput(ProcessBuilder.Redirect.PIPE); + processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE); + processBuilder.redirectError(ProcessBuilder.Redirect.PIPE); + try { + Process process = processBuilder.start(); + process.waitFor(); + versionString = IOUtils.toString(process.getInputStream()) + IOUtils.toString(process.getErrorStream()); + } catch (Exception e) { + e.printStackTrace(); + versionString = "<invalid>"; + } + } + return versionString; + } + + public String prettyPrint() { + return javaHome.getAbsolutePath() + ":\n" + + "\tJava Binary: " + getJavaBinary().getAbsolutePath() + "\n" + + "\tMajor Version: " + getMajorVersion() + "\n" + + "\tFull Version: " + getVersion() + "\n" + + "\tIs Jdk: " + isJdk() + "\n" + ; + } + + public String getVersion() { + String v = getVersionString(); + String[] s = v.split("\""); + if (s.length < 2) return null; + return s[1]; + } + + public int getMajorVersion() { + try { + return Integer.parseInt(getVersion().split("\\.")[0]); + } catch (Exception e) { + return -1; + } + } + } } |