mirror of
https://github.com/darksaid98/AntiCheatReloaded.git
synced 2026-12-03 22:15:28 +00:00
Use Updater 2.0
This commit is contained in:
1 parent
db77040560
commit
068af0606d
2 files changed
+231
-229
No files matched your search
@@ -47,6 +47,7 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
private static boolean verbose;
|
private static boolean verbose;
|
||||||
private static boolean developer;
|
private static boolean developer;
|
||||||
private static final long XRAY_TIME = 1200;
|
private static final long XRAY_TIME = 1200;
|
||||||
|
private static final int PROJECT_ID = 38723;
|
||||||
private static PacketManager packetManager;
|
private static PacketManager packetManager;
|
||||||
private static boolean protocolLib = false;
|
private static boolean protocolLib = false;
|
||||||
|
|
||||||
@@ -143,7 +144,7 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
private void setupUpdater() {
|
private void setupUpdater() {
|
||||||
if (config.getConfig().autoUpdate.getValue()) {
|
if (config.getConfig().autoUpdate.getValue()) {
|
||||||
verboseLog("Checking for a new update...");
|
verboseLog("Checking for a new update...");
|
||||||
Updater updater = new Updater(this, "anticheat", this.getFile(), Updater.UpdateType.DEFAULT, false);
|
Updater updater = new Updater(this, PROJECT_ID, this.getFile(), Updater.UpdateType.DEFAULT, false);
|
||||||
update = updater.getResult() != Updater.UpdateResult.NO_UPDATE;
|
update = updater.getResult() != Updater.UpdateResult.NO_UPDATE;
|
||||||
verboseLog("Update available: " + update);
|
verboseLog("Update available: " + update);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,6 @@
|
|||||||
|
|
||||||
package net.h31ix.anticheat.update;
|
package net.h31ix.anticheat.update;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -21,6 +14,12 @@ import java.util.Enumeration;
|
|||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check dev.bukkit.org to find updates for a given plugin, and download the updates if needed.
|
* Check dev.bukkit.org to find updates for a given plugin, and download the updates if needed.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -34,31 +33,39 @@ import java.util.zip.ZipFile;
|
|||||||
* <br>
|
* <br>
|
||||||
* If you are unsure about these rules, please read the plugin submission guidelines: http://goo.gl/8iU5l
|
* If you are unsure about these rules, please read the plugin submission guidelines: http://goo.gl/8iU5l
|
||||||
*
|
*
|
||||||
* @author H31IX
|
* @author Gravity
|
||||||
|
* @version 2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Updater {
|
public class Updater {
|
||||||
|
|
||||||
private Plugin plugin;
|
private Plugin plugin;
|
||||||
private UpdateType type;
|
private UpdateType type;
|
||||||
private String versionTitle;
|
private String versionName;
|
||||||
private String versionLink;
|
private String versionLink;
|
||||||
private long totalSize; // Holds the total size of the file
|
private String versionType;
|
||||||
//private double downloadedSize; TODO: Holds the number of bytes downloaded
|
private String versionGameVersion;
|
||||||
private int sizeLine; // Used for detecting file size
|
|
||||||
private int multiplier; // Used for determining when to broadcast download updates
|
|
||||||
private boolean announce; // Whether to announce file downloads
|
private boolean announce; // Whether to announce file downloads
|
||||||
|
|
||||||
private URL url; // Connecting to RSS
|
private URL url; // Connecting to RSS
|
||||||
private File file; // The plugin's file
|
private File file; // The plugin's file
|
||||||
private Thread thread; // Updater thread
|
private Thread thread; // Updater thread
|
||||||
private static final String DBOUrl = "http://dev.bukkit.org/server-mods/"; // Slugs will be appended to this to get to the project's RSS feed
|
|
||||||
private String[] noUpdateTag = {"-DEV", "-PRE", "-SNAPSHOT"}; // If the version number contains one of these, don't update.
|
private int id = -1; // Project's Curse ID
|
||||||
|
private String apiKey = null; // BukkitDev ServerMods API key
|
||||||
|
private static final String TITLE_VALUE = "name"; // Gets remote file's title
|
||||||
|
private static final String LINK_VALUE = "downloadUrl"; // Gets remote file's download link
|
||||||
|
private static final String TYPE_VALUE = "releaseType"; // Gets remote file's release type
|
||||||
|
private static final String VERSION_VALUE = "gameVersion"; // Gets remote file's build version
|
||||||
|
private static final String QUERY = "/servermods/files?projectIds="; // Path to GET
|
||||||
|
private static final String HOST = "https://api.curseforge.com"; // Slugs will be appended to this to get to the project's RSS feed
|
||||||
|
|
||||||
|
private static final String[] NO_UPDATE_TAG = { "-DEV", "-PRE", "-SNAPSHOT" }; // If the version number contains one of these, don't update.
|
||||||
private static final int BYTE_SIZE = 1024; // Used for downloading files
|
private static final int BYTE_SIZE = 1024; // Used for downloading files
|
||||||
private String updateFolder = YamlConfiguration.loadConfiguration(new File("bukkit.yml")).getString("settings.update-folder"); // The folder that downloads will be placed in
|
private YamlConfiguration config; // Config file
|
||||||
|
private String updateFolder;// The folder that downloads will be placed in
|
||||||
private Updater.UpdateResult result = Updater.UpdateResult.SUCCESS; // Used for determining the outcome of the update process
|
private Updater.UpdateResult result = Updater.UpdateResult.SUCCESS; // Used for determining the outcome of the update process
|
||||||
// Strings for reading RSS
|
|
||||||
private static final String TITLE = "title";
|
|
||||||
private static final String LINK = "link";
|
|
||||||
private static final String ITEM = "item";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gives the dev the result of the update process. Can be obtained by called getResult().
|
* Gives the dev the result of the update process. Can be obtained by called getResult().
|
||||||
@@ -72,6 +79,10 @@ public class Updater {
|
|||||||
* The updater did not find an update, and nothing was downloaded.
|
* The updater did not find an update, and nothing was downloaded.
|
||||||
*/
|
*/
|
||||||
NO_UPDATE,
|
NO_UPDATE,
|
||||||
|
/**
|
||||||
|
* The server administrator has disabled the updating system
|
||||||
|
*/
|
||||||
|
DISABLED,
|
||||||
/**
|
/**
|
||||||
* The updater found an update, but was unable to download it.
|
* The updater found an update, but was unable to download it.
|
||||||
*/
|
*/
|
||||||
@@ -85,9 +96,13 @@ public class Updater {
|
|||||||
*/
|
*/
|
||||||
FAIL_NOVERSION,
|
FAIL_NOVERSION,
|
||||||
/**
|
/**
|
||||||
* The slug provided by the plugin running the updater was invalid and doesn't exist on DBO.
|
* The id provided by the plugin running the updater was invalid and doesn't exist on DBO.
|
||||||
*/
|
*/
|
||||||
FAIL_BADSLUG,
|
FAIL_BADID,
|
||||||
|
/**
|
||||||
|
* The server administrator has improperly configured their API key in the configuration
|
||||||
|
*/
|
||||||
|
FAIL_APIKEY,
|
||||||
/**
|
/**
|
||||||
* The updater found an update, but because of the UpdateType being set to NO_DOWNLOAD, it wasn't downloaded.
|
* The updater found an update, but because of the UpdateType being set to NO_DOWNLOAD, it wasn't downloaded.
|
||||||
*/
|
*/
|
||||||
@@ -116,62 +131,125 @@ public class Updater {
|
|||||||
* Initialize the updater
|
* Initialize the updater
|
||||||
*
|
*
|
||||||
* @param plugin The plugin that is checking for an update.
|
* @param plugin The plugin that is checking for an update.
|
||||||
* @param slug The dev.bukkit.org slug of the project (http://dev.bukkit.org/server-mods/SLUG_IS_HERE)
|
* @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 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 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
|
* @param announce True if the program should announce the progress of new updates in console
|
||||||
*/
|
*/
|
||||||
public Updater(Plugin plugin, String slug, File file, UpdateType type, boolean announce) {
|
public Updater(Plugin plugin, int id, File file, UpdateType type, boolean announce) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.announce = announce;
|
this.announce = announce;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
try {
|
this.id = id;
|
||||||
// Obtain the results of the project's file feed
|
this.updateFolder = plugin.getServer().getUpdateFolder();
|
||||||
url = new URL(DBOUrl + slug + "/files.rss");
|
|
||||||
} catch (MalformedURLException ex) {
|
final File pluginFile = plugin.getDataFolder().getParentFile();
|
||||||
// Invalid slug
|
final File updaterFile = new File(pluginFile, "Updater");
|
||||||
plugin.getLogger().warning("The author of this plugin (" + plugin.getDescription().getAuthors().get(0) + ") has misconfigured their Auto Update system");
|
final File updaterConfigFile = new File(updaterFile, "config.yml");
|
||||||
plugin.getLogger().warning("The project slug given ('" + slug + "') is invalid. Please nag the author about this.");
|
|
||||||
result = Updater.UpdateResult.FAIL_BADSLUG; // Bad slug! Bad!
|
if (!updaterFile.exists()) {
|
||||||
|
updaterFile.mkdir();
|
||||||
}
|
}
|
||||||
thread = new Thread(new UpdateRunnable());
|
if (!updaterConfigFile.exists()) {
|
||||||
thread.start();
|
try {
|
||||||
|
updaterConfigFile.createNewFile();
|
||||||
|
} catch (final IOException e) {
|
||||||
|
plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.thread = new Thread(new UpdateRunnable());
|
||||||
|
this.thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the result of the update process.
|
* Get the result of the update process.
|
||||||
*/
|
*/
|
||||||
public Updater.UpdateResult getResult() {
|
public Updater.UpdateResult getResult() {
|
||||||
waitForThread();
|
this.waitForThread();
|
||||||
return result;
|
return this.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the total bytes of the file (can only be used after running a version check or a normal run).
|
* Get the latest version's release type (release, beta, or alpha).
|
||||||
*/
|
*/
|
||||||
public long getFileSize() {
|
public String getLatestType() {
|
||||||
waitForThread();
|
this.waitForThread();
|
||||||
return totalSize;
|
return this.versionType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the version string latest file avaliable online.
|
* Get the latest version's game version.
|
||||||
*/
|
*/
|
||||||
public String getLatestVersionString() {
|
public String getLatestGameVersion() {
|
||||||
waitForThread();
|
this.waitForThread();
|
||||||
return versionTitle;
|
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
|
* As the result of Updater output depends on the thread's completion, it is necessary to wait for the thread to finish
|
||||||
* before alloowing anyone to check the result.
|
* before allowing anyone to check the result.
|
||||||
*/
|
*/
|
||||||
public void waitForThread() {
|
private void waitForThread() {
|
||||||
if (thread.isAlive()) {
|
if ((this.thread != null) && this.thread.isAlive()) {
|
||||||
try {
|
try {
|
||||||
thread.join();
|
this.thread.join();
|
||||||
} catch (InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,39 +266,43 @@ public class Updater {
|
|||||||
FileOutputStream fout = null;
|
FileOutputStream fout = null;
|
||||||
try {
|
try {
|
||||||
// Download the file
|
// Download the file
|
||||||
URL url = new URL(u);
|
final URL url = new URL(u);
|
||||||
int fileLength = url.openConnection().getContentLength();
|
final int fileLength = url.openConnection().getContentLength();
|
||||||
in = new BufferedInputStream(url.openStream());
|
in = new BufferedInputStream(url.openStream());
|
||||||
fout = new FileOutputStream(folder.getAbsolutePath() + "/" + file);
|
fout = new FileOutputStream(folder.getAbsolutePath() + "/" + file);
|
||||||
|
|
||||||
byte[] data = new byte[BYTE_SIZE];
|
final byte[] data = new byte[Updater.BYTE_SIZE];
|
||||||
int count;
|
int count;
|
||||||
if (announce) plugin.getLogger().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;
|
long downloaded = 0;
|
||||||
while ((count = in.read(data, 0, BYTE_SIZE)) != -1) {
|
while ((count = in.read(data, 0, Updater.BYTE_SIZE)) != -1) {
|
||||||
downloaded += count;
|
downloaded += count;
|
||||||
fout.write(data, 0, count);
|
fout.write(data, 0, count);
|
||||||
int percent = (int) (downloaded * 100 / fileLength);
|
final int percent = (int) ((downloaded * 100) / fileLength);
|
||||||
if (announce & (percent % 10 == 0)) {
|
if (this.announce && ((percent % 10) == 0)) {
|
||||||
plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes.");
|
this.plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Just a quick check to make sure we didn't leave any files from last time...
|
//Just a quick check to make sure we didn't leave any files from last time...
|
||||||
for (File xFile : new File("plugins/" + updateFolder).listFiles()) {
|
for (final File xFile : new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles()) {
|
||||||
if (xFile.getName().endsWith(".zip")) {
|
if (xFile.getName().endsWith(".zip")) {
|
||||||
xFile.delete();
|
xFile.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check to see if it's a zip file, if it is, unzip it.
|
// Check to see if it's a zip file, if it is, unzip it.
|
||||||
File dFile = new File(folder.getAbsolutePath() + "/" + file);
|
final File dFile = new File(folder.getAbsolutePath() + "/" + file);
|
||||||
if (dFile.getName().endsWith(".zip")) {
|
if (dFile.getName().endsWith(".zip")) {
|
||||||
// Unzip
|
// Unzip
|
||||||
unzip(dFile.getCanonicalPath());
|
this.unzip(dFile.getCanonicalPath());
|
||||||
}
|
}
|
||||||
if (announce) plugin.getLogger().info("Finished updating.");
|
if (this.announce) {
|
||||||
} catch (Exception ex) {
|
this.plugin.getLogger().info("Finished updating.");
|
||||||
plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful.");
|
}
|
||||||
result = Updater.UpdateResult.FAIL_DOWNLOAD;
|
} 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 {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
@@ -229,41 +311,41 @@ public class Updater {
|
|||||||
if (fout != null) {
|
if (fout != null) {
|
||||||
fout.close();
|
fout.close();
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (final Exception ex) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Part of Zip-File-Extractor, modified by H31IX for use with Bukkit
|
* Part of Zip-File-Extractor, modified by Gravity for use with Bukkit
|
||||||
*/
|
*/
|
||||||
private void unzip(String file) {
|
private void unzip(String file) {
|
||||||
try {
|
try {
|
||||||
File fSourceZip = new File(file);
|
final File fSourceZip = new File(file);
|
||||||
String zipPath = file.substring(0, file.length() - 4);
|
final String zipPath = file.substring(0, file.length() - 4);
|
||||||
ZipFile zipFile = new ZipFile(fSourceZip);
|
ZipFile zipFile = new ZipFile(fSourceZip);
|
||||||
Enumeration<? extends ZipEntry> e = zipFile.entries();
|
Enumeration<? extends ZipEntry> e = zipFile.entries();
|
||||||
while (e.hasMoreElements()) {
|
while (e.hasMoreElements()) {
|
||||||
ZipEntry entry = (ZipEntry) e.nextElement();
|
ZipEntry entry = e.nextElement();
|
||||||
File destinationFilePath = new File(zipPath, entry.getName());
|
File destinationFilePath = new File(zipPath, entry.getName());
|
||||||
destinationFilePath.getParentFile().mkdirs();
|
destinationFilePath.getParentFile().mkdirs();
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
|
final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
|
||||||
int b;
|
int b;
|
||||||
byte buffer[] = new byte[BYTE_SIZE];
|
final byte buffer[] = new byte[Updater.BYTE_SIZE];
|
||||||
FileOutputStream fos = new FileOutputStream(destinationFilePath);
|
final FileOutputStream fos = new FileOutputStream(destinationFilePath);
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(fos, BYTE_SIZE);
|
final BufferedOutputStream bos = new BufferedOutputStream(fos, Updater.BYTE_SIZE);
|
||||||
while ((b = bis.read(buffer, 0, BYTE_SIZE)) != -1) {
|
while ((b = bis.read(buffer, 0, Updater.BYTE_SIZE)) != -1) {
|
||||||
bos.write(buffer, 0, b);
|
bos.write(buffer, 0, b);
|
||||||
}
|
}
|
||||||
bos.flush();
|
bos.flush();
|
||||||
bos.close();
|
bos.close();
|
||||||
bis.close();
|
bis.close();
|
||||||
String name = destinationFilePath.getName();
|
final String name = destinationFilePath.getName();
|
||||||
if (name.endsWith(".jar") && pluginFile(name)) {
|
if (name.endsWith(".jar") && this.pluginFile(name)) {
|
||||||
destinationFilePath.renameTo(new File("plugins/" + updateFolder + "/" + name));
|
destinationFilePath.renameTo(new File(this.plugin.getDataFolder().getParent(), this.updateFolder + "/" + name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
entry = null;
|
entry = null;
|
||||||
@@ -272,16 +354,17 @@ public class Updater {
|
|||||||
e = null;
|
e = null;
|
||||||
zipFile.close();
|
zipFile.close();
|
||||||
zipFile = null;
|
zipFile = null;
|
||||||
|
|
||||||
// Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
|
// Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
|
||||||
for (File dFile : new File(zipPath).listFiles()) {
|
for (final File dFile : new File(zipPath).listFiles()) {
|
||||||
if (dFile.isDirectory()) {
|
if (dFile.isDirectory()) {
|
||||||
if (pluginFile(dFile.getName())) {
|
if (this.pluginFile(dFile.getName())) {
|
||||||
File oFile = new File("plugins/" + dFile.getName()); // Get current dir
|
final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir
|
||||||
File[] contents = oFile.listFiles(); // List of existing files in the current dir
|
final File[] contents = oFile.listFiles(); // List of existing files in the current dir
|
||||||
for (File cFile : dFile.listFiles()) // Loop through all the files in the new dir
|
for (final File cFile : dFile.listFiles()) // Loop through all the files in the new dir
|
||||||
{
|
{
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (File xFile : contents) // Loop through contents to see if it exists
|
for (final File xFile : contents) // Loop through contents to see if it exists
|
||||||
{
|
{
|
||||||
if (xFile.getName().equals(cFile.getName())) {
|
if (xFile.getName().equals(cFile.getName())) {
|
||||||
found = true;
|
found = true;
|
||||||
@@ -302,10 +385,10 @@ public class Updater {
|
|||||||
}
|
}
|
||||||
new File(zipPath).delete();
|
new File(zipPath).delete();
|
||||||
fSourceZip.delete();
|
fSourceZip.delete();
|
||||||
} catch (IOException ex) {
|
} 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();
|
ex.printStackTrace();
|
||||||
plugin.getLogger().warning("The auto-updater tried to unzip a new update file, but was unsuccessful.");
|
|
||||||
result = Updater.UpdateResult.FAIL_DOWNLOAD;
|
|
||||||
}
|
}
|
||||||
new File(file).delete();
|
new File(file).delete();
|
||||||
}
|
}
|
||||||
@@ -313,8 +396,8 @@ public class Updater {
|
|||||||
/**
|
/**
|
||||||
* Check if the name of a jar is one of the plugins currently installed, used for extracting the correct files out of a zip.
|
* 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) {
|
private boolean pluginFile(String name) {
|
||||||
for (File file : new File("plugins").listFiles()) {
|
for (final File file : new File("plugins").listFiles()) {
|
||||||
if (file.getName().equals(name)) {
|
if (file.getName().equals(name)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -322,104 +405,38 @@ public class Updater {
|
|||||||
return false;
|
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("<li class=\"user-action user-action-download\">")) {
|
|
||||||
// Get the raw link
|
|
||||||
download = line.split("<a href=\"")[1].split("\">Download</a>")[0];
|
|
||||||
}
|
|
||||||
// Search for size
|
|
||||||
else if (line.contains("<dt>Size</dt>")) {
|
|
||||||
sizeLine = counter + 1;
|
|
||||||
} else if (counter == sizeLine) {
|
|
||||||
String size = line.replaceAll("<dd>", "").replaceAll("</dd>", "");
|
|
||||||
multiplier = size.contains("MiB") ? 1048576 : 1024;
|
|
||||||
size = size.replace(" KiB", "").replace(" MiB", "");
|
|
||||||
totalSize = (long) (Double.parseDouble(size) * multiplier);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
urlConn = null;
|
|
||||||
inStream = null;
|
|
||||||
buff.close();
|
|
||||||
buff = null;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
plugin.getLogger().warning("The auto-updater tried to contact dev.bukkit.org, but was unsuccessful.");
|
|
||||||
result = Updater.UpdateResult.FAIL_DBO;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return download;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check to see if the program should continue by evaluation whether the plugin is already updated, or shouldn't be updated
|
* Check to see if the program should continue by evaluation whether the plugin is already updated, or shouldn't be updated
|
||||||
*/
|
*/
|
||||||
private boolean versionCheck(String title) {
|
private boolean versionCheck(String title) {
|
||||||
if (type != UpdateType.NO_VERSION_CHECK) {
|
if (this.type != UpdateType.NO_VERSION_CHECK) {
|
||||||
String version = plugin.getDescription().getVersion();
|
final String version = this.plugin.getDescription().getVersion();
|
||||||
if (title.split(" v").length == 2) {
|
if (title.split(" v").length == 2) {
|
||||||
String remoteVersion = title.split(" v")[1].split(" ")[0]; // Get the newest file's version number
|
final String remoteVersion = title.split(" v")[1].split(" ")[0]; // Get the newest file's version number
|
||||||
int remVer = -1, curVer = 0;
|
|
||||||
try {
|
if (this.hasTag(version) || version.equalsIgnoreCase(remoteVersion)) {
|
||||||
remVer = calVer(remoteVersion);
|
|
||||||
curVer = calVer(version);
|
|
||||||
} catch (NumberFormatException nfe) {
|
|
||||||
remVer = -1;
|
|
||||||
}
|
|
||||||
if (hasTag(version) || version.equalsIgnoreCase(remoteVersion) || curVer >= remVer) {
|
|
||||||
// We already have the latest version, or this build is tagged for no-update
|
// We already have the latest version, or this build is tagged for no-update
|
||||||
result = Updater.UpdateResult.NO_UPDATE;
|
this.result = Updater.UpdateResult.NO_UPDATE;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// The file's name did not contain the string 'vVersion'
|
// The file's name did not contain the string 'vVersion'
|
||||||
plugin.getLogger().warning("The author of this plugin (" + plugin.getDescription().getAuthors().get(0) + ") has misconfigured their Auto Update system");
|
final String authorInfo = this.plugin.getDescription().getAuthors().size() == 0 ? "" : " (" + this.plugin.getDescription().getAuthors().get(0) + ")";
|
||||||
plugin.getLogger().warning("Files uploaded to BukkitDev should contain the version number, seperated from the name by a 'v', such as PluginName v1.0");
|
this.plugin.getLogger().warning("The author of this plugin" + authorInfo + " has misconfigured their Auto Update system");
|
||||||
plugin.getLogger().warning("Please notify the author of this error.");
|
this.plugin.getLogger().warning("File versions should follow the format 'PluginName vVERSION'");
|
||||||
result = Updater.UpdateResult.FAIL_NOVERSION;
|
this.plugin.getLogger().warning("Please notify the author of this error.");
|
||||||
|
this.result = Updater.UpdateResult.FAIL_NOVERSION;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Used to calculate the version string as an Integer
|
|
||||||
*/
|
|
||||||
private Integer calVer(String s) throws NumberFormatException {
|
|
||||||
if (s.contains(".")) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for (int i = 0; i < s.length(); i++) {
|
|
||||||
Character c = s.charAt(i);
|
|
||||||
if (Character.isLetterOrDigit(c)) {
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Integer.parseInt(sb.toString());
|
|
||||||
}
|
|
||||||
return Integer.parseInt(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluate whether the version number is marked showing that it should not be updated by this program
|
* Evaluate whether the version number is marked showing that it should not be updated by this program
|
||||||
*/
|
*/
|
||||||
private boolean hasTag(String version) {
|
private boolean hasTag(String version) {
|
||||||
for (String string : noUpdateTag) {
|
for (final String string : Updater.NO_UPDATE_TAG) {
|
||||||
if (version.contains(string)) {
|
if (version.contains(string)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -427,84 +444,68 @@ public class Updater {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private boolean read() {
|
||||||
* Part of RSS Reader by Vogella, modified by H31IX for use with Bukkit
|
|
||||||
*/
|
|
||||||
private boolean readFeed() {
|
|
||||||
try {
|
try {
|
||||||
// Set header values intial to the empty string
|
final URLConnection conn = this.url.openConnection();
|
||||||
String title = "";
|
conn.setConnectTimeout(5000);
|
||||||
String link = "";
|
|
||||||
// First create a new XMLInputFactory
|
if (this.apiKey != null) {
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
conn.addRequestProperty("X-API-Key", this.apiKey);
|
||||||
// Setup a new eventReader
|
|
||||||
InputStream in = read();
|
|
||||||
if (in != null) {
|
|
||||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
|
|
||||||
// Read the XML document
|
|
||||||
while (eventReader.hasNext()) {
|
|
||||||
XMLEvent event = eventReader.nextEvent();
|
|
||||||
if (event.isStartElement()) {
|
|
||||||
if (event.asStartElement().getName().getLocalPart().equals(TITLE)) {
|
|
||||||
event = eventReader.nextEvent();
|
|
||||||
title = event.asCharacters().getData();
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if (event.asStartElement().getName().getLocalPart().equals(LINK)) {
|
conn.addRequestProperty("User-Agent", "Updater (by Gravity)");
|
||||||
event = eventReader.nextEvent();
|
|
||||||
link = event.asCharacters().getData();
|
conn.setDoOutput(true);
|
||||||
continue;
|
|
||||||
}
|
final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
} else if (event.isEndElement()) {
|
final String response = reader.readLine();
|
||||||
if (event.asEndElement().getName().getLocalPart().equals(ITEM)) {
|
|
||||||
// Store the title and link of the first entry we get - the first file on the list is all we need
|
final JSONArray array = (JSONArray) JSONValue.parse(response);
|
||||||
versionTitle = title;
|
|
||||||
versionLink = link;
|
if (array.size() == 0) {
|
||||||
// All done, we don't need to know about older files.
|
this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id);
|
||||||
break;
|
this.result = UpdateResult.FAIL_BADID;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (XMLStreamException e) {
|
|
||||||
plugin.getLogger().warning("Could not reach dev.bukkit.org for update checking. Is it offline?");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
this.versionName = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TITLE_VALUE);
|
||||||
* Open the RSS feed
|
this.versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.LINK_VALUE);
|
||||||
*/
|
this.versionType = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TYPE_VALUE);
|
||||||
private InputStream read() {
|
this.versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.VERSION_VALUE);
|
||||||
try {
|
|
||||||
return url.openStream();
|
return true;
|
||||||
} catch (IOException e) {
|
} catch (final IOException e) {
|
||||||
plugin.getLogger().warning("Could not reach BukkitDev file stream for update checking. Is dev.bukkit.org offline?");
|
if (e.getMessage().contains("HTTP response code: 403")) {
|
||||||
return null;
|
this.plugin.getLogger().warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml");
|
||||||
|
this.plugin.getLogger().warning("Please double-check your configuration to ensure it is correct.");
|
||||||
|
this.result = UpdateResult.FAIL_APIKEY;
|
||||||
|
} else {
|
||||||
|
this.plugin.getLogger().warning("The updater could not contact dev.bukkit.org for updating.");
|
||||||
|
this.plugin.getLogger().warning("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
|
||||||
|
this.result = UpdateResult.FAIL_DBO;
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class UpdateRunnable implements Runnable {
|
private class UpdateRunnable implements Runnable {
|
||||||
|
|
||||||
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (url != null) {
|
if (Updater.this.url != null) {
|
||||||
// Obtain the results of the project's file feed
|
// Obtain the results of the project's file feed
|
||||||
if (readFeed()) {
|
if (Updater.this.read()) {
|
||||||
if (versionCheck(versionTitle)) {
|
if (Updater.this.versionCheck(Updater.this.versionName)) {
|
||||||
String fileLink = getFile(versionLink);
|
if ((Updater.this.versionLink != null) && (Updater.this.type != UpdateType.NO_DOWNLOAD)) {
|
||||||
if (fileLink != null && type != UpdateType.NO_DOWNLOAD) {
|
String name = Updater.this.file.getName();
|
||||||
String name = file.getName();
|
|
||||||
// If it's a zip file, it shouldn't be downloaded as the plugin's name
|
// If it's a zip file, it shouldn't be downloaded as the plugin's name
|
||||||
if (fileLink.endsWith(".zip")) {
|
if (Updater.this.versionLink.endsWith(".zip")) {
|
||||||
String[] split = fileLink.split("/");
|
final String[] split = Updater.this.versionLink.split("/");
|
||||||
name = split[split.length - 1];
|
name = split[split.length - 1];
|
||||||
}
|
}
|
||||||
saveFile(new File("plugins/" + updateFolder), name, fileLink);
|
Updater.this.saveFile(new File(Updater.this.plugin.getDataFolder().getParent(), Updater.this.updateFolder), name, Updater.this.versionLink);
|
||||||
} else {
|
} else {
|
||||||
result = UpdateResult.UPDATE_AVAILABLE;
|
Updater.this.result = UpdateResult.UPDATE_AVAILABLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user