This should fix all issues regarding the "latest" version for all server types
Some checks failed
KnarCraft/Minecraft-Server-Launcher/pipeline/head There was a failure building this commit

This commit is contained in:
Kristian Knarvik 2020-08-15 20:40:46 +02:00
parent 70d064e590
commit 0bf355c4de
12 changed files with 389 additions and 67 deletions

View File

@ -5,6 +5,7 @@ import net.knarcraft.minecraftserverlauncher.profile.Profile;
import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.server.Server;
import net.knarcraft.minecraftserverlauncher.userinterface.GUI; import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles; import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import net.knarcraft.minecraftserverlauncher.utility.Updater; import net.knarcraft.minecraftserverlauncher.utility.Updater;
import java.awt.*; import java.awt.*;
@ -80,7 +81,7 @@ public class Main {
Server server = collection.getServer(); Server server = collection.getServer();
if (server.isEnabled() && server.getProcess() != null) { if (server.isEnabled() && server.getProcess() != null) {
try { try {
String readText = server.readFromServer(); String readText = CommonFunctions.readBufferedReader(server.getReader());
if (!readText.equals("")) { if (!readText.equals("")) {
collection.getServerConsole().output(readText); collection.getServerConsole().output(readText);
updatePlayerList(readText, server); updatePlayerList(readText, server);

View File

@ -29,9 +29,10 @@ import java.util.concurrent.Executors;
*/ */
public class Profile implements java.io.Serializable { public class Profile implements java.io.Serializable {
private static final ArrayList<Profile> profiles = new ArrayList<>(); private static final ArrayList<Profile> profiles = new ArrayList<>();
private static final String profilesDir = Main.getApplicationWorkDirectory() + File.separator + "files"; private static final String workingDirectory = Main.getApplicationWorkDirectory() + File.separator + "files";
private static final String profilesFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Profiles.txt"; private static final String profilesDir = workingDirectory;
private static final String jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "Jars" + File.separator; private static final String profilesFile = workingDirectory + File.separator + "Profiles.txt";
private static final String jarDirectory = workingDirectory + File.separator + "Jars" + File.separator;
private static Profile current; private static Profile current;
private static transient ServerLauncherGUI serverLauncherGui; private static transient ServerLauncherGUI serverLauncherGui;
private final ArrayList<Collection> collections; private final ArrayList<Collection> collections;

View File

@ -84,10 +84,24 @@ public class Server implements java.io.Serializable {
this.playerList = new ArrayList<>(); this.playerList = new ArrayList<>();
} }
/**
* Gets the list of available RAM choices allowed
*
* @return <p>All available RAM choices</p>
*/
public static String[] getRamList() { public static String[] getRamList() {
return ramList; return ramList;
} }
/**
* Gets the buffered reader used to read from this server
*
* @return <p>The buffered reader used to read from this server</p>
*/
public BufferedReader getReader() {
return this.reader;
}
/** /**
* Tries to stop all enabled servers. * Tries to stop all enabled servers.
* *
@ -357,21 +371,6 @@ public class Server implements java.io.Serializable {
} }
} }
/**
* Reads all available output from the server process
*
* @return <p>The server output</p>
* @throws IOException <p>If reading from the reader fails</p>
*/
public String readFromServer() throws IOException {
String line;
StringBuilder text = new StringBuilder();
while (reader.ready() && (line = reader.readLine()) != null) {
text.append(line).append("\n");
}
return text.toString().trim();
}
/** /**
* Starts the process running this server * Starts the process running this server
* *

View File

@ -1,9 +1,19 @@
package net.knarcraft.minecraftserverlauncher.server; package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import java.io.*;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* This class acts as a container for all "latest" server versions
*/
public class ServerVersionContainer implements java.io.Serializable { public class ServerVersionContainer implements java.io.Serializable {
private static ServerVersionContainer serverVersionContainer;
private String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv";
private String vanillaVersion; private String vanillaVersion;
private String snapshotVersion; private String snapshotVersion;
private String bungeeVersion; private String bungeeVersion;
@ -11,10 +21,157 @@ public class ServerVersionContainer implements java.io.Serializable {
private String travertineVersion; private String travertineVersion;
private Map<String, String> spongeVanillaVersions; private Map<String, String> spongeVanillaVersions;
public ServerVersionContainer() { /**
* Initializes a new server version container
*/
private ServerVersionContainer() {
spongeVanillaVersions = new HashMap<>();
loadState();
} }
/**
* Gives a server version container instance
*
* @return <p>A server version container instance</p>
*/
public static ServerVersionContainer getInstance() {
if (serverVersionContainer == null) {
serverVersionContainer = new ServerVersionContainer();
}
return serverVersionContainer;
}
/**
* Resets the state of the server version container
*/
public void reset() {
this.vanillaVersion = null;
this.snapshotVersion = null;
this.bungeeVersion = null;
this.waterfallVersion = null;
this.travertineVersion = null;
this.spongeVanillaVersions = new HashMap<>();
}
@Override
public String toString() {
return "vanillaVersion;" + vanillaVersion + "\n" +
"snapshotVersion;" + snapshotVersion + "\n" +
"bungeeVersion;" + bungeeVersion + "\n" +
"waterfallVersion;" + waterfallVersion + "\n" +
"travertineVersion;" + travertineVersion + "\n" +
"spongeVanillaVersions;" +
mapToString(spongeVanillaVersions);
}
/**
* Formats a map to a string given a map
*
* @param targetMap <p>The map to format</p>
* @return <p>A string representing the map</p>
*/
private String mapToString(Map<?, ?> targetMap) {
StringBuilder stringBuilder = new StringBuilder();
int mapSize = targetMap.keySet().size();
int mapIndex = 0;
for (Object key : targetMap.keySet()) {
stringBuilder.append(key).append("!").append(targetMap.get(key));
if (mapIndex < mapSize) {
stringBuilder.append(",");
}
mapIndex++;
}
return stringBuilder.toString();
}
/**
* Tries to save the state of this server version container
*/
public void saveState() {
PrintWriter file;
try {
file = new PrintWriter(versionFile);
file.println(this.toString());
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* Loads the object state from the save file
*/
private void loadState() {
BufferedReader reader;
if (!new File(versionFile).exists()) {
return;
}
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(versionFile)));
String currentData = CommonFunctions.readBufferedReader(reader);
for (String line : currentData.split("\n")) {
parseSaveLine(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Parses one line in the version save file and updates the appropriate variable
*
* @param line <p>The line from the save file to parse</p>
*/
private void parseSaveLine(String line) {
String[] lineData = line.split(";");
if (lineData.length != 2) {
return;
}
String variableKey = lineData[0];
String variableValue = lineData[1];
switch (variableKey) {
case "vanillaVersion":
vanillaVersion = variableValue;
break;
case "snapshotVersion":
snapshotVersion = variableValue;
break;
case "bungeeVersion":
bungeeVersion = variableValue;
break;
case "waterfallVersion":
waterfallVersion = variableValue;
break;
case "travertineVersion":
travertineVersion = variableValue;
break;
case "spongeVanillaVersions":
parseSpongeVanillaVersions(variableValue);
break;
default:
throw new IllegalArgumentException("Invalid key encountered in the server version file.");
}
}
/**
* Reads spongeVanilla versions from a text string and updates the version map
*
* @param data <p>The data string to parse</p>
*/
private void parseSpongeVanillaVersions(String data) {
String[] versions = data.split(",");
for (String version : versions) {
String[] versionData = version.split("!");
spongeVanillaVersions.put(versionData[0], versionData[1]);
}
}
/**
* Gets the current vanilla version
*
* @return <p>The current vanilla version</p>
*/
public String getVanillaVersion() { public String getVanillaVersion() {
return this.vanillaVersion; return this.vanillaVersion;
} }
@ -23,65 +180,126 @@ public class ServerVersionContainer implements java.io.Serializable {
* Sets the vanilla server version * Sets the vanilla server version
* *
* @param newVersion <p>The new vanilla server version</p> * @param newVersion <p>The new vanilla server version</p>
* @return <p>This object</p>
*/ */
public ServerVersionContainer setVanillaVersion(String newVersion) { public void setVanillaVersion(String newVersion) {
this.vanillaVersion = newVersion; this.vanillaVersion = newVersion;
return this; saveState();
} }
/**
* Gets the current snapshot version
*
* @return <p>The current snapshot version</p>
*/
public String getSnapshotVersion() { public String getSnapshotVersion() {
return this.snapshotVersion; return this.snapshotVersion;
} }
public ServerVersionContainer setSnapshotVersion(String newVersion) { /**
* Sets the snapshot server version
*
* @param newVersion <p>The new snapshot server version</p>
*/
public void setSnapshotVersion(String newVersion) {
this.snapshotVersion = newVersion; this.snapshotVersion = newVersion;
return this; saveState();
} }
/**
* Gets the current bungee version
*
* @return <p>The current bungee version</p>
*/
public String getBungeeVersion() { public String getBungeeVersion() {
return this.bungeeVersion; return this.bungeeVersion;
} }
public ServerVersionContainer setBungeeVersion(String newVersion) { /**
* Sets the bungee server version
*
* @param newVersion <p>The new bungee server version</p>
*/
public void setBungeeVersion(String newVersion) {
this.bungeeVersion = newVersion; this.bungeeVersion = newVersion;
return this; saveState();
} }
/**
* Gets the current waterfall version
*
* @return <p>The current waterfall version</p>
*/
public String getWaterfallVersion() { public String getWaterfallVersion() {
return this.waterfallVersion; return this.waterfallVersion;
} }
public ServerVersionContainer setWaterfallVersion(String newVersion) { /**
* Sets the waterfall server version
*
* @param newVersion <p>The new waterfall server version</p>
*/
public void setWaterfallVersion(String newVersion) {
this.waterfallVersion = newVersion; this.waterfallVersion = newVersion;
return this; saveState();
} }
/**
* Gets the current travertine server version
*
* @return <p>The current travertine version</p>
*/
public String getTravertineVersion() { public String getTravertineVersion() {
return this.travertineVersion; return this.travertineVersion;
} }
public ServerVersionContainer setTravertineVersion(String newVersion) { /**
* Sets the travertine server version
*
* @param newVersion <p>The new travertine server version</p>
*/
public void setTravertineVersion(String newVersion) {
this.travertineVersion = newVersion; this.travertineVersion = newVersion;
return this; saveState();
} }
/**
* Gets the entire sponge vanilla version map
*
* @return <p>The entire sponge vanilla version map</p>
*/
public Map<String, String> getSpongeVanillaVersions() { public Map<String, String> getSpongeVanillaVersions() {
return this.spongeVanillaVersions; return this.spongeVanillaVersions;
} }
/**
* Gets a specific sponge vanilla version
*
* @param versionKey <p>The version to check current version of</p>
* @return <p>The current sponge vanilla version</p>
*/
public String getSpongeVanillaVersion(String versionKey) { public String getSpongeVanillaVersion(String versionKey) {
return spongeVanillaVersions.get(versionKey); return spongeVanillaVersions.get(versionKey);
} }
public ServerVersionContainer setSpongeVanillaVersion(Map<String, String> newVersions) { /**
* Replaces the entire sponge vanilla version map
*
* @param newVersions <p>The new version map to use</p>
*/
public void setSpongeVanillaVersion(Map<String, String> newVersions) {
this.spongeVanillaVersions = newVersions; this.spongeVanillaVersions = newVersions;
return this; saveState();
} }
public ServerVersionContainer setSpongeVanillaVersion(String mapKey, String newValue) { /**
* Sets the current version for a given sponge vanilla version
*
* @param mapKey <p>The version key to set version for</p>
* @param newValue <p>The new current version</p>
*/
public void setSpongeVanillaVersion(String mapKey, String newValue) {
spongeVanillaVersions.put(mapKey, newValue); spongeVanillaVersions.put(mapKey, newValue);
return this; saveState();
} }
} }

View File

@ -1,5 +1,7 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes; package net.knarcraft.minecraftserverlauncher.server.servertypes;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -11,7 +13,6 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stri
public class BungeeCord extends AbstractServerType { public class BungeeCord extends AbstractServerType {
private String versionURL; private String versionURL;
private String lastVersion;
private String srcStart; private String srcStart;
private String srcEnd; private String srcEnd;
@ -35,11 +36,20 @@ public class BungeeCord extends AbstractServerType {
@Override @Override
public boolean downloadJar(String folder, String version) throws IOException { public boolean downloadJar(String folder, String version) throws IOException {
ServerVersionContainer versionContainer = ServerVersionContainer.getInstance();
String file = this.getName() + version + ".jar"; String file = this.getName() + version + ".jar";
File filePath = new File(folder + file); File filePath = new File(folder + file);
String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd);
String oldVersion = lastVersion; String oldVersion = versionContainer.getBungeeVersion();
lastVersion = newestVersion; //The file is already the newest version
return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(downloadURL, Paths.get(filePath.toURI())); if (filePath.isFile() && newestVersion.equals(oldVersion)) {
return true;
}
//The new jar file could not be downloaded
if (!downloadFile(downloadURL, Paths.get(filePath.toURI()))) {
return false;
}
versionContainer.setBungeeVersion(newestVersion);
return true;
} }
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes; package net.knarcraft.minecraftserverlauncher.server.servertypes;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import java.io.File; import java.io.File;
@ -11,7 +12,6 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.down
public class SpongeVanilla extends AbstractServerType { public class SpongeVanilla extends AbstractServerType {
private String versionURL; private String versionURL;
private String lastVersion;
private String srcStart; private String srcStart;
private String srcEnd; private String srcEnd;
private String downloadURLPart; private String downloadURLPart;
@ -39,6 +39,7 @@ public class SpongeVanilla extends AbstractServerType {
@Override @Override
public boolean downloadJar(String folder, String version) throws IOException { public boolean downloadJar(String folder, String version) throws IOException {
ServerVersionContainer versionContainer = ServerVersionContainer.getInstance();
String file = this.getName() + version + ".jar"; String file = this.getName() + version + ".jar";
File filePath = new File(folder + file); File filePath = new File(folder + file);
@ -46,9 +47,17 @@ public class SpongeVanilla extends AbstractServerType {
String newestVersion = CommonFunctions.stringBetween(versionText, srcStart, srcEnd); String newestVersion = CommonFunctions.stringBetween(versionText, srcStart, srcEnd);
String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar"; String jarURL = downloadURL + newestVersion + downloadURLPart + newestVersion + ".jar";
String oldVersion = lastVersion;
lastVersion = newestVersion;
return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(jarURL, Paths.get(filePath.toURI())); String oldVersion = versionContainer.getSpongeVanillaVersion(version);
//The file is already the newest version
if (filePath.isFile() && newestVersion.equals(oldVersion)) {
return true;
}
//The new jar file could not be downloaded
if (!downloadFile(jarURL, Paths.get(filePath.toURI()))) {
return false;
}
versionContainer.setSpongeVanillaVersion(version, newestVersion);
return true;
} }
} }

View File

@ -1,5 +1,7 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes; package net.knarcraft.minecraftserverlauncher.server.servertypes;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
public class Travertine extends Waterfall { public class Travertine extends Waterfall {
/** /**
@ -13,8 +15,11 @@ public class Travertine extends Waterfall {
* @param srcEnd <p>The string marking the end of the version id</p> * @param srcEnd <p>The string marking the end of the version id</p>
* @param downloadURL <p>The URL used for downloading the latest version</p> * @param downloadURL <p>The URL used for downloading the latest version</p>
*/ */
public Travertine(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart, String srcEnd, String downloadURL) { public Travertine(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart,
String srcEnd, String downloadURL) {
super(typeName, isProxy, versions, versionURL, srcStart, srcEnd, downloadURL); super(typeName, isProxy, versions, versionURL, srcStart, srcEnd, downloadURL);
this.oldVersion = ServerVersionContainer.getInstance().getTravertineVersion();
this.versionUpdateFunction = (newVersion) -> serverVersionContainer.setTravertineVersion(newVersion);
} }
} }

View File

@ -4,6 +4,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -19,8 +20,6 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.read
public class Vanilla extends AbstractServerType { public class Vanilla extends AbstractServerType {
private final String versionURL; private final String versionURL;
private String lastVanillaVersion;
private String lastSnapshotVersion;
/** /**
* Instantiates a vanilla server type * Instantiates a vanilla server type
@ -38,12 +37,14 @@ public class Vanilla extends AbstractServerType {
@Override @Override
public boolean downloadJar(String folder, String version) throws IOException { public boolean downloadJar(String folder, String version) throws IOException {
ServerVersionContainer versionContainer = ServerVersionContainer.getInstance();
String file = this.getName() + version + ".jar"; String file = this.getName() + version + ".jar";
File filePath = new File(folder + file); File filePath = new File(folder + file);
if (version.equals("Latest") || version.equals("Snapshot")) { if (version.equals("Latest") || version.equals("Snapshot")) {
String releaseType = version.equals("Latest") ? "release" : "snapshot"; String releaseType = version.equals("Latest") ? "release" : "snapshot";
String lastVersion = version.equals("Latest") ? lastVanillaVersion : lastSnapshotVersion; String lastVersion = version.equals("Latest") ? versionContainer.getVanillaVersion() :
return downloadLatestJar(filePath, releaseType, lastVersion); versionContainer.getSnapshotVersion();
return downloadLatestJar(filePath, releaseType, lastVersion, versionContainer);
} else { } else {
String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version)); String downloadURL = getVanillaDownloadURL(getServerFileVersionURL(version));
return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI())); return filePath.isFile() || downloadFile(downloadURL, Paths.get(filePath.toURI()));
@ -56,19 +57,29 @@ public class Vanilla extends AbstractServerType {
* @param filePath <p>The path of the jar file to download</p> * @param filePath <p>The path of the jar file to download</p>
* @param releaseType <p>The release type used for downloading</p> * @param releaseType <p>The release type used for downloading</p>
* @param lastVersion <p>The last server version found</p> * @param lastVersion <p>The last server version found</p>
* @param versionContainer <p>The version container to use</p>
* @return <p>True if the jar exists and is the latest version or was downloaded</p> * @return <p>True if the jar exists and is the latest version or was downloaded</p>
* @throws IOException <p>If the .jar cannot be downloaded</p> * @throws IOException <p>If the .jar cannot be downloaded</p>
*/ */
private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion) throws IOException { private boolean downloadLatestJar(File filePath, String releaseType, String lastVersion,
ServerVersionContainer versionContainer) throws IOException {
String[] latestData = getLatestFile(releaseType); String[] latestData = getLatestFile(releaseType);
String latest = latestData[0]; String latest = latestData[0];
String jarFile = latestData[1]; String jarFile = latestData[1];
if (releaseType.equals("release")) { //The file is already the newest version
lastVanillaVersion = latest; if (filePath.isFile() && latest.equals(lastVersion)) {
} else { return true;
lastSnapshotVersion = latest;
} }
return (filePath.isFile() && latest.equals(lastVersion)) || downloadFile(jarFile, Paths.get(filePath.toURI())); //The new jar file could not be downloaded
if (!downloadFile(jarFile, Paths.get(filePath.toURI()))) {
return false;
}
if (releaseType.equals("release")) {
versionContainer.setVanillaVersion(latest);
} else {
versionContainer.setSnapshotVersion(latest);
}
return true;
} }
/** /**

View File

@ -1,8 +1,12 @@
package net.knarcraft.minecraftserverlauncher.server.servertypes; package net.knarcraft.minecraftserverlauncher.server.servertypes;
import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.function.Function;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile; import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
@ -13,7 +17,9 @@ public class Waterfall extends AbstractServerType {
private String srcStart; private String srcStart;
private String srcEnd; private String srcEnd;
private String versionURL; private String versionURL;
private String lastVersion; String oldVersion;
Consumer<String> versionUpdateFunction;
ServerVersionContainer serverVersionContainer;
/** /**
* Instantiates a new Waterfall server type * Instantiates a new Waterfall server type
@ -29,9 +35,12 @@ public class Waterfall extends AbstractServerType {
public Waterfall(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart, public Waterfall(String typeName, boolean isProxy, String[] versions, String versionURL, String srcStart,
String srcEnd, String downloadURL) { String srcEnd, String downloadURL) {
super(typeName, isProxy, versions, downloadURL); super(typeName, isProxy, versions, downloadURL);
this.serverVersionContainer = ServerVersionContainer.getInstance();
this.srcStart = srcStart; this.srcStart = srcStart;
this.srcEnd = srcEnd; this.srcEnd = srcEnd;
this.versionURL = versionURL; this.versionURL = versionURL;
this.oldVersion = serverVersionContainer.getWaterfallVersion();
this.versionUpdateFunction = (newVersion) -> serverVersionContainer.setWaterfallVersion(newVersion);
} }
@Override @Override
@ -39,9 +48,16 @@ public class Waterfall extends AbstractServerType {
String file = this.getName() + version + ".jar"; String file = this.getName() + version + ".jar";
File filePath = new File(folder + file); File filePath = new File(folder + file);
String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd); String newestVersion = stringBetween(readFile(versionURL), srcStart, srcEnd);
String oldVersion = lastVersion;
lastVersion = newestVersion;
String fullURL = downloadURL + newestVersion + "/download"; String fullURL = downloadURL + newestVersion + "/download";
return (filePath.isFile() && newestVersion.equals(oldVersion)) || downloadFile(fullURL, Paths.get(filePath.toURI())); //The file is already the newest version
if (filePath.isFile() && newestVersion.equals(this.oldVersion)) {
return true;
}
//The new jar file could not be downloaded
if (!downloadFile(fullURL, Paths.get(filePath.toURI()))) {
return false;
}
versionUpdateFunction.accept(newestVersion);
return true;
} }
} }

View File

@ -2,13 +2,7 @@ package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
@ -161,4 +155,20 @@ public final class CommonFunctions {
e1.printStackTrace(); e1.printStackTrace();
} }
} }
/**
* Reads all lines from a buffered reader
*
* @param reader <p>The buffered reader to read from</p>
* @return <p>All lines currently readable from the reader, split by the \n character</p>
* @throws IOException <p>If unable to read from the buffered reader</p>
*/
public static String readBufferedReader(BufferedReader reader) throws IOException {
String line;
StringBuilder text = new StringBuilder();
while (reader.ready() && (line = reader.readLine()) != null) {
text.append(line).append("\n");
}
return text.toString().trim();
}
} }

View File

@ -0,0 +1,38 @@
package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import static junit.framework.TestCase.assertEquals;
public class ServerVersionContainerTest {
private String versionFile = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + "versions.csv";
private ServerVersionContainer serverVersionContainer;
@Before
public void setUp() {
serverVersionContainer = ServerVersionContainer.getInstance();
}
@Test
public void toStringTest() {
serverVersionContainer.reset();
assertEquals("vanillaVersion;null\nsnapshotVersion;null\nbungeeVersion;null\nwaterfallVersion;null\n" +
"travertineVersion;null\nspongeVanillaVersions;",
serverVersionContainer.toString());
}
@Test
public void saveStateTest() throws IOException {
serverVersionContainer.saveState();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(versionFile)));
String savedData = CommonFunctions.readBufferedReader(reader);
assertEquals(serverVersionContainer.toString(), savedData);
}
}

View File

@ -100,7 +100,11 @@ public class JarDownloaderTest {
* Removes downloaded test jars * Removes downloaded test jars
*/ */
private static void removeDownloadedFiles() { private static void removeDownloadedFiles() {
File[] oldFiles = new File(targetDirectory).listFiles(); File target = new File(targetDirectory);
if (!target.exists() && !target.mkdirs()) {
throw new IllegalArgumentException("Unable to create the test files directory");
}
File[] oldFiles = target.listFiles();
if (oldFiles == null) { if (oldFiles == null) {
throw new IllegalArgumentException("Unable to list files in jar test directory"); throw new IllegalArgumentException("Unable to list files in jar test directory");
} }