[FIX] Сравнение версии на гитхабе с текущей, а не просто вывод. + [FEATURE] Возможность выключения оповещения об обновлениях.

This commit is contained in:
zaxar163 2019-01-22 16:49:58 +03:00
parent f430879e14
commit d62d039a6a
No known key found for this signature in database
GPG key ID: E3B309DD3852DE06
3 changed files with 47 additions and 6 deletions

View file

@ -111,6 +111,8 @@ public static final class Config {
public String startScript;
public boolean updatesNotify = true; // Defaultly to true
public String getAddress() {
return address;
}

View file

@ -9,11 +9,16 @@
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.kohsuke.github.GHRelease;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import ru.gravit.launcher.Launcher;
import ru.gravit.utils.Version;
import ru.gravit.utils.Version.Type;
import ru.gravit.utils.helper.LogHelper;
public class Updater extends TimerTask {
@ -21,7 +26,6 @@ public class Updater extends TimerTask {
private static final long period = 1000*3600;
private final Timer taskPool;
private final GHRepository gravitLauncher;
private GHRelease parenRel = null;
public Updater(LaunchServer srv) {
this.taskPool = new Timer("Updater thread", true);
@ -35,20 +39,54 @@ public Updater(LaunchServer srv) {
this.gravitLauncher = gravitLauncherTmp;
run();
taskPool.schedule(this, new Date(System.currentTimeMillis()+period), period);
if (srv.config.updatesNotify) taskPool.schedule(this, new Date(System.currentTimeMillis()+period), period);
}
@Override
public void run() {
try {
GHRelease rel = gravitLauncher.getLatestRelease();
if (rel.equals(parenRel)) return;
LogHelper.warning("Latest release: %s", rel.getName());
LogHelper.warning("It`s published at: " + DATE_TIME_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(rel.getPublished_at().getTime()), ZoneId.systemDefault())));
parenRel = rel;
Version relV = parseVer(rel.getTagName());
if (Launcher.getVersion().major >= relV.major || Launcher.getVersion().minor >= relV.minor
|| Launcher.getVersion().patch >= relV.patch || Launcher.getVersion().build >= relV.build) return;
if (relV.release.equals(Type.STABLE) || relV.release.equals(Type.LTS)) {
LogHelper.warning("New %s release: %s", relV.getReleaseStatus(), relV.getVersionString());
LogHelper.warning("You can download it: " + rel.getHtmlUrl().toString());
LogHelper.warning("It`s published at: " + DATE_TIME_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(rel.getPublished_at().getTime()), ZoneId.systemDefault())));
} else {
LogHelper.debug("New %s release: %s", relV.getReleaseStatus(), relV.getVersionString());
LogHelper.debug("You can download it: " + rel.getHtmlUrl());
LogHelper.debug("It`s published at: " + DATE_TIME_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(rel.getPublished_at().getTime()), ZoneId.systemDefault())));
}
} catch (IOException e) {
LogHelper.error(e);
}
}
private static final Pattern startingVerPattern = Pattern.compile("\\d+\\.\\d+\\.\\d+");
private static final Pattern pointPatternStriper = Pattern.compile("\\.");
private static Version parseVer(String tag) {
String relS = "4.3.4-stable";
Matcher verMatcher = startingVerPattern.matcher(relS);
if (!verMatcher.find()) return null;
String[] ver = pointPatternStriper.split(relS.substring(verMatcher.start(), verMatcher.end()));
if (ver.length < 3) return null;
return new Version(Integer.parseInt(ver[0]), Integer.parseInt(ver[1]),
Integer.parseInt(ver[2]), ver.length > 3 ? Integer.parseInt(ver[3]) : 0, findRelType(relS.substring(verMatcher.end()+1)));
}
private static Type findRelType(String substring) {
if (substring.length() < 3 || substring.isEmpty()) return Type.UNKNOWN;
String tS = substring;
while (tS.startsWith("-")) tS = tS.substring(1);
tS = tS.toLowerCase(Locale.ENGLISH);
if (tS.contains("lts")) return Type.LTS;
if (tS.contains("stable")) return Type.STABLE;
if (tS.contains("dev")) return Type.DEV;
if (tS.contains("alpha")) return Type.ALPHA;
if (tS.contains("beta")) return Type.BETA;
if (tS.contains("exp")) return Type.EXPERIMENTAL;
return Type.UNKNOWN;
}
}

View file

@ -56,6 +56,7 @@
"isDownloadJava": false,
"isWarningMissArchJava": true,
"enabledProGuard": true,
"updatesNotify": true,
"stripLineNumbers": true,
"deleteTempFiles": true,
"startScript": ".\\start.sh"