Skip to content

Commit

Permalink
Relauncher stub + graphical console for the new instance
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenraven committed Feb 13, 2024
1 parent ce5d0e2 commit f693d1b
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 29 deletions.
42 changes: 25 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ minecraft {
injectedTags.put("RECOMMENDED_JAVA_ARGS", extraJavaArgs.joinToString("\t"))
}

lateinit var hotswapSet: SourceSet

sourceSets {
create("util") {
java {}
}
hotswapSet = create("hotswap") {
java {}
}
main {
java {
srcDir("src/generated/java")
}
}
}

lateinit var forgePatchesEmbedded: Configuration
lateinit var versionJsonElements: Configuration

Expand All @@ -78,10 +62,33 @@ configurations {
patchedMinecraft { extendsFrom(forgePatchesEmbedded) }
}

tasks.named<JavaCompile>("compileHotswapJava").configure {
lateinit var hotswapSet: SourceSet
lateinit var relauncherStubSet: SourceSet

sourceSets {
create("util")
hotswapSet = create("hotswap")
relauncherStubSet = create("relauncherStub") {
compileClasspath += forgePatchesEmbedded + configurations.shadowImplementation.get()
}
main {
java {
srcDir("src/generated/java")
}
}
}

tasks.named<JavaCompile>(hotswapSet.compileJavaTaskName).configure {
javaCompiler = javaToolchains.compilerFor(newJavaToolchainSpec)
sourceCompatibility = JavaVersion.VERSION_17.majorVersion
targetCompatibility = JavaVersion.VERSION_17.majorVersion
}

tasks.named<JavaCompile>(relauncherStubSet.compileJavaTaskName).configure {
javaCompiler = javaToolchains.compilerFor(newJavaToolchainSpec)
sourceCompatibility = JavaVersion.VERSION_17.majorVersion
targetCompatibility = JavaVersion.VERSION_17.majorVersion
options.release = 17
}

tasks.createMcLauncherFiles {
Expand Down Expand Up @@ -110,6 +117,7 @@ val forgePatchesJar = tasks.register<Jar>("forgePatchesJar") {
from(sourceSets.main.map { it.output.classesDirs }) {
include("me/eigenraven/lwjgl3ify/rfb/entry/ServerMain.class")
}
from(relauncherStubSet.output)
archiveClassifier.set("forgePatches")
manifest {
val libraryList = listOf(
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def addGtForTesting = Boolean.parseBoolean(project.getProperties().getOrDefault(
def addReikaModsForTesting = Boolean.parseBoolean(project.getProperties().getOrDefault("withReikaMods", "false").toString())

def asmVersion = '9.6'
def rfbVersion = '0.3.9'
def rfbVersion = '0.3.10'
// 100% binary compatible, 1 minor source compatibility issue (removed a throws IOException clause)
def gsonVersion = '2.10.1'

Expand Down
73 changes: 64 additions & 9 deletions src/main/java/me/eigenraven/lwjgl3ify/relauncher/Relauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
Expand Down Expand Up @@ -41,6 +45,7 @@ public class Relauncher {
final String gameVersion;
final Downloader downloader;
final RelauncherUserInterface gui;
Path forgePatchesJarPath;

public void runtimeExit(int exitCode) {
SafeRuntimeExit.exitRuntime(exitCode);
Expand Down Expand Up @@ -140,6 +145,7 @@ public List<String> createClasspath() {
logger.info("Using previously extracted bundled early classpath libraries from {}", jarFile);
}
classpath.add(jarFile.toString());
this.forgePatchesJarPath = jarFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -154,6 +160,14 @@ public List<String> createClasspath() {
return classpath;
}

private static long getCurrentPid() {
final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
// getName is implemented to return getPid() + "@" + hostname
final String combinedPidHostname = runtime.getName();
final String[] parts = combinedPidHostname.split("@", 2);
return Integer.parseInt(parts[0]);
}

@SuppressWarnings("deprecation")
public void run() throws IOException {
gui.startSettingsIfNeeded();
Expand All @@ -162,6 +176,22 @@ public void run() throws IOException {
return;
}

URL myJarUrl = Relauncher.class.getProtectionDomain()
.getCodeSource()
.getLocation();
// In case of broken LW-like classloaders that pass in jar: urls as the code source
while ("jar".equalsIgnoreCase(myJarUrl.getProtocol())) {
final String str = myJarUrl.toString();
myJarUrl = new URL(str.substring(4, str.lastIndexOf('!')));
}
String[] combinedArgs = args;
final Path myJarPath;
try {
myJarPath = Paths.get(myJarUrl.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}

final List<String> cmd = new ArrayList<>();
cmd.addAll(Arrays.asList(RECOMMENDED_JAVA_ARGS));
cmd.addAll(RelauncherConfig.config.toJvmArgs());
Expand Down Expand Up @@ -217,20 +247,45 @@ public void run() throws IOException {
javaPath = javas[javaIdx];
}
bootstrapCmd.add(javaPath);
bootstrapCmd.add("@" + argFile);
if (RelauncherConfig.config.forwardLogs) {
bootstrapCmd.add("@" + argFile);
} else {
bootstrapCmd.add("-cp");
bootstrapCmd.add(forgePatchesJarPath + File.pathSeparator + myJarPath);
bootstrapCmd.add("--disable-@files");
bootstrapCmd.add("me.eigenraven.lwjgl3ify.relauncherstub.RelauncherStubMain");
bootstrapCmd.add(Long.toString(getCurrentPid()));
bootstrapCmd.add("true");
bootstrapCmd.add(javaPath);
bootstrapCmd.add("@" + argFile);
}

final ProcessBuilder pb = new ProcessBuilder(bootstrapCmd);
pb.inheritIO();
if (!RelauncherConfig.config.forwardLogs) {
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
}
logger.info("Starting relaunched process using args {}", bootstrapCmd);
final Process p = pb.inheritIO()
.start();
while (p.isAlive()) {
final Process p = pb.start();
if (RelauncherConfig.config.forwardLogs) {
while (p.isAlive()) {
try {
p.waitFor();
break;
} catch (InterruptedException e) {
continue;
}
}
runtimeExit(p.exitValue());
} else {
// Wait for the "quit" message from the child process
try {
p.waitFor();
break;
} catch (InterruptedException e) {
continue;
p.getInputStream()
.read(new byte[6]);
} catch (IOException e) {
// ignored
}
runtimeExit(0);
}
runtimeExit(p.exitValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ConfigObject() {}
public GCOption garbageCollector = GCOption.G1GC;
public String[] customOptions = new String[0];
// Advanced
public boolean forwardLogs = true;
public boolean forwardLogs = false;
public boolean allowDebugger = false;
public boolean waitForDebugger = false;
public boolean mixinDebug = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void initSwingIfNeeded() {
Thread.currentThread()
.setContextClassLoader(mcLoader);
try {
System.setProperty("awt.useSystemAAFontSettings", "true");
System.setProperty("awt.useSystemAAFontSettings", "on");
LafManager.installTheme(new PreferredThemeStyle(ContrastRule.STANDARD, ColorToneRule.DARK));
} catch (Exception e) {
Relauncher.logger.warn("Could not initialize DarkLaf GUI theme", e);
Expand Down
Loading

0 comments on commit f693d1b

Please sign in to comment.