Skip to content

Commit

Permalink
Optimize upgrade process.
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Nov 10, 2024
1 parent 8610453 commit c378a99
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 51 deletions.
65 changes: 54 additions & 11 deletions HMCL/src/main/java/net/burningtnt/hmclprs/PRCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@
import javafx.scene.Node;
import javafx.scene.layout.VBox;
import net.burningtnt.hmclprs.hooks.EntryPoint;
import net.burningtnt.hmclprs.hooks.Final;
import net.burningtnt.hmclprs.hooks.HookContainer;
import net.burningtnt.hmclprs.impl.FinalValue;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.ui.animation.ContainerAnimations;
import org.jackhuang.hmcl.ui.animation.TransitionPane;
import org.jackhuang.hmcl.ui.construct.AnnouncementCard;
import org.jackhuang.hmcl.upgrade.RemoteVersion;

import javax.swing.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@HookContainer
public final class PRCollection {
private PRCollection() {
}

@Final
private static volatile String defaultFullName;
private static final FinalValue<String> defaultFullName = new FinalValue<>();

@Final
private static volatile String defaultVersion;
private static final FinalValue<String> defaultVersion = new FinalValue<>();

@EntryPoint(when = EntryPoint.LifeCycle.BOOTSTRAP, type = EntryPoint.Type.INJECT)
public static void onApplicationLaunch() {
Expand All @@ -40,19 +47,19 @@ public static String onInitApplicationName(String name) {

@EntryPoint(when = EntryPoint.LifeCycle.BOOTSTRAP, type = EntryPoint.Type.VALUE_MUTATION)
public static String onInitApplicationFullName(String fullName) {
defaultFullName = fullName;
defaultFullName.setValue(fullName);
return fullName + PRCollectionConstants.PR_COLLECTION_SUFFIX;
}

@EntryPoint(when = EntryPoint.LifeCycle.BOOTSTRAP, type = EntryPoint.Type.VALUE_MUTATION)
public static String onInitApplicationVersion(String version) {
defaultVersion = version;
defaultVersion.setValue(version);
return version + PRCollectionConstants.PR_COLLECTION_SUFFIX;
}

@EntryPoint(when = EntryPoint.LifeCycle.BOOTSTRAP, type = EntryPoint.Type.REDIRECT)
public static String onInitApplicationTitle() {
return defaultFullName + " v" + defaultVersion + PRCollectionConstants.PR_COLLECTION_SUFFIX;
return defaultFullName.getValue() + " v" + defaultVersion.getValue() + PRCollectionConstants.PR_COLLECTION_SUFFIX;
}

@EntryPoint(when = EntryPoint.LifeCycle.BOOTSTRAP, type = EntryPoint.Type.REDIRECT)
Expand All @@ -67,7 +74,7 @@ public static String onInitApplicationDefaultUpdateLink() {

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.REDIRECT)
public static String onGetApplicationRawVersion() {
return defaultVersion;
return defaultVersion.getValue();
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.VALUE_MUTATION)
Expand Down Expand Up @@ -96,6 +103,42 @@ public static void onUpdateFrom(Runnable updateRunnable) {
Platform.runLater(updateRunnable);
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.REDIRECT)
public static List<String> prepareFallbackURLs(RemoteVersion rv) {
if (rv.getChannel() == null) {
return Collections.emptyList();
}

return Arrays.stream(PRCollectionConstants.UPDATE_FALLBACKS).parallel().map(s -> {
try {
RemoteVersion r = RemoteVersion.fetch(null, s);
FileDownloadTask.IntegrityCheck r1 = r.getIntegrityCheck(), r2 = rv.getIntegrityCheck();
if (!Objects.equals(r1.getAlgorithm(), r2.getAlgorithm()) || !Objects.equals(r1.getChecksum(), r2.getChecksum())) {
throw new IllegalArgumentException("Integrity check is mismatched");
}

return r.getUrl();
} catch (IOException e) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList());
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.REDIRECT)
public static List<URL> onGetRemoteVersionUpdateLinks(RemoteVersion rv) {
return Stream.concat(Stream.of(rv.getUrl()), rv.prc$fallbackURL.stream()).map(s -> {
try {
return new URL(s);
} catch (MalformedURLException e) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList());
}

public static void onCreateUpgradeDialog() {
throw new UnsupportedOperationException("UpdateDialog has been deprecated");
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.INJECT)
public static void importRef(Class<?> clazz) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ final class PRCollectionConstants {
private PRCollectionConstants() {
}

static final String PR_COLLECTION_SUFFIX = " (PR Collection)";
public static final String PR_COLLECTION_SUFFIX = " (PR Collection)";

static final String HOME_PAGE = "https://github.com/burningtnt/HMCL/pull/9";
public static final String HOME_PAGE = "https://github.com/burningtnt/HMCL/pull/9";

static final String UPDATE_LINK = "https://hmcl-snapshot-update-73w.pages.dev/redirect/v1/type/pr-collection";
public static final String UPDATE_LINK = "https://hmcl-snapshot-update-73w.pages.dev/redirect/v1/type/pr-collection";

static final boolean SHOULD_DISPLAY_LAUNCH_WARNING = shouldDisplayWarningMessage("hmcl.pr.warning", "HMCL_PR_WARNING");
public static final String[] UPDATE_FALLBACKS = {
"https://cdn.crashmc.com/https://raw.githubusercontent.com/burningtnt/HMCL-Snapshot-Update/v5/artifacts/v5/uploaders/local-storage.proxy.crashmc/burningtnt/HMCL/prs/gradle.yml.jar.json"
};

public static final boolean SHOULD_DISPLAY_LAUNCH_WARNING = shouldDisplayWarningMessage("hmcl.pr.warning", "HMCL_PR_WARNING");

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME)
static String getWarningTitle() {
public static String getWarningTitle() {
return i18n("prs.title");
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME)
static String getWarningBody() {
public static String getWarningBody() {
return i18n("prs.warning", HOME_PAGE);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.burningtnt.hmclprs.hooks;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Documented
@Retention(RetentionPolicy.CLASS)
public @interface EntryPoint {
LifeCycle when();
Expand Down
14 changes: 0 additions & 14 deletions HMCL/src/main/java/net/burningtnt/hmclprs/hooks/Final.java

This file was deleted.

11 changes: 0 additions & 11 deletions HMCL/src/main/java/net/burningtnt/hmclprs/hooks/HookContainer.java

This file was deleted.

25 changes: 25 additions & 0 deletions HMCL/src/main/java/net/burningtnt/hmclprs/impl/FinalValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.burningtnt.hmclprs.impl;

public final class FinalValue<T> {
private volatile T value;

public FinalValue() {
}

public T getValue() {
T v = value;
if (v == null) {
throw new IllegalStateException("Final Value hasn't been initialized.");
}
return v;
}

public void setValue(T v) {
synchronized (this) {
if (value != null) {
throw new IllegalStateException("Final Value has been initialized.");
}
this.value = v;
}
}
}
4 changes: 2 additions & 2 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/UpgradeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

public final class UpgradeDialog extends JFXDialogLayout {
public UpgradeDialog(RemoteVersion remoteVersion, Runnable updateRunnable) {
net.burningtnt.hmclprs.PRCollection.onCreateUpgradeDialog();

setHeading(new Label(i18n("update.changelog")));
setBody(new ProgressIndicator());

Expand Down Expand Up @@ -87,7 +89,5 @@ public UpgradeDialog(RemoteVersion remoteVersion, Runnable updateRunnable) {

setActions(openInBrowser, updateButton, cancelButton);
onEscPressed(this, cancelButton::fire);

net.burningtnt.hmclprs.PRCollection.onUpdateFrom(updateRunnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class HMCLDownloadTask extends FileDownloadTask {
private RemoteVersion.Type archiveFormat;

public HMCLDownloadTask(RemoteVersion version, Path target) {
super(NetworkUtils.toURL(version.getUrl()), target.toFile(), version.getIntegrityCheck());
super(net.burningtnt.hmclprs.PRCollection.onGetRemoteVersionUpdateLinks(version), target.toFile(), version.getIntegrityCheck());
net.burningtnt.hmclprs.PRCollection.importRef(NetworkUtils.class);
archiveFormat = version.getType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ public static RemoteVersion fetch(UpdateChannel channel, String url) throws IOEx
private final IntegrityCheck integrityCheck;
private final boolean force;

public final java.util.List<String> prc$fallbackURL;

public RemoteVersion(UpdateChannel channel, String version, String url, Type type, IntegrityCheck integrityCheck, boolean force) {
this.channel = channel;
this.version = version;
this.url = url;
this.type = type;
this.integrityCheck = integrityCheck;
this.force = force;

this.prc$fallbackURL = net.burningtnt.hmclprs.PRCollection.prepareFallbackURLs(this);
}

public UpdateChannel getChannel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public static void updateFrom(RemoteVersion version) {
return;
}

Controllers.dialog(new UpgradeDialog(version, () -> {
net.burningtnt.hmclprs.PRCollection.importRef(UpgradeDialog.class);
{
Path downloaded;
try {
downloaded = Files.createTempFile("hmcl-update-", ".jar");
Expand Down Expand Up @@ -142,7 +143,7 @@ public static void updateFrom(RemoteVersion version) {
}
}
});
}));
}
}

private static void applyUpdate(Path target) throws IOException {
Expand Down

0 comments on commit c378a99

Please sign in to comment.