Skip to content

Commit

Permalink
add LeavesMC updater
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Sep 10, 2024
1 parent 257eaee commit a886848
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public final class UpdateBuilder {
registerUpdater(KaiijuUpdater::new, "kaiiju", "kaiijumc");
registerUpdater(DivineUpdater::new, "divine", "divinemc");
registerUpdater(LeafUpdater::new, "leaf");
registerUpdater(versionQuery -> new LeavesUpdater(versionQuery, "leaves"), "leaves", "leavesmc");
registerUpdater(versionQuery -> new LeavesUpdater(versionQuery, "lumina"), "lumina");
}

private final String project;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package me.hsgamer.mcserverupdater.updater;

import me.hsgamer.hscore.logger.common.Logger;
import me.hsgamer.hscore.web.UserAgent;
import me.hsgamer.hscore.web.WebUtils;
import me.hsgamer.mcserverupdater.UpdateBuilder;
import me.hsgamer.mcserverupdater.api.FileDigestChecksum;
import me.hsgamer.mcserverupdater.api.InputStreamUpdater;
import me.hsgamer.mcserverupdater.util.VersionQuery;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.security.MessageDigest;

public class LeavesUpdater implements InputStreamUpdater, FileDigestChecksum {
private final UpdateBuilder updateBuilder;
private final String version;
private final String build;
private final String projectUrl;
private final String versionUrl;
private final String buildUrl;
private final String downloadUrl;

public LeavesUpdater(VersionQuery versionQuery, String project) {
this.updateBuilder = versionQuery.updateBuilder;
projectUrl = String.format("https://api.leavesmc.org/v2/projects/%s/", project);
versionUrl = projectUrl + "versions/%s/";
buildUrl = versionUrl + "builds/%s/";
downloadUrl = buildUrl + "downloads/ghproxy";

version = versionQuery.isDefault ? getDefaultVersion() : versionQuery.version;
build = getBuild();
}

private String getDefaultVersion() {
updateBuilder.debug("Getting default version from " + projectUrl);
try {
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(projectUrl));
InputStream inputStream = connection.getInputStream();
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
JSONArray builds = jsonObject.getJSONArray("versions");
return builds.getString(builds.length() - 1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private String getBuild() {
String formattedUrl = String.format(versionUrl, version);
updateBuilder.debug("Getting latest build from " + formattedUrl);
try {
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(formattedUrl));
InputStream inputStream = connection.getInputStream();
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
JSONArray builds = jsonObject.getJSONArray("builds");
return Integer.toString(builds.getInt(builds.length() - 1));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private JSONObject getDownload() throws IOException {
String formattedUrl = String.format(buildUrl, version, build);
updateBuilder.debug("Getting download from " + formattedUrl);
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(formattedUrl));
InputStream inputStream = connection.getInputStream();
JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream));
JSONObject downloads = jsonObject.getJSONObject("downloads");
return downloads.getJSONObject("application");
}

@Override
public MessageDigest getMessageDigest() throws Exception {
return MessageDigest.getInstance("SHA-256");
}

@Override
public String getChecksum() {
try {
JSONObject application = getDownload();
return application.getString("sha256");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

@Override
public InputStream getInputStream() {
String formattedUrl = String.format(downloadUrl, version, build);
updateBuilder.debug("Getting input stream from " + formattedUrl);
try {
URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(formattedUrl));
return connection.getInputStream();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

@Override
public Logger getLogger() {
return updateBuilder.logger();
}
}

0 comments on commit a886848

Please sign in to comment.