Minor update

- Enabled Updater to restart program while it is running rather than freezing startup process
- TAL will now automatically delete the old version when it is done updating
This commit is contained in:
Gabriel Tofvesson 2016-11-13 22:53:31 +01:00
parent ba7da97c9b
commit d82325a3ab
2 changed files with 28 additions and 15 deletions

View File

@ -24,6 +24,9 @@ import javafx.scene.shape.Rectangle;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.StageStyle; import javafx.stage.StageStyle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import com.tofvesson.async.*; import com.tofvesson.async.*;
@ -34,7 +37,7 @@ public class Main extends Application {
public static final String semVerDevState = "PreDev"; // Development stage public static final String semVerDevState = "PreDev"; // Development stage
public static final int semVerMajor = 0; // Major version public static final int semVerMajor = 0; // Major version
public static final int semVerMinor = 2; // Minor version public static final int semVerMinor = 2; // Minor version
public static final int semVerPatch = 0; // Patch version public static final int semVerPatch = 1; // Patch version
private double xOffset = 0, yOffset = 0; // Offsets for dragging private double xOffset = 0, yOffset = 0; // Offsets for dragging
@ -155,8 +158,12 @@ public class Main extends Application {
icon.setImage(appIcon); icon.setImage(appIcon);
} }
public static void main(String[] args) { public static void main(String[] args) throws Exception{
Updater u = Updater.getInstance(); if(args.length<2 || !args[1].equals("false")) Updater.checkUpdate(); // Check for update
if(args.length>0){
File f = new File(args[0]);
if(f.isFile()) while(!f.delete()) Thread.sleep(50); // Delete previous jar
}
launch(args); launch(args);
} }

View File

@ -1,5 +1,6 @@
package Launcher.net; package Launcher.net;
import Launcher.Main;
import com.tofvesson.async.Async; import com.tofvesson.async.Async;
import com.tofvesson.reflection.SafeReflection; import com.tofvesson.reflection.SafeReflection;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -18,8 +19,8 @@ import static Launcher.Main.semVerPatch;
public class Updater { public class Updater {
private static volatile Updater instance; private static volatile Updater instance;
private static final Async<Updater> setup = new Async<>(SafeReflection.getFirstConstructor(Updater.class)); private static Async<Updater> setup;
public static final Pattern version = Pattern.compile("(?s)<span class=\"css-truncate-target\">.*(\\d).(\\d).(\\d)</span>.*<a href=\"/GabrielTofvesson/TeamAvionLauncher/releases/download/(.*)\\.jar\" rel=\"nofollow\">"); // Pattern to match when finding refs public static final Pattern version = Pattern.compile("(?s)<span class=\"css-truncate-target\">.*?(\\d)\\.(\\d)\\.(\\d)</span>.*?<a href=\"/GabrielTofvesson/TeamAvionLauncher/releases/download/(.*?)\\.jar\" rel=\"nofollow\">"); // Pattern to match when finding refs
private HttpsURLConnection conn; private HttpsURLConnection conn;
public static final URL updateURL; public static final URL updateURL;
@ -44,24 +45,27 @@ public class Updater {
conn.getInputStream())); conn.getInputStream()));
String inputLine; String inputLine;
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) response.append(inputLine);
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close(); in.close();
Matcher m = version.matcher(response.toString()); Matcher m = version.matcher(response.toString());
String downloadLink = ""; String downloadLink = "";
int semMajor = semVerMajor, semMinor = semVerMinor, semPatch = semVerPatch;
while(m.find()){ while(m.find()){
int semMaj = Integer.parseInt(m.group(1)), int semMaj = Integer.parseInt(m.group(1)),
semMin = Integer.parseInt(m.group(2)), semMin = Integer.parseInt(m.group(2)),
semPat = Integer.parseInt(m.group(3)); semPat = Integer.parseInt(m.group(3));
if(semMaj < semVerMajor || (semMaj==semVerMajor && semMin<semVerMinor) || if(semMaj < semMajor || (semMaj==semMajor && semMin<semMinor) ||
(semMaj==semVerMajor && semMin==semVerMinor && semPat<=semVerPatch)) continue; // Version found isn't new (semMaj==semMajor && semMin==semMinor && semPat<=semPatch)) continue; // Version found isn't new
downloadLink = "https://github.com/GabrielTofvesson/TeamAvionLauncher/releases/download/"+m.group(4)+".jar"; downloadLink = "https://github.com/GabrielTofvesson/TeamAvionLauncher/releases/download/"+m.group(4)+".jar";
semMajor = semMaj;
semMinor = semMin;
semPatch = semPat;
} }
if(downloadLink.equals("")) return; if(downloadLink.equals("")) return;
File f = new File("Tal1.jar"); 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();
f.createNewFile(); f.createNewFile();
OutputStream o = new FileOutputStream(f); OutputStream o = new FileOutputStream(f);
HttpsURLConnection dl = (HttpsURLConnection) new URL(downloadLink).openConnection(); // Downloader HttpsURLConnection dl = (HttpsURLConnection) new URL(downloadLink).openConnection(); // Downloader
@ -77,15 +81,17 @@ public class Updater {
while((i=reader.read(buffer))!=-1) o.write(buffer, 0, i); while((i=reader.read(buffer))!=-1) o.write(buffer, 0, i);
reader.close(); reader.close();
o.close(); o.close();
Runtime.getRuntime().exec("java -jar Tal1.jar"); Runtime.getRuntime().exec("java -jar "+f.getName()+" "+f1.getAbsolutePath()+" "+false);
System.exit(0); System.exit(0);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); //e.printStackTrace();
System.out.println("No internet connection available!"); System.out.println("No internet connection available!");
} }
} }
public static void checkUpdate(){ setup = new Async<>(SafeReflection.getFirstConstructor(Updater.class)); }
public static Updater getInstance() { public static Updater getInstance() {
return instance==null?instance=setup.await():instance; // Await async creation return instance==null?setup!=null?instance=setup.await():null:instance; // Await async creation
} }
} }