Skip to content

Commit

Permalink
Improve update notification to include button to download page
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Mar 16, 2021
1 parent c433d1a commit e5e928c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ public File downloadUpdate() {
return source.downloadUpdate(this);
}

/**
* Get the URL where to download updates from manually
* @return The update url
*/
public String getUpdateUrl() {
return source.getUpdateUrl(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ModPackLauncher {
private final String name;
private final String version;
private final String latestVersion;
private final ModPackConfig updateConfig;
private final File tempFolder;
private final Properties properties = new Properties();
private final Map<SourceType, ModPackSource> sources = new EnumMap<>(SourceType.class);
Expand Down Expand Up @@ -97,10 +98,11 @@ private ModPackLauncher() {
sources.put(SourceType.GITHUB, new GitHubSource(this));
sources.put(SourceType.GITLAB, new GitLabSource(this));

latestVersion = new ModPackConfig(getName(), getSource(SourceType.GITHUB), mapOf(
updateConfig = new ModPackConfig(getName(), getSource(SourceType.GITHUB), mapOf(
"user", "MoepTv",
"repository", "AmongUs-ModPackLauncher"
)).getLatestVersion();
));
latestVersion = updateConfig.getLatestVersion();

// TODO: Read remote or from config
modPackConfigs.add(new ModPackConfig("TheOtherRoles", getSource(SourceType.GITHUB), mapOf(
Expand Down Expand Up @@ -152,7 +154,7 @@ private ModPackLauncher() {
}

if (System.console() == null && !GraphicsEnvironment.isHeadless()) {
new ModPackLauncherGui(this).setVisible(true);
new ModPackLauncherGui(this);
} else {
log(Level.SEVERE, "This software requires a GUI!\n");
}
Expand Down Expand Up @@ -187,6 +189,10 @@ public String getLatestVersion() {
return latestVersion;
}

public String getUpdateUrl() {
return updateConfig.getUpdateUrl();
}

public boolean hasNewerVersion() {
if (latestVersion == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand All @@ -27,6 +28,8 @@
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Collections;

Expand Down Expand Up @@ -169,8 +172,26 @@ public void mouseClicked(MouseEvent e) {
setLocationRelativeTo(null);

if (launcher.hasNewerVersion()) {
JOptionPane.showMessageDialog(this, "The update " + launcher.getLatestVersion() + " is available! (Installed: " + launcher.getVersion() + ")", "Update available!", JOptionPane.INFORMATION_MESSAGE);
int n = JOptionPane.showOptionDialog(
this,
"The update " + launcher.getLatestVersion() + " is available! (Installed: " + launcher.getVersion() + ") ",
"Update available!",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[] {"Go to download site", "Download later"},
"Go to download site"
);
if (n == 0) {
try {
Desktop.getDesktop().browse(new URI(launcher.getUpdateUrl()));
ModPackLauncherGui.this.setVisible(false);
ModPackLauncherGui.this.dispose();
return;
} catch (URISyntaxException | IOException ignored) {}
}
}
setVisible(true);
}

private void updateModPackList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public File downloadUpdate(ModPackConfig config) {
return null;
}

@Override
public String getUpdateUrl(ModPackConfig config) {
return new Replacer().replace(config.getPlaceholders()).replaceIn(download);
}

@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class GitHubSource extends ModPackSource {
private static final List<String> REQUIRED_PLACEHOLDERS = Arrays.asList("user");
private static final String API_HEADER = "application/vnd.github.v3+json";
private static final String RELEASES_URL = "https://api.github.com/repos/%user%/%repository%/releases";
private static final String UPDATE_URL = "https://github.com/%user%/%repository%/releases/tag/%version%";

public GitHubSource(ModPackLauncher launcher) {
super(launcher, REQUIRED_PLACEHOLDERS);
Expand Down Expand Up @@ -161,6 +162,12 @@ public File downloadUpdate(ModPackConfig config) {
return null;
}

@Override
public String getUpdateUrl(ModPackConfig config) {
String latestVersion = getLatestVersion(config);
return new Replacer().replace(config.getPlaceholders("repository")).replace("version", latestVersion).replaceIn(UPDATE_URL);
}

@Override
public String getName() {
return getType().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
public class GitLabSource extends ModPackSource {

private static final List<String> REQUIRED_PLACEHOLDERS = Arrays.asList("user");
private static final String API_URL = "https://gitlab.com/api/v4/";
private static final String URL = "https://gitlab.com";
private static final String API_URL = "%url%/api/v4/";
private static final String RELEASES_URL = "%apiurl%projects/%user%%2F%repository%/releases";
private static final String UPDATE_URL = "%url%/%user%/%repository%/-/releases/%version%";

public GitLabSource(ModPackLauncher launcher) {
super(launcher, REQUIRED_PLACEHOLDERS);
Expand All @@ -52,7 +54,7 @@ public GitLabSource(ModPackLauncher launcher) {
@Override
public String getLatestVersion(ModPackConfig config) {
try {
Replacer replacer = new Replacer().replace("apiurl", API_URL).replace(config.getPlaceholders("repository"));
Replacer replacer = new Replacer().replace("apiurl", API_URL).replace("url", URL).replace(config.getPlaceholders("repository"));
String[] properties = new String[0];
if (config.getPlaceholders().containsKey("token")) {
properties = new String[] {"Private-Token", config.getPlaceholders().get("token")};
Expand Down Expand Up @@ -148,6 +150,12 @@ public File downloadUpdate(ModPackConfig config) {
return null;
}

@Override
public String getUpdateUrl(ModPackConfig config) {
String version = getLatestVersion(config);
return new Replacer().replace("url", URL).replace(config.getPlaceholders("repository")).replace("version", version).replaceIn(UPDATE_URL);
}

@Override
public String getName() {
return getType().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,25 @@ public Collection<String> getRequiredPlaceholders() {

/**
* Get the latest version of a plugin.
* @param config The plugin config
* @param config The mod pack config
* @return The latest version string or <code>null</code> if not found or an error occured
*/
public abstract String getLatestVersion(ModPackConfig config);

/**
* Download the latest version of a plugin into the target folder specified by the Updater.
* @param config The plugin config
* @param config The mod pack config
* @return A reference to the newly downloaded file or <code>null</code> if not found
*/
public abstract File downloadUpdate(ModPackConfig config);

/**
* Get the URL where to download updates from manually
* @param config The mod pack config
* @return The update url
*/
public abstract String getUpdateUrl(ModPackConfig config);

/**
* Get the name of the source
* @return The name of the source
Expand Down

0 comments on commit e5e928c

Please sign in to comment.