|
| 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 | +} |
0 commit comments