2019-04-14 12:18:58 +02:00

95 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package update;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Optional;
import guis.MainGui;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import main.Start;
import manager.SettingManager;
public class Updater {
Thread uT;
public void checkForUpdate(){
try{
UpdateChecker uc = new UpdateChecker("cookiestudios.org", 9999);
float tmpversion = uc.getCurrentVersion();
System.out.println("got version: " + tmpversion);
if(tmpversion > Start.VERSION){
String dl = uc.getDownloadLink();
Alert updateAlert = new Alert(AlertType.INFORMATION,
"There is a newer version of QuickLaunch available\nDownload now?",
ButtonType.YES,
ButtonType.NO);
updateAlert.setTitle("Update available!");
Optional<ButtonType> result = updateAlert.showAndWait();
if(result.isPresent() && result.get() == ButtonType.YES){
try {
System.out.println("here");
cleanDirectory();
URL website = new URL(dl);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(SettingManager.getJarDirectory()+File.separator+"Squirrel.jar");
fos.getChannel().transferFrom(rbc, 0, Integer.MAX_VALUE);
fos.close();
System.out.println("Done");
try {
ProcessBuilder pb = new ProcessBuilder("java","-jar",SettingManager.getJarDirectory()+File.separator+"Squirrel.jar");
pb.directory(new File(SettingManager.getJarDirectory()));
pb.redirectErrorStream(true);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.start();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("started newer version");
System.exit(0);
} catch (MalformedURLException e) {
e.printStackTrace();
MainGui.addNotification("Couldn´t reach update server", 2);
} catch (FileNotFoundException e) {
e.printStackTrace();
MainGui.addNotification("Couldn´t reach update server", 2);
} catch (IOException e) {
e.printStackTrace();
MainGui.addNotification("Couldn´t reach update server", 2);
}
}
}
else{
uc.upToDate();
MainGui.addNotification("QuickLaunch is up to date", 2);
uT = new Thread(new UpdateThread());
uT.start();
}
uc.close();
}catch(Exception e){
}
}
private void cleanDirectory(){
File squirrel = new File(SettingManager.getJarDirectory()+File.separator+"Squirrel.jar");
if(squirrel.exists()){
squirrel.delete();
}
File fMark = new File(SettingManager.getJarDirectory()+File.separator+"f.MARK");
if(fMark.exists()){
fMark.delete();
}
}
}