2016-06-18 17:16:32 +02:00
|
|
|
package me.libraryaddict.disguise.utilities;
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
import com.google.gson.Gson;
|
2020-02-01 23:59:18 +01:00
|
|
|
import lombok.Getter;
|
2020-04-19 04:55:39 +02:00
|
|
|
import me.libraryaddict.disguise.LibsDisguises;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils;
|
2018-12-16 02:47:42 +01:00
|
|
|
|
2020-02-13 07:47:02 +01:00
|
|
|
import java.io.BufferedReader;
|
2020-04-19 04:55:39 +02:00
|
|
|
import java.io.File;
|
2018-12-16 02:47:42 +01:00
|
|
|
import java.io.InputStream;
|
2020-02-13 07:47:02 +01:00
|
|
|
import java.io.InputStreamReader;
|
2016-06-18 17:16:32 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2018-12-16 02:47:42 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Map;
|
2020-04-07 04:23:33 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-02-13 07:47:02 +01:00
|
|
|
import java.util.stream.Collectors;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2016-11-28 22:10:55 +01:00
|
|
|
public class UpdateChecker {
|
2018-12-16 02:47:42 +01:00
|
|
|
private final String resourceID;
|
2020-02-01 23:59:18 +01:00
|
|
|
@Getter
|
2016-06-18 17:16:32 +02:00
|
|
|
private String latestVersion;
|
2020-02-01 23:59:18 +01:00
|
|
|
@Getter
|
2018-12-16 02:47:42 +01:00
|
|
|
private int latestSnapshot;
|
2020-04-07 04:23:33 +02:00
|
|
|
private final long started = System.currentTimeMillis();
|
2020-04-19 04:55:39 +02:00
|
|
|
private int lastDownload;
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
public UpdateChecker(String resourceID) {
|
|
|
|
this.resourceID = resourceID;
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 04:55:39 +02:00
|
|
|
public boolean grabSnapshotBuild() {
|
|
|
|
if (getLatestSnapshot() == 0) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastDownload == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getLatestSnapshot() == lastDownload) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return grabSnapshotBuild(getLatestSnapshot());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean grabSnapshotBuild(int buildNo) {
|
|
|
|
boolean result = grabSnapshotBuild(
|
|
|
|
"https://ci.md-5.net/job/LibsDisguises/" + buildNo + "/artifact/target/LibsDisguises.jar");
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
lastDownload = buildNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean grabLatestSnapshot() {
|
|
|
|
boolean result = grabSnapshotBuild(
|
|
|
|
"https://ci.md-5.net/job/LibsDisguises/lastSuccessfulBuild/artifact/target/LibsDisguises.jar");
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
lastDownload = LibsDisguises.getInstance().getBuildNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isDownloading() {
|
|
|
|
return lastDownload == -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLastDownload() {
|
|
|
|
return lastDownload;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean grabSnapshotBuild(String urlString) {
|
|
|
|
DisguiseUtilities.getLogger().info("Now downloading latest build of Lib's Disguises from " + urlString);
|
|
|
|
lastDownload = -1;
|
|
|
|
|
|
|
|
File dest = new File(Bukkit.getUpdateFolderFile(), "LibsDisguises.jar");
|
|
|
|
|
2020-04-22 04:37:58 +02:00
|
|
|
if (dest.exists()) {
|
|
|
|
dest.delete();
|
2020-04-19 04:55:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 04:37:58 +02:00
|
|
|
dest.getParentFile().mkdirs();
|
|
|
|
|
2020-04-19 04:55:39 +02:00
|
|
|
try {
|
|
|
|
// We're connecting to spigot's API
|
|
|
|
URL url = new URL(urlString);
|
|
|
|
// Creating a connection
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
// Get the input stream, what we receive
|
|
|
|
try (InputStream input = con.getInputStream()) {
|
|
|
|
FileUtils.copyInputStreamToFile(input, dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
DisguiseUtilities.getLogger().info("Download success!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
// Failed, set the last download back to previous build
|
|
|
|
dest.delete();
|
|
|
|
DisguiseUtilities.getLogger().warning("Failed to download snapshot build.");
|
|
|
|
lastDownload = 0;
|
2020-04-22 04:37:58 +02:00
|
|
|
ex.printStackTrace();
|
2020-04-19 04:55:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
public void checkSnapshotUpdate(int buildNumber) {
|
|
|
|
Map<String, Object> lastBuild = fetchLastSnapshotBuild();
|
|
|
|
|
|
|
|
if (lastBuild == null || !lastBuild.containsKey("id") || !lastBuild.containsKey("timestamp")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int newBuildNumber = Integer.parseInt((String) lastBuild.get("id"));
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
// If new build number is same or older
|
|
|
|
if (newBuildNumber <= buildNumber) {
|
2016-06-18 17:16:32 +02:00
|
|
|
return;
|
2018-12-16 02:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Date newBuildDate = new Date(((Number) lastBuild.get("timestamp")).longValue());
|
2016-06-18 17:16:32 +02:00
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
// If the new snapshot is at least 3 days old
|
|
|
|
/*if (newBuildDate.getTime() >= System.currentTimeMillis() - TimeUnit.DAYS.toMillis(3)) {
|
2016-06-18 17:16:32 +02:00
|
|
|
return;
|
2018-12-16 02:47:42 +01:00
|
|
|
}*/
|
|
|
|
|
|
|
|
latestSnapshot = newBuildNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkOfficialUpdate(String currentVersion) {
|
|
|
|
String version = fetchSpigotVersion();
|
|
|
|
|
|
|
|
if (version == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNewerVersion(currentVersion, version)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
|
|
|
|
latestVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks spigot for the version
|
|
|
|
*/
|
2018-12-16 02:47:42 +01:00
|
|
|
private String fetchSpigotVersion() {
|
2016-11-28 22:10:55 +01:00
|
|
|
try {
|
2018-12-16 02:47:42 +01:00
|
|
|
// We're connecting to spigot's API
|
2020-01-18 02:52:53 +01:00
|
|
|
URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceID);
|
2018-12-16 02:47:42 +01:00
|
|
|
// Creating a connection
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
// Get the input stream, what we receive
|
|
|
|
try (InputStream input = con.getInputStream()) {
|
|
|
|
// Read it to string
|
2020-04-05 04:35:39 +02:00
|
|
|
String version = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()
|
|
|
|
.collect(Collectors.joining("\n"));
|
2018-12-16 02:47:42 +01:00
|
|
|
|
|
|
|
// If the version is not empty, return it
|
|
|
|
if (!version.isEmpty()) {
|
|
|
|
return version;
|
|
|
|
}
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-28 22:10:55 +01:00
|
|
|
catch (Exception ex) {
|
2018-08-14 02:42:35 +02:00
|
|
|
DisguiseUtilities.getLogger().warning("Failed to check for a update on spigot.");
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
2018-08-14 02:42:35 +02:00
|
|
|
|
2016-06-18 17:16:32 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
private boolean isNewerVersion(String currentVersion, String newVersion) {
|
2020-04-07 04:23:33 +02:00
|
|
|
currentVersion = currentVersion.replaceAll("(v)|(-SNAPSHOT)", "");
|
|
|
|
newVersion = newVersion.replaceAll("(v)|(-SNAPSHOT)", "");
|
|
|
|
|
|
|
|
// If the server has been online for less than 6 hours and both versions are 1.1.1 kind of versions
|
|
|
|
if (started + TimeUnit.HOURS.toMillis(6) > System.currentTimeMillis() &&
|
|
|
|
currentVersion.matches("[0-9]+(\\.[0-9]+)*") && newVersion.matches("[0-9]+(\\.[0-9]+)*")) {
|
2020-04-07 06:15:17 +02:00
|
|
|
|
|
|
|
int cVersion = Integer.parseInt(currentVersion.replace(".", ""));
|
|
|
|
int nVersion = Integer.parseInt(newVersion.replace(".", ""));
|
2020-04-07 04:23:33 +02:00
|
|
|
|
|
|
|
// If the current version is a higher version, and is only a higher version by 3 minor numbers
|
|
|
|
// Then we have a cache problem
|
2020-04-07 06:15:17 +02:00
|
|
|
if (cVersion > nVersion && nVersion + 3 > cVersion) {
|
2020-04-07 04:23:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 04:35:39 +02:00
|
|
|
// Lets just ignore all this fancy logic, and say that if you're not on the current release, you're outdated!
|
2020-04-07 04:23:33 +02:00
|
|
|
return !currentVersion.equals(newVersion);
|
2020-04-05 04:35:39 +02:00
|
|
|
|
|
|
|
/*
|
2019-03-05 05:47:21 +01:00
|
|
|
// Remove 'v' and '-SNAPSHOT' from string, split by decimal points
|
|
|
|
String[] cSplit = currentVersion.replaceAll("(v)|(-SNAPSHOT)", "").split("\\.");
|
|
|
|
String[] nSplit = newVersion.replaceAll("(v)|(-SNAPSHOT)", "").split("\\.");
|
2018-12-16 02:47:42 +01:00
|
|
|
|
2020-04-05 04:35:39 +02:00
|
|
|
// Lets just ignore all this fancy logic, and say that if you're not on the current release, you're outdated!
|
|
|
|
return !Arrays.equals(cSplit, nSplit);
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
// Iterate over the versions from left to right
|
|
|
|
for (int i = 0; i < Math.max(cSplit.length, nSplit.length); i++) {
|
|
|
|
// If the current version doesn't have the next version, then it's older
|
|
|
|
if (cSplit.length <= i) {
|
|
|
|
return true;
|
|
|
|
} else if (nSplit.length <= i) {
|
|
|
|
// If the new version doesn't have the next version, then it's older
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:48:53 +01:00
|
|
|
// If both strings are numerical
|
|
|
|
if (cSplit[i].matches("[0-9]+") && nSplit[i].matches("[0-9]+")) {
|
|
|
|
int cInt = Integer.parseInt(cSplit[i]);
|
|
|
|
int nInt = Integer.parseInt(nSplit[i]);
|
|
|
|
|
|
|
|
// Same version
|
|
|
|
if (cInt == nInt) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return if current version is inferior to new version
|
|
|
|
return cInt < nInt;
|
|
|
|
}
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
// String compare the versions, should perform the same as an int compare
|
|
|
|
int compareResult = cSplit[i].compareTo(nSplit[i]);
|
|
|
|
|
|
|
|
// Same version
|
|
|
|
if (compareResult == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return if current version is inferior to new versio
|
|
|
|
return compareResult < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both versions should be the same, return false as it's not a newer version
|
2020-04-05 04:35:39 +02:00
|
|
|
return false;*/
|
2018-12-16 02:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches from jenkins, using the REST api the last snapshot build information
|
|
|
|
*/
|
|
|
|
private Map<String, Object> fetchLastSnapshotBuild() {
|
|
|
|
try {
|
|
|
|
// We're connecting to md_5's jenkins REST api
|
|
|
|
URL url = new URL("https://ci.md-5.net/job/LibsDisguises/lastSuccessfulBuild/api/json");
|
|
|
|
// Creating a connection
|
|
|
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
|
Map<String, Object> jsonObject;
|
|
|
|
|
|
|
|
// Get the input stream, what we receive
|
|
|
|
try (InputStream input = con.getInputStream()) {
|
|
|
|
// Read it to string
|
2020-04-05 04:35:39 +02:00
|
|
|
String json = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()
|
|
|
|
.collect(Collectors.joining("\n"));
|
2018-12-16 02:47:42 +01:00
|
|
|
|
|
|
|
jsonObject = new Gson().fromJson(json, Map.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
DisguiseUtilities.getLogger().warning("Failed to check for a snapshot update on jenkins.");
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
2018-12-16 02:47:42 +01:00
|
|
|
|
|
|
|
return null;
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
|
|
|
}
|