Skip to content

Commit

Permalink
Better Java VM handling with third party servers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasluqui committed Sep 8, 2024
1 parent 18fb18a commit ed4852e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 67 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/luuqui/launcher/JVMPatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,17 @@ private static void patch() {
jvmPatcherProgressBar.setValue(2);
jvmPatcherState.setText(Locale.getValue("m.jvm_patcher_delete"));
try {
if (!FileUtil.fileExists(LauncherGlobals.USER_DIR + File.separator + "java_vm_unpatched")) {
FileUtils.moveDirectory(new File(LauncherGlobals.USER_DIR, "java_vm"), new File(LauncherGlobals.USER_DIR, "java_vm_unpatched"));
if (!FileUtil.fileExists(LauncherApp.javaVMPatchDir + File.separator + "java_vm_unpatched")) {
FileUtils.moveDirectory(new File(LauncherApp.javaVMPatchDir, "java_vm"), new File(LauncherApp.javaVMPatchDir, "java_vm_unpatched"));
}
} catch (IOException e) {
log.error(e);
}

jvmPatcherProgressBar.setValue(3);
jvmPatcherState.setText(Locale.getValue("m.jvm_patcher_extract"));
Compressor.unzip(LauncherGlobals.USER_DIR + File.separator + "jvm_pack.zip", LauncherGlobals.USER_DIR, false);
new File(LauncherGlobals.USER_DIR, "jvm_pack.zip").delete();
Compressor.unzip(LauncherApp.javaVMPatchDir + File.separator + "jvm_pack.zip", LauncherApp.javaVMPatchDir, false);
new File(LauncherApp.javaVMPatchDir, "jvm_pack.zip").delete();

jvmPatcherProgressBar.setValue(4);
jvmPatcherState.setText(Locale.getValue("m.jvm_patcher_finish"));
Expand All @@ -249,7 +249,7 @@ private static void downloadPackagedJVM() {
try {
FileUtils.copyURLToFile(
new URL(downloadUrl),
new File(LauncherGlobals.USER_DIR, "jvm_pack.zip"),
new File(LauncherApp.javaVMPatchDir, "jvm_pack.zip"),
0,
0
);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/luuqui/launcher/LauncherApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class LauncherApp {
public static Server selectedServer = null;
public static boolean flamingoOnline = false;

public static String javaVMPatchDir = null;

public static void main(String[] args) {

LauncherApp app = new LauncherApp();
Expand Down Expand Up @@ -331,7 +333,12 @@ private void logGameVMInfo() {
private boolean requiresJVMPatch() {

// First of all see if we're being forced to patch.
if(_args.length > 0 && _args[0].equals("forceJVMPatch")) return true;
if(_args.length > 0 && _args[0].equals("forceJVMPatch")) {
// set the path dir to wherever we're being forced to patch to.
// this is primarily used for patching when third party servers were selected.
javaVMPatchDir = _args[1];
return true;
}

// You need a 64-bit system to begin with.
if(!SystemUtil.is64Bit()) return false;
Expand All @@ -348,6 +355,8 @@ private boolean requiresJVMPatch() {
return false;
}

javaVMPatchDir = LauncherGlobals.USER_DIR;

return true;
}

Expand Down
125 changes: 68 additions & 57 deletions src/main/java/com/luuqui/launcher/LauncherEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -264,7 +265,6 @@ public static void selectedServerChanged(ActionEvent event) {
SettingsGUI.switchUseIngameRPC.setEnabled(true);
SettingsGUI.choicePlatform.setEnabled(true);
SettingsGUI.forceRebuildButton.setEnabled(true);
SettingsGUI.jvmPatchButton.setEnabled(true);
SettingsGUI.argumentsPane.setEnabled(true);
SettingsGUI.labelDisclaimer.setVisible(false);
SettingsGUI.serverAddressTextField.setEnabled(true);
Expand Down Expand Up @@ -292,7 +292,6 @@ public static void selectedServerChanged(ActionEvent event) {
SettingsGUI.switchUseIngameRPC.setEnabled(false);
SettingsGUI.choicePlatform.setEnabled(false);
SettingsGUI.forceRebuildButton.setEnabled(false);
SettingsGUI.jvmPatchButton.setEnabled(false);
SettingsGUI.argumentsPane.setEnabled(false);
SettingsGUI.labelDisclaimer.setVisible(true);
SettingsGUI.serverAddressTextField.setEnabled(false);
Expand All @@ -309,6 +308,7 @@ public static void selectedServerChanged(ActionEvent event) {
}

updateBanner();
updateGameJavaVMData();
saveSelectedServer();
}

Expand Down Expand Up @@ -359,6 +359,17 @@ public static void updateBanner() {
refreshThread.start();
}

public static void updateGameJavaVMData() {
Thread thread = new Thread(() -> {
SettingsGUI.javaVMBadge.setText("Your Java VM: " + JavaUtil.getReadableGameJVMData());

boolean is64Bit = JavaUtil.getJVMArch(JavaUtil.getGameJVMExePath()) == 64;
SettingsGUI.memorySlider.setMaximum(is64Bit ? 4096 : 1024);
SettingsEventHandler.memoryChangeEvent(SettingsGUI.memorySlider.getValue());
});
thread.start();
}

public static void updateLauncher() {
// delete any existing updaters from previous updates
new File(LauncherGlobals.USER_DIR + "/updater.jar").delete();
Expand All @@ -380,66 +391,66 @@ public static void showLatestChangelog() {
}

private static String[] getThirdPartyClientStartCommand(Server server, boolean altMode) {
String[] args;
List<String> argsList = new ArrayList<>();
String sanitizedServerName = LauncherApp.getSanitizedServerName(server.name);

if(SystemUtil.isWindows()) {
args = new String[]{
LauncherGlobals.USER_DIR + File.separator + "thirdparty" + File.separator + sanitizedServerName + File.separator + "java_vm" + File.separator + "bin" + File.separator + "java",
"-classpath",
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/config.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/projectx-config.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/projectx-pcode.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/lwjgl.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/lwjgl_util.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/jinput.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/jshortcut.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/commons-beanutils.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/commons-digester.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/commons-logging.jar;",
"-Dcom.threerings.getdown=false",
Settings.gameDisableExplicitGC ? "-XX:+DisableExplicitGC" : "",
Settings.gameUseCustomGC && Settings.gameGarbageCollector.equalsIgnoreCase("ParallelOld") ? "-XX:+UseParallelGC" : "",
Settings.gameUseCustomGC ? "-XX:+Use" + Settings.gameGarbageCollector + "GC" : "",
altMode ? "-Xms256M" : Settings.gameGarbageCollector.equalsIgnoreCase("G1") ? "-Xms" + Settings.gameMemory + "M" : "-Xms" + Settings.gameMemory / 2 + "M",
altMode ? "-Xmx512M" : "-Xmx" + Settings.gameMemory + "M",
"-XX:+AggressiveOpts",
"-XX:SoftRefLRUPolicyMSPerMB=10",
"-Djava.library.path=" + LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./native",
"-Dorg.lwjgl.util.NoChecks=true",
"-Dsun.java2d.d3d=false",
"-Dappdir=" + LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + ".",
"-Dresource_dir=" + LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./rsrc",
"com.threerings.projectx.client.ProjectXApp",
};
argsList.add(LauncherGlobals.USER_DIR + File.separator + "thirdparty" + File.separator + sanitizedServerName + File.separator + "java_vm" + File.separator + "bin" + File.separator + "java");
argsList.add("-classpath");
argsList.add(LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/config.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/projectx-config.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/projectx-pcode.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/lwjgl.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/lwjgl_util.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/jinput.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/jshortcut.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/commons-beanutils.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/commons-digester.jar;" +
LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./code/commons-logging.jar;");
argsList.add("-Dcom.threerings.getdown=false");
if(Settings.gameDisableExplicitGC) argsList.add("-XX:+DisableExplicitGC");
if(Settings.gameUseCustomGC && Settings.gameGarbageCollector.equalsIgnoreCase("ParallelOld")) argsList.add("-XX:+UseParallelGC");
if(Settings.gameUseCustomGC) argsList.add("-XX:+Use" + Settings.gameGarbageCollector + "GC");
argsList.add(altMode ? "-Xms256M" : Settings.gameGarbageCollector.equalsIgnoreCase("G1") ? "-Xms" + Settings.gameMemory + "M" : "-Xms" + Settings.gameMemory / 2 + "M");
argsList.add(altMode ? "-Xmx512M" : "-Xmx" + Settings.gameMemory + "M");
argsList.add("-XX:+AggressiveOpts");
argsList.add("-XX:SoftRefLRUPolicyMSPerMB=10");
argsList.add("-Djava.library.path=" + LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./native");
argsList.add("-Dorg.lwjgl.util.NoChecks=true");
argsList.add("-Dsun.java2d.d3d=false");
argsList.add("-Dappdir=" + LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + ".");
argsList.add("-Dresource_dir=" + LauncherGlobals.USER_DIR + "\\thirdparty\\" + sanitizedServerName + File.separator + "./rsrc");
argsList.add("com.threerings.projectx.client.ProjectXApp");
} else {
args = new String[]{
LauncherGlobals.USER_DIR + File.separator + "thirdparty" + File.separator + sanitizedServerName + File.separator + "java" + File.separator + "bin" + File.separator + "java",
"-classpath",
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/config.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/projectx-config.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/projectx-pcode.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/lwjgl.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/lwjgl_util.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/jinput.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/jshortcut.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/commons-beanutils.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/commons-digester.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/commons-logging.jar:",
"-Dcom.threerings.getdown=false",
altMode ? "-Xms256M" : "-Xms512M",
altMode ? "-Xmx512M" : "-Xmx1024M",
"-XX:+AggressiveOpts",
"-XX:SoftRefLRUPolicyMSPerMB=10",
"-Djava.library.path=" + LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "native",
"-Dorg.lwjgl.util.NoChecks=true",
"-Dsun.java2d.d3d=false",
"-Dappdir=" + LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator,
"-Dresource_dir=" + LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "rsrc",
"com.threerings.projectx.client.ProjectXApp",
};
argsList.add(LauncherGlobals.USER_DIR + File.separator + "thirdparty" + File.separator + sanitizedServerName + File.separator + "java" + File.separator + "bin" + File.separator + "java");
argsList.add("-classpath");
argsList.add(LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/config.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/projectx-config.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/projectx-pcode.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/lwjgl.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/lwjgl_util.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/jinput.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/jshortcut.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/commons-beanutils.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/commons-digester.jar:" +
LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "code/commons-logging.jar:");
argsList.add("-Dcom.threerings.getdown=false");
if(Settings.gameDisableExplicitGC) argsList.add("-XX:+DisableExplicitGC");
if(Settings.gameUseCustomGC && Settings.gameGarbageCollector.equalsIgnoreCase("ParallelOld")) argsList.add("-XX:+UseParallelGC");
if(Settings.gameUseCustomGC) argsList.add("-XX:+Use" + Settings.gameGarbageCollector + "GC");
argsList.add(altMode ? "-Xms256M" : Settings.gameGarbageCollector.equalsIgnoreCase("G1") ? "-Xms" + Settings.gameMemory + "M" : "-Xms" + Settings.gameMemory / 2 + "M");
argsList.add(altMode ? "-Xmx512M" : "-Xmx" + Settings.gameMemory + "M");
argsList.add("-XX:+AggressiveOpts");
argsList.add("-XX:SoftRefLRUPolicyMSPerMB=10");
argsList.add("-Djava.library.path=" + LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "native");
argsList.add("-Dorg.lwjgl.util.NoChecks=true");
argsList.add("-Dsun.java2d.d3d=false");
argsList.add("-Dappdir=" + LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator);
argsList.add("-Dresource_dir=" + LauncherGlobals.USER_DIR + "/thirdparty/" + sanitizedServerName + File.separator + "rsrc");
argsList.add("com.threerings.projectx.client.ProjectXApp");
}

return args;
return argsList.toArray(new String[argsList.size()]);
}

private static void checkGameLaunched() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public static void autoUpdateChangeEvent(ActionEvent action) {

public static void jvmPatchEvent(ActionEvent action) {
SettingsProperties.setValue("launcher.jvm_patched", "false");
ProcessUtil.run(new String[] { "java", "-jar", LauncherGlobals.USER_DIR + File.separator + "KnightLauncher.jar", "forceJVMPatch"}, true);
String javaVMPatchDir = LauncherGlobals.USER_DIR;
if(!LauncherApp.selectedServer.name.equalsIgnoreCase("Official")) {
javaVMPatchDir += File.separator + "thirdparty" + File.separator + LauncherApp.getSanitizedServerName(LauncherApp.selectedServer.name);
}
ProcessUtil.run(new String[] { "java", "-jar", LauncherGlobals.USER_DIR + File.separator + "KnightLauncher.jar", "forceJVMPatch", javaVMPatchDir}, true);
SettingsGUI.settingsGUIFrame.dispose();
System.exit(1);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/luuqui/launcher/setting/SettingsGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class SettingsGUI extends BaseGUI {
public static JButton betaCodeRevalidateButton;
public static JButton betaCodeClearLocalButton;
public static JButton resetGameSettingsButton;
public static JLabel javaVMBadge = new JLabel();

public SettingsGUI(LauncherApp app) {
super();
Expand Down Expand Up @@ -311,7 +312,7 @@ protected JPanel createGamePanel() {
headerLabel.setFont(Fonts.fontMedGiant);
gamePanel.add(headerLabel);

JLabel javaVMBadge = new JLabel("Your Java VM: " + JavaUtil.getReadableGameJVMData());
javaVMBadge = new JLabel("Your Java VM: " + JavaUtil.getReadableGameJVMData());
javaVMBadge.setBounds(370, 35, 235, 18);
javaVMBadge.setHorizontalAlignment(SwingConstants.CENTER);
javaVMBadge.setFont(Fonts.fontRegSmall);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/luuqui/util/JavaUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.luuqui.util;

import com.luuqui.launcher.LauncherApp;
import com.luuqui.launcher.LauncherGlobals;

import java.io.File;
Expand Down Expand Up @@ -118,11 +119,19 @@ public static String getReadableGameJVMData() {
}

public static String getGameJavaDirPath() {
File javaVMDir = new File(LauncherGlobals.USER_DIR, "/java_vm");
String startingDirPath = LauncherGlobals.USER_DIR;

if(LauncherApp.selectedServer != null) {
if(!LauncherApp.selectedServer.name.equalsIgnoreCase("Official")) {
startingDirPath += File.separator + "thirdparty" + File.separator + LauncherApp.getSanitizedServerName(LauncherApp.selectedServer.name);
}
}

File javaVMDir = new File(startingDirPath, "/java_vm");
if (javaVMDir.exists() && javaVMDir.isDirectory()) {
return javaVMDir.getAbsolutePath();
}
File javaDir = new File(LauncherGlobals.USER_DIR, "/java");
File javaDir = new File(startingDirPath, "/java");
if (javaDir.exists() && javaDir.isDirectory()) {
return javaDir.getAbsolutePath();
}
Expand Down

0 comments on commit ed4852e

Please sign in to comment.