Download ServerUtilsUpdater on the fly instead of shading it in
This commit is contained in:
parent
0f754e1c38
commit
99b5aadefe
4 changed files with 57 additions and 54 deletions
|
|
@ -3,6 +3,7 @@ main: net.frankheijden.serverutils.bukkit.ServerUtils
|
||||||
version: ${version}
|
version: ${version}
|
||||||
author: FrankHeijden
|
author: FrankHeijden
|
||||||
api-version: '1.13'
|
api-version: '1.13'
|
||||||
|
softdepend: [ServerUtilsUpdater]
|
||||||
|
|
||||||
commands:
|
commands:
|
||||||
serverutils:
|
serverutils:
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,11 @@ version = rootProject.version
|
||||||
archivesBaseName = rootProject.name + '-Common'
|
archivesBaseName = rootProject.name + '-Common'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
maven { url 'https://jitpack.io' }
|
||||||
flatDir { dirs 'src/main/resources' }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly name: 'ServerUtilsUpdater'
|
compileOnly 'com.github.FrankHeijden:ServerUtilsUpdater:v1.0.0'
|
||||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import com.google.gson.JsonObject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
@ -34,6 +33,8 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
private final boolean startup;
|
private final boolean startup;
|
||||||
|
|
||||||
private static final String GITHUB_LINK = "https://api.github.com/repos/FrankHeijden/ServerUtils/releases/latest";
|
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 UPDATE_CHECK_START = "Checking for updates...";
|
||||||
private static final String GENERAL_ERROR = "Error fetching new version of ServerUtils";
|
private static final String GENERAL_ERROR = "Error fetching new version of ServerUtils";
|
||||||
|
|
@ -71,33 +72,15 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
plugin.getLogger().info(UPDATE_CHECK_START);
|
plugin.getLogger().info(UPDATE_CHECK_START);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonElement jsonElement;
|
JsonObject pluginJson = getJson(GITHUB_LINK);
|
||||||
try {
|
JsonObject updaterJson = getJson(GITHUB_UPDATER_LINK);
|
||||||
jsonElement = FileUtils.readJsonFromUrl(GITHUB_LINK);
|
if (pluginJson == null || updaterJson == null) return;
|
||||||
} catch (ConnectException | UnknownHostException | SocketTimeoutException ex) {
|
|
||||||
plugin.getLogger().severe(String.format(CONNECTION_ERROR, ex.getClass().getSimpleName(), ex.getMessage()));
|
|
||||||
return;
|
|
||||||
} catch (FileNotFoundException ex) {
|
|
||||||
plugin.getLogger().severe(String.format(UNAVAILABLE, ex.getClass().getSimpleName(), ex.getMessage()));
|
|
||||||
return;
|
|
||||||
} catch (IOException ex) {
|
|
||||||
plugin.getLogger().log(Level.SEVERE, ex, () -> GENERAL_ERROR);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (jsonElement == null) {
|
GitHubAsset pluginAsset = getAsset(pluginJson);
|
||||||
plugin.getLogger().warning(TRY_LATER);
|
GitHubAsset updaterAsset = getAsset(updaterJson);
|
||||||
return;
|
|
||||||
}
|
|
||||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
||||||
if (jsonObject.has("message")) {
|
|
||||||
plugin.getLogger().warning(jsonObject.get("message").getAsString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String githubVersion = getVersion(jsonObject);
|
String githubVersion = getVersion(pluginJson);
|
||||||
String body = jsonObject.getAsJsonPrimitive("body").getAsString();
|
String body = pluginJson.getAsJsonPrimitive("body").getAsString();
|
||||||
GitHubAsset asset = getAsset(jsonObject);
|
|
||||||
|
|
||||||
String downloaded = versionManager.getDownloadedVersion();
|
String downloaded = versionManager.getDownloadedVersion();
|
||||||
String current = versionManager.getCurrentVersion();
|
String current = versionManager.getCurrentVersion();
|
||||||
|
|
@ -106,9 +89,10 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion));
|
plugin.getLogger().info(String.format(UPDATE_AVAILABLE, githubVersion));
|
||||||
plugin.getLogger().info("Release info: " + body);
|
plugin.getLogger().info("Release info: " + body);
|
||||||
}
|
}
|
||||||
if (canDownloadPlugin() && asset != null) {
|
if (canDownloadPlugin() && pluginAsset != null && updaterAsset != null) {
|
||||||
if (isStartupCheck()) {
|
if (isStartupCheck()) {
|
||||||
plugin.getLogger().info(String.format(DOWNLOAD_START, asset.downloadUrl));
|
plugin.getLogger().info(String.format(DOWNLOAD_START, pluginAsset.downloadUrl));
|
||||||
|
plugin.getLogger().info(String.format(DOWNLOAD_START, updaterAsset.downloadUrl));
|
||||||
} else {
|
} else {
|
||||||
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
Messenger.sendMessage(sender, "serverutils.update.downloading",
|
||||||
"%old%", current,
|
"%old%", current,
|
||||||
|
|
@ -116,10 +100,14 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
"%info%", body);
|
"%info%", body);
|
||||||
}
|
}
|
||||||
|
|
||||||
File target = new File(plugin.getPluginManager().getPluginsFolder(), asset.name);
|
File updaterTarget = new File(plugin.getPluginManager().getPluginsFolder(), updaterAsset.name);
|
||||||
downloadPlugin(githubVersion, asset.downloadUrl, target);
|
download(githubVersion, updaterAsset.downloadUrl, updaterTarget);
|
||||||
|
|
||||||
|
File pluginTarget = new File(plugin.getPluginManager().getPluginsFolder(), pluginAsset.name);
|
||||||
|
downloadPlugin(githubVersion, pluginAsset.downloadUrl, pluginTarget);
|
||||||
|
|
||||||
plugin.getPluginManager().getPluginFile((Object) ServerUtilsApp.getPlatformPlugin()).delete();
|
plugin.getPluginManager().getPluginFile((Object) ServerUtilsApp.getPlatformPlugin()).delete();
|
||||||
tryReloadPlugin(target);
|
tryReloadPlugin(pluginTarget, updaterTarget);
|
||||||
} else if (!isStartupCheck()) {
|
} else if (!isStartupCheck()) {
|
||||||
Messenger.sendMessage(sender, "serverutils.update.available",
|
Messenger.sendMessage(sender, "serverutils.update.available",
|
||||||
"%old%", current,
|
"%old%", current,
|
||||||
|
|
@ -134,6 +122,33 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JsonObject getJson(String urlString) {
|
||||||
|
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;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
plugin.getLogger().log(Level.SEVERE, ex, () -> GENERAL_ERROR);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonElement == null) {
|
||||||
|
plugin.getLogger().warning(TRY_LATER);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||||
|
if (jsonObject.has("message")) {
|
||||||
|
plugin.getLogger().warning(jsonObject.get("message").getAsString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return jsonObject;
|
||||||
|
}
|
||||||
|
|
||||||
private String getVersion(JsonObject jsonObject) {
|
private String getVersion(JsonObject jsonObject) {
|
||||||
return jsonObject.getAsJsonPrimitive("tag_name").getAsString().replace("v", "");
|
return jsonObject.getAsJsonPrimitive("tag_name").getAsString().replace("v", "");
|
||||||
}
|
}
|
||||||
|
|
@ -161,6 +176,11 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
download(githubVersion, downloadLink, target);
|
||||||
|
versionManager.setDownloaded(githubVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void download(String githubVersion, String downloadLink, File target) {
|
||||||
if (downloadLink == null) {
|
if (downloadLink == null) {
|
||||||
broadcastDownloadStatus(githubVersion, true);
|
broadcastDownloadStatus(githubVersion, true);
|
||||||
return;
|
return;
|
||||||
|
|
@ -172,39 +192,22 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
broadcastDownloadStatus(githubVersion, true);
|
broadcastDownloadStatus(githubVersion, true);
|
||||||
throw new RuntimeException(DOWNLOAD_ERROR, ex);
|
throw new RuntimeException(DOWNLOAD_ERROR, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
versionManager.setDownloaded(githubVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String UPDATER_FILE_NAME = "ServerUtilsUpdater.jar";
|
private void tryReloadPlugin(File pluginFile, File updaterFile) {
|
||||||
|
|
||||||
private File saveUpdater() {
|
|
||||||
InputStream in = getClass().getClassLoader().getResourceAsStream(UPDATER_FILE_NAME);
|
|
||||||
File file = new File(plugin.getDataFolder().getParent(), UPDATER_FILE_NAME);
|
|
||||||
if (file.exists()) file.delete();
|
|
||||||
try {
|
|
||||||
FileUtils.saveResource(in, file);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void tryReloadPlugin(File pluginFile) {
|
|
||||||
plugin.getTaskManager().runTask(() -> {
|
plugin.getTaskManager().runTask(() -> {
|
||||||
String downloadedVersion = versionManager.getDownloadedVersion();
|
String downloadedVersion = versionManager.getDownloadedVersion();
|
||||||
|
|
||||||
if (isStartupCheck()) {
|
if (isStartupCheck()) {
|
||||||
plugin.getLogger().info(String.format(DOWNLOADED_RESTART, downloadedVersion));
|
plugin.getLogger().info(String.format(DOWNLOADED_RESTART, downloadedVersion));
|
||||||
|
|
||||||
File file = saveUpdater();
|
Updater updater = (Updater) plugin.getPluginManager().loadPlugin(updaterFile).get();
|
||||||
Updater updater = (Updater) plugin.getPluginManager().loadPlugin(file).get();
|
|
||||||
plugin.getPluginManager().enablePlugin(updater);
|
plugin.getPluginManager().enablePlugin(updater);
|
||||||
|
|
||||||
plugin.getPluginManager().disablePlugin(ServerUtilsApp.getPlatformPlugin());
|
plugin.getPluginManager().disablePlugin(ServerUtilsApp.getPlatformPlugin());
|
||||||
plugin.getPluginManager().unloadPlugin((Object)ServerUtilsApp.getPlatformPlugin()).tryClose();
|
plugin.getPluginManager().unloadPlugin((Object)ServerUtilsApp.getPlatformPlugin()).tryClose();
|
||||||
updater.update(pluginFile);
|
updater.update(pluginFile);
|
||||||
file.delete();
|
updaterFile.delete();
|
||||||
} else {
|
} else {
|
||||||
broadcastDownloadStatus(downloadedVersion, false);
|
broadcastDownloadStatus(downloadedVersion, false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue