Skip to content

Commit

Permalink
Add MohistUpdater (#24)
Browse files Browse the repository at this point in the history
* Add PandaSpigotUpdater

Signed-off-by: Minionguyjpro <minionguyjpro@gmail.com>

* Add MohistUpdater

Signed-off-by: Minionguyjpro <minionguyjpro@gmail.com>

* Remove unnecessary fileName variable from MohistUpdater.java

---------

Signed-off-by: Minionguyjpro <minionguyjpro@gmail.com>
  • Loading branch information
Minionguyjpro authored Sep 23, 2024
1 parent 028c00a commit e481fb2
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class UpdateBuilder {
registerUpdater(versionQuery -> new SpongeUpdater(versionQuery, false, true), "spongevanilla-recommended");
registerUpdater(versionQuery -> new SpongeUpdater(versionQuery, true, false), "spongeforge");
registerUpdater(versionQuery -> new SpongeUpdater(versionQuery, true, true), "spongeforge-recommended");
registerUpdater(versionQuery -> new MohistUpdater(versionQuery, "mohist"), "mohist");
registerUpdater(PlazmaUpdater::new, "plazma");
registerUpdater(KaiijuUpdater::new, "kaiiju", "kaiijumc");
registerUpdater(DivineUpdater::new, "divine", "divinemc");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

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 MohistUpdater 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 MohistUpdater(VersionQuery versionQuery, String project) {
this.updateBuilder = versionQuery.updateBuilder;
projectUrl = String.format("https://mohistmc.com/api/v2/projects/%s/", project);
versionUrl = projectUrl + "%s/";
buildUrl = versionUrl + "builds/";
downloadUrl = buildUrl + "%s/download";

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(buildUrl, 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");
int lastIndex = builds.length() - 1;
return Integer.toString(builds.getJSONObject(lastIndex).getInt("number"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private JSONObject getDownload() throws IOException {
String formattedUrl = String.format(buildUrl, version);
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));
JSONArray builds = jsonObject.getJSONArray("builds");
return builds.getJSONObject(builds.length() - 1); // Return the last build's JSONObject
}

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

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

@Override
public InputStream getInputStream() {
try {
JSONObject download = getDownload();
} catch (Exception e) {
e.printStackTrace();
return null;
}

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 e481fb2

Please sign in to comment.