允许多个相同版本的Java共存

This commit is contained in:
yushijinhun 2018-06-06 23:43:58 +08:00
parent afa37c0d2f
commit 6b02dc3cd5
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
4 changed files with 37 additions and 13 deletions

View File

@ -181,6 +181,7 @@ public final class LauncherHelper {
if (java == null) { if (java == null) {
Controllers.dialog(Launcher.i18n("launch.wrong_javadir"), Launcher.i18n("message.error"), MessageBox.ERROR_MESSAGE, onAccept); Controllers.dialog(Launcher.i18n("launch.wrong_javadir"), Launcher.i18n("message.error"), MessageBox.ERROR_MESSAGE, onAccept);
setting.setJava(null); setting.setJava(null);
setting.setDefaultJavaPath(null);
java = JavaVersion.fromCurrentEnvironment(); java = JavaVersion.fromCurrentEnvironment();
flag = true; flag = true;
} }

View File

@ -26,7 +26,9 @@ import org.jackhuang.hmcl.util.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* *
@ -87,6 +89,20 @@ public final class VersionSetting {
javaProperty.set(java); javaProperty.set(java);
} }
private final ImmediateStringProperty defaultJavaPathProperty = new ImmediateStringProperty(this, "defaultJavaPath", "");
/**
* Path to Java executable, or null if user customizes java directory.
* It's used to determine which JRE to use when multiple JREs match the selected Java version.
*/
public String getDefaultJavaPath() {
return defaultJavaPathProperty.get();
}
public void setDefaultJavaPath(String defaultJavaPath) {
defaultJavaPathProperty.set(defaultJavaPath);
}
private final ImmediateStringProperty javaDirProperty = new ImmediateStringProperty(this, "javaDir", ""); private final ImmediateStringProperty javaDirProperty = new ImmediateStringProperty(this, "javaDir", "");
public ImmediateStringProperty javaDirProperty() { public ImmediateStringProperty javaDirProperty() {
@ -444,12 +460,18 @@ public final class VersionSetting {
return null; // Custom Java Directory not found, return null; // Custom Java Directory not found,
} }
} else if (StringUtils.isNotBlank(getJava())) { } else if (StringUtils.isNotBlank(getJava())) {
JavaVersion c = JavaVersion.getJREs().get(getJava()); List<JavaVersion> matchedJava = JavaVersion.getJREs().stream()
if (c == null) { .filter(java -> java.getVersion().equals(getJava()))
.collect(Collectors.toList());
if (matchedJava.isEmpty()) {
setJava("Default"); setJava("Default");
return JavaVersion.fromCurrentEnvironment(); return JavaVersion.fromCurrentEnvironment();
} else } else {
return c; return matchedJava.stream()
.filter(java -> java.getBinary().toString().equals(getDefaultJavaPath()))
.findFirst()
.orElse(matchedJava.get(0));
}
} else throw new Error(); } else throw new Error();
} }
@ -535,6 +557,7 @@ public final class VersionSetting {
obj.addProperty("gameDir", src.getGameDir()); obj.addProperty("gameDir", src.getGameDir());
obj.addProperty("launcherVisibility", src.getLauncherVisibility().ordinal()); obj.addProperty("launcherVisibility", src.getLauncherVisibility().ordinal());
obj.addProperty("gameDirType", src.getGameDirType().ordinal()); obj.addProperty("gameDirType", src.getGameDirType().ordinal());
obj.addProperty("defaultJavaPath", src.getDefaultJavaPath());
return obj; return obj;
} }
@ -571,6 +594,7 @@ public final class VersionSetting {
vs.setShowLogs(Optional.ofNullable(obj.get("showLogs")).map(JsonElement::getAsBoolean).orElse(false)); vs.setShowLogs(Optional.ofNullable(obj.get("showLogs")).map(JsonElement::getAsBoolean).orElse(false));
vs.setLauncherVisibility(LauncherVisibility.values()[Optional.ofNullable(obj.get("launcherVisibility")).map(JsonElement::getAsInt).orElse(1)]); vs.setLauncherVisibility(LauncherVisibility.values()[Optional.ofNullable(obj.get("launcherVisibility")).map(JsonElement::getAsInt).orElse(1)]);
vs.setGameDirType(EnumGameDirectory.values()[Optional.ofNullable(obj.get("gameDirType")).map(JsonElement::getAsInt).orElse(0)]); vs.setGameDirType(EnumGameDirectory.values()[Optional.ofNullable(obj.get("gameDirType")).map(JsonElement::getAsInt).orElse(0)]);
vs.setDefaultJavaPath(Optional.ofNullable(obj.get("defaultJavaPath")).map(JsonElement::getAsString).orElse(null));
return vs; return vs;
} }

