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; 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.AdvancedServerType;
import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.Server;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
@ -367,8 +370,10 @@ public class Profile {
"Error", "Error",
JOptionPane.ERROR_MESSAGE 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) { } catch (IOException e) {
@ -514,9 +519,17 @@ public class Profile {
} }
try { try {
downloadAll(); downloadAll();
gui.setStatus("Finished downloading jars"); printToGui("Finished downloading jars");
} catch (FileNotFoundException e) { } 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; AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
Boolean success; Boolean success;
if (gui != null) { printToGui("Downloading: " + name + version + ".jar");
gui.setStatus("Downloading: " + name + version + ".jar");
}
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + type.getName() + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
switch (type.getName()) { switch (type.getName()) {
case "Vanilla": case "Vanilla":
case "Snapshot": case "Snapshot":
if (version.equals("Latest")) { if (version.equals("Latest")) {
newestVersion = stringBetween(readFile(Objects.requireNonNull(advType).getVersionURL()), advType.getSrcStart(), advType.getSrcEnd()); String versionText = readFile(Objects.requireNonNull(advType).getVersionURL());
setVersion(name, newestVersion); JsonObject jsonObject = new JsonParser().parse(versionText).getAsJsonObject();
success = (file.isFile() && newestVersion.equals(getVersion(name))) || downloadFile(url + newestVersion + advType.getDownloadURLPart() + newestVersion + ".jar", filePath); 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 { } else {
success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath); success = file.isFile() || downloadFile(url + version + Objects.requireNonNull(advType).getDownloadURLPart() + version + ".jar", filePath);
} }
@ -565,9 +585,7 @@ public class Profile {
success = true; success = true;
} }
if (!success) { if (!success) {
if (gui != null) { printToGui("Error downloading: " + name + version + ".jar");
gui.setStatus("Error downloading: " + name + version + ".jar");
}
throw new FileNotFoundException("Error downloading: " + name + version + ".jar"); throw new FileNotFoundException("Error downloading: " + name + version + ".jar");
} }
} }

View File

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

View File

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