diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java index ef0a3f82..04484391 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java @@ -19,21 +19,15 @@ package com.comphenix.protocol; import java.io.File; import java.io.IOException; -import java.net.UnknownHostException; - import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import com.comphenix.protocol.error.ErrorReporter; -import com.comphenix.protocol.error.Report; -import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.metrics.Updater; -import com.comphenix.protocol.metrics.Updater.UpdateResult; import com.comphenix.protocol.metrics.Updater.UpdateType; import com.comphenix.protocol.timing.TimedListenerManager; import com.comphenix.protocol.timing.TimingReportGenerator; -import com.comphenix.protocol.utility.WrappedScheduler; /** * Handles the "protocol" administration command. @@ -46,10 +40,6 @@ class CommandProtocol extends CommandBase { */ public static final String NAME = "protocol"; - public static final ReportType REPORT_NETWORK_ERROR = new ReportType("Network error: %s"); - public static final ReportType REPORT_CANNOT_CHECK_FOR_UPDATES = new ReportType("Cannot check updates for ProtocolLib."); - public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib."); - private Plugin plugin; private Updater updater; private ProtocolConfig config; @@ -80,49 +70,31 @@ class CommandProtocol extends CommandBase { } public void checkVersion(final CommandSender sender) { - // Perform on an async thread - WrappedScheduler.runAsynchronouslyOnce(plugin, new Runnable() { - @Override - public void run() { - try { - UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true); - sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); - } catch (Exception e) { - if (isNetworkError(e)) { - getReporter().reportWarning(CommandProtocol.this, - Report.newBuilder(REPORT_NETWORK_ERROR).messageParam(e.getCause().getMessage()) - ); - } else { - getReporter().reportDetailed(CommandProtocol.this, Report.newBuilder(REPORT_CANNOT_CHECK_FOR_UPDATES).error(e).callerParam(sender)); - } - } - } - }, 0L); - - updateFinished(); + performUpdate(sender, UpdateType.NO_DOWNLOAD); } public void updateVersion(final CommandSender sender) { + performUpdate(sender, UpdateType.DEFAULT); + } + + private void performUpdate(final CommandSender sender, UpdateType type) { + if (updater.isChecking()) { + sender.sendMessage(ChatColor.RED + "Already checking for an update."); + return; + } + // Perform on an async thread - WrappedScheduler.runAsynchronouslyOnce(plugin, new Runnable() { + Runnable notify = new Runnable() { @Override public void run() { - try { - UpdateResult result = updater.update(UpdateType.DEFAULT, true); - sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); - } catch (Exception e) { - if (isNetworkError(e)) { - getReporter().reportWarning(CommandProtocol.this, - Report.newBuilder(REPORT_NETWORK_ERROR).messageParam(e.getCause().getMessage()) - ); - } else { - getReporter().reportDetailed(CommandProtocol.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(e).callerParam(sender)); - } - } + sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + updater.getResult()); + + updater.removeListener(this); + updateFinished(); } - }, 0L); - - updateFinished(); + }; + updater.start(type); + updater.addListener(notify); } private void toggleTimings(CommandSender sender, String[] args) { @@ -174,20 +146,6 @@ class CommandProtocol extends CommandBase { } } - private boolean isNetworkError(Exception e) { - Throwable cause = e.getCause(); - - if (cause instanceof UnknownHostException) { - // These are always network problems - return true; - } if (cause instanceof IOException) { - // Thanks for making the message a part of the API ... - return cause.getMessage().contains("HTTP response"); - } else { - return false; - } - } - /** * Prevent further automatic updates until the next delay. */ diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index a48c2302..bd40ca90 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -47,6 +47,7 @@ import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks; import com.comphenix.protocol.metrics.Statistics; import com.comphenix.protocol.metrics.Updater; import com.comphenix.protocol.metrics.Updater.UpdateResult; +import com.comphenix.protocol.metrics.Updater.UpdateType; import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; import com.comphenix.protocol.utility.ChatExtensions; import com.comphenix.protocol.utility.MinecraftVersion; @@ -93,6 +94,10 @@ public class ProtocolLibrary extends JavaPlugin { */ public static final String MINECRAFT_LAST_RELEASE_DATE = "2013-07-08"; + // Update information + static final String BUKKIT_DEV_SLUG = "protocollib"; + static final int BUKKIT_DEV_ID = 45564; + /** * The number of milliseconds per second. */ @@ -179,8 +184,8 @@ public class ProtocolLibrary extends JavaPlugin { // Handle unexpected Minecraft versions MinecraftVersion version = verifyMinecraftVersion(); - // Set updater - updater = new Updater(this, logger, "protocollib", getFile(), "protocol.info"); + // Set updater - this will not perform any update automatically + updater = new Updater(this, BUKKIT_DEV_ID, getFile(), UpdateType.NO_DOWNLOAD, true); unhookTask = new DelayedSingleTask(this); protocolManager = PacketFilterManager.newBuilder(). diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Updater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Updater.java index 5e908ef0..0c0367bd 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Updater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Updater.java @@ -1,623 +1,608 @@ -package com.comphenix.protocol.metrics; - -// EXTENSIVELY MODIFIED BY AADNK/COMPHENIX -// CHECK GIT FOR DETAILS - /* * Updater for Bukkit. * - * This class provides the means to safetly and easily update a plugin, or check to see if it is updated using dev.bukkit.org + * This class provides the means to safely and easily update a plugin, or check to see if it is updated using dev.bukkit.org */ + +// Somewhat modified by aadnk. +package com.comphenix.protocol.metrics; + import java.io.*; -import java.net.ConnectException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.XMLEvent; +import java.util.Enumeration; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.error.Report; +import com.comphenix.protocol.error.ReportType; import com.google.common.base.Preconditions; /** * Check dev.bukkit.org to find updates for a given plugin, and download the updates if needed. - *
+ *
* VERY, VERY IMPORTANT: Because there are no standards for adding auto-update toggles in your plugin's config, this system provides NO CHECK WITH YOUR CONFIG to make sure the user has allowed auto-updating. *+ *
* An example of a good configuration option would be something similar to 'auto-update: true' - if this value is set to false you may NOT run the auto-updater. *+ * Call {@link #start()} to actually start looking (and downloading) updates. + * + * @param plugin The plugin that is checking for an update. + * @param id The dev.bukkit.org id of the project + * @param file The file that the plugin is running from, get this by doing this.getFile() from within your main class. + * @param type Specify the type of update this will be. See {@link UpdateType} + * @param announce True if the program should announce the progress of new updates in console + */ + public Updater(Plugin plugin, int id, File file, UpdateType type, boolean announce) { + this.plugin = plugin; + this.type = type; + this.announce = announce; + this.file = file; + this.id = id; + this.updateFolder = plugin.getServer().getUpdateFolder(); + + final File pluginFile = plugin.getDataFolder().getParentFile(); + final File updaterFile = new File(pluginFile, "Updater"); + final File updaterConfigFile = new File(updaterFile, "config.yml"); + + if (!updaterFile.exists()) { + updaterFile.mkdir(); + } + if (!updaterConfigFile.exists()) { + try { + updaterConfigFile.createNewFile(); + } catch (final IOException e) { + plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath()); + e.printStackTrace(); } } - - return result; + this.config = YamlConfiguration.loadConfiguration(updaterConfigFile); + + this.config.options().header("This configuration file affects all plugins using the Updater system (version 2+ - http://forums.bukkit.org/threads/96681/ )" + '\n' + + "If you wish to use your API key, read http://wiki.bukkit.org/ServerMods_API and place it below." + '\n' + + "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration."); + this.config.addDefault("api-key", "PUT_API_KEY_HERE"); + this.config.addDefault("disable", false); + + if (this.config.get("api-key", null) == null) { + this.config.options().copyDefaults(true); + try { + this.config.save(updaterConfigFile); + } catch (final IOException e) { + plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath()); + e.printStackTrace(); + } + } + + if (this.config.getBoolean("disable")) { + this.result = UpdateResult.DISABLED; + return; + } + + String key = this.config.getString("api-key"); + if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) { + key = null; + } + + this.apiKey = key; + + try { + this.url = new URL(Updater.HOST + Updater.QUERY + id); + } catch (final MalformedURLException e) { + plugin.getLogger().severe("The project ID provided for updating, " + id + " is invalid."); + this.result = UpdateResult.FAIL_BADID; + e.printStackTrace(); + } + } + + // aadnk - decouple the thread start and the constructor. + /** + * Begin looking for updates. + * @param type - the update type. + */ + public void start(UpdateType type) { + waitForThread(); + + this.type = type; + this.thread = new Thread(new UpdateRunnable()); + this.thread.start(); + } + + /** + * Add a listener to be executed when we have determined if an update is available. + *
+ * The listener will be executed on the main thread. + * @param listener - the listener to add. + */ + public void addListener(Runnable listener) { + listeners.add(Preconditions.checkNotNull(listener, "listener cannot be NULL")); + } + + /** + * Remove a listener. + * @param listener - the listener to remove. + * @return TRUE if the listener was removed, FALSE otherwise. + */ + public boolean removeListener(Runnable listener) { + return listeners.remove(listener); } /** * Get the result of the update process. - */ - public Updater.UpdateResult getResult() - { - return result; + */ + public Updater.UpdateResult getResult() { + this.waitForThread(); + return this.result; } - + /** - * Get the total bytes of the file (can only be used after running a version check or a normal run). - */ - public long getFileSize() - { - return totalSize; - } - + * Get the latest version's release type (release, beta, or alpha). + */ + public String getLatestType() { + this.waitForThread(); + return this.versionType; + } + /** - * Get the version string latest file avaliable online. - */ - public String getLatestVersionString() - { - return versionTitle; + * Get the latest version's game version. + */ + public String getLatestGameVersion() { + this.waitForThread(); + return this.versionGameVersion; + } + + /** + * Get the latest version's name. + */ + public String getLatestName() { + this.waitForThread(); + return this.versionName; + } + + /** + * Get the latest version's file link. + */ + public String getLatestFileLink() { + this.waitForThread(); + return this.versionLink; + } + + /** + * As the result of Updater output depends on the thread's completion, it is necessary to wait for the thread to finish + * before allowing anyone to check the result. + */ + private void waitForThread() { + if ((this.thread != null) && this.thread.isAlive()) { + try { + this.thread.join(); + } catch (final InterruptedException e) { + e.printStackTrace(); + } + } + } + + /** + * Determine if we are already checking for an update. + * @return TRUE if we are, FALSE otherwise. + */ + public boolean isChecking() { + return this.thread != null && this.thread.isAlive(); } /** * Save an update from dev.bukkit.org into the server's update folder. - */ - private void saveFile(File folder, String file, String u) - { - if(!folder.exists()) - { + */ + private void saveFile(File folder, String file, String u) { + if (!folder.exists()) { folder.mkdir(); } BufferedInputStream in = null; FileOutputStream fout = null; - try - { + try { // Download the file - URL url = new URL(u); - int fileLength = url.openConnection().getContentLength(); + final URL url = new URL(u); + final int fileLength = url.openConnection().getContentLength(); in = new BufferedInputStream(url.openStream()); fout = new FileOutputStream(folder.getAbsolutePath() + "/" + file); - byte[] data = new byte[BYTE_SIZE]; + final byte[] data = new byte[Updater.BYTE_SIZE]; int count; - if(announce) logger.info("About to download a new update: " + versionTitle); + if (this.announce) { + this.plugin.getLogger().info("About to download a new update: " + this.versionName); + } long downloaded = 0; - while ((count = in.read(data, 0, BYTE_SIZE)) != -1) - { + while ((count = in.read(data, 0, Updater.BYTE_SIZE)) != -1) { downloaded += count; fout.write(data, 0, count); - int percent = (int) (downloaded * 100 / fileLength); - if(announce && (percent % 10 == 0)) - { - logger.info("Downloading update: " + percent + "% of " + fileLength + " bytes."); + final int percent = (int) ((downloaded * 100) / fileLength); + if (this.announce && ((percent % 10) == 0)) { + this.plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes."); } } - - if(announce) - logger.info("Finished updating."); - } - catch (Exception ex) - { - logger.warning("The auto-updater tried to download a new update, but was unsuccessful."); - logger.log(Level.INFO, "Error message to submit as a ticket.", ex); - result = Updater.UpdateResult.FAIL_DOWNLOAD; - } - finally - { - try - { - if (in != null) - { + //Just a quick check to make sure we didn't leave any files from last time... + for (final File xFile : new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles()) { + if (xFile.getName().endsWith(".zip")) { + xFile.delete(); + } + } + // Check to see if it's a zip file, if it is, unzip it. + final File dFile = new File(folder.getAbsolutePath() + "/" + file); + if (dFile.getName().endsWith(".zip")) { + // Unzip + this.unzip(dFile.getCanonicalPath()); + } + if (this.announce) { + this.plugin.getLogger().info("Finished updating."); + } + } catch (final Exception ex) { + this.plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful."); + this.result = Updater.UpdateResult.FAIL_DOWNLOAD; + } finally { + try { + if (in != null) { in.close(); } - if (fout != null) - { + if (fout != null) { fout.close(); } - } - catch (Exception ex) - { + } catch (final Exception ex) { } } } - + + /** + * Part of Zip-File-Extractor, modified by Gravity for use with Bukkit + */ + private void unzip(String file) { + try { + final File fSourceZip = new File(file); + final String zipPath = file.substring(0, file.length() - 4); + ZipFile zipFile = new ZipFile(fSourceZip); + Enumeration extends ZipEntry> e = zipFile.entries(); + while (e.hasMoreElements()) { + ZipEntry entry = e.nextElement(); + File destinationFilePath = new File(zipPath, entry.getName()); + destinationFilePath.getParentFile().mkdirs(); + if (entry.isDirectory()) { + continue; + } else { + final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); + int b; + final byte buffer[] = new byte[Updater.BYTE_SIZE]; + final FileOutputStream fos = new FileOutputStream(destinationFilePath); + final BufferedOutputStream bos = new BufferedOutputStream(fos, Updater.BYTE_SIZE); + while ((b = bis.read(buffer, 0, Updater.BYTE_SIZE)) != -1) { + bos.write(buffer, 0, b); + } + bos.flush(); + bos.close(); + bis.close(); + final String name = destinationFilePath.getName(); + if (name.endsWith(".jar") && this.pluginFile(name)) { + destinationFilePath.renameTo(new File(this.plugin.getDataFolder().getParent(), this.updateFolder + "/" + name)); + } + } + entry = null; + destinationFilePath = null; + } + e = null; + zipFile.close(); + zipFile = null; + + // Move any plugin data folders that were included to the right place, Bukkit won't do this for us. + for (final File dFile : new File(zipPath).listFiles()) { + if (dFile.isDirectory()) { + if (this.pluginFile(dFile.getName())) { + final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir + final File[] contents = oFile.listFiles(); // List of existing files in the current dir + for (final File cFile : dFile.listFiles()) // Loop through all the files in the new dir + { + boolean found = false; + for (final File xFile : contents) // Loop through contents to see if it exists + { + if (xFile.getName().equals(cFile.getName())) { + found = true; + break; + } + } + if (!found) { + // Move the new file into the current dir + cFile.renameTo(new File(oFile.getCanonicalFile() + "/" + cFile.getName())); + } else { + // This file already exists, so we don't need it anymore. + cFile.delete(); + } + } + } + } + dFile.delete(); + } + new File(zipPath).delete(); + fSourceZip.delete(); + } catch (final IOException ex) { + this.plugin.getLogger().warning("The auto-updater tried to unzip a new update file, but was unsuccessful."); + this.result = Updater.UpdateResult.FAIL_DOWNLOAD; + ex.printStackTrace(); + } + new File(file).delete(); + } + /** * Check if the name of a jar is one of the plugins currently installed, used for extracting the correct files out of a zip. - */ - public boolean pluginFile(String name) - { - for(File file : new File("plugins").listFiles()) - { - if(file.getName().equals(name)) - { + */ + private boolean pluginFile(String name) { + for (final File file : new File("plugins").listFiles()) { + if (file.getName().equals(name)) { return true; } } return false; - } - - /** - * Obtain the direct download file url from the file's page. - */ - private String getFile(String link) - { - String download = null; - try - { - // Open a connection to the page - URL url = new URL(link); - URLConnection urlConn = url.openConnection(); - InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream()); - BufferedReader buff = new BufferedReader(inStream); - - int counter = 0; - String line; - while((line = buff.readLine()) != null) - { - counter++; - // Search for the download link - if(line.contains("