Fixes downloading of jar files

Links to knarcraft.net updated.
Code for parsing the new minecraft vanilla json files added.
This commit is contained in:
Kristian Knarvik 2018-09-30 17:06:57 +02:00
parent 5ac22babab
commit a3a8e4f377
4 changed files with 59 additions and 23 deletions

View File

@ -1,5 +1,8 @@
package net.knarcraft.serverlauncher.profile;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.knarcraft.serverlauncher.server.AdvancedServerType;
import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType;
@ -367,8 +370,10 @@ public class Profile {
"Error",
JOptionPane.ERROR_MESSAGE
);
throw new FileNotFoundException("Unable to save to the profiles file.");
} else {
System.out.println("Unable to save to file. Try running the software as an administrator.");
}
throw new FileNotFoundException("Unable to save to the profiles file.");
}
}
} catch (IOException e) {
@ -514,9 +519,17 @@ public class Profile {
}
try {
downloadAll();
gui.setStatus("Finished downloading jars");
printToGui("Finished downloading jars");
} catch (FileNotFoundException e) {
gui.setStatus("One or more downloads failed: " + e.getMessage());
printToGui("One or more downloads failed: " + e.getMessage());
}
}
private static void printToGui(String str) {
if (gui != null) {
gui.setStatus(str);
} else {
System.out.println(str);
}
}
@ -531,18 +544,25 @@ public class Profile {
AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
for (String version : type.getVersions()) {
Boolean success;
if (gui != null) {
gui.setStatus("Downloading: " + name + version + ".jar");
}
printToGui("Downloading: " + name + version + ".jar");
File file = new File(jarDir + type.getName() + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
switch (type.getName()) {
case "Vanilla":
case "Snapshot":
if (version.equals("Latest")) {
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd());
setVersion(name, newestVersion);
success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath);
String versionText = readFile(Objects.requireNonNull(advType).getVersionURL());
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString();
JsonElement ver = jsonObject.getAsJsonArray("versions").get(0);
String versionFile = ver.getAsJsonObject().get("url").getAsString();
versionText = readFile(versionFile);
jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
setVersion(name, latest);
success = (file.isFile() && latest.equals(getVersion(name))) || downloadFile(jarFile, filePath);
} else {
success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath);
}
@ -565,9 +585,7 @@ public class Profile {
success = true;
}
if (!success) {
if (gui != null) {
gui.setStatus("Error downloading: " + name + version + ".jar");
}
printToGui("Error downloading: " + name + version + ".jar");
throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
}
}

View File

@ -1,16 +1,19 @@
package net.knarcraft.serverlauncher.server;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.knarcraft.serverlauncher.Main;
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import java.io.*;
import java.nio.file.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import static net.knarcraft.serverlauncher.Shared.downloadFile;
import static net.knarcraft.serverlauncher.Shared.readFile;
import static net.knarcraft.serverlauncher.Shared.stringBetween;
import static net.knarcraft.serverlauncher.Shared.*;
/**
@ -275,6 +278,7 @@ public class Server {
this.downloadJar();
Profile.getGUI().setStatus("File downloaded");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
Profile.getGUI().setStatus("Error: Jar file not found");
e.printStackTrace();
this.started = false;
@ -374,10 +378,24 @@ public class Server {
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd());
if (!file.isFile() || !newestVersion.equals(this.getVersion(name))) {
this.setVersion(name, newestVersion);
if (!downloadFile(url + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath)) {
JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get("release").getAsString();
JsonElement verElem = jsonObject.getAsJsonArray("versions").get(0);
String versionFile = verElem.getAsJsonObject().get("url").getAsString();
try {
versionText = readFile(versionFile);
} catch (IOException e) {
throw new FileNotFoundException("Version file could not be downloaded.");
}
if (!file.isFile() || !latest.equals(this.getVersion(name))) {
this.setVersion(name, latest);
jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
String jarFile = jsonObject.getAsJsonObject("downloads").getAsJsonObject("server").get("url").getAsString();
if (!downloadFile(jarFile, filePath)) {
throw new FileNotFoundException("Jar file could not be downloaded.");
}
}

View File

@ -1,6 +1,6 @@
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.ServerType;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import javax.naming.ConfigurationException;
import java.io.IOException;

View File

@ -1,12 +1,12 @@
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.ServerType;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import javax.naming.ConfigurationException;
import java.io.FileNotFoundException;
import static net.knarcraft.serverlauncher.Shared.stringBetween;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Tests {
@Test