diff --git a/ExamplePack/ExamplePack-MultiMC-Instance.zip b/ExamplePack/ExamplePack-MultiMC-Instance.zip index 3f40b71..d868edc 100644 Binary files a/ExamplePack/ExamplePack-MultiMC-Instance.zip and b/ExamplePack/ExamplePack-MultiMC-Instance.zip differ diff --git a/src/at/chaosfield/packupdate/FileManager.java b/src/at/chaosfield/packupdate/FileManager.java index d94cc59..572bd95 100644 --- a/src/at/chaosfield/packupdate/FileManager.java +++ b/src/at/chaosfield/packupdate/FileManager.java @@ -2,8 +2,6 @@ import java.io.*; import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; @@ -55,7 +53,11 @@ public static boolean unzipLocalFile(String zipFile, String outputPath){ ZipInputStream zis = new ZipInputStream(new FileInputStream(input)); ZipEntry ze; + Boolean hadFiles = false; + while((ze = zis.getNextEntry()) != null){ + hadFiles = true; + if(ze.isDirectory()) continue; @@ -70,7 +72,7 @@ public static boolean unzipLocalFile(String zipFile, String outputPath){ zis.closeEntry(); zis.close(); - return true; + return hadFiles; }catch(Exception e){ return false; } @@ -91,21 +93,6 @@ public static BufferedReader getLocalFile(String fileName) throws IOException{ return reader; } - //open a local file for writing. Create an empty one if it doesn't exist - public static BufferedWriter writeLocalFile(String fileName) throws IOException{ - File file = new File(fileName); - BufferedWriter writer = null; - for(int i = 0; i < 3; i++){ - try{ - writer = new BufferedWriter(new FileWriter(file)); - }catch(FileNotFoundException e){ - file.getParentFile().mkdirs(); - file.createNewFile(); - } - } - return writer; - } - //Download a binary file to a given location public static void downloadFile(String fileUrl, String destination) throws IOException{ FileUtils.copyURLToFile(new URL(fileUrl), new File(destination)); @@ -117,10 +104,10 @@ public static void downloadFile(String fileUrl, String destination) throws IOExc //mod had to be a jar file //resource has to be a zip file that gets extracted into the resources folder private static HashMap parsePackinfo(BufferedReader packinfo) throws IOException{ - HashMap parsedInfo = new HashMap(); + HashMap parsedInfo = new HashMap<>(); String tmp; while((tmp = packinfo.readLine()) != null){ - if(!tmp.equals("")){ //Ignore empty lines + if(!tmp.equals("") || !tmp.startsWith("#")){ //Ignore empty lines and allow comments with "#" String[] parsed = tmp.split(","); if(parsed.length == 4){ parsedInfo.put(parsed[0], new String[]{parsed[1], parsed[2], parsed[3]}); @@ -135,7 +122,7 @@ private static HashMap parsePackinfo(BufferedReader packinfo) public static HashMap getAvailableUpdates(String onlineVersionFile, String localVersionFile) throws IOException{ HashMap onlinePackInfo = parsePackinfo(getOnlineFile(onlineVersionFile)); HashMap localPackInfo = parsePackinfo(getLocalFile(localVersionFile)); - HashMap needsUpdate = new HashMap(); //Key: Name Value: New Version, Old Version, Download URL, Type + HashMap needsUpdate = new HashMap<>(); //Key: Name Value: New Version, Old Version, Download URL, Type if(onlinePackInfo.isEmpty()) return needsUpdate; for(Map.Entry entry : onlinePackInfo.entrySet()){ if(localPackInfo.containsKey(entry.getKey())){ @@ -155,4 +142,32 @@ public static HashMap getAvailableUpdates(String onlineVersion } return needsUpdate; } + + public static boolean writeLocalConfig(HashMap objects, String fileName){ + + HashMap packInfo = new HashMap<>(); + + try{ + packInfo = parsePackinfo(getLocalFile(fileName)); + }catch(IOException e){ + System.out.println("[PackInfo] Warning: could not get previous config. Ignore this if it is the first launch of the pack."); + } + + for(Map.Entry entry : objects.entrySet()){ + packInfo.put(entry.getKey(), new String[]{entry.getValue()[0], entry.getValue()[2], entry.getValue()[3]}); + } + + try{ + PrintWriter writer = new PrintWriter(fileName, "UTF-8"); + for(Map.Entry entry : packInfo.entrySet()){ + if(!entry.getValue()[2].equals("") && !entry.getValue()[0].equals("")) + writer.println(entry.getKey() + "," + entry.getValue()[0] + "," + entry.getValue()[1] + "," + entry.getValue()[2]); + } + writer.close(); + }catch(Exception e){ + e.printStackTrace(); + return false; + } + return true; + } } diff --git a/src/at/chaosfield/packupdate/FxController.java b/src/at/chaosfield/packupdate/FxController.java index ba80d23..e8f44a9 100644 --- a/src/at/chaosfield/packupdate/FxController.java +++ b/src/at/chaosfield/packupdate/FxController.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.lang.Override; import java.lang.String; +import java.net.URL; import java.util.*; import java.util.HashMap; import java.util.List; @@ -37,7 +38,16 @@ public void setMain(PackUpdate main) { Task updater = new Task>() { @Override protected List call() { - List ret = new ArrayList<>(); + + class ErrorLog extends ArrayList { + @Override + public boolean add(String e) { + System.out.println(e); + return super.add(e); + } + } + + List ret = new ErrorLog(); HashMap updated = new HashMap<>(); HashMap updateables = null; @@ -62,19 +72,20 @@ protected List call() { switch (entry.getValue()[3]) { case "mod": if (!entry.getValue()[2].equals("")) { //If URL is not empty -> download new Version - if (!entry.getValue()[1].equals("")) //If old version exists delete it - if (!FileManager.deleteLocalFile(modsPath + entry.getKey() + "-" + entry.getValue()[1] + ".jar")) { - ret.add("[" + entry.getKey() + "]" + "Deletion of file " + entry.getKey() + "-" + entry.getValue()[1] + ".jar failed"); - continue; - } try { FileManager.downloadFile(entry.getValue()[2], modsPath + entry.getKey() + "-" + entry.getValue()[0] + ".jar"); } catch (IOException e) { - ret.add("[" + entry.getKey() + "]" + "Download failed."); + ret.add("[" + entry.getKey() + "] " + "Download failed."); + continue; } + if (!entry.getValue()[1].equals("")) //If old version exists delete it + if (!FileManager.deleteLocalFile(modsPath + entry.getKey() + "-" + entry.getValue()[1] + ".jar")) { + ret.add("[" + entry.getKey() + "] " + "Deletion of file " + entry.getKey() + "-" + entry.getValue()[1] + ".jar failed"); + continue; + } } else { if (!FileManager.deleteLocalFile(modsPath + entry.getKey() + "-" + entry.getValue()[1] + ".jar")) { - ret.add("[" + entry.getKey() + "]" + "Deletion of file " + entry.getKey() + "-" + entry.getValue()[1] + ".jar failed"); + ret.add("[" + entry.getKey() + "] " + "Deletion of file " + entry.getKey() + "-" + entry.getValue()[1] + ".jar failed"); continue; } } @@ -83,23 +94,24 @@ protected List call() { case "config": if (!entry.getValue()[2].equals("")) { //If URL is not empty -> download new Version if (!FileManager.deleteLocalFolderContents(configPath)) { //delete current config files - ret.add("[" + entry.getKey() + "]" + "Either deleting the config folder's content or creating an empty config folder failed."); + ret.add("[" + entry.getKey() + "] " + "Either deleting the config folder's content or creating an empty config folder failed."); continue; } try { FileManager.downloadFile(entry.getValue()[2], configPath + File.separator + entry.getKey() + "-" + entry.getValue()[0] + ".zip"); } catch (IOException e) { - ret.add("[" + entry.getKey() + "]" + "Download failed."); + ret.add("[" + entry.getKey() + "] " + "Download failed."); + continue; } if (!FileManager.unzipLocalFile(configPath + File.separator + entry.getKey() + "-" + entry.getValue()[0] + ".zip", configPath + File.separator)) { - ret.add("[" + entry.getKey() + "]" + "Unpack failed: The zip file seems to be corrupted."); + ret.add("[" + entry.getKey() + "] " + "Unpack failed: The zip file seems to be corrupted."); continue; } } else { if (!FileManager.deleteLocalFolderContents(configPath)) { - ret.add("[" + entry.getKey() + "]" + "Either deleting the config folder's content or creating an empty config folder failed."); + ret.add("[" + entry.getKey() + "] " + "Either deleting the config folder's content or creating an empty config folder failed."); continue; } } @@ -108,31 +120,32 @@ protected List call() { case "resources": if (!entry.getValue()[2].equals("")) { //If URL is not empty -> download new Version if (!FileManager.deleteLocalFolderContents(resourcesPath + File.separator + "resources")) { //delete current config files - ret.add("[" + entry.getKey() + "]" + "Either deleting the resources folder's content or creating an empty resources folder failed."); + ret.add("[" + entry.getKey() + "] " + "Either deleting the resources folder's content or creating an empty resources folder failed."); continue; } if (!FileManager.deleteLocalFolderContents(resourcesPath + File.separator + "scripts")) { - ret.add("[" + entry.getKey() + "]" + "Either deleting the scripts folder's content or creating an empty scripts folder failed."); + ret.add("[" + entry.getKey() + "] " + "Either deleting the scripts folder's content or creating an empty scripts folder failed."); continue; } try { FileManager.downloadFile(entry.getValue()[2], resourcesPath + File.separator + "resources" + File.separator + entry.getKey() + "-" + entry.getValue()[0] + ".zip"); } catch (IOException e) { - ret.add("[" + entry.getKey() + "]" + "Download failed."); + ret.add("[" + entry.getKey() + "] " + "Download failed."); + continue; } if (!FileManager.unzipLocalFile(resourcesPath + File.separator + "resources" + File.separator + entry.getKey() + "-" + entry.getValue()[0] + ".zip", resourcesPath + File.separator)) { - ret.add("[" + entry.getKey() + "]" + "Unpack failed: The zip file seems to be corrupted."); + ret.add("[" + entry.getKey() + "] " + "Unpack failed: The zip file seems to be corrupted."); continue; } } else { if (!FileManager.deleteLocalFolderContents(resourcesPath + File.separator + "resources")) { - ret.add("[" + entry.getKey() + "]" + "Either deleting the resources folder's content or creating an empty resources folder failed."); + ret.add("[" + entry.getKey() + "] " + "Either deleting the resources folder's content or creating an empty resources folder failed."); continue; } if (!FileManager.deleteLocalFolderContents(resourcesPath + File.separator + "scripts")) { - ret.add("[" + entry.getKey() + "]" + "Either deleting the scripts folder's content or creating an empty scripts folder failed."); + ret.add("[" + entry.getKey() + "] " + "Either deleting the scripts folder's content or creating an empty scripts folder failed."); continue; } } @@ -152,19 +165,19 @@ protected List call() { //FileManager.deleteLocalFile(parameters.get(2) + File.separator + parameters.get(1)); //FileManager.downloadFile(parameters.get(0), parameters.get(2) + File.separator + parameters.get(1)); - //TODO generate cfg file, save it and return errors if any. + if(!FileManager.writeLocalConfig(updated, parameters.get(2) + File.separator + parameters.get(1))) + ret.add("[PackInfo]" + "Error writing " + parameters.get(1)); - return null; + return ret; } }; progress.progressProperty().bind(updater.progressProperty()); status.textProperty().bind(updater.messageProperty()); updater.setOnSucceeded(t -> { - String[] returnValue = (String[]) updater.getValue(); - if (returnValue != null) { - System.out.println(returnValue[1] + ": " + returnValue[2]); - main.errorAlert(returnValue[0], returnValue[1], returnValue[2]); + List returnValue = (List) updater.getValue(); + if (returnValue.size() > 0) { + main.errorAlert(returnValue); } primaryStage.close(); }); diff --git a/src/at/chaosfield/packupdate/PackUpdate.java b/src/at/chaosfield/packupdate/PackUpdate.java index 6523050..1307367 100644 --- a/src/at/chaosfield/packupdate/PackUpdate.java +++ b/src/at/chaosfield/packupdate/PackUpdate.java @@ -4,6 +4,10 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Alert; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.stage.Stage; @@ -59,6 +63,34 @@ public void initRootLayout(){ } } + public void errorAlert(List errors){ + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("PackUpdate Error"); + alert.setHeaderText("Something went wrong while updating your pack :("); + + Label label = new Label("Log:"); + String textAreaText = ""; + for(String error : errors){ + textAreaText += "\n" + error; + } + TextArea textArea = new TextArea(textAreaText); + textArea.setEditable(false); + textArea.setWrapText(true); + textArea.setMaxWidth(Double.MAX_VALUE); + textArea.setMaxHeight(Double.MAX_VALUE); + GridPane.setVgrow(textArea, Priority.ALWAYS); + GridPane.setHgrow(textArea, Priority.ALWAYS); + GridPane expContent = new GridPane(); + expContent.setMaxWidth(Double.MAX_VALUE); + expContent.add(label, 0, 0); + expContent.add(textArea, 0, 1); + + alert.getDialogPane().setContent(expContent); + + alert.initOwner(this.primaryStage); + alert.showAndWait(); + } + public void errorAlert(String title, String header, String message){ Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle(title);