Refactor UpdaterCheckerTask
Update checking/downloading/installing can now be configured precisely and more intuitive in the config.
This commit is contained in:
parent
1fde72e522
commit
b46f6fbb1f
18 changed files with 395 additions and 302 deletions
|
|
@ -5,7 +5,6 @@ import java.util.logging.Logger;
|
|||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.bukkit.managers.BukkitPluginManager;
|
||||
import net.frankheijden.serverutils.bukkit.managers.BukkitTaskManager;
|
||||
import net.frankheijden.serverutils.bukkit.managers.BukkitVersionManager;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
|
||||
public class BukkitPlugin extends ServerUtilsPlugin {
|
||||
|
|
@ -15,7 +14,6 @@ public class BukkitPlugin extends ServerUtilsPlugin {
|
|||
private final BukkitTaskManager taskManager;
|
||||
private final BukkitResourceProvider resourceProvider;
|
||||
private final BukkitChatProvider chatProvider;
|
||||
private final BukkitVersionManager versionManager;
|
||||
|
||||
/**
|
||||
* Creates a new BukkitPlugin instance of ServerUtils.
|
||||
|
|
@ -27,7 +25,6 @@ public class BukkitPlugin extends ServerUtilsPlugin {
|
|||
this.taskManager = new BukkitTaskManager();
|
||||
this.resourceProvider = new BukkitResourceProvider(plugin);
|
||||
this.chatProvider = new BukkitChatProvider();
|
||||
this.versionManager = new BukkitVersionManager(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -52,11 +49,6 @@ public class BukkitPlugin extends ServerUtilsPlugin {
|
|||
return chatProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BukkitVersionManager getVersionManager() {
|
||||
return versionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return plugin.getLogger();
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
package net.frankheijden.serverutils.bukkit.managers;
|
||||
|
||||
import net.frankheijden.serverutils.bukkit.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||
|
||||
public class BukkitVersionManager extends AbstractVersionManager {
|
||||
|
||||
/**
|
||||
* Creates a new VersionManager instance.
|
||||
* Used for automatic updating.
|
||||
*/
|
||||
public BukkitVersionManager(ServerUtils plugin) {
|
||||
super(plugin.getDescription().getVersion());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
settings:
|
||||
disable-plugins-command: false
|
||||
check-updates: true
|
||||
check-updates-boot: true
|
||||
check-updates-login: false
|
||||
download-updates: false
|
||||
download-at-startup-and-update: false
|
||||
download-updates-boot: false
|
||||
download-updates-login: false
|
||||
install-updates-boot: false
|
||||
install-updates-login: false
|
||||
|
||||
disabled-commands:
|
||||
- "plugin:command"
|
||||
|
|
@ -5,7 +5,6 @@ import java.util.logging.Logger;
|
|||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
|
||||
import net.frankheijden.serverutils.bungee.managers.BungeeTaskManager;
|
||||
import net.frankheijden.serverutils.bungee.managers.BungeeVersionManager;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||
|
||||
|
|
@ -16,7 +15,6 @@ public class BungeePlugin extends ServerUtilsPlugin {
|
|||
private final BungeeTaskManager taskManager;
|
||||
private final BungeeResourceProvider resourceProvider;
|
||||
private final BungeeChatProvider chatProvider;
|
||||
private final BungeeVersionManager versionManager;
|
||||
|
||||
/**
|
||||
* Creates a new BungeePlugin instance of ServerUtils.
|
||||
|
|
@ -28,7 +26,6 @@ public class BungeePlugin extends ServerUtilsPlugin {
|
|||
this.taskManager = new BungeeTaskManager();
|
||||
this.resourceProvider = new BungeeResourceProvider(plugin);
|
||||
this.chatProvider = new BungeeChatProvider();
|
||||
this.versionManager = new BungeeVersionManager(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -53,11 +50,6 @@ public class BungeePlugin extends ServerUtilsPlugin {
|
|||
return chatProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BungeeVersionManager getVersionManager() {
|
||||
return versionManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return plugin.getLogger();
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
package net.frankheijden.serverutils.bungee.managers;
|
||||
|
||||
import net.frankheijden.serverutils.bungee.ServerUtils;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||
|
||||
public class BungeeVersionManager extends AbstractVersionManager {
|
||||
|
||||
/**
|
||||
* Creates a new VersionManager instance.
|
||||
* Used for automatic updating.
|
||||
*/
|
||||
public BungeeVersionManager(ServerUtils plugin) {
|
||||
super(plugin.getDescription().getVersion());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
settings:
|
||||
check-updates: true
|
||||
check-updates-boot: true
|
||||
check-updates-login: false
|
||||
download-updates: false
|
||||
download-at-startup-and-update: false
|
||||
download-updates-boot: false
|
||||
download-updates-login: false
|
||||
install-updates-boot: false
|
||||
install-updates-login: false
|
||||
|
|
@ -1,3 +1,7 @@
|
|||
plugins {
|
||||
id 'net.kyori.blossom' version '1.1.0'
|
||||
}
|
||||
|
||||
group = rootProject.group + '.common'
|
||||
version = rootProject.version
|
||||
archivesBaseName = rootProject.name + '-Common'
|
||||
|
|
@ -11,6 +15,11 @@ dependencies {
|
|||
compileOnly 'com.google.code.gson:gson:2.8.6'
|
||||
}
|
||||
|
||||
blossom {
|
||||
replaceTokenIn('src/main/java/net/frankheijden/serverutils/common/ServerUtilsApp.java')
|
||||
replaceToken '{version}', version
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
exclude 'net/frankheijden/serverutilsupdater/**'
|
||||
exclude 'plugin.yml'
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package net.frankheijden.serverutils.common;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
|
||||
|
||||
public class ServerUtilsApp<T> {
|
||||
|
||||
public static final int BSTATS_METRICS_ID = 7790;
|
||||
public static final String VERSION = "{version}";
|
||||
|
||||
private final T platformPlugin;
|
||||
private final ServerUtilsPlugin plugin;
|
||||
|
|
@ -28,9 +28,7 @@ public class ServerUtilsApp<T> {
|
|||
* Tries checking for updates if enabled by the config.
|
||||
*/
|
||||
public static void tryCheckForUpdates() {
|
||||
if (Config.getInstance().getConfig().getBoolean("settings.check-updates")) {
|
||||
UpdateCheckerTask.start(getPlugin().getChatProvider().getConsoleSender(), true);
|
||||
}
|
||||
UpdateCheckerTask.tryStart(getPlugin().getChatProvider().getConsoleSender(), "boot");
|
||||
}
|
||||
|
||||
public static ServerUtilsPlugin getPlugin() {
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@ package net.frankheijden.serverutils.common.entities;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractPluginManager;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractTaskManager;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||
import net.frankheijden.serverutils.common.managers.UpdateManager;
|
||||
import net.frankheijden.serverutils.common.providers.ChatProvider;
|
||||
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||
|
||||
public abstract class ServerUtilsPlugin {
|
||||
|
||||
private final UpdateManager updateManager = new UpdateManager();
|
||||
|
||||
public abstract <T> AbstractPluginManager<T> getPluginManager();
|
||||
|
||||
public abstract <T> AbstractTaskManager<T> getTaskManager();
|
||||
|
|
@ -20,7 +23,9 @@ public abstract class ServerUtilsPlugin {
|
|||
|
||||
public abstract ChatProvider getChatProvider();
|
||||
|
||||
public abstract AbstractVersionManager getVersionManager();
|
||||
public UpdateManager getUpdateManager() {
|
||||
return updateManager;
|
||||
}
|
||||
|
||||
public abstract Logger getLogger();
|
||||
|
||||
|
|
@ -44,7 +49,7 @@ public abstract class ServerUtilsPlugin {
|
|||
|
||||
File file = new File(getDataFolder(), targetName);
|
||||
if (!file.exists()) {
|
||||
getLogger().info(String.format("'%s' not found, creating!", targetName));
|
||||
getLogger().log(Level.INFO, "'{}' not found, creating!", targetName);
|
||||
try {
|
||||
FileUtils.saveResource(getResourceProvider().getResource(resource), file);
|
||||
} catch (IOException ex) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package net.frankheijden.serverutils.common.entities.http;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class GitHubAsset {
|
||||
|
||||
private final String name;
|
||||
private final String downloadUrl;
|
||||
|
||||
public GitHubAsset(String name, String downloadUrl) {
|
||||
this.name = name;
|
||||
this.downloadUrl = downloadUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new GitHubAsset from given release url.
|
||||
*/
|
||||
public static GitHubAsset from(JsonObject jsonObject) {
|
||||
JsonArray assets = jsonObject.getAsJsonArray("assets");
|
||||
if (assets != null && assets.size() > 0) {
|
||||
JsonObject assetJson = assets.get(0).getAsJsonObject();
|
||||
return new GitHubAsset(
|
||||
assetJson.get("name").getAsString(),
|
||||
assetJson.get("browser_download_url").getAsString()
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDownloadUrl() {
|
||||
return downloadUrl;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package net.frankheijden.serverutils.common.entities.http;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
public class GitHubRateLimit {
|
||||
|
||||
private final int limit;
|
||||
private final int used;
|
||||
private final int remaining;
|
||||
private final long reset;
|
||||
|
||||
/**
|
||||
* Constructs a new GitHubRateLimit object from given params.
|
||||
*/
|
||||
public GitHubRateLimit(int limit, int used, int remaining, long reset) {
|
||||
this.limit = limit;
|
||||
this.used = used;
|
||||
this.remaining = remaining;
|
||||
this.reset = reset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new GitHubRateLimit object from the given opened connection.
|
||||
*/
|
||||
public static GitHubRateLimit from(HttpURLConnection connection) {
|
||||
return new GitHubRateLimit(
|
||||
connection.getHeaderFieldInt("x-ratelimit-limit", 60),
|
||||
connection.getHeaderFieldInt("x-ratelimit-used", 0),
|
||||
connection.getHeaderFieldInt("x-ratelimit-remaining", 60),
|
||||
connection.getHeaderFieldLong("x-ratelimit-reset", System.currentTimeMillis() / 1000)
|
||||
);
|
||||
}
|
||||
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
public int getUsed() {
|
||||
return used;
|
||||
}
|
||||
|
||||
public int getRemaining() {
|
||||
return remaining;
|
||||
}
|
||||
|
||||
public boolean isRateLimited() {
|
||||
return remaining == 0;
|
||||
}
|
||||
|
||||
public long getReset() {
|
||||
return reset * 1000;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package net.frankheijden.serverutils.common.entities.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
public class GitHubResponse {
|
||||
|
||||
private final HttpURLConnection connection;
|
||||
private final GitHubRateLimit rateLimit;
|
||||
|
||||
public GitHubResponse(HttpURLConnection connection, GitHubRateLimit rateLimit) {
|
||||
this.connection = connection;
|
||||
this.rateLimit = rateLimit;
|
||||
}
|
||||
|
||||
public static GitHubResponse from(HttpURLConnection connection) {
|
||||
return new GitHubResponse(connection, GitHubRateLimit.from(connection));
|
||||
}
|
||||
|
||||
public HttpURLConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public GitHubRateLimit getRateLimit() {
|
||||
return rateLimit;
|
||||
}
|
||||
|
||||
public InputStream getStream() throws IOException {
|
||||
int res = connection.getResponseCode();
|
||||
return (res >= 200 && res <= 299) ? connection.getInputStream() : connection.getErrorStream();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +1,17 @@
|
|||
package net.frankheijden.serverutils.common.listeners;
|
||||
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.YamlConfig;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.tasks.UpdateCheckerTask;
|
||||
|
||||
public class ServerListener {
|
||||
|
||||
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||
|
||||
/**
|
||||
* Handles the update check on the given ServerCommandSender.
|
||||
* @param sender The sender which triggered the update.
|
||||
*/
|
||||
public static void handleUpdate(ServerCommandSender sender) {
|
||||
if (!config.getBoolean("settings.check-updates-login")) return;
|
||||
|
||||
if (sender.hasPermission("serverutils.notification.update")) {
|
||||
UpdateCheckerTask.start(sender);
|
||||
UpdateCheckerTask.tryStart(sender, "login");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
package net.frankheijden.serverutils.common.managers;
|
||||
|
||||
public abstract class AbstractVersionManager {
|
||||
|
||||
private final String currentVersion;
|
||||
private String downloadedVersion;
|
||||
|
||||
/**
|
||||
* Creates a new VersionManager instance.
|
||||
* Used for automatic updating.
|
||||
* @param currentVersion The current version of the plugin.
|
||||
*/
|
||||
public AbstractVersionManager(String currentVersion) {
|
||||
this.currentVersion = currentVersion;
|
||||
this.downloadedVersion = currentVersion;
|
||||
}
|
||||
|
||||
public String getCurrentVersion() {
|
||||
return currentVersion;
|
||||
}
|
||||
|
||||
public String getDownloadedVersion() {
|
||||
return downloadedVersion;
|
||||
}
|
||||
|
||||
public boolean hasDownloaded() {
|
||||
return !downloadedVersion.equals(currentVersion);
|
||||
}
|
||||
|
||||
public boolean isDownloaded(String version) {
|
||||
return downloadedVersion.equals(version);
|
||||
}
|
||||
|
||||
public void setDownloaded(String version) {
|
||||
this.downloadedVersion = version;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package net.frankheijden.serverutils.common.managers;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
|
||||
public class UpdateManager {
|
||||
|
||||
private JsonElement lastResponse;
|
||||
private String downloadedVersion;
|
||||
|
||||
public UpdateManager() {
|
||||
this.lastResponse = null;
|
||||
this.downloadedVersion = ServerUtilsApp.VERSION;
|
||||
}
|
||||
|
||||
public JsonElement getLastResponse() {
|
||||
return lastResponse;
|
||||
}
|
||||
|
||||
public void setLastResponse(JsonElement lastResponse) {
|
||||
this.lastResponse = lastResponse;
|
||||
}
|
||||
|
||||
public String getDownloadedVersion() {
|
||||
return downloadedVersion;
|
||||
}
|
||||
|
||||
public void setDownloadedVersion(String downloadedVersion) {
|
||||
this.downloadedVersion = downloadedVersion;
|
||||
}
|
||||
|
||||
public boolean hasDownloaded() {
|
||||
return !downloadedVersion.equals(ServerUtilsApp.VERSION);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package net.frankheijden.serverutils.common.tasks;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import java.io.File;
|
||||
|
|
@ -10,7 +9,6 @@ import java.net.ConnectException;
|
|||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import net.frankheijden.serverutils.common.ServerUtilsApp;
|
||||
import net.frankheijden.serverutils.common.config.Config;
|
||||
import net.frankheijden.serverutils.common.config.Messenger;
|
||||
|
|
@ -19,8 +17,10 @@ import net.frankheijden.serverutils.common.entities.LoadResult;
|
|||
import net.frankheijden.serverutils.common.entities.Result;
|
||||
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
|
||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||
import net.frankheijden.serverutils.common.entities.http.GitHubAsset;
|
||||
import net.frankheijden.serverutils.common.entities.http.GitHubResponse;
|
||||
import net.frankheijden.serverutils.common.managers.UpdateManager;
|
||||
import net.frankheijden.serverutils.common.utils.GitHubUtils;
|
||||
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
||||
import net.frankheijden.serverutilsupdater.common.Updater;
|
||||
|
||||
|
|
@ -29,110 +29,160 @@ public class UpdateCheckerTask implements Runnable {
|
|||
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
|
||||
private static final YamlConfig config = Config.getInstance().getConfig();
|
||||
|
||||
private final AbstractVersionManager versionManager;
|
||||
private final ServerCommandSender sender;
|
||||
private final boolean startup;
|
||||
private final boolean download;
|
||||
private final boolean install;
|
||||
|
||||
private static final String GITHUB_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtils/releases/latest";
|
||||
private static final String GITHUB_UPDATER_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtilsUpdater"
|
||||
+ "/releases/latest";
|
||||
|
||||
private static final String UPDATE_CHECK_START = "Checking for updates...";
|
||||
private static final String RATE_LIMIT = "Received ratelimit from GitHub.";
|
||||
private static final String GENERAL_ERROR = "Error fetching new version of ServerUtils";
|
||||
private static final String TRY_LATER = GENERAL_ERROR + ", please try again later!";
|
||||
private static final String CONNECTION_ERROR = GENERAL_ERROR + ": (%s) %s (maybe check your connection?)";
|
||||
private static final String UNAVAILABLE = GENERAL_ERROR + ": (%s) %s (no update available)";
|
||||
private static final String UPDATE_AVAILABLE = "ServerUtils %s is available!";
|
||||
private static final String DOWNLOAD_START = "Started downloading from \"%s\"...";
|
||||
private static final String CONNECTION_ERROR = GENERAL_ERROR + ": ({0}) {1} (maybe check your connection?)";
|
||||
private static final String UNAVAILABLE = GENERAL_ERROR + ": ({0}) {1} (no update available)";
|
||||
private static final String UPDATE_AVAILABLE = "ServerUtils {0} is available!";
|
||||
private static final String RELEASE_INFO = "Release info: {0}";
|
||||
private static final String DOWNLOAD_START = "Started downloading from \"{0}\"...";
|
||||
private static final String DOWNLOAD_ERROR = "Error downloading a new version of ServerUtils";
|
||||
private static final String DOWNLOADED_RESTART = "Downloaded ServerUtils version v%s. Restarting plugin now...";
|
||||
private static final String DOWNLOADED = "Downloaded ServerUtils version v{0}.";
|
||||
private static final String DOWNLOADED_RESTART = DOWNLOADED + " Restarting plugin now...";
|
||||
private static final String UPDATER_LOAD_ERROR = "Failed to load ServerUtilsUpdater: {0}";
|
||||
private static final String UPDATER_ENABLE_ERROR = "Failed to enable ServerUtilsUpdater: {0}";
|
||||
private static final String UP_TO_DATE = "We are up-to-date!";
|
||||
|
||||
private UpdateCheckerTask(ServerCommandSender sender, boolean startup) {
|
||||
this.versionManager = plugin.getVersionManager();
|
||||
private UpdateCheckerTask(ServerCommandSender sender, boolean download, boolean install) {
|
||||
this.sender = sender;
|
||||
this.startup = startup;
|
||||
this.download = download;
|
||||
this.install = install;
|
||||
}
|
||||
|
||||
public static void start(ServerCommandSender sender) {
|
||||
start(sender, false);
|
||||
/**
|
||||
* Checks for updates if enabled per config for the specific action.
|
||||
* Action must be 'login' or 'boot'.
|
||||
*/
|
||||
public static void tryStart(ServerCommandSender sender, String action) {
|
||||
if (config.getBoolean("settings.check-updates-" + action)) {
|
||||
start(sender, action);
|
||||
}
|
||||
}
|
||||
|
||||
public static void start(ServerCommandSender sender, boolean startup) {
|
||||
UpdateCheckerTask task = new UpdateCheckerTask(sender, startup);
|
||||
plugin.getTaskManager().runTaskAsynchronously(task);
|
||||
}
|
||||
|
||||
public boolean isStartupCheck() {
|
||||
return this.startup;
|
||||
/**
|
||||
* Checks for updates and downloads/installs if configured.
|
||||
* Action must be 'login' or 'boot'.
|
||||
*/
|
||||
public static void start(ServerCommandSender sender, String action) {
|
||||
plugin.getTaskManager().runTaskAsynchronously(new UpdateCheckerTask(
|
||||
sender,
|
||||
config.getBoolean("settings.download-updates-" + action),
|
||||
config.getBoolean("settings.install-updates-" + action)
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(UPDATE_CHECK_START);
|
||||
}
|
||||
plugin.getLogger().info(UPDATE_CHECK_START);
|
||||
|
||||
JsonObject pluginJson = getJson(GITHUB_LINK);
|
||||
JsonObject updaterJson = getJson(GITHUB_UPDATER_LINK);
|
||||
if (pluginJson == null || updaterJson == null) return;
|
||||
UpdateManager updateManager = plugin.getUpdateManager();
|
||||
|
||||
GitHubAsset pluginAsset = getAsset(pluginJson);
|
||||
GitHubAsset updaterAsset = getAsset(updaterJson);
|
||||
GitHubResponse pluginResponse = getResponse(GITHUB_LINK);
|
||||
if (pluginResponse == null) return;
|
||||
|
||||
JsonObject pluginJson = getJson(pluginResponse);
|
||||
if (pluginJson == null) return;
|
||||
|
||||
String githubVersion = getVersion(pluginJson);
|
||||
String body = pluginJson.getAsJsonPrimitive("body").getAsString();
|
||||
|
||||
String downloaded = versionManager.getDownloadedVersion();
|
||||
String current = versionManager.getCurrentVersion();
|
||||
if (VersionUtils.isNewVersion(downloaded, githubVersion)) {
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion));
|
||||
plugin.getLogger().info("Release info: " + body);
|
||||
if (!VersionUtils.isNewVersion(updateManager.getDownloadedVersion(), githubVersion)) {
|
||||
if (updateManager.hasDownloaded()) {
|
||||
broadcastDownloadStatus(githubVersion, false);
|
||||
} else {
|
||||
plugin.getLogger().info(UP_TO_DATE);
|
||||
}
|
||||
if (canDownloadPlugin() && pluginAsset != null && updaterAsset != null) {
|
||||
if (isStartupCheck()) {
|
||||
plugin.getLogger().info(String.format(DOWNLOAD_START, pluginAsset.downloadUrl));
|
||||
plugin.getLogger().info(String.format(DOWNLOAD_START, updaterAsset.downloadUrl));
|
||||
} else {
|
||||
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
||||
"%old%", current,
|
||||
"%new%", githubVersion,
|
||||
"%info%", body);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
File updaterTarget = new File(plugin.getPluginManager().getPluginsFolder(), updaterAsset.name);
|
||||
download(githubVersion, updaterAsset.downloadUrl, updaterTarget);
|
||||
plugin.getLogger().log(Level.INFO, UPDATE_AVAILABLE, githubVersion);
|
||||
plugin.getLogger().log(Level.INFO, RELEASE_INFO, body);
|
||||
|
||||
File pluginTarget = new File(plugin.getPluginManager().getPluginsFolder(), pluginAsset.name);
|
||||
downloadPlugin(githubVersion, pluginAsset.downloadUrl, pluginTarget);
|
||||
|
||||
plugin.getPluginManager().getPluginFile((Object) ServerUtilsApp.getPlatformPlugin()).delete();
|
||||
tryReloadPlugin(pluginTarget, updaterTarget);
|
||||
} else if (!isStartupCheck()) {
|
||||
GitHubAsset pluginAsset = GitHubAsset.from(pluginJson);
|
||||
if (!download || pluginAsset == null) {
|
||||
if (sender.isPlayer()) {
|
||||
Messenger.sendMessage(sender, "serverutils.update.available",
|
||||
"%old%", current,
|
||||
"%old%", ServerUtilsApp.VERSION,
|
||||
"%new%", githubVersion,
|
||||
"%info%", body);
|
||||
}
|
||||
} else if (versionManager.hasDownloaded()) {
|
||||
Messenger.sendMessage(sender, "serverutils.update.success",
|
||||
"%new%", versionManager.getDownloadedVersion());
|
||||
} else if (isStartupCheck()) {
|
||||
plugin.getLogger().info(UP_TO_DATE);
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getLogger().log(Level.INFO, DOWNLOAD_START, pluginAsset.getDownloadUrl());
|
||||
if (sender.isPlayer()) {
|
||||
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
||||
"%old%", ServerUtilsApp.VERSION,
|
||||
"%new%", githubVersion,
|
||||
"%info%", body);
|
||||
}
|
||||
|
||||
File pluginTarget = new File(plugin.getPluginManager().getPluginsFolder(), pluginAsset.getName());
|
||||
download(githubVersion, pluginAsset.getDownloadUrl(), pluginTarget);
|
||||
updateManager.setDownloadedVersion(githubVersion);
|
||||
if (!install) {
|
||||
deletePlugin();
|
||||
if (sender.isPlayer()) {
|
||||
broadcastDownloadStatus(githubVersion, false);
|
||||
} else {
|
||||
plugin.getLogger().log(Level.INFO, DOWNLOADED, githubVersion);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
GitHubResponse updaterResponse = getResponse(GITHUB_UPDATER_LINK);
|
||||
if (updaterResponse == null) return;
|
||||
|
||||
JsonObject updaterJson = getJson(updaterResponse);
|
||||
if (updaterJson == null) return;
|
||||
|
||||
GitHubAsset updaterAsset = GitHubAsset.from(updaterJson);
|
||||
if (updaterAsset == null) return;
|
||||
|
||||
plugin.getLogger().log(Level.INFO, DOWNLOAD_START, updaterAsset.getDownloadUrl());
|
||||
File updaterTarget = new File(plugin.getPluginManager().getPluginsFolder(), updaterAsset.getName());
|
||||
download(githubVersion, updaterAsset.getDownloadUrl(), updaterTarget);
|
||||
plugin.getLogger().log(Level.INFO, DOWNLOADED_RESTART, githubVersion);
|
||||
|
||||
deletePlugin();
|
||||
tryReloadPlugin(pluginTarget, updaterTarget);
|
||||
}
|
||||
|
||||
private JsonObject getJson(String urlString) {
|
||||
private GitHubResponse getResponse(String urlString) {
|
||||
try {
|
||||
GitHubResponse response = GitHubUtils.stream(urlString);
|
||||
if (!response.getRateLimit().isRateLimited()) return response;
|
||||
plugin.getLogger().info(RATE_LIMIT);
|
||||
} catch (ConnectException | UnknownHostException | SocketTimeoutException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, CONNECTION_ERROR, new Object[] {
|
||||
ex.getClass().getSimpleName(),
|
||||
ex.getMessage()
|
||||
});
|
||||
} catch (FileNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, UNAVAILABLE, new Object[] {
|
||||
ex.getClass().getSimpleName(),
|
||||
ex.getMessage()
|
||||
});
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, ex, () -> GENERAL_ERROR);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private JsonObject getJson(GitHubResponse res) {
|
||||
JsonElement jsonElement;
|
||||
try {
|
||||
jsonElement = FileUtils.readJsonFromUrl(urlString);
|
||||
} catch (ConnectException | UnknownHostException | SocketTimeoutException ex) {
|
||||
plugin.getLogger().severe(String.format(CONNECTION_ERROR, ex.getClass().getSimpleName(), ex.getMessage()));
|
||||
return null;
|
||||
} catch (FileNotFoundException ex) {
|
||||
plugin.getLogger().severe(String.format(UNAVAILABLE, ex.getClass().getSimpleName(), ex.getMessage()));
|
||||
return null;
|
||||
jsonElement = GitHubUtils.parseJson(res);
|
||||
} catch (IOException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, ex, () -> GENERAL_ERROR);
|
||||
return null;
|
||||
|
|
@ -142,6 +192,7 @@ public class UpdateCheckerTask implements Runnable {
|
|||
plugin.getLogger().warning(TRY_LATER);
|
||||
return null;
|
||||
}
|
||||
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
if (jsonObject.has("message")) {
|
||||
plugin.getLogger().warning(jsonObject.get("message").getAsString());
|
||||
|
|
@ -154,33 +205,6 @@ public class UpdateCheckerTask implements Runnable {
|
|||
return jsonObject.getAsJsonPrimitive("tag_name").getAsString().replace("v", "");
|
||||
}
|
||||
|
||||
private GitHubAsset getAsset(JsonObject jsonObject) {
|
||||
JsonArray assets = jsonObject.getAsJsonArray("assets");
|
||||
if (assets != null && assets.size() > 0) {
|
||||
JsonObject assetJson = assets.get(0).getAsJsonObject();
|
||||
return new GitHubAsset(
|
||||
assetJson.get("name").getAsString(),
|
||||
assetJson.get("browser_download_url").getAsString()
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean canDownloadPlugin() {
|
||||
if (isStartupCheck()) return config.getBoolean("settings.download-at-startup-and-update");
|
||||
return config.getBoolean("settings.download-updates");
|
||||
}
|
||||
|
||||
private void downloadPlugin(String githubVersion, String downloadLink, File target) {
|
||||
if (versionManager.isDownloaded(githubVersion)) {
|
||||
broadcastDownloadStatus(githubVersion, false);
|
||||
return;
|
||||
}
|
||||
|
||||
download(githubVersion, downloadLink, target);
|
||||
versionManager.setDownloaded(githubVersion);
|
||||
}
|
||||
|
||||
private void download(String githubVersion, String downloadLink, File target) {
|
||||
if (downloadLink == null) {
|
||||
broadcastDownloadStatus(githubVersion, true);
|
||||
|
|
@ -188,41 +212,43 @@ public class UpdateCheckerTask implements Runnable {
|
|||
}
|
||||
|
||||
try {
|
||||
FileUtils.download(downloadLink, target);
|
||||
GitHubResponse response = GitHubUtils.stream(downloadLink);
|
||||
if (response.getRateLimit().isRateLimited()) {
|
||||
plugin.getLogger().info(RATE_LIMIT);
|
||||
return;
|
||||
}
|
||||
|
||||
GitHubUtils.download(response, target);
|
||||
} catch (IOException ex) {
|
||||
broadcastDownloadStatus(githubVersion, true);
|
||||
throw new RuntimeException(DOWNLOAD_ERROR, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void deletePlugin() {
|
||||
plugin.getPluginManager().getPluginFile((Object) ServerUtilsApp.getPlatformPlugin()).delete();
|
||||
}
|
||||
|
||||
private void tryReloadPlugin(File pluginFile, File updaterFile) {
|
||||
plugin.getTaskManager().runTask(() -> {
|
||||
String downloadedVersion = versionManager.getDownloadedVersion();
|
||||
|
||||
if (isStartupCheck()) {
|
||||
Logger logger = plugin.getLogger();
|
||||
logger.info(String.format(DOWNLOADED_RESTART, downloadedVersion));
|
||||
|
||||
LoadResult<?> loadResult = plugin.getPluginManager().loadPlugin(updaterFile);
|
||||
Updater updater = (Updater) plugin.getPluginManager().getPlugin("ServerUtilsUpdater");
|
||||
if (!loadResult.isSuccess() && updater == null) {
|
||||
logger.severe("Failed to load ServerUtilsUpdater: " + loadResult.getResult().name());
|
||||
return;
|
||||
}
|
||||
|
||||
Result result = plugin.getPluginManager().enablePlugin(updater);
|
||||
if (result != Result.SUCCESS && result != Result.ALREADY_ENABLED) {
|
||||
logger.severe("Failed to enable ServerUtilsUpdater: " + loadResult.getResult().name());
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getPluginManager().disablePlugin(ServerUtilsApp.getPlatformPlugin());
|
||||
plugin.getPluginManager().unloadPlugin((Object) ServerUtilsApp.getPlatformPlugin()).tryClose();
|
||||
updater.update(pluginFile);
|
||||
updaterFile.delete();
|
||||
} else {
|
||||
broadcastDownloadStatus(downloadedVersion, false);
|
||||
LoadResult<?> loadResult = plugin.getPluginManager().loadPlugin(updaterFile);
|
||||
Updater updater = (Updater) plugin.getPluginManager().getPlugin("ServerUtilsUpdater");
|
||||
if (!loadResult.isSuccess() && updater == null) {
|
||||
plugin.getLogger().log(Level.INFO, UPDATER_LOAD_ERROR,
|
||||
loadResult.getResult().name());
|
||||
return;
|
||||
}
|
||||
|
||||
Result result = plugin.getPluginManager().enablePlugin(updater);
|
||||
if (result != Result.SUCCESS && result != Result.ALREADY_ENABLED) {
|
||||
plugin.getLogger().log(Level.INFO, UPDATER_ENABLE_ERROR, result.name());
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getPluginManager().disablePlugin(ServerUtilsApp.getPlatformPlugin());
|
||||
plugin.getPluginManager().unloadPlugin((Object) ServerUtilsApp.getPlatformPlugin()).tryClose();
|
||||
updater.update(pluginFile);
|
||||
updaterFile.delete();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -231,14 +257,4 @@ public class UpdateCheckerTask implements Runnable {
|
|||
String message = Messenger.getMessage(path, "%new%", githubVersion);
|
||||
plugin.getChatProvider().broadcast("serverutils.notification.update", message);
|
||||
}
|
||||
|
||||
private static class GitHubAsset {
|
||||
private final String name;
|
||||
private final String downloadUrl;
|
||||
|
||||
public GitHubAsset(String name, String downloadUrl) {
|
||||
this.name = name;
|
||||
this.downloadUrl = downloadUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,89 +4,28 @@ import com.google.gson.JsonElement;
|
|||
import com.google.gson.JsonParser;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0)"
|
||||
+ " Gecko/20100101"
|
||||
+ " Firefox/77.0";
|
||||
private FileUtils() {}
|
||||
|
||||
/**
|
||||
* Downloads file from a URL to a file location.
|
||||
* @param urlString The url to download.
|
||||
* @param target The location to save the output to.
|
||||
* @throws IOException If an I/O exception occurs.
|
||||
* Parses an InputStream into a JsonElement.
|
||||
*/
|
||||
public static void download(String urlString, File target) throws IOException {
|
||||
try (InputStream is = stream(urlString)) {
|
||||
if (is == null) return;
|
||||
try (ReadableByteChannel rbc = Channels.newChannel(is);
|
||||
FileOutputStream fos = new FileOutputStream(target)) {
|
||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a stream to an url.
|
||||
* @param url The url to stream.
|
||||
* @return An InputStream of the url.
|
||||
* @throws IOException If an I/O exception occurs.
|
||||
*/
|
||||
public static InputStream stream(String url) throws IOException {
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||
conn.setRequestProperty("User-Agent", USER_AGENT);
|
||||
conn.setConnectTimeout(10000);
|
||||
int res = conn.getResponseCode();
|
||||
return (res >= 200 && res <= 299) ? conn.getInputStream() : conn.getErrorStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all bytes from a reader and appends them to a string result.
|
||||
* @param reader The reader to read from.
|
||||
* @return All byte characters.
|
||||
* @throws IOException If an I/O exception occurs.
|
||||
*/
|
||||
public static String readAll(BufferedReader reader) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int cp;
|
||||
while ((cp = reader.read()) != -1) {
|
||||
sb.append((char) cp);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an url and converts it into a JsonElement.
|
||||
* @param url The url to read from.
|
||||
* @return The JsonElement.
|
||||
* @throws IOException If an I/O exception occurs.
|
||||
*/
|
||||
public static JsonElement readJsonFromUrl(String url) throws IOException {
|
||||
try (InputStream is = stream(url)) {
|
||||
if (is == null) return null;
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
|
||||
String jsonText = readAll(reader);
|
||||
return new JsonParser().parse(jsonText);
|
||||
public static JsonElement parseJson(InputStream in) throws IOException {
|
||||
if (in == null) return null;
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
|
||||
return new JsonParser().parse(reader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves an InputStream to a file.
|
||||
* @param in The InputStream.
|
||||
* @param target The target file.
|
||||
* @return Whether or not file was created / already exists.
|
||||
* @throws IOException If an I/O exception occurs.
|
||||
*/
|
||||
public static boolean saveResource(InputStream in, File target) throws IOException {
|
||||
if (target.exists()) return false;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
package net.frankheijden.serverutils.common.utils;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import net.frankheijden.serverutils.common.entities.http.GitHubResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
|
||||
public class GitHubUtils {
|
||||
|
||||
private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0)"
|
||||
+ " Gecko/20100101"
|
||||
+ " Firefox/77.0";
|
||||
|
||||
private GitHubUtils() {}
|
||||
|
||||
/**
|
||||
* Downloads file from a GitHubResponse to a file location.
|
||||
*/
|
||||
public static boolean download(GitHubResponse res, File target) throws IOException {
|
||||
if (res.getRateLimit().isRateLimited()) return false;
|
||||
try (InputStream is = res.getStream()) {
|
||||
if (is == null) return false;
|
||||
try (
|
||||
ReadableByteChannel rbc = Channels.newChannel(is);
|
||||
FileOutputStream fos = new FileOutputStream(target)
|
||||
) {
|
||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonElement parseJson(GitHubResponse res) throws IOException {
|
||||
return FileUtils.parseJson(res.getStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a stream to a github url and returns the response.
|
||||
*/
|
||||
public static GitHubResponse stream(String url) throws IOException {
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||
conn.setRequestProperty("User-Agent", USER_AGENT);
|
||||
conn.setConnectTimeout(10000);
|
||||
return GitHubResponse.from(conn);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue