Skip to content

Commit

Permalink
Partial updates, continue on failure & better error messages!
Browse files Browse the repository at this point in the history
  • Loading branch information
XDjackieXD committed Jan 13, 2016
1 parent a4ee2e8 commit 1188816
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 45 deletions.
Binary file modified ExamplePack/ExamplePack-MultiMC-Instance.zip
Binary file not shown.
57 changes: 36 additions & 21 deletions src/at/chaosfield/packupdate/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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));
Expand All @@ -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<String, String[]> parsePackinfo(BufferedReader packinfo) throws IOException{
HashMap<String, String[]> parsedInfo = new HashMap<String, String[]>();
HashMap<String, String[]> 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]});
Expand All @@ -135,7 +122,7 @@ private static HashMap<String, String[]> parsePackinfo(BufferedReader packinfo)
public static HashMap<String, String[]> getAvailableUpdates(String onlineVersionFile, String localVersionFile) throws IOException{
HashMap<String, String[]> onlinePackInfo = parsePackinfo(getOnlineFile(onlineVersionFile));
HashMap<String, String[]> localPackInfo = parsePackinfo(getLocalFile(localVersionFile));
HashMap<String, String[]> needsUpdate = new HashMap<String, String[]>(); //Key: Name Value: New Version, Old Version, Download URL, Type
HashMap<String, String[]> needsUpdate = new HashMap<>(); //Key: Name Value: New Version, Old Version, Download URL, Type
if(onlinePackInfo.isEmpty()) return needsUpdate;
for(Map.Entry<String, String[]> entry : onlinePackInfo.entrySet()){
if(localPackInfo.containsKey(entry.getKey())){
Expand All @@ -155,4 +142,32 @@ public static HashMap<String, String[]> getAvailableUpdates(String onlineVersion
}
return needsUpdate;
}

public static boolean writeLocalConfig(HashMap<String, String[]> objects, String fileName){

HashMap<String, String[]> 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<String, String[]> 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<String, String[]> 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;
}
}
61 changes: 37 additions & 24 deletions src/at/chaosfield/packupdate/FxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +38,16 @@ public void setMain(PackUpdate main) {
Task updater = new Task<List<String>>() {
@Override
protected List<String> call() {
List<String> ret = new ArrayList<>();

class ErrorLog extends ArrayList<String> {
@Override
public boolean add(String e) {
System.out.println(e);
return super.add(e);
}
}

List<String> ret = new ErrorLog();
HashMap<String, String[]> updated = new HashMap<>();
HashMap<String, String[]> updateables = null;

Expand All @@ -62,19 +72,20 @@ protected List<String> 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;
}
}
Expand All @@ -83,23 +94,24 @@ protected List<String> 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;
}
}
Expand All @@ -108,31 +120,32 @@ protected List<String> 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;
}
}
Expand All @@ -152,19 +165,19 @@ protected List<String> 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<String> returnValue = (List<String>) updater.getValue();
if (returnValue.size() > 0) {
main.errorAlert(returnValue);
}
primaryStage.close();
});
Expand Down
32 changes: 32 additions & 0 deletions src/at/chaosfield/packupdate/PackUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,6 +63,34 @@ public void initRootLayout(){
}
}

public void errorAlert(List<String> 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);
Expand Down

0 comments on commit 1188816

Please sign in to comment.