2017-11-06 10:42:31 +01:00
/ * *
* cemu_UI
*
* Copyright 2017 < @Seil0 >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
* /
2017-11-13 16:44:39 +01:00
package com.cemu_UI.controller ;
2017-11-06 10:42:31 +01:00
import java.io.BufferedReader ;
import java.io.File ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.HttpURLConnection ;
import java.net.URL ;
2017-11-13 16:44:39 +01:00
2017-11-06 10:42:31 +01:00
import javax.swing.ProgressMonitor ;
import javax.swing.ProgressMonitorInputStream ;
import org.apache.commons.io.FileUtils ;
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-11-13 16:44:39 +01:00
import com.cemu_UI.application.MainWindowController ;
2017-11-06 10:42:31 +01:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonArray ;
import com.eclipsesource.json.JsonObject ;
import com.eclipsesource.json.JsonValue ;
import javafx.application.Platform ;
2017-11-15 22:00:28 +01:00
public class UpdateController implements Runnable {
2017-11-06 10:42:31 +01:00
private MainWindowController mainWindowController ;
private String buildNumber ;
private String apiOutput ;
2017-11-15 22:00:28 +01:00
private String updateBuildNumber ; // tag_name from Github
2017-12-21 11:02:54 +01:00
// private String updateName;
// private String updateChanges;
2017-11-15 22:00:28 +01:00
private String browserDownloadUrl ; // update download link
2017-11-10 15:58:20 +01:00
private String githubApiRelease = " https://api.github.com/repos/Seil0/cemu_UI/releases/latest " ;
private String githubApiBeta = " https://api.github.com/repos/Seil0/cemu_UI/releases " ;
2017-12-19 21:35:49 +01:00
2017-11-10 15:58:20 +01:00
private URL githubApiUrl ;
private boolean useBeta ;
2017-11-06 10:42:31 +01:00
private static final Logger LOGGER = LogManager . getLogger ( UpdateController . class . getName ( ) ) ;
2017-11-15 22:00:28 +01:00
2017-11-06 10:42:31 +01:00
/ * *
2017-11-15 22:00:28 +01:00
* updater for cemu_UI based on Project HomeFlix checks for Updates and download
* it in case there is one
2017-11-06 10:42:31 +01:00
* /
2017-11-15 22:00:28 +01:00
public UpdateController ( MainWindowController mwc , String buildNumber , boolean useBeta ) {
2017-11-10 15:58:20 +01:00
mainWindowController = mwc ;
this . buildNumber = buildNumber ;
this . useBeta = useBeta ;
2017-11-06 10:42:31 +01:00
}
2017-11-15 22:00:28 +01:00
@Override
public void run ( ) {
2017-12-11 17:07:20 +01:00
LOGGER . info ( " beta: " + useBeta + " ; checking for updates ... " ) ;
2017-11-06 10:42:31 +01:00
Platform . runLater ( ( ) - > {
2017-12-19 21:35:49 +01:00
mainWindowController . getUpdateBtn ( ) . setText ( mainWindowController . getBundle ( ) . getString ( " updateBtnChecking " ) ) ;
2017-11-15 22:00:28 +01:00
} ) ;
try {
2017-11-06 10:42:31 +01:00
2017-11-15 22:00:28 +01:00
if ( useBeta ) {
githubApiUrl = new URL ( githubApiBeta ) ;
2017-11-10 15:58:20 +01:00
} else {
githubApiUrl = new URL ( githubApiRelease ) ;
}
2017-11-15 22:00:28 +01:00
// URL githubApiUrl = new URL(githubApiRelease);
BufferedReader ina = new BufferedReader ( new InputStreamReader ( githubApiUrl . openStream ( ) ) ) ;
2017-11-06 10:42:31 +01:00
apiOutput = ina . readLine ( ) ;
2017-11-15 22:00:28 +01:00
ina . close ( ) ;
2017-11-06 10:42:31 +01:00
} catch ( IOException e ) {
Platform . runLater ( ( ) - > {
LOGGER . error ( " could not check update version " , e ) ;
} ) ;
}
2017-11-15 22:00:28 +01:00
if ( useBeta ) {
JsonArray objectArray = Json . parse ( " { \" items \" : " + apiOutput + " } " ) . asObject ( ) . get ( " items " ) . asArray ( ) ;
JsonValue object = objectArray . get ( 0 ) ;
JsonArray objectAssets = object . asObject ( ) . get ( " assets " ) . asArray ( ) ;
updateBuildNumber = object . asObject ( ) . getString ( " tag_name " , " " ) ;
2017-12-21 11:02:54 +01:00
// updateName = object.asObject().getString("name", "");
// updateChanges = object.asObject().getString("body", "");
2017-11-10 15:58:20 +01:00
2017-11-15 22:00:28 +01:00
for ( JsonValue asset : objectAssets ) {
browserDownloadUrl = asset . asObject ( ) . getString ( " browser_download_url " , " " ) ;
}
2017-11-10 15:58:20 +01:00
} else {
2017-11-15 22:00:28 +01:00
JsonObject object = Json . parse ( apiOutput ) . asObject ( ) ;
JsonArray objectAssets = Json . parse ( apiOutput ) . asObject ( ) . get ( " assets " ) . asArray ( ) ;
updateBuildNumber = object . getString ( " tag_name " , " " ) ;
2017-12-21 11:02:54 +01:00
// updateName = object.getString("name", "");
// updateChanges = object.getString("body", "");
2017-11-15 22:00:28 +01:00
for ( JsonValue asset : objectAssets ) {
browserDownloadUrl = asset . asObject ( ) . getString ( " browser_download_url " , " " ) ;
}
2017-11-10 15:58:20 +01:00
}
2017-11-15 22:00:28 +01:00
LOGGER . info ( " Build: " + buildNumber + " , Update: " + updateBuildNumber ) ;
// Compares the program BuildNumber with the current BuildNumber if program
// BuildNumber < current BuildNumber then perform a update
2017-11-06 10:42:31 +01:00
int iversion = Integer . parseInt ( buildNumber ) ;
int iaktVersion = Integer . parseInt ( updateBuildNumber . replace ( " . " , " " ) ) ;
2017-11-15 22:00:28 +01:00
if ( iversion > = iaktVersion ) {
2017-11-06 10:42:31 +01:00
Platform . runLater ( ( ) - > {
2017-12-19 21:35:49 +01:00
mainWindowController . getUpdateBtn ( ) . setText ( mainWindowController . getBundle ( ) . getString ( " updateBtnNoUpdateAvailable " ) ) ;
2017-11-15 22:00:28 +01:00
} ) ;
2017-11-06 10:42:31 +01:00
LOGGER . info ( " no update available " ) ;
2017-11-15 22:00:28 +01:00
} else {
2017-11-06 10:42:31 +01:00
Platform . runLater ( ( ) - > {
2017-12-19 21:35:49 +01:00
mainWindowController . getUpdateBtn ( ) . setText ( mainWindowController . getBundle ( ) . getString ( " updateBtnUpdateAvailable " ) ) ;
2017-11-15 22:00:28 +01:00
} ) ;
2017-11-06 10:42:31 +01:00
LOGGER . info ( " update available " ) ;
LOGGER . info ( " download link: " + browserDownloadUrl ) ;
2017-11-15 22:00:28 +01:00
try {
// open new Http connection, ProgressMonitorInputStream for downloading the data
2017-12-26 17:33:14 +01:00
HttpURLConnection connection = ( HttpURLConnection ) new URL ( browserDownloadUrl ) . openConnection ( ) ;
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream ( null , " Downloading... " , connection . getInputStream ( ) ) ;
2017-11-06 10:42:31 +01:00
ProgressMonitor pm = pmis . getProgressMonitor ( ) ;
2017-11-15 22:00:28 +01:00
pm . setMillisToDecideToPopup ( 0 ) ;
pm . setMillisToPopup ( 0 ) ;
2017-12-26 17:33:14 +01:00
pm . setMinimum ( 0 ) ; // set beginning of the progress bar to 0
pm . setMaximum ( connection . getContentLength ( ) ) ; // set the end to the file length
2017-11-15 22:00:28 +01:00
FileUtils . copyInputStreamToFile ( pmis , new File ( " cemu_UI_update.jar " ) ) ; // download update
2017-12-26 17:33:14 +01:00
org . apache . commons . io . FileUtils . copyFile ( new File ( " cemu_UI_update.jar " ) , new File ( " cemu_UI.jar " ) ) ;
2017-11-15 22:00:28 +01:00
org . apache . commons . io . FileUtils . deleteQuietly ( new File ( " cemu_UI_update.jar " ) ) ; // delete update
Runtime . getRuntime ( ) . exec ( " java -jar cemu_UI.jar " ) ; // start again
System . exit ( 0 ) ; // finishes itself
2017-11-06 10:42:31 +01:00
} catch ( IOException e ) {
Platform . runLater ( ( ) - > {
LOGGER . info ( " could not download update files " , e ) ;
} ) ;
}
}
}
}