Fix auto-updater failing
- Implemented a new self-reload system, which will boot up a separate plugin to load ServerUtils back up again.
This commit is contained in:
parent
e2a9fb1dc3
commit
2c1c92fef6
10 changed files with 75 additions and 7 deletions
|
|
@ -177,7 +177,7 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
public void onUnloadPlugin(CommandSender commandSender, String pluginName) {
|
public void onUnloadPlugin(CommandSender commandSender, String pluginName) {
|
||||||
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
ServerCommandSender sender = BukkitUtils.wrap(commandSender);
|
||||||
|
|
||||||
Result disableResult = BukkitPluginManager.disablePlugin(pluginName);
|
Result disableResult = BukkitPluginManager.get().disablePlugin(pluginName);
|
||||||
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
if (disableResult != Result.SUCCESS && disableResult != Result.ALREADY_DISABLED) {
|
||||||
disableResult.sendTo(sender, "disabl", pluginName);
|
disableResult.sendTo(sender, "disabl", pluginName);
|
||||||
return;
|
return;
|
||||||
|
|
@ -234,7 +234,7 @@ public class CommandServerUtils extends BaseCommand {
|
||||||
@CommandPermission("serverutils.disableplugin")
|
@CommandPermission("serverutils.disableplugin")
|
||||||
@Description("Disables the specified plugin.")
|
@Description("Disables the specified plugin.")
|
||||||
public void onDisablePlugin(CommandSender sender, String pluginName) {
|
public void onDisablePlugin(CommandSender sender, String pluginName) {
|
||||||
Result result = BukkitPluginManager.disablePlugin(pluginName);
|
Result result = BukkitPluginManager.get().disablePlugin(pluginName);
|
||||||
result.sendTo(BukkitUtils.wrap(sender), "disabl", pluginName);
|
result.sendTo(BukkitUtils.wrap(sender), "disabl", pluginName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,4 +67,10 @@ public class BukkitPlugin extends ServerUtilsPlugin {
|
||||||
public File getDataFolder() {
|
public File getDataFolder() {
|
||||||
return plugin.getDataFolder();
|
return plugin.getDataFolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public File fetchUpdaterData() {
|
||||||
|
return pluginManager.getPluginFile("ServerUtils");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,7 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
|
||||||
* @param pluginName The plugin to disable.
|
* @param pluginName The plugin to disable.
|
||||||
* @return The result of the disable call.
|
* @return The result of the disable call.
|
||||||
*/
|
*/
|
||||||
public static Result disablePlugin(String pluginName) {
|
public Result disablePlugin(String pluginName) {
|
||||||
return disablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
return disablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,7 +107,8 @@ public class BukkitPluginManager extends AbstractPluginManager<Plugin> {
|
||||||
* @param plugin The plugin to disable.
|
* @param plugin The plugin to disable.
|
||||||
* @return The result of the disable call.
|
* @return The result of the disable call.
|
||||||
*/
|
*/
|
||||||
public static Result disablePlugin(Plugin plugin) {
|
@Override
|
||||||
|
public Result disablePlugin(Plugin plugin) {
|
||||||
if (plugin == null) return Result.NOT_ENABLED;
|
if (plugin == null) return Result.NOT_ENABLED;
|
||||||
if (!plugin.isEnabled()) return Result.ALREADY_DISABLED;
|
if (!plugin.isEnabled()) return Result.ALREADY_DISABLED;
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import net.frankheijden.serverutils.bungee.managers.BungeePluginManager;
|
||||||
import net.frankheijden.serverutils.bungee.managers.BungeeTaskManager;
|
import net.frankheijden.serverutils.bungee.managers.BungeeTaskManager;
|
||||||
import net.frankheijden.serverutils.bungee.managers.BungeeVersionManager;
|
import net.frankheijden.serverutils.bungee.managers.BungeeVersionManager;
|
||||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
import net.md_5.bungee.api.plugin.PluginDescription;
|
||||||
|
|
||||||
public class BungeePlugin extends ServerUtilsPlugin {
|
public class BungeePlugin extends ServerUtilsPlugin {
|
||||||
|
|
||||||
|
|
@ -67,4 +68,15 @@ public class BungeePlugin extends ServerUtilsPlugin {
|
||||||
public File getDataFolder() {
|
public File getDataFolder() {
|
||||||
return plugin.getDataFolder();
|
return plugin.getDataFolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public PluginDescription fetchUpdaterData() {
|
||||||
|
try {
|
||||||
|
return BungeePluginManager.getPluginDescription(pluginManager.getPluginFile("ServerUtils"));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,19 @@ public class BungeePluginManager extends AbstractPluginManager<Plugin> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result disablePlugin(Plugin plugin) {
|
||||||
|
PluginDescription desc = plugin.getDescription();
|
||||||
|
String name = desc.getName();
|
||||||
|
try {
|
||||||
|
plugin.onDisable();
|
||||||
|
return Result.SUCCESS;
|
||||||
|
} catch (Throwable th) {
|
||||||
|
proxy.getLogger().log(Level.WARNING, "Exception encountered when disabling plugin: " + name, th);
|
||||||
|
return Result.ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result reloadPlugin(String pluginName) {
|
public Result reloadPlugin(String pluginName) {
|
||||||
Plugin plugin = proxy.getPluginManager().getPlugin(pluginName);
|
Plugin plugin = proxy.getPluginManager().getPlugin(pluginName);
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,16 @@ archivesBaseName = rootProject.name + '-Common'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
flatDir { dirs 'src/main/resources' }
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
compileOnly name: 'ServerUtilsUpdater'
|
||||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
exclude 'net/frankheijden/serverutilsupdater/**'
|
||||||
|
exclude 'plugin.yml'
|
||||||
|
exclude 'bungee.yml'
|
||||||
|
}
|
||||||
|
|
@ -27,6 +27,8 @@ public abstract class ServerUtilsPlugin {
|
||||||
|
|
||||||
public abstract File getDataFolder();
|
public abstract File getDataFolder();
|
||||||
|
|
||||||
|
public abstract <T> T fetchUpdaterData();
|
||||||
|
|
||||||
public void createDataFolderIfNotExists() {
|
public void createDataFolderIfNotExists() {
|
||||||
if (getDataFolder().exists()) return;
|
if (getDataFolder().exists()) return;
|
||||||
getDataFolder().mkdirs();
|
getDataFolder().mkdirs();
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ public abstract class AbstractPluginManager<T> extends PluginProvider<T> {
|
||||||
|
|
||||||
public abstract Result enablePlugin(T plugin);
|
public abstract Result enablePlugin(T plugin);
|
||||||
|
|
||||||
|
public abstract Result disablePlugin(T plugin);
|
||||||
|
|
||||||
public abstract Result reloadPlugin(String pluginName);
|
public abstract Result reloadPlugin(String pluginName);
|
||||||
|
|
||||||
public abstract Result reloadPlugin(T plugin);
|
public abstract Result reloadPlugin(T plugin);
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ 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;
|
||||||
|
|
@ -21,6 +22,7 @@ import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
import net.frankheijden.serverutils.common.managers.AbstractVersionManager;
|
||||||
import net.frankheijden.serverutils.common.utils.FileUtils;
|
import net.frankheijden.serverutils.common.utils.FileUtils;
|
||||||
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
import net.frankheijden.serverutils.common.utils.VersionUtils;
|
||||||
|
import net.frankheijden.serverutilsupdater.common.Updater;
|
||||||
|
|
||||||
public class UpdateCheckerTask implements Runnable {
|
public class UpdateCheckerTask implements Runnable {
|
||||||
|
|
||||||
|
|
@ -118,7 +120,7 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
File target = new File(plugin.getPluginManager().getPluginsFolder(), asset.name);
|
File target = new File(plugin.getPluginManager().getPluginsFolder(), asset.name);
|
||||||
downloadPlugin(githubVersion, asset.downloadUrl, target);
|
downloadPlugin(githubVersion, asset.downloadUrl, target);
|
||||||
plugin.getPluginManager().getPluginFile((Object) ServerUtilsApp.getPlatformPlugin()).delete();
|
plugin.getPluginManager().getPluginFile((Object) ServerUtilsApp.getPlatformPlugin()).delete();
|
||||||
tryReloadPlugin();
|
tryReloadPlugin(target);
|
||||||
} else if (!isStartupCheck()) {
|
} else if (!isStartupCheck()) {
|
||||||
Messenger.sendMessage(sender, "serverutils.update.available",
|
Messenger.sendMessage(sender, "serverutils.update.available",
|
||||||
"%old%", current,
|
"%old%", current,
|
||||||
|
|
@ -175,13 +177,35 @@ public class UpdateCheckerTask implements Runnable {
|
||||||
versionManager.setDownloaded(githubVersion);
|
versionManager.setDownloaded(githubVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryReloadPlugin() {
|
private static final String UPDATER_FILE_NAME = "ServerUtilsUpdater.jar";
|
||||||
|
|
||||||
|
private File saveUpdater() {
|
||||||
|
InputStream in = getClass().getClassLoader().getResourceAsStream(UPDATER_FILE_NAME);
|
||||||
|
File file = new File(plugin.getDataFolder().getParent(), UPDATER_FILE_NAME);
|
||||||
|
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));
|
||||||
plugin.getPluginManager().reloadPlugin((Object)ServerUtilsApp.getPlatformPlugin());
|
|
||||||
|
File file = saveUpdater();
|
||||||
|
Updater updater = (Updater) plugin.getPluginManager().loadPlugin(file).get();
|
||||||
|
plugin.getPluginManager().enablePlugin(updater);
|
||||||
|
|
||||||
|
plugin.getPluginManager().disablePlugin(ServerUtilsApp.getPlatformPlugin());
|
||||||
|
plugin.getPluginManager().unloadPlugin((Object)ServerUtilsApp.getPlatformPlugin()).tryClose();
|
||||||
|
updater.update(pluginFile);
|
||||||
|
file.delete();
|
||||||
|
|
||||||
plugin.getLogger().info(String.format(UPGRADE_SUCCESS, downloadedVersion));
|
plugin.getLogger().info(String.format(UPGRADE_SUCCESS, downloadedVersion));
|
||||||
} else {
|
} else {
|
||||||
broadcastDownloadStatus(downloadedVersion, false);
|
broadcastDownloadStatus(downloadedVersion, false);
|
||||||
|
|
|
||||||
BIN
Common/src/main/resources/ServerUtilsUpdater.jar
Normal file
BIN
Common/src/main/resources/ServerUtilsUpdater.jar
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue