fix(javafx): should download javafx at beginning, not let user choose download source first.

This commit is contained in:
huanghongxun 2021-10-13 13:58:33 +08:00
parent 3fad76aae1
commit 673bc75cd5
3 changed files with 31 additions and 39 deletions

View File

@ -35,6 +35,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.CancellationException;
import java.util.logging.Level; import java.util.logging.Level;
import static org.jackhuang.hmcl.util.Lang.thread; import static org.jackhuang.hmcl.util.Lang.thread;
@ -99,7 +100,7 @@ public final class Main {
} catch (SelfDependencyPatcher.IncompatibleVersionException e) { } catch (SelfDependencyPatcher.IncompatibleVersionException e) {
LOG.log(Level.SEVERE, "unable to patch JVM", e); LOG.log(Level.SEVERE, "unable to patch JVM", e);
showErrorAndExit(i18n("fatal.javafx.incompatible")); showErrorAndExit(i18n("fatal.javafx.incompatible"));
} catch (SelfDependencyPatcher.CanceledException e) { } catch (CancellationException e) {
LOG.log(Level.SEVERE, "User cancels downloading JavaFX", e); LOG.log(Level.SEVERE, "User cancels downloading JavaFX", e);
System.exit(0); System.exit(0);
} }

View File

@ -41,14 +41,14 @@
*/ */
package org.jackhuang.hmcl.util; package org.jackhuang.hmcl.util;
import static java.lang.Class.forName; import com.google.gson.Gson;
import static java.nio.charset.StandardCharsets.UTF_8; import com.google.gson.reflect.TypeToken;
import static java.util.stream.Collectors.toSet; import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
import static org.jackhuang.hmcl.Metadata.HMCL_DIRECTORY; import org.jackhuang.hmcl.util.io.IOUtils;
import static org.jackhuang.hmcl.util.Logging.LOG; import org.jackhuang.hmcl.util.platform.Architecture;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import static org.jackhuang.hmcl.util.platform.JavaVersion.CURRENT_JAVA;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
@ -59,18 +59,18 @@ import java.io.UncheckedIOException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*;
import java.util.List; import java.util.List;
import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.*; import static java.lang.Class.forName;
import static java.nio.charset.StandardCharsets.UTF_8;
import org.jackhuang.hmcl.util.io.ChecksumMismatchException; import static java.util.stream.Collectors.toSet;
import org.jackhuang.hmcl.util.io.IOUtils; import static org.jackhuang.hmcl.Metadata.HMCL_DIRECTORY;
import org.jackhuang.hmcl.util.platform.Architecture; import static org.jackhuang.hmcl.util.Logging.LOG;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
import static org.jackhuang.hmcl.util.platform.JavaVersion.CURRENT_JAVA;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
// From: https://github.com/Col-E/Recaf/blob/7378b397cee664ae81b7963b0355ef8ff013c3a7/src/main/java/me/coley/recaf/util/self/SelfDependencyPatcher.java // From: https://github.com/Col-E/Recaf/blob/7378b397cee664ae81b7963b0355ef8ff013c3a7/src/main/java/me/coley/recaf/util/self/SelfDependencyPatcher.java
public final class SelfDependencyPatcher { public final class SelfDependencyPatcher {
@ -202,7 +202,7 @@ public final class SelfDependencyPatcher {
/** /**
* Patch in any missing dependencies, if any. * Patch in any missing dependencies, if any.
*/ */
public static void patch() throws PatchException, IncompatibleVersionException, CanceledException { public static void patch() throws PatchException, IncompatibleVersionException, CancellationException {
// Do nothing if JavaFX is detected // Do nothing if JavaFX is detected
try { try {
try { try {
@ -316,10 +316,6 @@ public final class SelfDependencyPatcher {
* @throws IOException When the files cannot be fetched or saved. * @throws IOException When the files cannot be fetched or saved.
*/ */
private static void fetchDependencies(List<DependencyDescriptor> dependencies) throws IOException { private static void fetchDependencies(List<DependencyDescriptor> dependencies) throws IOException {
class BooleanHole {
volatile boolean value = false;
}
boolean isFirstTime = true; boolean isFirstTime = true;
byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE]; byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
@ -327,16 +323,16 @@ public final class SelfDependencyPatcher {
int count = 0; int count = 0;
while (true) { while (true) {
BooleanHole isCancelled = new BooleanHole(); AtomicBoolean isCancelled = new AtomicBoolean();
BooleanHole showDetails = new BooleanHole(); AtomicBoolean showDetails = new AtomicBoolean();
ProgressFrame dialog = new ProgressFrame(i18n("download.javafx")); ProgressFrame dialog = new ProgressFrame(i18n("download.javafx"));
dialog.setProgressMaximum(dependencies.size() + 1); dialog.setProgressMaximum(dependencies.size() + 1);
dialog.setProgress(count); dialog.setProgress(count);
dialog.setOnCancel(() -> isCancelled.value = true); dialog.setOnCancel(() -> isCancelled.set(true));
dialog.setOnChangeSource(() -> { dialog.setOnChangeSource(() -> {
isCancelled.value = true; isCancelled.set(true);
showDetails.value = true; showDetails.set(true);
}); });
dialog.setVisible(true); dialog.setVisible(true);
try { try {
@ -350,8 +346,8 @@ public final class SelfDependencyPatcher {
} }
Files.createDirectories(DependencyDescriptor.DEPENDENCIES_DIR_PATH); Files.createDirectories(DependencyDescriptor.DEPENDENCIES_DIR_PATH);
for (int i = count; i < dependencies.size(); i++) { for (int i = count; i < dependencies.size(); i++) {
if (isCancelled.value) { if (isCancelled.get()) {
throw new CanceledException(); throw new CancellationException();
} }
DependencyDescriptor dependency = dependencies.get(i); DependencyDescriptor dependency = dependencies.get(i);
@ -369,13 +365,13 @@ public final class SelfDependencyPatcher {
int read; int read;
while ((read = is.read(buffer, 0, IOUtils.DEFAULT_BUFFER_SIZE)) >= 0) { while ((read = is.read(buffer, 0, IOUtils.DEFAULT_BUFFER_SIZE)) >= 0) {
if (isCancelled.value) { if (isCancelled.get()) {
try { try {
os.close(); os.close();
} finally { } finally {
Files.deleteIfExists(dependency.localPath()); Files.deleteIfExists(dependency.localPath());
} }
throw new CanceledException(); throw new CancellationException();
} }
os.write(buffer, 0, read); os.write(buffer, 0, read);
} }
@ -383,9 +379,9 @@ public final class SelfDependencyPatcher {
verifyChecksum(dependency); verifyChecksum(dependency);
count++; count++;
} }
} catch (CanceledException e) { } catch (CancellationException e) {
dialog.dispose(); dialog.dispose();
if (showDetails.value) { if (showDetails.get()) {
repository = showChooseRepositoryDialog(); repository = showChooseRepositoryDialog();
continue; continue;
} else { } else {
@ -439,9 +435,6 @@ public final class SelfDependencyPatcher {
public static class IncompatibleVersionException extends Exception { public static class IncompatibleVersionException extends Exception {
} }
public static class CanceledException extends RuntimeException {
}
public static class ProgressFrame extends JDialog { public static class ProgressFrame extends JDialog {
private final JProgressBar progressBar; private final JProgressBar progressBar;

View File

@ -17,7 +17,6 @@
*/ */
package org.jackhuang.hmcl.game; package org.jackhuang.hmcl.game;
import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.Range; import org.jackhuang.hmcl.util.Range;
import org.jackhuang.hmcl.util.platform.Architecture; import org.jackhuang.hmcl.util.platform.Architecture;
import org.jackhuang.hmcl.util.platform.JavaVersion; import org.jackhuang.hmcl.util.platform.JavaVersion;
@ -28,7 +27,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.Objects; import java.util.Objects;
import static org.jackhuang.hmcl.download.LibraryAnalyzer.LAUNCH_WRAPPER_MAIN; import static org.jackhuang.hmcl.download.LibraryAnalyzer.LAUNCH_WRAPPER_MAIN;
import static org.jackhuang.hmcl.util.Pair.pair;
public enum JavaVersionConstraint { public enum JavaVersionConstraint {