Skip to content

Commit

Permalink
SKCraftGH-472 Add warning popup for mismatched Java versions
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed May 30, 2022
1 parent b8a6769 commit 1c64de1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import com.skcraft.launcher.dialog.AccountSelectDialog;
import com.skcraft.launcher.dialog.ProgressDialog;
import com.skcraft.launcher.launch.LaunchOptions.UpdatePolicy;
import com.skcraft.launcher.launch.runtime.JavaRuntime;
import com.skcraft.launcher.model.minecraft.JavaVersion;
import com.skcraft.launcher.persistence.Persistence;
import com.skcraft.launcher.swing.SwingHelper;
import com.skcraft.launcher.update.Updater;
import com.skcraft.launcher.util.SharedLocale;
import com.skcraft.launcher.util.SwingExecutor;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.apache.commons.io.FileUtils;

Expand All @@ -29,6 +32,8 @@
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.function.BiPredicate;
import java.util.logging.Level;

import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
Expand Down Expand Up @@ -119,7 +124,7 @@ private void launch(Window window, Instance instance, Session session, final Lau
final File extractDir = launcher.createExtractDir();

// Get the process
Runner task = new Runner(launcher, instance, session, extractDir);
Runner task = new Runner(launcher, instance, session, extractDir, new RuntimeVerifier(instance));
ObservableFuture<Process> processFuture = new ObservableFuture<Process>(
launcher.getExecutor().submit(task), task);

Expand Down Expand Up @@ -169,4 +174,38 @@ public void run() {
}
}, sameThreadExecutor());
}

@RequiredArgsConstructor
static class RuntimeVerifier implements BiPredicate<JavaRuntime, JavaVersion> {
private final Instance instance;

@Override
public boolean test(JavaRuntime javaRuntime, JavaVersion javaVersion) {
ListenableFuture<Boolean> fut = SwingExecutor.INSTANCE.submit(() -> {
Object[] options = new Object[]{
tr("button.cancel"),
tr("button.launchAnyway"),
};

String message = tr("runner.wrongJavaVersion",
instance.getTitle(), javaVersion.getMajorVersion(), javaRuntime.getVersion());
int picked = JOptionPane.showOptionDialog(null,
SwingHelper.htmlWrap(message),
tr("launcher.javaMismatchTitle"),
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
options,
null);

return picked == 1;
});

try {
return fut.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
30 changes: 27 additions & 3 deletions launcher/src/main/java/com/skcraft/launcher/launch/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.function.BiPredicate;

import static com.skcraft.launcher.LauncherUtils.checkInterrupted;
import static com.skcraft.launcher.util.SharedLocale.tr;
Expand All @@ -54,6 +56,7 @@ public class Runner implements Callable<Process>, ProgressObservable {
private final Instance instance;
private final Session session;
private final File extractDir;
private final BiPredicate<JavaRuntime, JavaVersion> javaRuntimeMismatch;
@Getter @Setter private Environment environment = Environment.getInstance();

private VersionManifest versionManifest;
Expand All @@ -66,18 +69,20 @@ public class Runner implements Callable<Process>, ProgressObservable {

/**
* Create a new instance launcher.
*
* @param launcher the launcher
* @param launcher the launcher
* @param instance the instance
* @param session the session
* @param extractDir the directory to extract to
* @param javaRuntimeMismatch
*/
public Runner(@NonNull Launcher launcher, @NonNull Instance instance,
@NonNull Session session, @NonNull File extractDir) {
@NonNull Session session, @NonNull File extractDir,
BiPredicate<JavaRuntime, JavaVersion> javaRuntimeMismatch) {
this.launcher = launcher;
this.instance = instance;
this.session = session;
this.extractDir = extractDir;
this.javaRuntimeMismatch = javaRuntimeMismatch;
this.featureList = new FeatureList.Mutable();
}

Expand Down Expand Up @@ -149,6 +154,8 @@ public Process call() throws Exception {

callLaunchModifier();

verifyJavaRuntime();

ProcessBuilder processBuilder = new ProcessBuilder(builder.buildCommand());
processBuilder.directory(instance.getContentDir());
Runner.log.info("Launching: " + builder);
Expand All @@ -166,6 +173,23 @@ private void callLaunchModifier() {
instance.modify(builder);
}

private void verifyJavaRuntime() {
JavaRuntime pickedRuntime = builder.getRuntime();
JavaVersion targetVersion = versionManifest.getJavaVersion();

if (pickedRuntime == null || targetVersion == null) {
return;
}

if (pickedRuntime.getMajorVersion() != targetVersion.getMajorVersion()) {
boolean launchAnyway = javaRuntimeMismatch.test(pickedRuntime, targetVersion);

if (!launchAnyway) {
throw new CancellationException("Launch cancelled by user.");
}
}
}

/**
* Add platform-specific arguments.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public static String htmlEscape(String str) {
.replace("&", "&amp;");
}

public static String htmlWrap(String message) {
// To force the label to wrap, convert the message to broken HTML
return "<html><div style=\"width: 250px\">" + htmlEscape(message);
}

public static void setClipboard(String text) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(text), clipboardOwner);
Expand Down Expand Up @@ -189,8 +194,7 @@ public static void showMessageDialog(final Component parentComponent,
final int messageType) {

if (SwingUtilities.isEventDispatchThread()) {
// To force the label to wrap, convert the message to broken HTML
String htmlMessage = "<html><div style=\"width: 250px\">" + htmlEscape(message);
String htmlMessage = htmlWrap(message);

JPanel panel = new JPanel(new BorderLayout(0, detailsText != null ? 20 : 0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ errors.selfUpdateCheckError=Checking for an update to the launcher has failed.
button.cancel=Cancel
button.ok=OK
button.save=Save
button.launchAnyway=Launch Anyway

options.title = Options
options.useProxyCheck = Use following proxy in Minecraft
Expand Down Expand Up @@ -88,6 +89,7 @@ launcher.noInstanceError=Please select a modpack to launch.
launcher.noInstanceTitle=No Modpack Selected
launcher.launchingTItle=Launching the game...
launcher.launchingStatus=Launching ''{0}''. Please wait.
launcher.javaMismatchTitle=Java version warning
launcher.modpackColumn=Modpack
launcher.notInstalledHint=(not installed)
launcher.requiresUpdateHint=(requires update)
Expand Down Expand Up @@ -191,6 +193,7 @@ runner.updateRequired=This instance must be updated before it can be run.
runner.missingLibrary={0} needs to be relaunched and updated because the library ''{1}'' is missing.
runner.missingAssetsIndex={0} needs to be relaunched and updated because its asset index is missing.
runner.corruptAssetsIndex={0} needs to be relaunched and updated because its asset index is corrupt.
runner.wrongJavaVersion=Instance ''{0}'' requires Java version {1}, but could only find {2}.

assets.expanding1=Expanding {0} asset... ({1} remaining)
assets.expandingN=Expanding {0} assets... ({1} remaining)
Expand Down

0 comments on commit 1c64de1

Please sign in to comment.