Skip to content

Commit

Permalink
Merge pull request #246 from vengestone-dragoon/Custom-check-suport
Browse files Browse the repository at this point in the history
Custom check suport
  • Loading branch information
Osiris-Team authored Jun 23, 2024
2 parents 40c8156 + 54fd737 commit 938c363
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 19 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ enforce {
r.dependencies.add('org.jetbrains.kotlin:kotlin-stdlib-common:1.9.23')
r.dependencies.add('org.slf4j:slf4j-api:2.0.13')
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@
package com.osiris.autoplug.client.tasks.updater.plugins;

public class MinecraftPlugin {
private boolean isPremium;
private String configPath;
private String installationPath;
private String name;
private String version;
private String author;
private int spigotId;
private int bukkitId;
private boolean ignoreContentType;
private String customDownloadURL;
private String githubRepoName;
private String githubAssetName;
private String jenkinsProjectUrl;
private String jenkinsArtifactName;
private int jenkinsBuildId;
private String modrinthId;
public boolean isPremium;
public String configPath;
public String installationPath;
public String name;
public String version;
public String author;
public int spigotId;
public int bukkitId;
public boolean ignoreContentType;
public String customDownloadURL;
public String customCheckURL;
public String githubRepoName;
public String githubAssetName;
public String jenkinsProjectUrl;
public String jenkinsArtifactName;
public int jenkinsBuildId;
public String modrinthId;
public String forceUpdate;

public MinecraftPlugin(String installationPath, String name, String version, String author, int spigotId, int bukkitId, String customDownloadURL) {
this.installationPath = installationPath;
Expand Down Expand Up @@ -174,6 +176,14 @@ public void setCustomDownloadURL(String customDownloadURL) {
this.customDownloadURL = customDownloadURL;
}

public String getCustomCheckURL() {
return customCheckURL;
}

public void setCustomCheckURL(String customCheckURL) {
this.customCheckURL = customCheckURL;
}

public String toPrintString() {
return "name='" + name + "' version='" + version + "' author='" + author + "' path='" + installationPath + "'";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.osiris.autoplug.client.tasks.updater.search.spigot.SpigotSearchByAuthor;
import com.osiris.autoplug.client.tasks.updater.search.spigot.SpigotSearchById;
import com.osiris.autoplug.client.tasks.updater.search.spigot.SpigotSearchByName;
import com.osiris.autoplug.client.tasks.updater.search.CustomCheckURL;

public class ResourceFinder {

Expand Down Expand Up @@ -119,4 +120,10 @@ public SearchResult findByJenkinsUrl(MinecraftPlugin plugin) {
sr.plugin = plugin;
return sr;
}

public SearchResult findByCustomCheckURL(MinecraftPlugin plugin) {
SearchResult sr = new CustomCheckURL().doCustomCheck(plugin);
sr.plugin = plugin;
return sr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ else if (e.getHttpErrorCode() == 404) {
YamlSection jenkinsProjectUrl = pluginsConfig.put(name, plName, "alternatives", "jenkins", "project-url");
YamlSection jenkinsArtifactName = pluginsConfig.put(name, plName, "alternatives", "jenkins", "artifact-name");
YamlSection jenkinsBuildId = pluginsConfig.put(name, plName, "alternatives", "jenkins", "build-id").setDefValues("0");
YamlSection forceUpdate = pluginsConfig.put(name, plName, "force-update");

// The plugin devs can add their spigot/bukkit ids to their plugin.yml files
if (installedPlugin.getSpigotId() != 0 && spigotId.asString() != null && spigotId.asInt() == 0) // Don't update the value, if the user has already set it
Expand Down Expand Up @@ -212,12 +213,14 @@ else if (e.getHttpErrorCode() == 404) {
installedPlugin.setBukkitId(bukkitId.asInt());
installedPlugin.setIgnoreContentType(ignoreContentType.asBoolean());
installedPlugin.setCustomDownloadURL(customDownloadURL.asString());
installedPlugin.setCustomCheckURL(customCheckURL.asString());
installedPlugin.setGithubRepoName(githubRepoName.asString());
installedPlugin.setGithubAssetName(githubAssetName.asString());
installedPlugin.setJenkinsProjectUrl(jenkinsProjectUrl.asString());
installedPlugin.setJenkinsArtifactName(jenkinsArtifactName.asString());
installedPlugin.setJenkinsBuildId(jenkinsBuildId.asInt());
installedPlugin.setModrinthId(modrinthId.asString());
installedPlugin.forceUpdate = forceUpdate.asString();

// Check for missing plugin details in plugin.yml
if (jenkinsArtifactName.asString() != null && jenkinsProjectUrl.asString() != null)
Expand Down Expand Up @@ -279,6 +282,7 @@ else if (installedPlugin.getVersion() == null || installedPlugin.getVersion().tr
setMax(includedSize);

// TODO USE THIS FOR RESULT REPORT
int sizeCustomPlugins = 0;
int sizeJenkinsPlugins = 0;
int sizeGithubPlugins = 0;
int sizeSpigotPlugins = 0;
Expand All @@ -301,7 +305,10 @@ else if (installedPlugin.getVersion() == null || installedPlugin.getVersion().tr
includedPlugins) {
try {
setStatus("Initialising update check for " + pl.getName() + "...");
if (pl.getJenkinsProjectUrl() != null) { // JENKINS PLUGIN
if (pl.customCheckURL != null) { // Custome Check
sizeCustomPlugins++;
activeFutures.add(executorService.submit(() -> new ResourceFinder().findByCustomCheckURL(pl)));
} else if (pl.getJenkinsProjectUrl() != null) { // JENKINS PLUGIN
sizeJenkinsPlugins++;
activeFutures.add(executorService.submit(() -> new ResourceFinder().findByJenkinsUrl(pl)));
} else if (pl.getGithubRepoName() != null) { // GITHUB PLUGIN
Expand Down Expand Up @@ -481,6 +488,9 @@ private void doDownloadLogic(@NotNull MinecraftPlugin pl, SearchResult result) {
String resultBukkitId = result.getBukkitId();
if (pl.getCustomDownloadURL() != null) downloadUrl = pl.getCustomDownloadURL();

if (pl.forceUpdate.equals("true") && code == 0)
code = 1;

if (code == 0) {
//getSummary().add("Plugin " +pl.getName()+ " is already on the latest version (" + pl.getVersion() + ")"); // Only for testing right now
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.osiris.autoplug.client.tasks.updater.search;

import com.google.gson.JsonObject;
import com.osiris.autoplug.client.tasks.updater.plugins.MinecraftPlugin;
import com.osiris.autoplug.client.tasks.updater.search.SearchResult;
import com.osiris.autoplug.client.utils.UtilsURL;
import com.osiris.jlib.json.Json;
import com.osiris.jlib.logger.AL;

import java.io.File;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;


public class CustomCheckURL {

public CustomCheckURL(){}

private boolean isInt(String s) {
try {
Integer.parseInt(s);
return true;
} catch (Exception e) {
return false;
}
}

public SearchResult doCustomCheck(MinecraftPlugin plugin) {

String url = plugin.getCustomCheckURL();
url = new UtilsURL().clean(url);
Exception exception = null;
String latest = null;
String type = ".jar";
String downloadUrl = null;
byte code = 0;
try {
JsonObject release;
try {
release = Json.getAsJsonArray(url)
.get(0).getAsJsonObject();
} catch (Exception e) {
throw e;
}

String[] versionNaming = {"version_number", "version"};

for (String naming : versionNaming) {
if (release.has(naming)) {
String version = release.get(naming).getAsString().replaceAll("[^0-9.]", "");
if (!version.isEmpty()) {
latest = version;
break;
}
}
}

String[] downloadNaming = {"download_url", "download", "file", "download_file"};

for (String naming : downloadNaming) {
if (release.has(naming)) {
String durl = release.get(naming).getAsString();
if (!durl.isEmpty()) {
downloadUrl = durl;
break;
}
}
}

String[] pluginVersionComponents = plugin.getVersion().split("\\.");
String[] latestVersionComponents = latest.split("\\.");

for (int i = 0; i < Math.min(pluginVersionComponents.length, latestVersionComponents.length); i++) {
int pluginComponent = Integer.parseInt(pluginVersionComponents[i]);
int latestComponent = Integer.parseInt(latestVersionComponents[i]);

if (pluginComponent < latestComponent) {
// plugin.getVersion() is smaller than latest
code = 1;
break;
} else if (pluginComponent > latestComponent) {
// plugin.getVersion() is greater than latest
break;
}
}

} catch (Exception e) {
exception = e;
code = 2;
}

if (downloadUrl == null && plugin.customDownloadURL == null)
code = 2;
SearchResult result = new SearchResult(null, code, latest, downloadUrl, type, null, null, false);
result.setException(exception);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ void checkInternetAccessMethod2() throws Exception {
connection.connect();
connection.disconnect();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.osiris.autoplug.client.tasks.updater;

import com.osiris.autoplug.client.tasks.updater.plugins.MinecraftPlugin;
import com.osiris.autoplug.client.tasks.updater.search.SearchResult;
import com.osiris.autoplug.client.tasks.updater.search.CustomCheckURL;
import com.osiris.autoplug.client.tasks.updater.mods.ModrinthAPI;
import com.osiris.autoplug.client.UtilsTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;


class TestCustomUpdateCheck {
@Test
void test() {
MinecraftPlugin pl = new MinecraftPlugin("./plugins/", "Chunky", "0.0.0", "pop4959", 0, 0, null);
pl.customCheckURL = "https://api.modrinth.com/v2/project/chunky/version";
pl.customDownloadURL = "https://cdn.modrinth.com/data/fALzjamp/versions/dPliWter/Chunky-1.4.16.jar";
SearchResult sr = new CustomCheckURL().doCustomCheck(pl);
assertTrue(1 == sr.resultCode);
}
}

0 comments on commit 938c363

Please sign in to comment.