diff --git a/src/Launcher/Main.java b/src/Launcher/Main.java index ac653e0..6448fb4 100644 --- a/src/Launcher/Main.java +++ b/src/Launcher/Main.java @@ -12,6 +12,8 @@ package Launcher; import Launcher.net.Updater; import com.tofvesson.reflection.SafeReflection; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Node; import javafx.scene.Scene; @@ -22,13 +24,9 @@ import javafx.scene.layout.Pane; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.stage.StageStyle; - import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStream; -import java.net.URL; - import com.tofvesson.async.*; +import javafx.util.Duration; public class Main extends Application { @@ -39,6 +37,7 @@ public class Main extends Application { public static final int semVerPatch = 1; // Patch version private double xOffset = 0, yOffset = 0; // Offsets for dragging + private static String[] args; private Button exit, min, Home_btn, Modpack_btn, Settings_btn, Instance_btn; // Define buttons private ImageView icon; private TextField Search_modpacks; @@ -50,6 +49,24 @@ public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ + if(args.length<2 || !args[1].equals("false")){ + Stage d = new Stage(); + Timeline t = new Timeline(); + t.getKeyFrames().add(new KeyFrame(Duration.millis(1), event ->{ d.close(); primaryStage.show(); })); + d.initStyle(StageStyle.UNDECORATED); + Pane n = (Pane) Tabs.load("dialog_update"); + //n.getChildren().add(new Label("Hello World")); + d.setScene(new Scene(n)); + d.show(); + new Thread(()->{ + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Updater.getInstance(t); + }).start(); + } primaryStage.initStyle(StageStyle.UNDECORATED); // Remove ugly trash @@ -58,7 +75,6 @@ public class Main extends Application { .getText().replace("$v", semVerDevState+"-"+semVerMajor+"."+semVerMinor+"."+semVerPatch)); // Use variables to define version primaryStage.setTitle("Team-Avion Launcher [WIP]"); primaryStage.setScene(new Scene(root, 900, 500)); - primaryStage.show(); primaryStage.getIcons().clear(); primaryStage.getIcons().add(appIcon = new Image(getClass().getResourceAsStream("/assets/icons/app.png"))); @@ -156,7 +172,7 @@ public class Main extends Application { } public static void main(String[] args) throws Exception{ - if(args.length<2 || !args[1].equals("false")) Updater.checkUpdate(); // Check for update + Main.args = args; if(args.length>0){ File f = new File(args[0]); if(f.isFile()) while(!f.delete()) Thread.sleep(50); // Delete previous jar diff --git a/src/Launcher/net/Updater.java b/src/Launcher/net/Updater.java index ac1189b..09213e4 100644 --- a/src/Launcher/net/Updater.java +++ b/src/Launcher/net/Updater.java @@ -3,6 +3,7 @@ package Launcher.net; import Launcher.Main; import com.tofvesson.async.Async; import com.tofvesson.reflection.SafeReflection; +import javafx.animation.Timeline; import javax.net.ssl.HttpsURLConnection; import java.io.*; import java.net.MalformedURLException; @@ -30,14 +31,17 @@ public class Updater { updateURL = u; } - private Updater(){ + private Updater(Timeline t){ try { conn = (HttpsURLConnection) updateURL.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("GET"); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); - if(conn.getResponseCode()!=200) return; // Can't get update site + if(conn.getResponseCode()!=200){ + t.play(); + return; // Can't get update site + } conn.connect(); @@ -62,10 +66,16 @@ public class Updater { semMinor = semMin; semPatch = semPat; } - if(downloadLink.equals("")) return; + if(downloadLink.equals("")){ + t.play(); + return; + } + final int l=semMajor, j=semMinor, k=semPatch; File f = new File("TAL-"+semMajor+"_"+semMinor+"_"+semPatch+".jar"), f1; - if((f1=new File(Main.class.getResource("/assets/").getFile())).getParent().contains("!") && f1.getParent().contains("file:")) f1=new File(f1.getParent().substring(f1.getParent().indexOf("file:")+5, f1.getParent().length()-1)); - if(f.isFile()) f.delete(); + if((f1=new File(Main.class.getResource("/assets/").getFile())).getParent().contains("!") && + f1.getParent().contains("file:")) // Find .jar representation of this program + f1=new File(f1.getParent().substring(f1.getParent().indexOf("file:")+5, f1.getParent().length()-1)); // Prepare for deletion + if(f.isFile()) f.renameTo(new File("-"+f.getName())); f.createNewFile(); OutputStream o = new FileOutputStream(f); HttpsURLConnection dl = (HttpsURLConnection) new URL(downloadLink).openConnection(); // Downloader @@ -73,7 +83,10 @@ public class Updater { dl.setDoInput(true); dl.setRequestMethod("GET"); dl.setRequestProperty("User-Agent", "Mozilla/5.0"); - if(dl.getResponseCode()!=200) return; + if(dl.getResponseCode()!=200){ + t.play(); + return; + } dl.connect(); InputStream reader = dl.getInputStream(); int i; @@ -86,12 +99,13 @@ public class Updater { } catch (IOException e) { //e.printStackTrace(); System.out.println("No internet connection available!"); + t.play(); } } + public static void checkUpdate(Timeline t){ setup = new Async<>(SafeReflection.getFirstConstructor(Updater.class), t); } - public static void checkUpdate(){ setup = new Async<>(SafeReflection.getFirstConstructor(Updater.class)); } - - public static Updater getInstance() { - return instance==null?setup!=null?instance=setup.await():null:instance; // Await async creation + public static Updater getInstance(Timeline t) { + if(setup==null) checkUpdate(t); + return instance==null?instance=setup.await():instance; // Await async creation } } diff --git a/src/assets/layout/dialog_update.fxml b/src/assets/layout/dialog_update.fxml new file mode 100644 index 0000000..1b550af --- /dev/null +++ b/src/assets/layout/dialog_update.fxml @@ -0,0 +1,17 @@ + + + + + + + + + +