Skip to content

Commit 99381b2

Browse files
authored
Merge pull request #234 from J3fftw1/feature/blob_build
2 parents a6e15a0 + 3e2935a commit 99381b2

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.github.bakedlibs.dough.updater;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import io.github.bakedlibs.dough.versions.PrefixedVersion;
6+
import org.bukkit.plugin.Plugin;
7+
8+
import javax.annotation.Nonnull;
9+
import java.io.File;
10+
import java.net.MalformedURLException;
11+
import java.net.URI;
12+
import java.net.URISyntaxException;
13+
import java.net.URL;
14+
import java.util.logging.Level;
15+
16+
// TODO: add a way to get the current version.
17+
// TODO: checksum checking.
18+
public class BlobBuildUpdater extends AbstractPluginUpdater<PrefixedVersion> {
19+
20+
private static final String SITE_URL = "https://blob.build";
21+
private static final String API_URL = SITE_URL + "/api/projects";
22+
23+
private final String project;
24+
private final String releaseChannel;
25+
26+
public BlobBuildUpdater(@Nonnull Plugin plugin, @Nonnull File file, @Nonnull String project) {
27+
this(plugin, file, project, "Dev");
28+
}
29+
30+
public BlobBuildUpdater(@Nonnull Plugin plugin, @Nonnull File file, @Nonnull String project, @Nonnull String releaseChannel) {
31+
super(plugin, file, new PrefixedVersion(releaseChannel, 0));
32+
33+
this.project = project;
34+
this.releaseChannel = releaseChannel;
35+
}
36+
37+
@Override
38+
public void start() {
39+
try {
40+
URL versionsURL = new URI(API_URL + "/" + project + "/" + releaseChannel + "/latest").toURL();
41+
42+
scheduleAsyncUpdateTask(new UpdaterTask<PrefixedVersion>(this, versionsURL) {
43+
44+
@Override
45+
public UpdateInfo parse(String result) throws MalformedURLException, URISyntaxException {
46+
JsonObject json = (JsonObject) JsonParser.parseString(result);
47+
48+
if (json == null) {
49+
getLogger().log(Level.WARNING, "The Auto-Updater could not connect to Blob.build, is it down?");
50+
return null;
51+
}
52+
53+
JsonObject data = json.getAsJsonObject("data");
54+
int latestVersion = data.get("build_id").getAsInt();
55+
URL downloadURL = new URI(data.get("file_download_url").getAsString()).toURL();
56+
57+
return new UpdateInfo(downloadURL, new PrefixedVersion(releaseChannel, latestVersion));
58+
}
59+
});
60+
} catch (MalformedURLException | URISyntaxException e ) {
61+
getLogger().log(Level.SEVERE, "Auto-Updater URL is malformed", e);
62+
}
63+
}
64+
}

dough-updater/src/main/java/io/github/bakedlibs/dough/updater/UpdaterTask.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.IOException;
88
import java.io.InputStreamReader;
99
import java.net.MalformedURLException;
10+
import java.net.URISyntaxException;
1011
import java.net.URL;
1112
import java.net.URLConnection;
1213
import java.nio.charset.StandardCharsets;
@@ -37,7 +38,7 @@ abstract class UpdaterTask<V extends Version> implements Runnable {
3738
}
3839

3940
@Nullable
40-
public abstract UpdateInfo parse(String result) throws MalformedURLException;
41+
public abstract UpdateInfo parse(String result) throws MalformedURLException, URISyntaxException;
4142

4243
@Override
4344
public void run() {
@@ -64,7 +65,7 @@ private UpdateInfo getLatestVersion(@Nonnull URL url) {
6465
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
6566
return parse(reader.readLine());
6667
}
67-
} catch (IOException e) {
68+
} catch (IOException | URISyntaxException e) {
6869
plugin.getLogger().log(Level.WARNING, "Could not connect to the updating site, is it down?", e);
6970
return null;
7071
}

0 commit comments

Comments
 (0)