View File

@ -84,7 +84,7 @@ public final class VersionSettingsController {
FXUtils.smoothScrolling(scroll); FXUtils.smoothScrolling(scroll);
Task.of(variables -> variables.set("list", JavaVersion.getJREs().values().stream().map(javaVersion -> Task.of(variables -> variables.set("list", JavaVersion.getJREs().stream().map(javaVersion ->
javaItem.createChildren(javaVersion.getVersion(), javaVersion.getBinary().getAbsolutePath(), javaVersion) javaItem.createChildren(javaVersion.getVersion(), javaVersion.getBinary().getAbsolutePath(), javaVersion)
).collect(Collectors.toList()))).subscribe(Schedulers.javafx(), variables -> ).collect(Collectors.toList()))).subscribe(Schedulers.javafx(), variables ->
javaItem.loadChildren(variables.<Collection<Node>>get("list")) javaItem.loadChildren(variables.<Collection<Node>>get("list"))
@ -165,8 +165,11 @@ public final class VersionSettingsController {
javaItem.setToggleSelectedListener(newValue -> { javaItem.setToggleSelectedListener(newValue -> {
if (javaItem.isCustomToggle(newValue)) { if (javaItem.isCustomToggle(newValue)) {
versionSetting.setJava("Custom"); versionSetting.setJava("Custom");
versionSetting.setDefaultJavaPath(null);
} else { } else {
versionSetting.setJava(((JavaVersion) newValue.getUserData()).getVersion()); JavaVersion java = (JavaVersion) newValue.getUserData();
versionSetting.setJava(java.getVersion());
versionSetting.setDefaultJavaPath(java.getBinary().toString());
} }
}); });

View File

@ -162,10 +162,10 @@ public final class JavaVersion implements Serializable {
Platform.PLATFORM Platform.PLATFORM
); );
private static Map<String, JavaVersion> JAVAS; private static List<JavaVersion> JAVAS;
private static final CountDownLatch LATCH = new CountDownLatch(1); private static final CountDownLatch LATCH = new CountDownLatch(1);
public static Map<String, JavaVersion> getJREs() throws InterruptedException { public static List<JavaVersion> getJREs() throws InterruptedException {
if (JAVAS != null) if (JAVAS != null)
return JAVAS; return JAVAS;
LATCH.await(); LATCH.await();
@ -175,8 +175,6 @@ public final class JavaVersion implements Serializable {
public static synchronized void initialize() throws IOException { public static synchronized void initialize() throws IOException {
if (JAVAS != null) if (JAVAS != null)
throw new IllegalStateException("JavaVersions have already been initialized."); throw new IllegalStateException("JavaVersions have already been initialized.");
HashMap<String, JavaVersion> temp = new HashMap<>();
temp.put(THIS_JAVA.getVersion(), THIS_JAVA);
List<JavaVersion> javaVersions; List<JavaVersion> javaVersions;
switch (OperatingSystem.CURRENT_OS) { switch (OperatingSystem.CURRENT_OS) {
case WINDOWS: case WINDOWS:
@ -192,9 +190,7 @@ public final class JavaVersion implements Serializable {
javaVersions = Collections.emptyList(); javaVersions = Collections.emptyList();
break; break;
} }
for (JavaVersion v : javaVersions) JAVAS = Collections.unmodifiableList(javaVersions);
temp.put(v.getVersion(), v);
JAVAS = Collections.unmodifiableMap(temp);
LATCH.countDown(); LATCH.countDown();
} }